Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIRC-2050 Add useStubItem parameter to allowed-service-points endpoint #1447

Merged
merged 19 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@
},
{
"id": "allowed-service-points",
"version": "1.0",
"version": "1.1",
"handlers": [
{
"methods": [
Expand Down
8 changes: 7 additions & 1 deletion ramls/circulation.raml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ resourceTypes:
description: "Instance ID"
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$"
required: false
useStubItem:
description: "Allows to apply circulation rules based on patron group only"
roman-barannyk marked this conversation as resolved.
Show resolved Hide resolved
type: string
enum: ["true", "false"]
default: "false"
required: false
responses:
200:
description: "List of allowed service points was retrieved successfully"
Expand All @@ -364,4 +370,4 @@ resourceTypes:
description: "Internal server error"
body:
text/plain:
example: "Internal server error"
example: "Internal server error"
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public class AllowedServicePointsRequest {
private String requesterId;
private String instanceId;
private String itemId;
private String requestId;
private String useStubItem;

public boolean isForTitleLevelRequest() {
return instanceId != null;
}

roman-barannyk marked this conversation as resolved.
Show resolved Hide resolved
public boolean isForItemLevelRequest() {
return itemId != null;
}
private String requestId;

public AllowedServicePointsRequest updateWithRequestInformation(Request request) {
log.debug("updateWithRequestInformation:: parameters request: {}", request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -95,6 +96,13 @@ public CompletableFuture<Result<Map<RequestPolicy, Set<Item>>>> lookupRequestPol
.thenCompose(r -> r.after(this::lookupRequestPolicies));
}

public CompletableFuture<Result<RequestPolicy>> lookupRequestPolicy(User user) {
OleksandrVidinieiev marked this conversation as resolved.
Show resolved Hide resolved
return lookupRequestPolicyId(UUID.randomUUID().toString(), user.getPatronGroupId(),
UUID.randomUUID().toString(), UUID.randomUUID().toString())
.thenCompose(r -> r.after(this::lookupRequestPolicy))
.thenApply(result -> result.map(RequestPolicy::from));
}

private BinaryOperator<Set<Item>> itemsMergeOperator() {
return (items1, items2) -> Stream.concat(items1.stream(), items2.stream())
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private static Result<AllowedServicePointsRequest> buildRequest(RoutingContext r

AllowedServicePointsRequest request = new AllowedServicePointsRequest(operation,
queryParams.get("requesterId"), queryParams.get("instanceId"), queryParams.get("itemId"),
queryParams.get("requestId"));
queryParams.get("requestId"), queryParams.get("useStubItem"));
roman-barannyk marked this conversation as resolved.
Show resolved Hide resolved

return validateAllowedServicePointsRequest(request);
}
Expand All @@ -86,31 +86,40 @@ private static Result<AllowedServicePointsRequest> validateAllowedServicePointsR
String instanceId = allowedServicePointsRequest.getInstanceId();
String itemId = allowedServicePointsRequest.getItemId();
String requestId = allowedServicePointsRequest.getRequestId();
String useStubItem = allowedServicePointsRequest.getUseStubItem();

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

// Checking UUID validity

if (requesterId != null && !isUuid(requesterId)) {
log.warn("Requester ID is not a valid UUID: {}", 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("Instance ID is not a valid UUID: {}", requesterId);
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("Item ID is not a valid UUID: {}", 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("Request ID is not a valid UUID: {}", 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -154,6 +155,12 @@ private CompletableFuture<Result<User>> fetchUser(AllowedServicePointsRequest re
? this::extractAllowedServicePointsIgnoringItemStatus
: this::extractAllowedServicePointsConsideringItemStatus;

if ("true".equals(request.getUseStubItem())) {
return requestPolicyRepository.lookupRequestPolicy(user)
.thenCompose(r -> r.after(policy -> extractAllowedServicePointsIgnoringItemStatus(
policy, new HashSet<>())));
}

return requestPolicyRepository.lookupRequestPolicies(items, user)
.thenCompose(r -> r.after(policies -> allOf(policies, mappingFunction)))
.thenApply(r -> r.map(this::combineAllowedServicePoints));
Expand Down
107 changes: 86 additions & 21 deletions src/test/java/api/requests/AllowedServicePointsAPITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.folio.circulation.domain.RequestType.HOLD;
import static org.folio.circulation.domain.RequestType.PAGE;
import static org.folio.circulation.domain.RequestType.RECALL;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -34,7 +35,6 @@

import org.apache.http.HttpStatus;
import org.folio.circulation.domain.ItemStatus;
import org.folio.circulation.domain.Request;
import org.folio.circulation.domain.RequestLevel;
import org.folio.circulation.domain.RequestType;
import org.folio.circulation.support.http.client.Response;
Expand Down Expand Up @@ -154,8 +154,8 @@ void shouldReturnListOfAllowedServicePointsForRequest(RequestType requestType,
.collect(Collectors.toSet()));

var response = requestLevel == TITLE
? get("create", requesterId, instanceId, null, null, HttpStatus.SC_OK).getJson()
: get("create", requesterId, null, itemId, null, HttpStatus.SC_OK).getJson();
? get("create", requesterId, instanceId, null, null, null, HttpStatus.SC_OK).getJson()
: get("create", requesterId, null, itemId, null, null, HttpStatus.SC_OK).getJson();

assertThat(response, allowedServicePointMatcher(Map.of(requestType, allowedSpInResponse)));
}
Expand Down Expand Up @@ -223,7 +223,7 @@ void shouldReturnListOfAllowedServicePointsForRequestReplacement(
var requestId = request == null ? null : request.getId().toString();

var response =
get("replace", null, null, null, requestId, HttpStatus.SC_OK).getJson();
get("replace", null, null, null, requestId, null, HttpStatus.SC_OK).getJson();

assertThat(response, allowedServicePointMatcher(Map.of(requestType, allowedSpInResponse)));
}
Expand Down Expand Up @@ -624,7 +624,7 @@ void shouldReturnAllowedServicePointsForAllEnabledRequestTypes() {
@Test
void getReplaceFailsWhenRequestDoesNotExist() {
String requestId = randomId();
Response response = get("replace", null, null, null, requestId,
Response response = get("replace", null, null, null, requestId, null,
HttpStatus.SC_UNPROCESSABLE_ENTITY);
assertThat(response.getJson(), hasErrorWith(hasMessage(
String.format("Request with ID %s was not found", requestId))));
Expand All @@ -634,7 +634,7 @@ void getReplaceFailsWhenRequestDoesNotExist() {
void getMoveFailsWhenRequestDoesNotExist() {
String requestId = randomId();
String itemId = itemsFixture.basedUponNod().getId().toString();
Response response = get("move", null, null, itemId, requestId,
Response response = get("move", null, null, itemId, requestId, null,
HttpStatus.SC_UNPROCESSABLE_ENTITY);
assertThat(response.getJson(), hasErrorWith(hasMessage(
String.format("Request with ID %s was not found", requestId))));
Expand Down Expand Up @@ -696,62 +696,101 @@ void shouldReturnListOfAllowedServicePointsForRequestMove(RequestLevel requestLe

// Valid "move" request
var moveResponse =
get("move", null, null, itemToMoveToId, requestId, HttpStatus.SC_OK).getJson();
get("move", null, null, itemToMoveToId, requestId, null, HttpStatus.SC_OK).getJson();
assertThat(moveResponse, allowedServicePointMatcher(Map.of(HOLD, List.of(sp2))));

// Invalid "move" requests
var invalidMoveResponse1 = get("move", null, null, null, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidMoveResponse1.getBody(), equalTo("Invalid combination of query parameters"));

var invalidMoveResponse2 = get("move", null, null, itemToMoveToId, null,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidMoveResponse2.getBody(), equalTo("Invalid combination of query parameters"));

var invalidMoveResponse3 = get("move", null, null, null, null,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidMoveResponse3.getBody(), equalTo("Invalid combination of query parameters"));

var invalidMoveResponse4 = get("move", requesterId, null, itemToMoveToId, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidMoveResponse4.getBody(), equalTo("Invalid combination of query parameters"));

var invalidMoveResponse5 = get("move", null, instanceId, itemToMoveToId, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidMoveResponse5.getBody(), equalTo("Invalid combination of query parameters"));

// Valid "replace" request
var replaceResponse =
get("replace", null, null, null, requestId, HttpStatus.SC_OK).getJson();
get("replace", null, null, null, requestId, null, HttpStatus.SC_OK).getJson();
assertThat(replaceResponse, allowedServicePointMatcher(Map.of(HOLD, List.of(sp2))));

// Invalid "replace" requests
var invalidReplaceResponse1 = get("replace", null, null, null, null,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidReplaceResponse1.getBody(),
equalTo("Invalid combination of query parameters"));

var invalidReplaceResponse2 = get("replace", requesterId, null, null, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidReplaceResponse2.getBody(),
equalTo("Invalid combination of query parameters"));

var invalidReplaceResponse3 = get("replace", null, instanceId, null, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidReplaceResponse3.getBody(),
equalTo("Invalid combination of query parameters"));

var invalidReplaceResponse4 = get("replace", null, null, requestedItemId, requestId,
HttpStatus.SC_BAD_REQUEST);
null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidReplaceResponse4.getBody(),
equalTo("Invalid combination of query parameters"));

var invalidReplaceResponse5 = get("replace", requesterId, instanceId,
requestedItemId, requestId, HttpStatus.SC_BAD_REQUEST);
requestedItemId, requestId, null, HttpStatus.SC_BAD_REQUEST);
assertThat(invalidReplaceResponse5.getBody(),
equalTo("Invalid combination of query parameters"));
}

@Test
void shouldUseStubItemParameterInCirculationRuleMatchingWhenPresent() {
var requesterId = usersFixture.steve().getId().toString();
var instanceId = itemsFixture.createMultipleItemsForTheSameInstance(2).get(0)
.getInstanceId().toString();
var cd1 = servicePointsFixture.cd1();
var cd2 = servicePointsFixture.cd2();
var cd4 = servicePointsFixture.cd4();
var cd5 = servicePointsFixture.cd5();
final UUID book = materialTypesFixture.book().getId();
final UUID patronGroup = patronGroupsFixture.regular().getId();
circulationRulesFixture.updateCirculationRules(createRules("m " + book +
"+ g " + patronGroup, "g " + patronGroup));

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));

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));

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));

Response errorResponse = getCreateOp(requesterId, instanceId, null, "invalid",
HttpStatus.SC_BAD_REQUEST);
assertThat(errorResponse.getBody(), is("useStubItem is not a valid boolean: invalid."));
}

private void assertServicePointsMatch(JsonArray response,
List<IndividualResource> expectedServicePoints) {

Expand All @@ -775,18 +814,24 @@ private void assertServicePointsMatch(JsonArray response,
.map(sp -> sp.getJson().getString("name")).toArray(String[]::new)));
}

private Response getCreateOp(String requesterId, String instanceId, String itemId,
String useStubItem, int expectedStatusCode) {

return get("create", requesterId, instanceId, itemId, null, useStubItem, expectedStatusCode);
}

private Response getCreateOp(String requesterId, String instanceId, String itemId,
int expectedStatusCode) {

return get("create", requesterId, instanceId, itemId, null, expectedStatusCode);
return get("create", requesterId, instanceId, itemId, null, null, expectedStatusCode);
}

private Response getReplaceOp(String requestId, int expectedStatusCode) {
return get("replace", null, null, null, requestId, expectedStatusCode);
return get("replace", null, null, null, requestId, null, expectedStatusCode);
}

private Response get(String operation, String requesterId, String instanceId, String itemId,
String requestId, int expectedStatusCode) {
String requestId, String useStubItem, int expectedStatusCode) {

List<QueryStringParameter> queryParams = new ArrayList<>();
queryParams.add(namedParameter("operation", operation));
Expand All @@ -802,6 +847,9 @@ private Response get(String operation, String requesterId, String instanceId, St
if (requestId != null) {
queryParams.add(namedParameter("requestId", requestId));
}
if (useStubItem != null) {
queryParams.add(namedParameter("useStubItem", useStubItem));
}

return restAssuredClient.get(allowedServicePointsUrl(), queryParams, expectedStatusCode,
"allowed-service-points");
Expand All @@ -824,4 +872,21 @@ private ServicePointBuilder servicePointBuilder() {
.withPickupLocation(TRUE)
.withHoldShelfExpriyPeriod(30, "Days");
}

private String createRules(String firstRuleCondition, String secondRuleCondition) {
final var loanPolicy = loanPoliciesFixture.canCirculateRolling().getId().toString();
final var allowAllRequestPolicy = requestPoliciesFixture.allowAllRequestPolicy()
.getId().toString();
final var holdAndRecallRequestPolicy = requestPoliciesFixture.allowHoldAndRecallRequestPolicy()
.getId().toString();
final var noticePolicy = noticePoliciesFixture.activeNotice().getId().toString();
final var overdueFinePolicy = overdueFinePoliciesFixture.facultyStandard().getId().toString();
final var lostItemFeePolicy = lostItemFeePoliciesFixture.facultyStandard().getId().toString();

return String.join("\n",
"priority: t, s, c, b, a, m, g",
"fallback-policy: l " + loanPolicy + " r " + allowAllRequestPolicy + " n " + noticePolicy + " o " + overdueFinePolicy + " i " + lostItemFeePolicy,
firstRuleCondition + " : l " + loanPolicy + " r " + allowAllRequestPolicy + " n " + noticePolicy + " o " + overdueFinePolicy + " i " + lostItemFeePolicy,
secondRuleCondition + " : l " + loanPolicy + " r " + holdAndRecallRequestPolicy + " n " + noticePolicy + " o " + overdueFinePolicy + " i " + lostItemFeePolicy);
}
}