Skip to content

Commit b67719a

Browse files
committed
add tests
1 parent a8f5029 commit b67719a

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed

cqrs/src/main/java/com/iluwatar/cqrs/app/App.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.cqrs.app;
224

325
import java.math.BigInteger;
@@ -12,7 +34,8 @@
1234
import com.iluwatar.cqrs.util.HibernateUtil;
1335

1436
/**
15-
* This is the entry of the application
37+
* CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from commands or writes
38+
* services.
1639
*
1740
*/
1841
public class App {
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.iluwatar.cqrs;
22

3-
import static org.junit.Assert.*;
4-
53
import org.junit.Test;
64

5+
import com.iluwatar.cqrs.app.App;
6+
7+
/**
8+
* Application test
9+
*
10+
*/
711
public class AppTest {
812

913
@Test
1014
public void test() {
11-
fail("Not yet implemented");
15+
String[] args = {};
16+
App.main(args);
1217
}
1318

1419
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.iluwatar.cqrs;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.math.BigInteger;
7+
import java.util.List;
8+
9+
import org.junit.AfterClass;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
13+
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
14+
import com.iluwatar.cqrs.commandes.ICommandService;
15+
import com.iluwatar.cqrs.dto.Author;
16+
import com.iluwatar.cqrs.dto.Book;
17+
import com.iluwatar.cqrs.queries.IQueryService;
18+
import com.iluwatar.cqrs.queries.QueryServiceImpl;
19+
import com.iluwatar.cqrs.util.HibernateUtil;
20+
21+
/**
22+
* Integration test of IQueryService and ICommandService with h2 data
23+
*
24+
*/
25+
public class IntegrationTest {
26+
27+
private static IQueryService queryService;
28+
private static ICommandService commandService;
29+
30+
@BeforeClass
31+
public static void initialize() {
32+
commandService = new CommandServiceImpl();
33+
queryService = new QueryServiceImpl();
34+
}
35+
36+
@BeforeClass
37+
public static void populateDatabase() {
38+
// create first author1
39+
commandService.authorCreated("username1", "name1", "email1");
40+
41+
// create author1 and update all its data
42+
commandService.authorCreated("username2", "name2", "email2");
43+
commandService.authorEmailUpdated("username2", "new_email2");
44+
commandService.authorNameUpdated("username2", "new_name2");
45+
commandService.authorUsernameUpdated("username2", "new_username2");
46+
47+
// add book1 to author1
48+
commandService.bookAddedToAuthor("title1", 10, "username1");
49+
50+
// add book2 to author1 and update all its data
51+
commandService.bookAddedToAuthor("title2", 20, "username1");
52+
commandService.bookPriceUpdated("title2", 30);
53+
commandService.bookTitleUpdated("title2", "new_title2");
54+
55+
}
56+
57+
@Test
58+
public void testGetAuthorByUsername() {
59+
Author author = queryService.getAuthorByUsername("username1");
60+
assertEquals("username1", author.getUsername());
61+
assertEquals("name1", author.getName());
62+
assertEquals("email1", author.getEmail());
63+
}
64+
65+
@Test
66+
public void testGetUpdatedAuthorByUsername() {
67+
Author author = queryService.getAuthorByUsername("new_username2");
68+
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
69+
assertEquals(expectedAuthor, author);
70+
71+
}
72+
73+
@Test
74+
public void testGetBook() {
75+
Book book = queryService.getBook("title1");
76+
assertEquals("title1", book.getTitle());
77+
assertEquals(10, book.getPrice(), 0);
78+
}
79+
80+
@Test
81+
public void testGetAuthorBooks() {
82+
List<Book> books = queryService.getAuthorBooks("username1");
83+
assertTrue(books.size() == 2);
84+
assertTrue(books.contains(new Book("title1", 10)));
85+
assertTrue(books.contains(new Book("new_title2", 30)));
86+
}
87+
88+
@Test
89+
public void testGetAuthorBooksCount() {
90+
BigInteger bookCount = queryService.getAuthorBooksCount("username1");
91+
assertEquals(new BigInteger("2"), bookCount);
92+
}
93+
94+
@Test
95+
public void testGetAuthorsCount() {
96+
BigInteger authorCount = queryService.getAuthorsCount();
97+
assertEquals(new BigInteger("2"), authorCount);
98+
}
99+
100+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE hibernate-configuration SYSTEM
3+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
4+
5+
<hibernate-configuration>
6+
<session-factory>
7+
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
8+
<property name="connection.driver_class">org.h2.Driver</property>
9+
<property name="connection.url">jdbc:h2:mem:test</property>
10+
<property name="connection.username">sa</property>
11+
<property name="hbm2ddl.auto">create</property>
12+
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
13+
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
14+
</session-factory>
15+
</hibernate-configuration>

cqrs/src/test/resources/logback.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="info">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
</configuration>

0 commit comments

Comments
 (0)