diff --git a/src/main/java/org/folio/search/utils/CallNumberUtils.java b/src/main/java/org/folio/search/utils/CallNumberUtils.java index 7f2fcbdb5..0becca77f 100644 --- a/src/main/java/org/folio/search/utils/CallNumberUtils.java +++ b/src/main/java/org/folio/search/utils/CallNumberUtils.java @@ -15,13 +15,9 @@ import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.folio.search.domain.dto.CallNumberBrowseItem; -import org.folio.search.domain.dto.Holding; import org.folio.search.domain.dto.Item; import org.folio.search.model.types.CallNumberType; import org.jetbrains.annotations.NotNull; -import org.marc4j.callnum.DeweyCallNumber; -import org.marc4j.callnum.LCCallNumber; -import org.marc4j.callnum.NlmCallNumber; import org.springframework.util.Assert; @UtilityClass @@ -178,19 +174,13 @@ public static List excludeIrrelevantResultItems(String cal return records; } - records.forEach(r -> { - r.getInstance().setItems(getItemsFiltered(callNumberType, r)); - r.getInstance().setHoldings(getHoldingsFiltered(callNumberType, r)); - }); - return records; - } - - @NotNull - private static List<@Valid Holding> getHoldingsFiltered(String callNumberType, CallNumberBrowseItem item) { - return item.getInstance().getHoldings() + records.forEach(r -> r.getInstance().setItems(getItemsFiltered(callNumberType, r))); + return records .stream() - .filter(h -> isInGivenType(callNumberType, h.getCallNumber())) - .toList(); + .filter(r -> r.getInstance().getItems() + .stream() + .anyMatch(i -> r.getFullCallNumber() == null + || i.getEffectiveCallNumberComponents().getCallNumber().equals(r.getFullCallNumber()))).toList(); } @NotNull @@ -202,26 +192,6 @@ public static List excludeIrrelevantResultItems(String cal .toList(); } - private boolean isInGivenType(String callNumberType, String callNumber) { - switch (callNumberType) { - case "dewey" -> { - return new DeweyCallNumber(callNumber).isValid(); - } - case "lc" -> { - return new LCCallNumber(callNumber).isValid(); - } - case "nlm" -> { - return new NlmCallNumber(callNumber).isValid(); - } - default -> { - return false; - } -// case "sudoc" -> { -// return new SuDocCallNumber(callNumber).isValid(); -// }; - } - } - private static long callNumberToLong(String callNumber, long startVal, int maxChars) { var cleanCallNumber = cleanCallNumber(callNumber, maxChars); if (StringUtils.isBlank(cleanCallNumber)) { diff --git a/src/test/java/org/folio/search/controller/BrowseTypedCallNumberIrrelevantResultIT.java b/src/test/java/org/folio/search/controller/BrowseTypedCallNumberIrrelevantResultIT.java index 2eecf46b6..abe6b6e2d 100644 --- a/src/test/java/org/folio/search/controller/BrowseTypedCallNumberIrrelevantResultIT.java +++ b/src/test/java/org/folio/search/controller/BrowseTypedCallNumberIrrelevantResultIT.java @@ -14,6 +14,7 @@ import static org.folio.search.utils.TestUtils.randomId; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -56,28 +57,49 @@ void browseByCallNumber_browsingAroundWithEnabledIntermediateValues() { .param("precedingRecordsCount", "5") .param("expandAll", "true"); var actual = parseResponse(doGet(request), CallNumberBrowseResult.class); - final CallNumberBrowseResult expected = new CallNumberBrowseResult() + var expected = new CallNumberBrowseResult() .totalRecords(18) .prev("3308 H975") .next("3308 H981") - .items(List.of( - cnBrowseItem(INSTANCE_MAP.get("instance #18"), "308 H975"), - cnBrowseItem(INSTANCE_MAP.get("instance #01"), "308 H976"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H972"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H973"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H974"), - cnBrowseItem(INSTANCE_MAP.get("instance #09"), "308 H977", true), - cnBrowseItem(INSTANCE_MAP.get("instance #01"), "308 H978"), - cnBrowseItem(INSTANCE_MAP.get("instance #10"), "308 H979"), - cnBrowseItem(INSTANCE_MAP.get("instance #02"), "308 H980"), - cnBrowseItem(INSTANCE_MAP.get("instance #08"), "308 H981") + .items(Arrays.asList( + cnBrowseItem(instance("instance #18"), "308 H975"), + cnBrowseItem(instance("instance #01"), "308 H976"), + cnBrowseItem(instance("instance #11"), "308 H972"), + cnBrowseItem(instance("instance #11"), "308 H973"), + cnBrowseItem(instance("instance #11"), "308 H974"), + cnBrowseItem(instance("instance #09"), "308 H977", true), + cnBrowseItem(instance("instance #01"), "308 H978"), + cnBrowseItem(instance("instance #10"), "308 H979"), + cnBrowseItem(instance("instance #02"), "308 H980"), + cnBrowseItem(instance("instance #08"), "308 H981") )); expected.setItems(CallNumberUtils.excludeIrrelevantResultItems("dewey", expected.getItems())); assertThat(actual).isEqualTo(expected); } @Test - void browseByCallNumber_browsingAroundWithDisabledIntermediateValuesAndLowLimit() { + void browseByCallNumber_browsingAroundWithoutExpandAll() { + var request = get(instanceCallNumberBrowsePath()) + .param("callNumberType", "lc") + .param("query", prepareQuery("typedCallNumber >= {value} or typedCallNumber < {value}", "Z669.R360 1975")) + .param("limit", "10") + .param("highlightMatch", "true") + .param("precedingRecordsCount", "5"); + var actual = parseResponse(doGet(request), CallNumberBrowseResult.class); + var expected = new CallNumberBrowseResult() + .totalRecords(4) + .items(Arrays.asList( + cnBrowseItem(instance("instance #10"), "Z669.R360 1975", true), + cnBrowseItem(instance("instance #02"), "Z669.R360 1976"), + cnBrowseItem(instance("instance #10"), "Z669.R360 1977") + )); + expected.setItems(CallNumberUtils.excludeIrrelevantResultItems("lc", expected.getItems())); + leaveOnlyBasicProps(expected); + assertThat(actual).isEqualTo(expected); + } + + @Test + void browseByCallNumber_browsingAroundWithLowLimit() { var limit = 12; var request = get(instanceCallNumberBrowsePath()) .param("callNumberType", "dewey") @@ -87,31 +109,42 @@ void browseByCallNumber_browsingAroundWithDisabledIntermediateValuesAndLowLimit( .param("precedingRecordsCount", "7") .param("expandAll", "true"); var result = parseResponse(doGet(request), CallNumberBrowseResult.class); - final CallNumberBrowseResult expected = new CallNumberBrowseResult() + var expected = new CallNumberBrowseResult() .next("3308 H981") .totalRecords(18) - .items( - List.of( - cnBrowseItem(INSTANCE_MAP.get("instance #10"), "308 H970"), - cnBrowseItem(INSTANCE_MAP.get("instance #01"), "308 H971"), - cnBrowseItem(INSTANCE_MAP.get("instance #18"), "308 H975"), - cnBrowseItem(INSTANCE_MAP.get("instance #01"), "308 H976"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H972"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H973"), - cnBrowseItem(INSTANCE_MAP.get("instance #11"), "308 H974"), - cnBrowseItem(INSTANCE_MAP.get("instance #09"), "308 H977", true), - cnBrowseItem(INSTANCE_MAP.get("instance #01"), "308 H978"), - cnBrowseItem(INSTANCE_MAP.get("instance #10"), "308 H979"), - cnBrowseItem(INSTANCE_MAP.get("instance #02"), "308 H980"), - cnBrowseItem(INSTANCE_MAP.get("instance #08"), "308 H981") - ) - ); + .items(Arrays.asList( + cnBrowseItem(instance("instance #10"), "308 H970"), + cnBrowseItem(instance("instance #01"), "308 H971"), + cnBrowseItem(instance("instance #18"), "308 H975"), + cnBrowseItem(instance("instance #01"), "308 H976"), + cnBrowseItem(instance("instance #11"), "308 H972"), + cnBrowseItem(instance("instance #11"), "308 H973"), + cnBrowseItem(instance("instance #11"), "308 H974"), + cnBrowseItem(instance("instance #09"), "308 H977", true), + cnBrowseItem(instance("instance #01"), "308 H978"), + cnBrowseItem(instance("instance #10"), "308 H979"), + cnBrowseItem(instance("instance #02"), "308 H980"), + cnBrowseItem(instance("instance #08"), "308 H981") + )); expected.setItems(CallNumberUtils.excludeIrrelevantResultItems("dewey", expected.getItems())); assertThat(result.getItems()).hasSizeLessThanOrEqualTo(limit); assertThat(result).isEqualTo(expected); } + private void leaveOnlyBasicProps(CallNumberBrowseResult expected) { + expected.getItems().forEach(i -> { + Instance instance = i.getInstance(); + instance.setTenantId(null); + instance.setShared(null); + instance.getItems().forEach(item -> { + item.setId(null); + item.setTenantId(null); + item.setDiscoverySuppress(null); + }); + }); + } + private static Instance[] instances() { List> instanceData = callNumberBrowseInstanceData(); Map>> collectedByTitle = instanceData @@ -127,6 +160,21 @@ private static Instance[] instances() { return instanceArray; } + private static Instance instance(String title) { + Instance instance1 = INSTANCE_MAP.get(title); + + Instance instance = new Instance(); + instance.setId(instance1.getId()); + instance.setTitle(title); + instance.setItems(new ArrayList<>(instance1.getItems())); + instance.setTenantId(instance1.getTenantId()); + instance.setShared(instance1.getShared()); + instance.staffSuppress(instance1.getStaffSuppress()); + instance.discoverySuppress(instance1.getDiscoverySuppress()); + instance.isBoundWith(instance1.getIsBoundWith()); + return instance; + } + private static Instance instance(List> data, String title) { var items = data.stream().map(d -> new Item() .id(randomId()) diff --git a/src/test/java/org/folio/search/utils/CallNumberUtilsTest.java b/src/test/java/org/folio/search/utils/CallNumberUtilsTest.java index 0a07b7196..3620a110e 100644 --- a/src/test/java/org/folio/search/utils/CallNumberUtilsTest.java +++ b/src/test/java/org/folio/search/utils/CallNumberUtilsTest.java @@ -123,16 +123,6 @@ void excludeIrrelevantResultItems(String callNumberType, CallNumberBrowseItem gi assertThat(unchangedItems).isEqualTo(List.of(given)); } - @ParameterizedTest(name = "[{index}] callNumber={0}, records={1}, expected={2}") - @MethodSource("eliminateIrrelevantItemsOnCallNumberBrowsingDataHoldings") - void excludeIrrelevantResultHoldings(String callNumberType, CallNumberBrowseItem given, - CallNumberBrowseItem expected) { - var items = CallNumberUtils.excludeIrrelevantResultItems(callNumberType, List.of(given)); - assertThat(items).isEqualTo(List.of(expected)); - var unchangedItems = CallNumberUtils.excludeIrrelevantResultItems("", List.of(given)); - assertThat(unchangedItems).isEqualTo(List.of(given)); - } - private static Stream supportedCharactersDataset() { return StreamEx.empty() .append(letterCharacterDataProvider())