|
| 1 | +package com.madadipouya.elasticsearch.springdata.example.service.impl; |
| 2 | + |
| 3 | +import com.madadipouya.elasticsearch.springdata.BookElasticsearchContainer; |
| 4 | +import com.madadipouya.elasticsearch.springdata.example.model.Book; |
| 5 | +import com.madadipouya.elasticsearch.springdata.example.service.BookService; |
| 6 | +import com.madadipouya.elasticsearch.springdata.example.service.exception.BookNotFoundException; |
| 7 | +import com.madadipouya.elasticsearch.springdata.example.service.exception.DuplicateIsbnException; |
| 8 | +import org.junit.jupiter.api.AfterAll; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
| 16 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 17 | +import org.testcontainers.elasticsearch.ElasticsearchContainer; |
| 18 | +import org.testcontainers.junit.jupiter.Container; |
| 19 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | + |
| 27 | +@Testcontainers |
| 28 | +@ExtendWith(SpringExtension.class) |
| 29 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 30 | +class DefaultBookServiceIT { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private BookService bookService; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + ElasticsearchTemplate template; |
| 37 | + |
| 38 | + @Container |
| 39 | + private static ElasticsearchContainer elasticsearchContainer = new BookElasticsearchContainer(); |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + static void setUp() { |
| 43 | + elasticsearchContainer.start(); |
| 44 | + } |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void testIsContainerRunning() { |
| 48 | + assertTrue(elasticsearchContainer.isRunning()); |
| 49 | + recreateIndex(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testGetBookByIsbn() throws DuplicateIsbnException { |
| 54 | + bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 55 | + Optional<Book> result = bookService.getByIsbn("978-0345816023"); |
| 56 | + assertTrue(result.isPresent()); |
| 57 | + Book createdBook = result.get(); |
| 58 | + assertNotNull(createdBook); |
| 59 | + assertEquals("12 rules for life", createdBook.getTitle()); |
| 60 | + assertEquals("Jordan Peterson", createdBook.getAuthorName()); |
| 61 | + assertEquals(2018, createdBook.getPublicationYear()); |
| 62 | + assertEquals("978-0345816023", createdBook.getIsbn()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testGetAllBooks() throws DuplicateIsbnException { |
| 67 | + bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 68 | + bookService.create(createBook("The Cathedral and the Bazaar", "Eric Raymond", 1999, "9780596106386")); |
| 69 | + List<Book> books = bookService.getAll(); |
| 70 | + |
| 71 | + assertNotNull(books); |
| 72 | + assertEquals(2, books.size()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testFindByAuthor() throws DuplicateIsbnException { |
| 77 | + bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 78 | + bookService.create(createBook("Maps of Meaning", "Jordan Peterson", 1999, "9781280407253")); |
| 79 | + |
| 80 | + List<Book> books = bookService.findByAuthor("Jordan Peterson"); |
| 81 | + |
| 82 | + assertNotNull(books); |
| 83 | + assertEquals(2, books.size()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testFindByTitleAndAuthor() throws DuplicateIsbnException { |
| 88 | + bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 89 | + bookService.create(createBook("Rules or not rules?", "Jordan Miller", 2010, "978128000000")); |
| 90 | + bookService.create(createBook("Poor economy", "Jordan Miller", 2006, "9781280789000")); |
| 91 | + bookService.create(createBook("The Cathedral and the Bazaar", "Eric Raymond", 1999, "9780596106386")); |
| 92 | + |
| 93 | + List<Book> books = bookService.findByTitleAndAuthor("rules", "jordan"); |
| 94 | + |
| 95 | + assertNotNull(books); |
| 96 | + assertEquals(2, books.size()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testCreateBook() throws DuplicateIsbnException { |
| 101 | + Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 102 | + assertNotNull(createdBook); |
| 103 | + assertNotNull(createdBook.getId()); |
| 104 | + assertEquals("12 rules for life", createdBook.getTitle()); |
| 105 | + assertEquals("Jordan Peterson", createdBook.getAuthorName()); |
| 106 | + assertEquals(2018, createdBook.getPublicationYear()); |
| 107 | + assertEquals("978-0345816023", createdBook.getIsbn()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testCreateBookWithDuplicateISBNThrowsException() throws DuplicateIsbnException { |
| 112 | + Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 113 | + assertNotNull(createdBook); |
| 114 | + assertThrows(DuplicateIsbnException.class, () -> { |
| 115 | + bookService.create(createBook("Test title", "Test author", 2010, "978-0345816023")); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testDeleteBookById() throws DuplicateIsbnException { |
| 121 | + Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023")); |
| 122 | + |
| 123 | + assertNotNull(createdBook); |
| 124 | + assertNotNull(createdBook.getId()); |
| 125 | + |
| 126 | + bookService.deleteById(createdBook.getId()); |
| 127 | + List<Book> books = bookService.findByAuthor("Jordan Peterson"); |
| 128 | + |
| 129 | + assertTrue(books.isEmpty()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testUpdateBook() throws DuplicateIsbnException, BookNotFoundException { |
| 134 | + Book bookToUpdate = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2000, "978-0345816023")); |
| 135 | + |
| 136 | + assertNotNull(bookToUpdate); |
| 137 | + assertNotNull(bookToUpdate.getId()); |
| 138 | + |
| 139 | + bookToUpdate.setPublicationYear(2018); |
| 140 | + Book updatedBook = bookService.update(bookToUpdate.getId(), bookToUpdate); |
| 141 | + |
| 142 | + assertNotNull(updatedBook); |
| 143 | + assertNotNull(updatedBook.getId()); |
| 144 | + assertEquals("12 rules for life", updatedBook.getTitle()); |
| 145 | + assertEquals("Jordan Peterson", updatedBook.getAuthorName()); |
| 146 | + assertEquals(2018, updatedBook.getPublicationYear()); |
| 147 | + assertEquals("978-0345816023", updatedBook.getIsbn()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testUpdateBookThrowsExceptionIfCannotFindBook() { |
| 152 | + Book updatedBook = createBook("12 rules for life", "Jordan Peterson", 2000, "978-0345816023"); |
| 153 | + |
| 154 | + assertThrows(BookNotFoundException.class, () -> { |
| 155 | + bookService.update("1A2B3C", updatedBook); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + private Book createBook(String title, String authorName, int publicationYear, String isbn) { |
| 160 | + Book book = new Book(); |
| 161 | + book.setTitle(title); |
| 162 | + book.setAuthorName(authorName); |
| 163 | + book.setPublicationYear(publicationYear); |
| 164 | + book.setIsbn(isbn); |
| 165 | + return book; |
| 166 | + } |
| 167 | + |
| 168 | + private void recreateIndex() { |
| 169 | + if (template.indexExists(Book.class)) { |
| 170 | + template.deleteIndex(Book.class); |
| 171 | + template.createIndex(Book.class); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @AfterAll |
| 176 | + static void destroy() { |
| 177 | + elasticsearchContainer.stop(); |
| 178 | + } |
| 179 | +} |
0 commit comments