Skip to content

Commit

Permalink
CIRC-2111 Validate UUID - GET and DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkurash committed Jun 20, 2024
1 parent 02870e1 commit 042bd4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,16 @@ void replace(RoutingContext routingContext) {
.thenAccept(context::writeResultToHttpResponse);
}

private Function<Result<CirculationSetting>, Result<CirculationSetting>>
refuseWhenCirculationSettingIsInvalid() {

return r -> r.failWhen(circulationSetting -> succeeded(circulationSetting == null),
circulationSetting -> singleValidationError("Circulation setting JSON is malformed", "", ""));
}

@Override
void get(RoutingContext routingContext) {
final var context = new WebContext(routingContext);
final var clients = Clients.create(context, client);
final var circulationSettingsRepository = new CirculationSettingsRepository(clients);

final var id = routingContext.request().getParam("id");
log.debug("get:: Requested circulation setting ID: {}", id);

circulationSettingsRepository.getById(id)
ofAsync(routingContext.request().getParam("id"))
.thenApply(refuseWhenIdIsInvalid())
.thenApply(r -> r.map(providedId -> UUID.fromString(providedId).toString()))
.thenCompose(r -> r.after(circulationSettingsRepository::getById))
.thenApply(r -> r.map(CirculationSetting::getRepresentation))
.thenApply(r -> r.map(JsonHttpResponse::ok))
.thenAccept(context::writeResultToHttpResponse);
Expand All @@ -96,10 +89,10 @@ void delete(RoutingContext routingContext) {
final var context = new WebContext(routingContext);
final var clients = Clients.create(context, client);

String id = routingContext.request().getParam("id");
log.debug("delete:: Deleting circulation setting ID: {}", id);

clients.circulationSettingsStorageClient().delete(id)
ofAsync(routingContext.request().getParam("id"))
.thenApply(refuseWhenIdIsInvalid())
.thenApply(r -> r.map(providedId -> UUID.fromString(providedId).toString()))
.thenCompose(r -> r.after(clients.circulationSettingsStorageClient()::delete))
.thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent)))
.thenAccept(context::writeResultToHttpResponse);
}
Expand Down Expand Up @@ -136,4 +129,26 @@ private void setRandomIdIfMissing(JsonObject representation) {
representation.put("id", UUID.randomUUID().toString());
}
}

private Function<Result<CirculationSetting>, Result<CirculationSetting>>
refuseWhenCirculationSettingIsInvalid() {

return r -> r.failWhen(circulationSetting -> succeeded(circulationSetting == null),
circulationSetting -> singleValidationError("Circulation setting JSON is malformed", "", ""));
}

private Function<Result<String>, Result<String>> refuseWhenIdIsInvalid() {
return r -> r.failWhen(id -> succeeded(!uuidIsValid(id)),
circulationSetting -> singleValidationError("Circulation setting ID is not a valid UUID",
"", ""));
}

private boolean uuidIsValid(String providedId) {
try {
return providedId != null && providedId.equals(UUID.fromString(providedId).toString());
} catch(IllegalArgumentException e) {
log.debug("refuseWhenIdIsInvalid:: Invalid UUID");
return false;
}
}
}
8 changes: 7 additions & 1 deletion src/test/java/api/settings/CirculationSettingsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ void invalidRequestsTest() {
.withName("initial-name")
.withValue(new JsonObject().put("initial-key", "initial-value")));

// Testing GET with invalid ID
// Testing GET with wrong UUID
restAssuredClient.get(circulationSettingsUrl("/" + randomId()), 404,
"get-circulation-setting");

// Testing GET with invalid ID (not a UUID)
var getErrors = restAssuredClient.get(circulationSettingsUrl("/not-a-uuid"), 422,
"get-circulation-setting");
assertThat(getErrors.getJson().getJsonArray("errors").getJsonObject(0).getString("message"),
is("Circulation setting ID is not a valid UUID"));

// Testing DELETE with invalid ID
restAssuredClient.delete(circulationSettingsUrl("/" + randomId()), 204,
"delete-circulation-setting");
Expand Down

0 comments on commit 042bd4f

Please sign in to comment.