Search in sources :

Example 1 with Owner

use of com.aidanwhiteley.books.domain.Owner in project books by aidanwhiteley.

the class BookRepositoryTest method removeCommentFromBook.

@Test
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);
    Optional<Book> oBook = bookRepository.findById(savedBook.getId());
    Book updatedBook = null;
    if (oBook.isPresent()) {
        // noinspection OptionalGetWithoutIsPresent
        updatedBook = bookRepository.findById(savedBook.getId()).get();
    } else {
        fail("Optional not expected to be empty");
    }
    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) BookControllerTest(com.aidanwhiteley.books.controller.BookControllerTest) Test(org.junit.jupiter.api.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 2 with Owner

use of com.aidanwhiteley.books.domain.Owner in project books by aidanwhiteley.

the class BookSecureController method createBook.

@RequestMapping(value = "/books", method = POST)
public ResponseEntity<Book> createBook(@Valid @RequestBody Book book, Principal principal, HttpServletRequest request) throws MalformedURLException, URISyntaxException {
    Optional<User> user = authUtils.extractUserFromPrincipal(principal, false);
    if (user.isPresent()) {
        book.setCreatedBy(new Owner(user.get()));
        // implementation.
        if (book.getGoogleBookId() != null && book.getGoogleBookId().length() > 0) {
            book.setGoogleBookDetails(googleBooksDaoSync.searchGoogleBooksByGoogleBookId(book.getGoogleBookId()));
        }
        Book insertedBook = bookRepository.insert(book);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(insertedBook.getId()).toUri();
        // Basic GET of book details are not on a secure API
        location = new URI(location.toURL().toString().replaceAll("/secure", ""));
        return ResponseEntity.created(location).build();
    } else {
        LOGGER.error("Couldnt create a book as user to own book not found! Principal: {}", principal);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
}
Also used : Owner(com.aidanwhiteley.books.domain.Owner) User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) URI(java.net.URI)

Example 3 with Owner

use of com.aidanwhiteley.books.domain.Owner in project books by aidanwhiteley.

the class BookRepositoryTest method addCommentToBook.

@Test
void addCommentToBook() {
    Book book = createTestBook();
    Book savedBook = bookRepository.insert(book);
    Comment comment = new Comment(A_COMMENT, new Owner());
    // Returned book holds just the Book's comments - no other data other than the book id.
    Book updatedBook = bookRepository.addCommentToBook(savedBook.getId(), comment);
    assertEquals(1, updatedBook.getComments().size());
    assertEquals(A_COMMENT, updatedBook.getComments().get(0).getCommentText());
}
Also used : Comment(com.aidanwhiteley.books.domain.Comment) Owner(com.aidanwhiteley.books.domain.Owner) Book(com.aidanwhiteley.books.domain.Book) BookControllerTest(com.aidanwhiteley.books.controller.BookControllerTest) Test(org.junit.jupiter.api.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Aggregations

Book (com.aidanwhiteley.books.domain.Book)3 Owner (com.aidanwhiteley.books.domain.Owner)3 BookControllerTest (com.aidanwhiteley.books.controller.BookControllerTest)2 Comment (com.aidanwhiteley.books.domain.Comment)2 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)2 Test (org.junit.jupiter.api.Test)2 User (com.aidanwhiteley.books.domain.User)1 URI (java.net.URI)1