Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public interface BookRepository extends CrudRepository<Book, Long> {

@Query("SELECT DISTINCT b FROM Book b LEFT JOIN FETCH b.authors")
List<Book> findAllBooks();


Book findBookByIsbn13(String isbn13);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public Iterable<Book> findAllBooks() {
public Iterable<Author> findAllAuthors() {
return authorRepository.findAllAuthors();
}

public Book findBookByIsbn13(String isbn13) {
return bookRepository.findBookByIsbn13(isbn13);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/graphql/query.graphqls
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
type Query {
findAllBooks: [Book]!
}
findBookByIsbn13(isbn13: String!): Book!
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

import com.karankumar.booksapi.model.Author;
import com.karankumar.booksapi.model.Book;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import com.karankumar.booksapi.model.Language;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +26,9 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Arrays;
import java.util.List;

import static com.karankumar.booksapi.repository.RepositoryTestUtils.createBook;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
Expand All @@ -39,7 +37,8 @@
@ExtendWith(SpringExtension.class)
@DisplayName("BookRepository should")
class BookRepositoryTest {

private static final String ISBN = "978-3-16-148410-0";

private final BookRepository bookRepository;
private final AuthorRepository authorRepository;

Expand Down Expand Up @@ -74,15 +73,35 @@ void findSavedBooks() {
softly.assertThat(result).containsExactlyInAnyOrder(book);
});
}

@Test
@DisplayName("find book with isbn")
void findBookByIsbn() {
// given
createAndSaveAuthors();
Book book = createBookwithISBN();
bookRepository.save(book);

// when
Book result = bookRepository.findBookByIsbn13(ISBN);

// then
assertThat(result).isEqualTo(book);
}

private void createAndSaveAuthors() {
author1 = new Author("Kevlin", "Henney");
author2 = new Author("Trisha", "Gee");
saveAllAuthors(author1, author2);
}

private Book createBookwithISBN() {
Book book = new Book("Game of APIs", new Author[]{author1, author2} , Language.ENGLISH);
book.setIsbn13(ISBN);
return book;
}

private void saveAllAuthors(Author... authors) {
Arrays.stream(authors).forEach(authorRepository::save);
}

}