Search in sources :

Example 11 with Book

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());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 12 with Book

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());
}
Also used : Comment(com.aidanwhiteley.books.domain.Comment) Owner(com.aidanwhiteley.books.domain.Owner) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 13 with Book

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));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Book(com.aidanwhiteley.books.domain.Book) URI(java.net.URI) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 14 with Book

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());
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 15 with Book

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));
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) BookRepositoryTest(com.aidanwhiteley.books.repository.BookRepositoryTest) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Aggregations

Book (com.aidanwhiteley.books.domain.Book)23 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)17 Test (org.junit.Test)17 User (com.aidanwhiteley.books.domain.User)15 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)12 URI (java.net.URI)4 Comment (com.aidanwhiteley.books.domain.Comment)3 Owner (com.aidanwhiteley.books.domain.Owner)3 HttpHeaders (org.springframework.http.HttpHeaders)3 Principal (java.security.Principal)2 Around (org.aspectj.lang.annotation.Around)2 PageRequest (org.springframework.data.domain.PageRequest)2 AccessForbiddenException (com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)1 Page (org.springframework.data.domain.Page)1 HttpEntity (org.springframework.http.HttpEntity)1