From 8f6da1b6786897cbc6452733600217868606273b Mon Sep 17 00:00:00 2001 From: Askhat_Abishev Date: Tue, 24 Dec 2024 16:56:30 +0500 Subject: [PATCH] fix: remove unnecessary LccnNormalizerStructureB --- NEWS.md | 2 +- .../lccn/LccnNormalizerStructureB.java | 67 ------------------- .../common/LinkedDataLccnProcessor.java | 2 - .../SearchLinkedDataInstanceIT.java | 2 +- .../controller/SearchLinkedDataWorkIT.java | 2 +- .../lccn/LccnNormalizerStructurebTest.java | 65 ------------------ .../samples/linked-data/instance.json | 2 +- .../resources/samples/linked-data/work.json | 2 +- 8 files changed, 5 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/org/folio/search/service/lccn/LccnNormalizerStructureB.java delete mode 100644 src/test/java/org/folio/search/service/lccn/LccnNormalizerStructurebTest.java diff --git a/NEWS.md b/NEWS.md index 31d7af3fe..a04e66cd6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,7 +22,7 @@ * Fix the "Invalid reference" appears after updating ownership ([MSEARCH-915](https://folio-org.atlassian.net/browse/MSEARCH-915)) ### Tech Dept -* Description ([ISSUE](https://folio-org.atlassian.net/browse/ISSUE)) +* Remove unnecessary LccnNormalizerStructureB ([MSEARCH-926](https://folio-org.atlassian.net/browse/MSEARCH-926)) ### Dependencies * Bump `opensearch` from `2.17.1` to `2.18.0` fixing protobuf-java CVE-2024-7254 ([MSEARCH-889](https://folio-org.atlassian.net/browse/MSEARCH-889)) diff --git a/src/main/java/org/folio/search/service/lccn/LccnNormalizerStructureB.java b/src/main/java/org/folio/search/service/lccn/LccnNormalizerStructureB.java deleted file mode 100644 index 07e4bbdc6..000000000 --- a/src/main/java/org/folio/search/service/lccn/LccnNormalizerStructureB.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.folio.search.service.lccn; - -import jakarta.validation.constraints.NotNull; -import java.util.Optional; -import lombok.extern.log4j.Log4j2; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Service; - -/** - * Class responsible for normalizing Structure B LCCN values. - */ -@Log4j2 -@Service -public class LccnNormalizerStructureB implements LccnNormalizer { - private static final String NORMALIZED_LCCN_REGEX = "\\d{10}"; - private static final char HYPHEN = '-'; - - /** - * Normalizes the given LCCN value and returns the normalized LCCN. - * If the given LCCN is invalid, an empty Optional is returned. - * - * @param lccn LCCN to be normalized - * @return Returns the normalized LCCN. If the given LCCN is invalid, returns an empty Optional - */ - public Optional apply(@NotNull final String lccn) { - var normalizedLccn = lccn; - - // Remove white spaces - normalizedLccn = normalizedLccn.replaceAll("\\s", StringUtils.EMPTY); - - // If lccn contains "/", remove it & all characters to the right of "/" - normalizedLccn = normalizedLccn.replaceAll("/.*", StringUtils.EMPTY); - - // Process the serial number component of LCCN - normalizedLccn = processSerialNumber(normalizedLccn); - - if (normalizedLccn.matches(NORMALIZED_LCCN_REGEX)) { - return Optional.of(normalizedLccn); - } - - log.warn("LCCN is not in expected format: [{}]", lccn); - return Optional.empty(); - } - - /** - * Serial number is demarcated by a hyphen (fifth character in the value). Further, the serial number must be six - * digits in length. If fewer than six digits, remove the hyphen and left fill with zeroes so that there are six - * digits in the serial number. - */ - private String processSerialNumber(String lccn) { - if (lccn.length() >= 5 && lccn.charAt(4) == HYPHEN) { - var lccnParts = lccn.split(String.valueOf(HYPHEN)); - if (lccnParts.length == 2) { - String prefix = lccnParts[0]; - StringBuilder serialNumber = new StringBuilder(lccnParts[1]); - - // Left fill the serial number with zeroes to make it six digits - while (serialNumber.length() < 6) { - serialNumber.insert(0, "0"); - } - - return serialNumber.insert(0, prefix).toString(); - } - } - return lccn; - } -} diff --git a/src/main/java/org/folio/search/service/setter/linkeddata/common/LinkedDataLccnProcessor.java b/src/main/java/org/folio/search/service/setter/linkeddata/common/LinkedDataLccnProcessor.java index 76c2173e7..f57ac864b 100644 --- a/src/main/java/org/folio/search/service/setter/linkeddata/common/LinkedDataLccnProcessor.java +++ b/src/main/java/org/folio/search/service/setter/linkeddata/common/LinkedDataLccnProcessor.java @@ -14,14 +14,12 @@ import org.folio.search.domain.dto.LinkedDataIdentifier; import org.folio.search.service.lccn.LccnNormalizer; import org.folio.search.service.setter.FieldProcessor; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component @RequiredArgsConstructor public class LinkedDataLccnProcessor implements FieldProcessor, Set> { - @Qualifier("lccnNormalizerStructureB") private final LccnNormalizer lccnNormalizer; @Override diff --git a/src/test/java/org/folio/search/controller/SearchLinkedDataInstanceIT.java b/src/test/java/org/folio/search/controller/SearchLinkedDataInstanceIT.java index 5c3a0b4af..69afbb0ec 100644 --- a/src/test/java/org/folio/search/controller/SearchLinkedDataInstanceIT.java +++ b/src/test/java/org/folio/search/controller/SearchLinkedDataInstanceIT.java @@ -200,7 +200,7 @@ void searchByLinkedDataInstance_parameterized_singleResult(int index, String que .andExpect(jsonPath(toHubAap(toParentWork(), 1), is("hubAAP2"))) .andExpect(jsonPath(toIdValue(toRootContent(), 0), is("1234567890123"))) .andExpect(jsonPath(toIdType(toRootContent(), 0), is("ISBN"))) - .andExpect(jsonPath(toIdValue(toRootContent(), 1), is(" 2023-202345/AC/r932"))) + .andExpect(jsonPath(toIdValue(toRootContent(), 1), is(" 2023202345"))) .andExpect(jsonPath(toIdType(toRootContent(), 1), is("LCCN"))) .andExpect(jsonPath(toLanguage(toParentWork(), 0), is("eng"))) .andExpect(jsonPath(toLanguage(toParentWork(), 1), is("rus"))) diff --git a/src/test/java/org/folio/search/controller/SearchLinkedDataWorkIT.java b/src/test/java/org/folio/search/controller/SearchLinkedDataWorkIT.java index 9f01963df..30c2f0857 100644 --- a/src/test/java/org/folio/search/controller/SearchLinkedDataWorkIT.java +++ b/src/test/java/org/folio/search/controller/SearchLinkedDataWorkIT.java @@ -210,7 +210,7 @@ void searchByLinkedDataWork_parameterized_singleResult(int index, String query) .andExpect(jsonPath(toFormat(toInstance()), is("Monograph"))) .andExpect(jsonPath(toIdValue(toInstance(), 0), is("1234567890123"))) .andExpect(jsonPath(toIdType(toInstance(), 0), is("ISBN"))) - .andExpect(jsonPath(toIdValue(toInstance(), 1), is(" 2023-202345/AC/r932"))) + .andExpect(jsonPath(toIdValue(toInstance(), 1), is(" 2023202345"))) .andExpect(jsonPath(toIdType(toInstance(), 1), is("LCCN"))) .andExpect(jsonPath(toNoteValue(toInstance(), 0), is("first instance note"))) .andExpect(jsonPath(toNoteType(toInstance(), 0), is("firstInstanceNoteType"))) diff --git a/src/test/java/org/folio/search/service/lccn/LccnNormalizerStructurebTest.java b/src/test/java/org/folio/search/service/lccn/LccnNormalizerStructurebTest.java deleted file mode 100644 index 528a7afab..000000000 --- a/src/test/java/org/folio/search/service/lccn/LccnNormalizerStructurebTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.folio.search.service.lccn; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Optional; -import org.folio.spring.testing.type.UnitTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import org.junit.jupiter.params.provider.ValueSource; - -@UnitTest -class LccnNormalizerStructurebTest { - private final LccnNormalizerStructureB lccnNormalizer = new LccnNormalizerStructureB(); - - @Test - void shouldRemoveSpaces() { - assertThat(lccnNormalizer.apply(" 2017000002")).isEqualTo(Optional.of("2017000002")); - assertThat(lccnNormalizer.apply("2017000002 ")).isEqualTo(Optional.of("2017000002")); - assertThat(lccnNormalizer.apply("2017 000002")).isEqualTo(Optional.of("2017000002")); - assertThat(lccnNormalizer.apply(" 20 17000 002 ")).isEqualTo(Optional.of("2017000002")); - } - - @ParameterizedTest - @CsvSource({ - "2012425165//r75, 2012425165", - "2022139101/AC/r932, 2022139101", - }) - void shouldRemoveForwardSlash(String input, String expected) { - assertThat(lccnNormalizer.apply(input)).isEqualTo(Optional.of(expected)); - } - - @Test - void shouldRemoveHyphen() { - assertThat(lccnNormalizer.apply("2022-890351")).isEqualTo(Optional.of("2022890351")); - } - - @ParameterizedTest - @CsvSource({ - "2011-89035, 2011089035", - "2020-2, 2020000002", - }) - void shouldNormalizeSerialNumber(String input, String expected) { - assertThat(lccnNormalizer.apply(input)).isEqualTo(Optional.of(expected)); - } - - @Test - void shouldNormalizeSpacesAndHyphenAndForwardSlash() { - assertThat(lccnNormalizer.apply(" 20 20-2 //r23/AC")).isEqualTo(Optional.of("2020000002")); - } - - @ParameterizedTest - @ValueSource(strings = { - "01234567891", // more than 10 digits - "2020-2-34", // more than one hyphen - "A017000002", // non-digit character - "/2017000002", // slash in the beginning - "", // empty string - "202-0234334", // "-" is in third index (instead of forth) - "2020-", // "-" is the last character - }) - void shouldReturnEmptyOptionalWhenLccnIsNotValid(String toNormalize) { - assertThat(lccnNormalizer.apply(toNormalize)).isEmpty(); - } -} diff --git a/src/test/resources/samples/linked-data/instance.json b/src/test/resources/samples/linked-data/instance.json index 2292708ab..2b00cce85 100644 --- a/src/test/resources/samples/linked-data/instance.json +++ b/src/test/resources/samples/linked-data/instance.json @@ -33,7 +33,7 @@ "type": "ISBN" }, { - "value": " 2023-202345/AC/r932", + "value": " 2023202345", "type": "LCCN" } ], diff --git a/src/test/resources/samples/linked-data/work.json b/src/test/resources/samples/linked-data/work.json index 378cc0d9e..f3aa9a2c1 100644 --- a/src/test/resources/samples/linked-data/work.json +++ b/src/test/resources/samples/linked-data/work.json @@ -105,7 +105,7 @@ "type": "ISBN" }, { - "value": " 2023-202345/AC/r932", + "value": " 2023202345", "type": "LCCN" } ],