diff --git a/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java b/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java index 8e7a81d014..97fee14762 100644 --- a/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java +++ b/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java @@ -1,5 +1,6 @@ package org.folio.circulation.resources; +import static java.util.Comparator.comparing; import static org.folio.circulation.support.results.Result.failed; import static org.folio.circulation.support.results.Result.ofAsync; import static org.folio.circulation.support.results.Result.succeeded; @@ -166,7 +167,10 @@ private static JsonObject toJson(Map> allo } allowedServicePoints.forEach((key, value) -> response.put(key.getValue(), - new JsonArray(value.stream().toList()))); + new JsonArray(value.stream() + .sorted(comparing(AllowedServicePoint::getName)) + .toList()))); + log.info("allowedServicePoints:: result={}", response); return response; diff --git a/src/test/java/api/requests/AllowedServicePointsAPITests.java b/src/test/java/api/requests/AllowedServicePointsAPITests.java index 5cf30e0352..e6995df3f4 100644 --- a/src/test/java/api/requests/AllowedServicePointsAPITests.java +++ b/src/test/java/api/requests/AllowedServicePointsAPITests.java @@ -15,7 +15,7 @@ import static org.folio.circulation.domain.RequestType.PAGE; import static org.folio.circulation.domain.RequestType.RECALL; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.equalTo; @@ -31,10 +31,10 @@ import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; +import java.util.stream.Stream; 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; @@ -621,6 +621,35 @@ void shouldReturnAllowedServicePointsForAllEnabledRequestTypes() { assertServicePointsMatch(allowedRecallServicePointsTlr, List.of(cd1, cd2)); } + @Test + void allowedServicePointsAreSortedByName() { + String requesterId = usersFixture.steve().getId().toString(); + List items = itemsFixture.createMultipleItemForTheSameInstance(2, List.of( + ib -> ib.withStatus(ItemBuilder.AVAILABLE), + ib -> ib.withStatus(ItemBuilder.CHECKED_OUT))); + String instanceId = items.get(0).getInstanceId().toString(); + + IndividualResource cd4 = servicePointsFixture.cd4(); // Circ Desk 4 + IndividualResource cd5 = servicePointsFixture.cd5(); // Circ Desk 5 + IndividualResource cd6 = servicePointsFixture.cd6(); // Circ Desk 6 + + Map> allowedServicePointsInPolicy = Map.of( + PAGE, Set.of(cd6.getId(), cd4.getId(), cd5.getId()), + HOLD, Set.of(cd5.getId(), cd6.getId(), cd4.getId()), + RECALL, Set.of(cd4.getId(), cd6.getId(), cd5.getId())); + + policiesActivation.use(PoliciesToActivate.builder().requestPolicy( + requestPoliciesFixture.createRequestPolicyWithAllowedServicePoints( + allowedServicePointsInPolicy, PAGE, HOLD, RECALL))); + + JsonObject response = getCreateOp(requesterId, instanceId, null, HttpStatus.SC_OK).getJson(); + + Stream.of(PAGE, HOLD, RECALL) + .map(RequestType::getValue) + .map(response::getJsonArray) + .forEach(servicePoints -> assertServicePointsMatch(servicePoints, List.of(cd4, cd5, cd6))); + } + @Test void getReplaceFailsWhenRequestDoesNotExist() { String requestId = randomId(); @@ -770,8 +799,9 @@ private void assertServicePointsMatch(JsonArray response, .map(allowedSp -> allowedSp.getString("name")) .collect(Collectors.toList()); - assertThat(servicePointIds, containsInAnyOrder(expectedIds.toArray(String[]::new))); - assertThat(servicePointNames, containsInAnyOrder(expectedServicePoints.stream() + // order is important: service points must be sorted by name + assertThat(servicePointIds, contains(expectedIds.toArray(String[]::new))); + assertThat(servicePointNames, contains(expectedServicePoints.stream() .map(sp -> sp.getJson().getString("name")).toArray(String[]::new))); }