From b67719ab329f51731664dff9f0dbaf79306674b5 Mon Sep 17 00:00:00 2001 From: Sabiq Ihab Date: Sat, 24 Jun 2017 00:31:43 +0000 Subject: [PATCH] add tests --- .../main/java/com/iluwatar/cqrs/app/App.java | 25 ++++- .../test/java/com/iluwatar/cqrs/AppTest.java | 11 +- .../com/iluwatar/cqrs/IntegrationTest.java | 100 ++++++++++++++++++ cqrs/src/test/resources/hibernate.cfg.xml | 15 +++ cqrs/src/test/resources/logback.xml | 13 +++ 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java create mode 100644 cqrs/src/test/resources/hibernate.cfg.xml create mode 100644 cqrs/src/test/resources/logback.xml diff --git a/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java b/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java index 0fac42e69..063d237e0 100644 --- a/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java +++ b/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java @@ -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 { diff --git a/cqrs/src/test/java/com/iluwatar/cqrs/AppTest.java b/cqrs/src/test/java/com/iluwatar/cqrs/AppTest.java index ce9181370..ad1cf490b 100644 --- a/cqrs/src/test/java/com/iluwatar/cqrs/AppTest.java +++ b/cqrs/src/test/java/com/iluwatar/cqrs/AppTest.java @@ -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); } } diff --git a/cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java b/cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java new file mode 100644 index 000000000..204aa08bc --- /dev/null +++ b/cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java @@ -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 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); + } + +} diff --git a/cqrs/src/test/resources/hibernate.cfg.xml b/cqrs/src/test/resources/hibernate.cfg.xml new file mode 100644 index 000000000..151983337 --- /dev/null +++ b/cqrs/src/test/resources/hibernate.cfg.xml @@ -0,0 +1,15 @@ + + + + + + org.hibernate.dialect.H2Dialect + org.h2.Driver + jdbc:h2:mem:test + sa + create + + + + \ No newline at end of file diff --git a/cqrs/src/test/resources/logback.xml b/cqrs/src/test/resources/logback.xml new file mode 100644 index 000000000..6b5d24345 --- /dev/null +++ b/cqrs/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + +