use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookRepositoryTest method findByAuthor.
@Test
public void findByAuthor() {
PageRequest pageObj = PageRequest.of(PAGE, PAGE_SIZE);
Page<Book> books = bookRepository.findAllByAuthorOrderByEnteredDesc(pageObj, DR_ZEUSS);
assertTrue(books.getContent().size() >= 1);
assertTrue(books.getContent().get(0).getAuthor().equals(DR_ZEUSS));
// The book should have a system created id value.
assertNotNull(books.getContent().get(0).getId());
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookRepositoryTest method removeCommentFromBook.
@Test
public void removeCommentFromBook() {
// Set up a couple of comments
Book book = createTestBook();
Book savedBook = bookRepository.insert(book);
Comment comment = new Comment(A_COMMENT, new Owner());
bookRepository.addCommentToBook(savedBook.getId(), comment);
comment = new Comment(ANOTHER_COMMENT, new Owner());
bookRepository.addCommentToBook(savedBook.getId(), comment);
// noinspection ConstantConditions
Book updatedBook = bookRepository.findById(savedBook.getId()).get();
assertEquals(2, updatedBook.getComments().size());
// Returned Book holds just the updated comments
updatedBook = bookRepository.removeCommentFromBook(savedBook.getId(), updatedBook.getComments().get(0).getId(), COMMENT_REMOVER);
// There should still be two comments but the first should now be "marked" as deleted
assertEquals(2, updatedBook.getComments().size());
assertEquals("", updatedBook.getComments().get(0).getCommentText());
assertTrue(updatedBook.getComments().get(0).isDeleted());
assertEquals(COMMENT_REMOVER, updatedBook.getComments().get(0).getDeletedBy());
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookControllerTest method findBookById.
@Test
public void findBookById() {
ResponseEntity<Book> response = BookControllerTestUtils.postBookToServer(jwtUtils, testRestTemplate);
HttpHeaders headers = response.getHeaders();
URI uri = headers.getLocation();
Book book = testRestTemplate.getForObject(uri, Book.class);
assertEquals(book.getId(), uri.getPath().substring(uri.getPath().lastIndexOf("/") + 1));
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookSecureControllerTest method tryToCreateBookWithInsufficientPermissions.
@Test
public void tryToCreateBookWithInsufficientPermissions() {
Book testBook = BookRepositoryTest.createTestBook();
// Set up user with just the ROLE_USER role
User user = BookControllerTestUtils.getTestUser();
user.removeRole(Role.ROLE_ADMIN);
user.removeRole(Role.ROLE_EDITOR);
String token = jwtUtils.createTokenForUser(user);
HttpEntity<Book> putData = BookControllerTestUtils.getBookHttpEntity(testBook, user, token);
ResponseEntity<Book> postResponse = testRestTemplate.exchange("/secure/api/books", HttpMethod.POST, putData, Book.class);
// See comments in the tryToCreateBookWithNoPermissions test for why a 302 is expected.
assertEquals(HttpStatus.FOUND, postResponse.getStatusCode());
}
use of com.aidanwhiteley.books.domain.Book in project books by aidanwhiteley.
the class BookSecureControllerTest method testDebugHeaders.
@Test
public void testDebugHeaders() {
User user = BookControllerTestUtils.getTestUser();
String token = jwtUtils.createTokenForUser(user);
// Re-using "book related" code to get required headers easily set up
Book testBook = BookRepositoryTest.createTestBook();
HttpEntity<Book> request = BookControllerTestUtils.getBookHttpEntity(testBook, user, token, null);
ResponseEntity<String> response = testRestTemplate.exchange("/secure/api/debugheaders", HttpMethod.GET, request, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.getBody().contains(JwtAuthenticationService.JWT_COOKIE_NAME));
}
Aggregations