Skip to content

Commit

Permalink
CIRC-2094: Sort allowed service points by name (#1473)
Browse files Browse the repository at this point in the history
* CIRC-2094 Sort allowed service points by name

* CIRC-2094 Test refactoring

* CIRC-2094 Test refactoring
  • Loading branch information
OleksandrVidinieiev authored May 16, 2024
1 parent 6e24f85 commit 83d13a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -166,7 +167,10 @@ private static JsonObject toJson(Map<RequestType, Set<AllowedServicePoint>> 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;
Expand Down
38 changes: 34 additions & 4 deletions src/test/java/api/requests/AllowedServicePointsAPITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -621,6 +621,35 @@ void shouldReturnAllowedServicePointsForAllEnabledRequestTypes() {
assertServicePointsMatch(allowedRecallServicePointsTlr, List.of(cd1, cd2));
}

@Test
void allowedServicePointsAreSortedByName() {
String requesterId = usersFixture.steve().getId().toString();
List<ItemResource> 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<RequestType, Set<UUID>> 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();
Expand Down Expand Up @@ -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)));
}

Expand Down

0 comments on commit 83d13a3

Please sign in to comment.