|
| 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
F438
= 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 | +} |
0 commit comments