|
| 1 | +package com.library.app.book.services.impl; |
| 2 | + |
| 3 | +import com.library.app.author.exception.AuthorNotFoundException; |
| 4 | +import com.library.app.author.services.AuthorServices; |
| 5 | +import com.library.app.book.exception.BookNotFoundException; |
| 6 | +import com.library.app.book.model.Book; |
| 7 | +import com.library.app.book.model.filter.BookFilter; |
| 8 | +import com.library.app.book.repository.BookRepository; |
| 9 | +import com.library.app.book.services.BookServices; |
| 10 | +import com.library.app.category.exception.CategoryNotFoundException; |
| 11 | +import com.library.app.category.services.CategoryServices; |
| 12 | +import com.library.app.common.exception.FieldNotValidException; |
| 13 | +import com.library.app.common.model.PaginatedData; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.MockitoAnnotations; |
| 19 | + |
| 20 | +import javax.validation.Validation; |
| 21 | +import javax.validation.Validator; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | + |
| 25 | +import static com.library.app.commontests.author.AuthorForTestsRepository.erichGamma; |
| 26 | +import static com.library.app.commontests.author.AuthorForTestsRepository.robertMartin; |
| 27 | +import static com.library.app.commontests.book.BookArgumentMatcher.*; |
| 28 | +import static com.library.app.commontests.book.BookForTestsRepository.*; |
| 29 | +import static org.hamcrest.CoreMatchers.*; |
| 30 | +import static org.junit.Assert.assertThat; |
| 31 | +import static org.junit.Assert.fail; |
| 32 | +import static org.mockito.Matchers.anyLong; |
| 33 | +import static org.mockito.Matchers.anyObject; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author gabriel.freitas |
| 39 | + */ |
| 40 | +public class BookServicesUTest { |
| 41 | + |
| 42 | + private static Validator validator; |
| 43 | + private BookServices bookServices; |
| 44 | + |
| 45 | + @Mock |
| 46 | + private BookRepository bookRepository; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private CategoryServices categoryServices; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private AuthorServices authorServices; |
| 53 | + |
| 54 | + @BeforeClass |
| 55 | + public static void initTestClass() { |
| 56 | + validator = Validation.buildDefaultValidatorFactory().getValidator(); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void initTestCase() { |
| 61 | + MockitoAnnotations.initMocks(this); |
| 62 | + |
| 63 | + bookServices = new BookServicesImpl(); |
| 64 | + |
| 65 | + ((BookServicesImpl) bookServices).bookRepository = bookRepository; |
| 66 | + ((BookServicesImpl) bookServices).validator = validator; |
| 67 | + ((BookServicesImpl) bookServices).categoryServices = categoryServices; |
| 68 | + ((BookServicesImpl) bookServices).authorServices = authorServices; |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void addBookWithNullTitle() { |
| 73 | + final Book book = cleanCode(); |
| 74 | + book.setTitle(null); |
| 75 | + addBookWithInvalidField(book, "title"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void addBookWithNullCategory() { |
| 80 | + final Book book = cleanCode(); |
| 81 | + book.setCategory(null); |
| 82 | + addBookWithInvalidField(book, "category"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void addBookWithNoAuthors() { |
| 87 | + final Book book = cleanCode(); |
| 88 | + book.setAuthors(new ArrayList<>()); |
| 89 | + addBookWithInvalidField(book, "authors"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void addBookWithShortDescription() { |
| 94 | + final Book book = cleanCode(); |
| 95 | + book.setDescription("short"); |
| 96 | + addBookWithInvalidField(book, "description"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void addBookWithNullPrice() { |
| 101 | + final Book book = cleanCode(); |
| 102 | + book.setPrice(null); |
| 103 | + addBookWithInvalidField(book, "price"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = CategoryNotFoundException.class) |
| 107 | + public void addBookWithInexistentCategory() { |
| 108 | + when(categoryServices.findById(1L)).thenThrow(new CategoryNotFoundException()); |
| 109 | + |
| 110 | + final Book book = cleanCode(); |
| 111 | + book.getCategory().setId(1L); |
| 112 | + |
| 113 | + bookServices.add(book); |
| 114 | + } |
| 115 | + |
| 116 | + @Test(expected = AuthorNotFoundException.class) |
| 117 | + public void addBookWithInexistentAuthor() throws Exception { |
| 118 | + when(categoryServices.findById(anyLong())).thenReturn(designPatterns().getCategory()); |
| 119 | + when(authorServices.findById(1L)).thenReturn(erichGamma()); |
| 120 | + when(authorServices.findById(2L)).thenThrow(new AuthorNotFoundException()); |
| 121 | + |
| 122 | + final Book book = designPatterns(); |
| 123 | + book.getAuthors().get(0).setId(1L); |
| 124 | + book.getAuthors().get(1).setId(2L); |
| 125 | + |
| 126 | + bookServices.add(book); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void addValidBook() throws Exception { |
| 131 | + when(categoryServices.findById(anyLong())).thenReturn(cleanCode().getCategory()); |
| 132 | + when(authorServices.findById(anyLong())).thenReturn(robertMartin()); |
| 133 | + when(bookRepository.add(bookEq(cleanCode()))).thenReturn(bookWithId(cleanCode(), 1L)); |
| 134 | + |
| 135 | + final Book bookAdded = bookServices.add(cleanCode()); |
| 136 | + assertThat(bookAdded.getId(), equalTo(1L)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void updateAuthorWithShortTitle() { |
| 141 | + final Book book = cleanCode(); |
| 142 | + book.setTitle("short"); |
| 143 | + try { |
| 144 | + bookServices.update(book); |
| 145 | + fail("An error should have been thrown"); |
| 146 | + } catch (final FieldNotValidException e) { |
| 147 | + assertThat(e.getFieldName(), is(equalTo("title"))); |
| 148 | + } catch (final Exception e) { |
| 149 | + fail("An Exception should not have been thrown"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test(expected = BookNotFoundException.class) |
| 154 | + public void updateBookNotFound() throws Exception { |
| 155 | + when(bookRepository.existsById(1L)).thenReturn(false); |
| 156 | + |
| 157 | + bookServices.update(bookWithId(cleanCode(), 1L)); |
| 158 | + } |
| 159 | + |
| 160 | + @Test(expected = CategoryNotFoundException.class) |
| 161 | + public void updateBookWithInexistentCategory() throws Exception { |
| 162 | + when(bookRepository.existsById(1L)).thenReturn(true); |
| 163 | + when(categoryServices.findById(1L)).thenThrow(new CategoryNotFoundException()); |
| 164 | + |
| 165 | + final Book book = bookWithId(cleanCode(), 1L); |
| 166 | + book.getCategory().setId(1L); |
| 167 | + |
| 168 | + bookServices.update(book); |
| 169 | + } |
| 170 | + |
| 171 | + @Test(expected = AuthorNotFoundException.class) |
| 172 | + public void updateBookWithInexistentAuthor() throws Exception { |
| 173 | + when(bookRepository.existsById(1L)).thenReturn(true); |
| 174 | + when(categoryServices.findById(anyLong())).thenReturn(cleanCode().getCategory()); |
| 175 | + when(authorServices.findById(1L)).thenReturn(erichGamma()); |
| 176 | + when(authorServices.findById(2L)).thenThrow(new AuthorNotFoundException()); |
| 177 | + |
| 178 | + final Book book = bookWithId(designPatterns(), 1L); |
| 179 | + book.getAuthors().get(0).setId(1L); |
| 180 | + book.getAuthors().get(1).setId(2L); |
| 181 | + |
| 182 | + bookServices.update(book); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void updateValidBook() throws Exception { |
| 187 | + final Book bookToUpdate = bookWithId(cleanCode(), 1L); |
| 188 | + when(categoryServices.findById(anyLong())).thenReturn(cleanCode().getCategory()); |
| 189 | + when(authorServices.findById(anyLong())).thenReturn(robertMartin()); |
| 190 | + when(bookRepository.existsById(1L)).thenReturn(true); |
| 191 | + |
| 192 | + bookServices.update(bookToUpdate); |
| 193 | + verify(bookRepository).update(bookEq(bookToUpdate)); |
| 194 | + } |
| 195 | + |
| 196 | + @Test(expected = BookNotFoundException.class) |
| 197 | + public void findBookByIdNotFound() throws BookNotFoundException { |
| 198 | + when(bookRepository.findById(1L)).thenReturn(null); |
| 199 | + |
| 200 | + bookServices.findById(1L); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void findBookByFilter() { |
| 205 | + final PaginatedData<Book> books = new PaginatedData<Book>(1, Arrays.asList(bookWithId(cleanCode(), 1L))); |
| 206 | + when(bookRepository.findByFilter((BookFilter) anyObject())).thenReturn(books); |
| 207 | + |
| 208 | + final PaginatedData<Book> booksReturned = bookServices.findByFilter(new BookFilter()); |
| 209 | + assertThat(booksReturned.getNumberOfRows(), is(equalTo(1))); |
| 210 | + assertThat(booksReturned.getRow(0).getTitle(), is(equalTo(cleanCode().getTitle()))); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void findBookById() throws BookNotFoundException { |
| 215 | + when(bookRepository.findById(1L)).thenReturn(bookWithId(cleanCode(), 1L)); |
| 216 | + |
| 217 | + final Book book = bookServices.findById(1L); |
| 218 | + assertThat(book, is(notNullValue())); |
| 219 | + assertThat(book.getTitle(), is(equalTo(cleanCode().getTitle()))); |
| 220 | + } |
| 221 | + |
| 222 | + private void addBookWithInvalidField(final Book book, final String expectedInvalidFieldName) { |
| 223 | + try { |
| 224 | + bookServices.add(book); |
| 225 | + fail("An error should have been thrown"); |
| 226 | + } catch (final FieldNotValidException e) { |
| 227 | + assertThat(e.getFieldName(), is(equalTo(expectedInvalidFieldName))); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | +} |
0 commit comments