Skip to content

Commit

Permalink
Merge pull request #330 from Prankurtiwari/product_rating_fix
Browse files Browse the repository at this point in the history
GET product reviews request with an invalid value of param "product_rating" the system returns all product reviews.
  • Loading branch information
Sunagatov authored Aug 6, 2024
2 parents 35f88df + 81697b2 commit e6f81a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public ResponseEntity<ProductReviewsAndRatingsWithPagination> getProductReviewsA
@RequestParam(name = "sort_attribute", defaultValue = "createdAt") final String sortAttribute,
@RequestParam(name = "sort_direction", defaultValue = "desc") final String sortDirection,
@RequestParam(name = "product_ratings", required = false) List<Integer> productRatings) {
log.info("Received the request to get reviews and ratings for the product with the productId = '{}' and with the next pagination and sorting attributes: pageNumber - {}, pageSize - {}, sort_attribute - {}, sort_direction - {}",
productId, pageNumber, pageSize, sortAttribute, sortDirection);
log.info("Received the request to get reviews and ratings for the product with the productId = '{}' and with the next pagination and sorting attributes: pageNumber - {}, pageSize - {}, sort_attribute - {}, sort_direction - {}, productRatings - {}",
productId, pageNumber, pageSize, sortAttribute, sortDirection, productRatings);
Pageable pageable = createPageableObject(pageNumber, pageSize, sortAttribute, sortDirection);
getReviewsRequestValidator.validate(pageNumber, pageSize, sortAttribute, sortDirection, productRatings);
ProductReviewsAndRatingsWithPagination reviewsPaginationDto = productReviewsProvider.getProductReviews(productId, pageable, productRatings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public void validate(final Integer pageNumber,

private StringBuilder validateProductRatingsParameter(final List<Integer> productRatings) {
final StringBuilder errorMessages = new StringBuilder();
if (productRatings == null) {
String errorMessage = String.format("product's rating is required. Allowed 'productRating' values are '%s'.", ALLOWED_PRODUCT_RATING_VALUES);
errorMessages.append(createErrorMessage(errorMessage));
}
if ((productRatings != null && productRatings.stream().anyMatch(Objects::isNull))
|| (productRatings != null && !ALLOWED_PRODUCT_RATING_VALUES.containsAll(productRatings))) {
String errorMessage = String.format("Some values of this product's rating list = '%s' are incorrect. Allowed 'productRating' values are '%s'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,6 @@ void shouldAddReviewSuccessfully() {
removeReview(AFFOGATO_ID, response);
}

@Test
@DisplayName("Should fetch reviews and ratings with default pagination and sorting for unauthorized user")
void shouldFetchReviewsAndRatingsWithDefaultPaginationAndSortingForAnonymous() {
// No authorization is required
specification = given()
.log().all(true)
.port(port)
.basePath(ProductReviewEndpoint.PRODUCT_REVIEW_URL)
.contentType(ContentType.JSON)
.accept(ContentType.JSON);

Response response = given(specification)
.get("/{productId}/reviews", AMERICANO_ID);

assertRestApiBodySchemaResponse(response, HttpStatus.OK, REVIEWS_WITH_RATINGS_RESPONSE_SCHEMA)
.body("totalElements", equalTo(3))
.body("totalPages", equalTo(1))
.body("reviewsWithRatings[0].productRating", equalTo(5))
.body("reviewsWithRatings[0].text", startsWith(START_OF_REVIEW_FOR_AMERICANO))
.body("reviewsWithRatings[0].userName", equalTo("John"))
.body("reviewsWithRatings[1].productRating", equalTo(3))
.body("reviewsWithRatings[1].text", startsWith(START_OF_REVIEW_FOR_AMERICANO))
.body("reviewsWithRatings[1].userName", equalTo("Jane"))
.body("reviewsWithRatings[2].productRating", equalTo(1))
.body("reviewsWithRatings[2].text", startsWith(START_OF_REVIEW_FOR_AMERICANO))
.body("reviewsWithRatings[2].userName", equalTo("Michael"));
}

@Test
@DisplayName("Should like review successfully and return review object")
void shouldLikeReviewSuccessfully() {
Expand Down Expand Up @@ -185,6 +157,24 @@ void shouldFetchReviewStatsSuccessfully() {
.body("ratingMap.star5", equalTo(0));
}


@Test
@DisplayName("Reviews and ratings with default pagination and sorting for unauthorized user. Should return 400 Bad Request")
void shouldReturnBadRequestForDefaultPaginationAndSortingForAnonymous() {
// No authorization is required
specification = given()
.log().all(true)
.port(port)
.basePath(ProductReviewEndpoint.PRODUCT_REVIEW_URL)
.contentType(ContentType.JSON)
.accept(ContentType.JSON);

Response response = given(specification)
.get("/{productId}/reviews", AMERICANO_ID);

assertRestApiBadRequestResponse(response, FAILED_REVIEW_SCHEMA);
}

@Test
@DisplayName("Should return 404 Not Found on attempt to add review for invalid product ID")
void shouldReturnNotFoundOnAttemptToAddNonExistentProduct() {
Expand Down

0 comments on commit e6f81a7

Please sign in to comment.