Skip to content

Commit

Permalink
CIRC-2050 update schema with boolean field
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-barannyk committed Apr 2, 2024
1 parent 2e55351 commit 4ede011
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 47 deletions.
6 changes: 3 additions & 3 deletions ramls/circulation.raml
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ resourceTypes:
required: false
useStubItem:
description: "Allows to apply circulation rules based on patron group only"
type: string
enum: ["true", "false"]
default: "false"
type: boolean
enum: [true, false]
default: false
required: false
responses:
200:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AllowedServicePointsRequest {
private String instanceId;
private String itemId;
private String requestId;
private String useStubItem;
private boolean useStubItem;

public boolean isForTitleLevelRequest() {
return instanceId != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,61 +67,39 @@ private static Result<AllowedServicePointsRequest> buildRequest(RoutingContext r
.map(String::toUpperCase)
.map(Request.Operation::valueOf)
.orElse(null);

AllowedServicePointsRequest request = new AllowedServicePointsRequest(operation,
queryParams.get("requesterId"), queryParams.get("instanceId"), queryParams.get("itemId"),
queryParams.get("requestId"), queryParams.get("useStubItem"));

return validateAllowedServicePointsRequest(request);
}

private static Result<AllowedServicePointsRequest> validateAllowedServicePointsRequest(
AllowedServicePointsRequest allowedServicePointsRequest) {

log.debug("validateAllowedServicePointsRequest:: parameters allowedServicePointsRequest: {}",
allowedServicePointsRequest);

Request.Operation operation = allowedServicePointsRequest.getOperation();
String requesterId = allowedServicePointsRequest.getRequesterId();
String instanceId = allowedServicePointsRequest.getInstanceId();
String itemId = allowedServicePointsRequest.getItemId();
String requestId = allowedServicePointsRequest.getRequestId();
String useStubItem = allowedServicePointsRequest.getUseStubItem();
String requesterId = queryParams.get("requesterId");
String instanceId = queryParams.get("instanceId");
String itemId = queryParams.get("itemId");
String requestId = queryParams.get("requestId");
String useStubItem = queryParams.get("useStubItem");

List<String> errors = new ArrayList<>();

// Checking UUID validity

if (requesterId != null && !isUuid(requesterId)) {
log.warn("validateAllowedServicePointsRequest:: requester ID is not a valid UUID: {}", requesterId);
errors.add(String.format("Requester ID is not a valid UUID: %s.", requesterId));
}

if (instanceId != null && !isUuid(instanceId)) {
log.warn("validateAllowedServicePointsRequest:: instance ID is not a valid UUID: {}",
requesterId);
errors.add(String.format("Instance ID is not a valid UUID: %s.", instanceId));
}

if (itemId != null && !isUuid(itemId)) {
log.warn("validateAllowedServicePointsRequest:: item ID is not a valid UUID: {}", itemId);
errors.add(String.format("Item ID is not a valid UUID: %s.", itemId));
}

if (requestId != null && !isUuid(requestId)) {
log.warn("validateAllowedServicePointsRequest:: request ID is not a valid UUID: {}",
requestId);
errors.add(String.format("Request ID is not a valid UUID: %s.", requestId));
}

if (useStubItem != null && !"true".equals(useStubItem) && !"false".equals(useStubItem)) {
log.warn("validateAllowedServicePointsRequest:: useStubItem is not a valid boolean: {}",
useStubItem);
errors.add(String.format("useStubItem is not a valid boolean: %s.", useStubItem));
}

// Checking parameter combinations

boolean allowedCombinationOfParametersDetected = false;

if (operation == Request.Operation.CREATE && requesterId != null && instanceId != null &&
Expand All @@ -130,40 +108,36 @@ private static Result<AllowedServicePointsRequest> validateAllowedServicePointsR
log.info("validateAllowedServicePointsRequest:: TLR request creation case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.CREATE && requesterId != null && instanceId == null &&
itemId != null && requestId == null) {

log.info("validateAllowedServicePointsRequest:: ILR request creation case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.REPLACE && requesterId == null && instanceId == null &&
itemId == null && requestId != null) {

log.info("validateAllowedServicePointsRequest:: request replacement case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.MOVE && requesterId == null && instanceId == null &&
itemId != null && requestId != null) {

log.info("validateAllowedServicePointsRequest:: request movement case");
allowedCombinationOfParametersDetected = true;
}

if (!allowedCombinationOfParametersDetected) {
String errorMessage = "Invalid combination of query parameters";
errors.add(errorMessage);
}

if (!errors.isEmpty()) {
String errorMessage = String.join(" ", errors);
log.error("validateRequest:: allowed service points request failed: {}", errorMessage);
return failed(new BadRequestFailure(errorMessage));
}

return succeeded(allowedServicePointsRequest);
return succeeded(new AllowedServicePointsRequest(operation, requesterId, instanceId, itemId,
requestId, Boolean.parseBoolean(useStubItem)));
}

private static JsonObject toJson(Map<RequestType, Set<AllowedServicePoint>> allowedServicePoints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private CompletableFuture<Result<User>> fetchUser(AllowedServicePointsRequest re
? this::extractAllowedServicePointsIgnoringItemStatus
: this::extractAllowedServicePointsConsideringItemStatus;

if ("true".equals(request.getUseStubItem())) {
if (request.isUseStubItem()) {
return requestPolicyRepository.lookupRequestPolicy(user)
.thenCompose(r -> r.after(policy -> extractAllowedServicePointsIgnoringItemStatus(
policy, new HashSet<>())));
Expand Down
17 changes: 8 additions & 9 deletions src/test/java/api/requests/AllowedServicePointsAPITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,23 +768,22 @@ void shouldUseStubItemParameterInCirculationRuleMatchingWhenPresent() {

var response = getCreateOp(requesterId, instanceId, null, "true", HttpStatus.SC_OK).getJson();
assertThat(response, hasNoJsonPath(PAGE.getValue()));

JsonArray allowedPageServicePoints = response.getJsonArray(HOLD.getValue());
assertServicePointsMatch(allowedPageServicePoints, List.of(cd1, cd2, cd4, cd5));
allowedPageServicePoints = response.getJsonArray(RECALL.getValue());
assertServicePointsMatch(allowedPageServicePoints, List.of(cd1, cd2, cd4, cd5));
JsonArray allowedServicePoints = response.getJsonArray(HOLD.getValue());
assertServicePointsMatch(allowedServicePoints, List.of(cd1, cd2, cd4, cd5));
allowedServicePoints = response.getJsonArray(RECALL.getValue());
assertServicePointsMatch(allowedServicePoints, List.of(cd1, cd2, cd4, cd5));

response = getCreateOp(requesterId, instanceId, null, "false", HttpStatus.SC_OK).getJson();
assertThat(response, hasNoJsonPath(HOLD.getValue()));
assertThat(response, hasNoJsonPath(RECALL.getValue()));
allowedPageServicePoints = response.getJsonArray(PAGE.getValue());
assertServicePointsMatch(allowedPageServicePoints, List.of(cd1, cd2, cd4, cd5));
allowedServicePoints = response.getJsonArray(PAGE.getValue());
assertServicePointsMatch(allowedServicePoints, List.of(cd1, cd2, cd4, cd5));

response = getCreateOp(requesterId, instanceId, null, HttpStatus.SC_OK).getJson();
assertThat(response, hasNoJsonPath(HOLD.getValue()));
assertThat(response, hasNoJsonPath(RECALL.getValue()));
allowedPageServicePoints = response.getJsonArray(PAGE.getValue());
assertServicePointsMatch(allowedPageServicePoints, List.of(cd1, cd2, cd4, cd5));
allowedServicePoints = response.getJsonArray(PAGE.getValue());
assertServicePointsMatch(allowedServicePoints, List.of(cd1, cd2, cd4, cd5));

Response errorResponse = getCreateOp(requesterId, instanceId, null, "invalid",
HttpStatus.SC_BAD_REQUEST);
Expand Down

0 comments on commit 4ede011

Please sign in to comment.