Skip to content

feat: Add menu item description and rating fields (v1.0.1) - #6

Open
fxb-google wants to merge 5 commits into
mainfrom
feature/menu-description-rating
Open

fxb-google wants to merge 5 commits into
mainfrom
feature/menu-description-rating

Conversation

@fxb-google

Copy link
Copy Markdown
Owner

This PR implements the requested updates to the menu-service app:

  1. New Fields: Added description and rating to the Menu JPA entity.
  2. API Input Validation: Validates rating on POST /menu (must be non-null and between 1 and 5) and on PUT /menu/{id} (validates range if supplied), returning HTTP 400 Bad Request on failure.
  3. Version Bump: Bumps displayed version from 1.0.0 to 1.0.1 in pom.xml and index.html.
  4. Comprehensive Unit Tests: Created robust test coverage in MenuResourceTest.java asserting successful API paths and the input validation constraint rules. All 6 tests execute and pass successfully.
@fxb-google

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds description and rating fields to the Menu entity, implements validation logic (ratings must be between 1 and 5) in the MenuResource endpoints, updates unit tests to cover these new fields, and switches the database configuration from H2 to PostgreSQL. The review feedback highlights a potential data-overwriting bug in the update method where partial updates can silently reset the primitive spiceLevel field to 0. Additionally, it is recommended to extract the duplicated rating validation logic into a helper method and to revert the test database configuration back to H2 to avoid unnecessary Docker dependencies in CI/CD environments.


/** Tests updating a menu item with valid rating and description. */
@Test
public void testUpdateMenuSuccess() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In testUpdateMenuSuccess, the update payload only contains description and rating. However, because spiceLevel in Menu.java is a primitive int and is updated unconditionally in MenuResource.java (line 150: entity.spiceLevel = menu.spiceLevel;), any partial update that omits spiceLevel will silently reset it to 0 (the default value for int).

To fix this data-overwriting bug, consider:

  1. Changing spiceLevel from int to Integer in Menu.java.
  2. Wrapping the assignment in MenuResource.java with a null check:
if (menu.spiceLevel != null) {
    entity.spiceLevel = menu.spiceLevel;
}
Comment on lines +164 to +166
if (menu.rating < 1 || menu.rating > 5) {
throw new WebApplicationException("Rating must be an integer between 1 and 5", 400);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The rating validation logic (rating < 1 || rating > 5) and its corresponding error message are duplicated between the create and update methods. To improve maintainability and avoid duplication, consider extracting this validation into a private helper method.

@@ -1,3 +1,2 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
quarkus.datasource.db-kind=postgresql

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Switching the test database from H2 to PostgreSQL introduces a dependency on Docker (via Quarkus Dev Services) for running unit tests. This can significantly slow down test execution and cause tests to fail in CI/CD environments where Docker is not available or permitted. Unless PostgreSQL-specific features are being utilized, it is recommended to continue using an in-memory H2 database for unit and integration tests to keep them fast and self-contained.

quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a rating and description field to the Menu entity, along with corresponding validation logic in MenuResource and comprehensive unit tests. It also migrates the database configuration from H2 to PostgreSQL for both production and test environments. The review feedback suggests wrapping the validation exceptions in a Response object to ensure descriptive error messages are returned to the client. Additionally, it is recommended to revert the test database configuration back to H2 to avoid requiring a Docker daemon for running tests in CI/CD environments.

Comment on lines +113 to +118
if (menu.rating == null) {
throw new WebApplicationException("Rating is required and cannot be null", 400);
}
if (menu.rating < 1 || menu.rating > 5) {
throw new WebApplicationException("Rating must be an integer between 1 and 5", 400);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Throwing a WebApplicationException with a plain string message and status code (e.g., 400) might not include the message in the response body, depending on the JAX-RS provider's default configuration. This results in clients receiving a generic 400 Bad Request without knowing why the request failed.

To ensure the client receives the descriptive error message, wrap the message in a Response object with the appropriate media type.

    if (menu.rating == null) {
      throw new WebApplicationException(
          Response.status(Response.Status.BAD_REQUEST)
              .entity("Rating is required and cannot be null")
              .type(MediaType.TEXT_PLAIN)
              .build());
    }
    if (menu.rating < 1 || menu.rating > 5) {
      throw new WebApplicationException(
          Response.status(Response.Status.BAD_REQUEST)
              .entity("Rating must be an integer between 1 and 5")
              .type(MediaType.TEXT_PLAIN)
              .build());
    }
Comment on lines +163 to +168
if (menu.rating != null) {
if (menu.rating < 1 || menu.rating > 5) {
throw new WebApplicationException("Rating must be an integer between 1 and 5", 400);
}
entity.rating = menu.rating;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the creation endpoint, throwing a WebApplicationException with a plain string message and status code might not return the message in the response body. Wrap the message in a Response object to ensure the client receives the descriptive error message.

    if (menu.rating != null) {
      if (menu.rating < 1 || menu.rating > 5) {
        throw new WebApplicationException(
            Response.status(Response.Status.BAD_REQUEST)
                .entity("Rating must be an integer between 1 and 5")
                .type(MediaType.TEXT_PLAIN)
                .build());
      }
      entity.rating = menu.rating;
    }
@@ -1,3 +1,2 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
quarkus.datasource.db-kind=postgresql

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Changing the test database to PostgreSQL forces the use of Quarkus Dev Services, which requires a running Docker daemon. This can significantly slow down test execution and cause tests to fail in CI/CD environments where Docker is not available or permitted.

It is recommended to keep using an in-memory database like H2 for testing. You can achieve this by keeping the quarkus-jdbc-h2 dependency in pom.xml with test scope, and configuring the test profile in application.properties (e.g., %test.quarkus.datasource.db-kind=h2).

@fxb-google

Copy link
Copy Markdown
Owner Author

/gemini summary

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

This pull request introduces new description and rating fields to the menu item entity, providing more detailed information and quality metrics. It incorporates robust validation for the rating field in the API endpoints, ensuring data integrity and preventing invalid entries. The application's version has been updated, and the testing suite has been significantly expanded to ensure the correct functioning and validation of these new features.

Highlights

  • New Menu Fields: Added description (String) and rating (Integer) fields to the Menu JPA entity, enhancing the data model for menu items.
  • API Input Validation: Implemented robust validation for the rating field on POST /menu (must be non-null and between 1 and 5) and PUT /menu/{id} (validates range if supplied), returning HTTP 400 Bad Request on failure.
  • Version Update: Incremented the application version from 1.0.0 to 1.0.1 in pom.xml and index.html.
  • Comprehensive Unit Tests: Expanded the test suite in MenuResourceTest.java to cover successful API paths for the new fields and to assert the new input validation constraints.
  • Database Configuration Change: Switched the default and test database configuration from H2 to PostgreSQL, and removed the H2 JDBC dependency from the pom.xml.
Changelog
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/pom.xml
    • Updated the project version from 1.0.0 to 1.0.1.
    • Removed the quarkus-jdbc-h2 dependency.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/main/java/org/google/demo/Menu.java
    • Added description and rating fields to the Menu entity.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/main/java/org/google/demo/MenuResource.java
    • Added validation for the rating field during menu item creation (POST /menu), ensuring it is non-null and within the 1-5 range.
    • Added validation for the rating field during menu item updates (PUT /menu/{id}), ensuring it is within the 1-5 range if provided.
    • Updated the update method to handle the new description and rating fields.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/main/resources/META-INF/resources/index.html
    • Updated the displayed application version from 1.0.0 to 1.0.1.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/main/resources/application.properties
    • Changed the default database kind from H2 to PostgreSQL.
    • Removed the H2 JDBC URL configuration.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/test/java/org/google/demo/MenuResourceTest.java
    • Updated the setup method to include description and rating in the mocked Menu object.
    • Modified testCreateMenu to assert the presence and value of the new description and rating fields.
    • Added new test cases (testCreateMenuNullRating, testCreateMenuZeroRating, testCreateMenuInvalidRatingHigh) to verify rating validation during creation.
    • Added new test cases (testUpdateMenuSuccess, testUpdateMenuInvalidRating) to verify rating validation and successful updates.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/test/resources/application.properties
    • Changed the test database kind from H2 to PostgreSQL.
    • Removed the H2 JDBC URL configuration for tests.
  • projects/build-with-gemini-demo/gemini-powered-development/menu-service/src/test/resources/docker-java.properties
    • Added api.version=1.40 for Docker API configuration.
Activity
  • fxb-google requested a Gemini review.
  • gemini-code-assist[bot] identified a high-priority potential data-overwriting bug in MenuResource.java related to the spiceLevel field during updates, suggesting a change from int to Integer and a null check.
  • gemini-code-assist[bot] suggested a medium-priority improvement to extract the duplicated rating validation logic into a private helper method.
  • gemini-code-assist[bot] raised a medium-priority concern about switching the test database from H2 to PostgreSQL, noting potential slowdowns and CI/CD issues due to Docker dependency.
  • gemini-code-assist[bot] provided a medium-priority recommendation to wrap WebApplicationException messages in Response objects for better client error reporting in the create method.
  • gemini-code-assist[bot] provided a medium-priority recommendation to wrap WebApplicationException messages in Response objects for better client error reporting in the update method.
  • gemini-code-assist[bot] reiterated a medium-priority concern about the test database change, recommending the continued use of an in-memory H2 database for testing.
  • fxb-google requested a Gemini summary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant