You have a Spring Boot project with a REST endpoint /books that returns
all books from an in-memory H2 database. The code includes a Book entity,
BookRepository, BookController, and a Google Books integration (/books/google)
exposing a search that returns the upstream Google schema via GoogleBookService.
- Base URL:
https://www.googleapis.com/books/v1 - Search endpoint:
GET /volumes?q={query}(e.g.,q=effective+java, optionalmaxResults,startIndex). - Volume details endpoint:
GET /volumes/{id}to fetch a single book by Google volume ID. - This project uses
GoogleBookServicewith a configurable base URL. Setgoogle.books.base*urlinapplication.properties(or override in tests) to point to the real API or a mock server. The search route is exposed asGET /google?q={query}returning the upstream Google schema.
Implement the following, with accompanying tests for each change (tests are mandatory):
-
Add a new REST endpoint that takes a Google Books volume ID as a parameter and adds the book to your personal list.
- Endpoint:
POST /books/{googleId}(path variablegoogleId). - Behavior:
- Fetch the book details from the Google Books API (via
GoogleBookService). - Map appropriate fields from
GoogleBookto yourBookentity (e.g., id, title, first author, pageCount). - Persist the mapped
BookusingBookRepository. - Return
201 Createdwith the persistedBookin the response body.
- Fetch the book details from the Google Books API (via
- Tests (Spring Boot tests):
- Happy path: valid
googleIdreturns 201 and persists the book with correct fields. - Error path: invalid or missing upstream data returns an appropriate
error (e.g.,
400 Bad Request), and nothing is persisted. - Prefer mocking the downstream (e.g., MockWebServer/WireMock) to avoid flakiness; a smoke test hitting the real API is optional.
- Happy path: valid
- Endpoint:
-
Keep existing functionality intact and verify:
- The existing
GET /booksendpoint still returns all persisted books. - The Google search endpoint
/googlecontinues to return the Google schema payload as-is. - Tests should seed data where needed and assert on JSON responses and status codes.
- The existing
You may refactor or add code as needed, but keep the existing structure. Aim to complete within 30 minutes.