Skip to content

Commit

Permalink
use static final variables and include error tests validations
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Nov 14, 2023
1 parent 9411d3a commit 64d6ad8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class ValidationHandlerOnRoutes {
ShopResource shopResource;

private static final String ERROR_MESSAGE = "{\"error\": \"%s\"}";
private static final String SHOPPINGLIST_NOT_FOUND = "Shopping list not found in the list or does not exist with that name or price";

@PostConstruct
void initialize() {
Expand Down Expand Up @@ -76,7 +77,7 @@ public void validateHandlerSoppingList(@Observes Router router) {
String shoppingListFound = fetchProductDetailsFromQuery(shoppingListName, totalPrice);
queryAnswer.set(shoppingListFound);

if (queryAnswer.get().equalsIgnoreCase(HttpResponseStatus.NOT_FOUND.reasonPhrase())) {
if (queryAnswer.get().equalsIgnoreCase(SHOPPINGLIST_NOT_FOUND)) {
routingContext.response().setStatusCode(HttpResponseStatus.NOT_FOUND.code());
}

Expand All @@ -91,7 +92,7 @@ public void validateHandlerSoppingList(@Observes Router router) {
String errorMessage = routingContext.failure().toString();
routingContext.response()
.setStatusCode(HttpResponseStatus.BAD_REQUEST.code())
.putHeader("content-type", "application/json")
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.end(String.format(ERROR_MESSAGE, errorMessage));
}
});
Expand All @@ -103,7 +104,7 @@ public String fetchProductDetailsFromQuery(String name, Double price) {
.filter(product -> name.equalsIgnoreCase(product.getName()) && price.equals(product.getPrice()))
.map(ShoppingList::toString)
.findFirst()
.orElse("Product not found in the list or does not exist with that name or price");
.orElse(SHOPPINGLIST_NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public class VertxWebValidationIT {
private static final String LIST_NAME1 = "name=ListName1, products=[Carrots, Water, Cheese, Beer], price=25.0";
private static final String LIST_NAME2 = "name=ListName2";
private static final String FILTER_BY_NAME_PRICE_URL = "/filterList?shoppingListName=ListName1&shoppingListPrice=25";
private static final String FILTER_BY_WRONG_NAME_PRICE_URL = "/filterList?shoppingListName=ListName35&shoppingListPrice=25";
private static final String ONLY_FILTER_BY_NAME = "/filterList?shoppingListName=ListName1";
private static final String PRICE_OUT_OF_RANGE = "/filterList?shoppingListName=ListName1&shoppingListPrice=125";

private static final String ERROR_PARAMETER_MISSING = "ParameterProcessorException";
private static final String ERROR_PARAMETER_MISSING = "ParameterProcessorException";

@Test
void checkShoppingListUrl() {
Expand All @@ -48,6 +50,17 @@ void checkNamePriceParams() {
assertThat(response.asString(), containsString(LIST_NAME1));
}

@Test
void checkWrongNamePriceParams() {
Response response = restServiceApp
.given()
.get(FILTER_BY_WRONG_NAME_PRICE_URL)
.then()
.statusCode(HttpStatus.SC_NOT_FOUND).extract().response();
assertThat(response.asString(),
containsString("Shopping list not found in the list or does not exist with that name or price"));
}

@Test
void checkParameterMissingError() {
Response response = restServiceApp
Expand All @@ -58,6 +71,20 @@ void checkParameterMissingError() {
.extract()
.response();
assertThat(response.asString(), containsString(ERROR_PARAMETER_MISSING));
assertThat(response.asString(), containsString("Missing parameter shoppingListPrice in QUERY"));
}

@Test
void checkPriceOutOfRangeError() {
Response response = restServiceApp
.given()
.get(PRICE_OUT_OF_RANGE)
.then()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.extract()
.response();
assertThat(response.asString(), containsString(ERROR_PARAMETER_MISSING));
assertThat(response.asString(), containsString("value should be <= 100.0"));
}

}

0 comments on commit 64d6ad8

Please sign in to comment.