Skip to content

Commit 0f450e5

Browse files
committed
implementacao da camada de service de Book & testes;
1 parent 35a1032 commit 0f450e5

File tree

5 files changed

+398
-0
lines changed

5 files changed

+398
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.library.app.book.exception;
2+
3+
import javax.ejb.ApplicationException;
4+
5+
/**
6+
* @author gabriel.freitas
7+
*/
8+
@ApplicationException
9+
public class BookNotFoundException extends RuntimeException {
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.library.app.book.services;
2+
3+
import com.library.app.author.exception.AuthorNotFoundException;
4+
import com.library.app.book.exception.BookNotFoundException;
5+
import com.library.app.book.model.Book;
6+
import com.library.app.book.model.filter.BookFilter;
7+
import com.library.app.category.exception.CategoryNotFoundException;
8+
import com.library.app.common.exception.FieldNotValidException;
9+
import com.library.app.common.model.PaginatedData;
10+
11+
import javax.ejb.Local;
12+
13+
/**
14+
* @author gabriel.freitas
15+
*/
16+
@Local
17+
public interface BookServices {
18+
19+
Book add(Book book) throws FieldNotValidException, CategoryNotFoundException, AuthorNotFoundException;
20+
21+
void update(Book book) throws FieldNotValidException, CategoryNotFoundException, AuthorNotFoundException, BookNotFoundException;
22+
23+
Book findById(Long id) throws BookNotFoundException;
24+
25+
PaginatedData<Book> findByFilter(BookFilter bookFilter);
26+
27+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.library.app.book.services.impl;
2+
3+
import com.library.app.author.model.Author;
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.model.Category;
11+
import com.library.app.category.services.CategoryServices;
12+
import com.library.app.common.model.PaginatedData;
13+
import com.library.app.common.utils.ValidationUtils;
14+
15+
import javax.ejb.Stateless;
16+
import javax.inject.Inject;
17+
import javax.validation.Validator;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
/**
22+
* @author gabriel.freitas
23+
*/
24+
@Stateless
25+
public class BookServicesImpl implements BookServices {
26+
27+
@Inject
28+
BookRepository bookRepository;
29+
30+
@Inject
31+
Validator validator;
32+
33+
@Inject
34+
AuthorServices authorServices;
35+
36+
@Inject
37+
CategoryServices categoryServices;
38+
39+
@Override
40+
public Book add(final Book book) {
41+
ValidationUtils.validateEntityFields(validator, book);
42+
43+
checkCategoryAndSetItOnBook(book);
44+
checkAuthorsAndSetThemOnBook(book);
45+
46+
return bookRepository.add(book);
47+
}
48+
49+
@Override
50+
public void update(final Book book) {
51+
ValidationUtils.validateEntityFields(validator, book);
52+
53+
if (!bookRepository.existsById(book.getId())) {
54+
throw new BookNotFoundException();
55+
}
56+
57+
checkCategoryAndSetItOnBook(book);
58+
checkAuthorsAndSetThemOnBook(book);
59+
60+
bookRepository.update(book);
61+
}
62+
63+
@Override
64+
public Book findById(final Long id) {
65+
final Book book = bookRepository.findById(id);
66+
if (book == null) {
67+
throw new BookNotFoundException();
68+
}
69+
return book;
70+
}
71+
72+
@Override
73+
public PaginatedData<Book> findByFilter(final BookFilter bookFilter) {
74+
return bookRepository.findByFilter(bookFilter);
75+
}
76+
77+
private void checkAuthorsAndSetThemOnBook(final Book book) {
78+
final List<Author> newAuthorList = new ArrayList<>();
79+
for (final Author author : book.getAuthors()) {
80+
final Author authorExistent = authorServices.findById(author.getId());
81+
newAuthorList.add(authorExistent);
82+
}
83+
book.setAuthors(newAuthorList);
84+
}
85+
86+
private void checkCategoryAndSetItOnBook(final Book book) {
87+
final Category category = categoryServices.findById(book.getCategory().getId());
88+
book.setCategory(category);
89+
}
90+
91+
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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

Comments
 (0)