Search in sources :

Example 1 with Item

use of com.aidanwhiteley.books.domain.googlebooks.Item in project books by aidanwhiteley.

the class GoogleBooksDaoSycTest method findByGoogleBookId.

@Test
public void findByGoogleBookId() {
    Item result = theDao.searchGoogleBooksByGoogleBookId(SPRING_FRAMEWORK_GOOGLE_BOOK_ID);
    assertNotNull(result);
    assertEquals(SPRING_BOOK_TITLE, result.getVolumeInfo().getTitle());
    assertEquals(false, result.getAccessInfo().isPublicDomain());
    assertNotNull(result.getVolumeInfo().getImageLinks().getThumbnail());
}
Also used : Item(com.aidanwhiteley.books.domain.googlebooks.Item) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest) Test(org.junit.Test)

Example 2 with Item

use of com.aidanwhiteley.books.domain.googlebooks.Item in project books by aidanwhiteley.

the class GoogleBooksDaoSyncTest method findByGoogleBookId.

@Test
void findByGoogleBookId() {
    Item result = theDao.searchGoogleBooksByGoogleBookId(SPRING_FRAMEWORK_GOOGLE_BOOK_ID);
    assertNotNull(result);
    assertEquals(SPRING_BOOK_TITLE, result.getVolumeInfo().getTitle());
    assertFalse(result.getAccessInfo().isPublicDomain());
    assertNotNull(result.getVolumeInfo().getImageLinks().getThumbnail());
}
Also used : Item(com.aidanwhiteley.books.domain.googlebooks.Item) Test(org.junit.jupiter.api.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 3 with Item

use of com.aidanwhiteley.books.domain.googlebooks.Item in project books by aidanwhiteley.

the class BookSecureControllerWithMocksTest method updateBookSetSameGoogleBookData.

@Test
void updateBookSetSameGoogleBookData() {
    Book book1 = Book.builder().id(BOOK_ID_1).googleBookId(GOOGLE_BOOK_ID_1).googleBookDetails(new Item()).build();
    Principal principal = initTest(book1);
    BookSecureController controller = new BookSecureController(bookRepository, googleBooksDaoSync, null, jwtAuthenticationUtils);
    controller.updateBook(book1, principal);
    verify(googleBooksDaoSync, times(0)).searchGoogleBooksByGoogleBookId(anyString());
    verify(bookRepository, times(1)).save(book1);
}
Also used : Item(com.aidanwhiteley.books.domain.googlebooks.Item) Book(com.aidanwhiteley.books.domain.Book) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 4 with Item

use of com.aidanwhiteley.books.domain.googlebooks.Item in project books by aidanwhiteley.

the class GoogleBooksDaoAsync method updateBookWithGoogleBookDetails.

/**
 * The use of the Spring 5 WebClient in this code is more out of a desire to experiment
 * with it rather than for any performance gains.
 * <p>
 * We are calling block() on the Mono so the thread is going to be blocked until
 * the full response from Google Books API has been received. So that will
 * negate any of the usual non blocking benefits of the WebClient usage.
 * <p>
 * We're also continuing to use the blocking Mongo driver in this method.
 * <p>
 * The real benefit to this method is that it is @Async and, therefore, the
 * calling users HTTP call isn't blocked waiting while we go and get / store
 * details from the Google Books API.
 *
 * @param book         Details of the book to update
 * @param googleBookId The Google Books API book id to retrieve.
 */
@Async("threadPoolExecutor")
public void updateBookWithGoogleBookDetails(Book book, String googleBookId) {
    LOGGER.debug("Entered updateBookWithGoogleBookDetails");
    try {
        Mono<Item> monoItem = this.webClient.get().uri(googleBooksApiConfig.getGetByIdUrl() + googleBookId + "/?" + googleBooksApiConfig.getCountryCode()).retrieve().bodyToMono(Item.class);
        LOGGER.debug("Mono created");
        Item item = monoItem.block(Duration.ofSeconds(googleBooksApiConfig.getReadTimeout()));
        LOGGER.debug("Block completed");
        bookRepository.addGoogleBookItemToBook(book.getId(), item);
        LOGGER.debug("Google Books details added to Mongo for {}", book.getId());
    } catch (RuntimeException re) {
        LOGGER.error("Error retrieving or storing Google Book details for {}", googleBookId, re);
    }
}
Also used : Item(com.aidanwhiteley.books.domain.googlebooks.Item) Async(org.springframework.scheduling.annotation.Async)

Example 5 with Item

use of com.aidanwhiteley.books.domain.googlebooks.Item in project books by aidanwhiteley.

the class BookSecureController method updateBook.

@PutMapping(value = "/books")
public ResponseEntity<Book> updateBook(@Valid @RequestBody Book book, Principal principal) {
    Optional<User> user = authUtils.extractUserFromPrincipal(principal, false);
    if (user.isPresent()) {
        Book currentBookState = bookRepository.findById(book.getId()).orElseThrow(() -> new IllegalArgumentException("Didnt find book to update"));
        if (currentBookState.isOwner(user.get()) || user.get().getRoles().contains(User.Role.ROLE_ADMIN)) {
            boolean inputHasGoogleBookId = book.getGoogleBookId() != null && (!book.getGoogleBookId().isEmpty());
            if ((inputHasGoogleBookId && currentBookState.getGoogleBookDetails() == null) || (currentBookState.getGoogleBookId() != null && book.getGoogleBookId() != null && (!currentBookState.getGoogleBookId().equalsIgnoreCase(book.getGoogleBookId())))) {
                // Retrieve and update Google Book details synchronously
                Item item = googleBooksDaoSync.searchGoogleBooksByGoogleBookId(book.getGoogleBookId());
                book.setGoogleBookDetails(item);
            } else if (book.getGoogleBookId() == null || book.getGoogleBookId().isEmpty()) {
                book.setGoogleBookDetails(null);
            }
            bookRepository.save(book);
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
    } else {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
}
Also used : Item(com.aidanwhiteley.books.domain.googlebooks.Item) User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

Item (com.aidanwhiteley.books.domain.googlebooks.Item)5 Book (com.aidanwhiteley.books.domain.Book)2 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)2 Test (org.junit.jupiter.api.Test)2 User (com.aidanwhiteley.books.domain.User)1 Principal (java.security.Principal)1 Test (org.junit.Test)1 Async (org.springframework.scheduling.annotation.Async)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1