add tests

This commit is contained in:
Sabiq Ihab 2017-06-24 00:31:43 +00:00
parent a8f50297eb
commit b67719ab32
5 changed files with 160 additions and 4 deletions

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.cqrs.app;
import java.math.BigInteger;
@ -12,7 +34,8 @@ import com.iluwatar.cqrs.queries.QueryServiceImpl;
import com.iluwatar.cqrs.util.HibernateUtil;
/**
* This is the entry of the application
* CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from commands or writes
* services.
*
*/
public class App {

View File

@ -1,14 +1,19 @@
package com.iluwatar.cqrs;
import static org.junit.Assert.*;
import org.junit.Test;
import com.iluwatar.cqrs.app.App;
/**
* Application test
*
*/
public class AppTest {
@Test
public void test() {
fail("Not yet implemented");
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,100 @@
package com.iluwatar.cqrs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.math.BigInteger;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
import com.iluwatar.cqrs.queries.QueryServiceImpl;
import com.iluwatar.cqrs.util.HibernateUtil;
/**
* Integration test of IQueryService and ICommandService with h2 data
*
*/
public class IntegrationTest {
private static IQueryService queryService;
private static ICommandService commandService;
@BeforeClass
public static void initialize() {
commandService = new CommandServiceImpl();
queryService = new QueryServiceImpl();
}
@BeforeClass
public static void populateDatabase() {
// create first author1
commandService.authorCreated("username1", "name1", "email1");
// create author1 and update all its data
commandService.authorCreated("username2", "name2", "email2");
commandService.authorEmailUpdated("username2", "new_email2");
commandService.authorNameUpdated("username2", "new_name2");
commandService.authorUsernameUpdated("username2", "new_username2");
// add book1 to author1
commandService.bookAddedToAuthor("title1", 10, "username1");
// add book2 to author1 and update all its data
commandService.bookAddedToAuthor("title2", 20, "username1");
commandService.bookPriceUpdated("title2", 30);
commandService.bookTitleUpdated("title2", "new_title2");
}
@Test
public void testGetAuthorByUsername() {
Author author = queryService.getAuthorByUsername("username1");
assertEquals("username1", author.getUsername());
assertEquals("name1", author.getName());
assertEquals("email1", author.getEmail());
}
@Test
public void testGetUpdatedAuthorByUsername() {
Author author = queryService.getAuthorByUsername("new_username2");
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
assertEquals(expectedAuthor, author);
}
@Test
public void testGetBook() {
Book book = queryService.getBook("title1");
assertEquals("title1", book.getTitle());
assertEquals(10, book.getPrice(), 0);
}
@Test
public void testGetAuthorBooks() {
List<Book> books = queryService.getAuthorBooks("username1");
assertTrue(books.size() == 2);
assertTrue(books.contains(new Book("title1", 10)));
assertTrue(books.contains(new Book("new_title2", 30)));
}
@Test
public void testGetAuthorBooksCount() {
BigInteger bookCount = queryService.getAuthorBooksCount("username1");
assertEquals(new BigInteger("2"), bookCount);
}
@Test
public void testGetAuthorsCount() {
BigInteger authorCount = queryService.getAuthorsCount();
assertEquals(new BigInteger("2"), authorCount);
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>