diff --git a/src/main/java/org/folio/search/service/browse/CallNumberBrowseService.java b/src/main/java/org/folio/search/service/browse/CallNumberBrowseService.java index 0c523489c..f1a141fb9 100644 --- a/src/main/java/org/folio/search/service/browse/CallNumberBrowseService.java +++ b/src/main/java/org/folio/search/service/browse/CallNumberBrowseService.java @@ -12,6 +12,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; import org.folio.search.cql.CqlSearchQueryConverter; import org.folio.search.domain.dto.CallNumberBrowseItem; import org.folio.search.model.BrowseResult; @@ -146,7 +147,7 @@ private static void highlightMatchingCallNumber(BrowseContext ctx, } var firstBrowseItem = items.get(0); - if (!StringUtils.equals(firstBrowseItem.getShelfKey(), anchor)) { + if (!isAnchorMatching(firstBrowseItem, anchor)) { var browseItemsWithEmptyValue = new ArrayList(); browseItemsWithEmptyValue.add(getEmptyCallNumberBrowseItem(callNumber, anchor)); browseItemsWithEmptyValue.addAll(items); @@ -157,6 +158,15 @@ private static void highlightMatchingCallNumber(BrowseContext ctx, firstBrowseItem.setIsAnchor(true); } + private static boolean isAnchorMatching(CallNumberBrowseItem browseItem, String anchor) { + var suffix = browseItem.getInstance().getItems().get(0).getEffectiveCallNumberComponents().getSuffix(); + var shelfKey = browseItem.getShelfKey(); + if (Strings.isNotBlank(suffix)) { + shelfKey = StringUtils.removeEnd(shelfKey, suffix).trim(); + } + return StringUtils.equals(shelfKey, anchor); + } + private static CallNumberBrowseItem getEmptyCallNumberBrowseItem(String callNumber, String shelfKey) { return new CallNumberBrowseItem() .fullCallNumber(callNumber) diff --git a/src/test/java/org/folio/search/service/browse/CallNumberBrowseServiceTest.java b/src/test/java/org/folio/search/service/browse/CallNumberBrowseServiceTest.java index 1c9d38fd2..49af00b2f 100644 --- a/src/test/java/org/folio/search/service/browse/CallNumberBrowseServiceTest.java +++ b/src/test/java/org/folio/search/service/browse/CallNumberBrowseServiceTest.java @@ -167,6 +167,23 @@ void browse_positive_around_noHighlightMatch() { cnBrowseItem(instance("C 11"), "C 11")))); } + @Test + void browse_positive_around_highlightMatchWithSuffix() { + var request = request("callNumber >= B or callNumber < B", true); + + when(cqlSearchQueryConverter.convertToTermNode(anyString(), anyString())) + .thenReturn(new CQLTermNode(null, null, "B")); + + prepareMockForBrowsingAround(request, + contextAroundIncluding(), + BrowseResult.empty(), + BrowseResult.of(1, List.of(browseItemWithSuffix("B", "2005")))); + + var actual = callNumberBrowseService.browse(request); + + assertThat(actual.getRecords().get(0).getIsAnchor()).isTrue(); + } + @Test void browse_positive_around_noResults() { var request = request("callNumber >= B or callNumber < B", false); @@ -362,6 +379,16 @@ private static CallNumberBrowseItem browseItem(String callNumber) { .totalRecords(1); } + private static CallNumberBrowseItem browseItemWithSuffix(String callNumber, String suffix) { + var instance = instance(callNumber); + instance.getItems().get(0).getEffectiveCallNumberComponents().setSuffix(suffix); + return new CallNumberBrowseItem() + .fullCallNumber(callNumber) + .shelfKey(getShelfKeyFromCallNumber(callNumber) + " " + suffix) + .instance(instance) + .totalRecords(1); + } + private static List browseItems(String... shelfKeys) { return stream(shelfKeys) .map(CallNumberBrowseServiceTest::browseItem)