-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODLD-385: Create beans for Default and StructureB LCCN normalization
- Loading branch information
Showing
18 changed files
with
168 additions
and
114 deletions.
There are no files selected for viewing
7 changes: 5 additions & 2 deletions
7
src/main/java/org/folio/search/cql/LccnSearchTermProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package org.folio.search.cql; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.folio.search.utils.SearchUtils; | ||
import org.folio.search.service.lccn.LccnNormalizer; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class LccnSearchTermProcessor implements SearchTermProcessor { | ||
|
||
private final LccnNormalizer lccnNormalizer; | ||
|
||
@Override | ||
public String getSearchTerm(String inputTerm) { | ||
return SearchUtils.normalizeLccn(inputTerm); | ||
return lccnNormalizer.apply(inputTerm) | ||
.orElse(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/org/folio/search/service/lccn/DefaultLccnNormalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.folio.search.service.lccn; | ||
|
||
import java.util.Optional; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Primary | ||
public class DefaultLccnNormalizer implements LccnNormalizer { | ||
|
||
@Override | ||
public Optional<String> apply(String lccn) { | ||
if (StringUtils.isBlank(lccn)) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(StringUtils.deleteWhitespace(lccn)) | ||
.map(String::toLowerCase); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/folio/search/service/lccn/LccnNormalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.folio.search.service.lccn; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public interface LccnNormalizer extends Function<String, Optional<String>> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 16 additions & 3 deletions
19
src/test/java/org/folio/search/cql/LccnSearchTermProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
package org.folio.search.cql; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
import org.folio.search.service.lccn.LccnNormalizer; | ||
import org.folio.spring.testing.type.UnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class LccnSearchTermProcessorTest { | ||
@Mock | ||
private LccnNormalizer normalizer; | ||
@InjectMocks | ||
private LccnSearchTermProcessor lccnSearchTermProcessor; | ||
|
||
@Test | ||
void getSearchTerm_positive() { | ||
// given | ||
var searchTerm = " N 123456 "; | ||
var lccnSearchTermProcessor = new LccnSearchTermProcessor(); | ||
var normalizedTerm = "n123456"; | ||
when(normalizer.apply(searchTerm)).thenReturn(Optional.of(normalizedTerm)); | ||
|
||
// when | ||
var actual = lccnSearchTermProcessor.getSearchTerm(searchTerm); | ||
assertThat(actual).isEqualTo("n123456"); | ||
|
||
// then | ||
assertThat(actual).isEqualTo(normalizedTerm); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/org/folio/search/service/lccn/DefaultLccnNormalizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.folio.search.service.lccn; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.folio.spring.testing.type.UnitTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
@UnitTest | ||
class DefaultLccnNormalizerTest { | ||
private DefaultLccnNormalizer lccnNormalizer = new DefaultLccnNormalizer(); | ||
|
||
@DisplayName("LCCN value normalization") | ||
@CsvSource({"n 1234,n1234", " N 1234 ,n1234", "*1234,*1234", "1234*,1234*"}) | ||
@ParameterizedTest(name = "[{index}] value={0}, expected={1}") | ||
void getLccnNormalized_parameterized(String value, String expected) { | ||
var normalized = lccnNormalizer.apply(value); | ||
assertThat(normalized).contains(expected); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/org/folio/search/service/lccn/LccnNormalizerStructurebTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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.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")); | ||
} | ||
|
||
@Test | ||
void shouldRemoveForwardSlash() { | ||
assertThat(lccnNormalizer.apply("2012425165//r75")).isEqualTo(Optional.of("2012425165")); | ||
assertThat(lccnNormalizer.apply("2022139101/AC/r932")).isEqualTo(Optional.of("2022139101")); | ||
} | ||
|
||
@Test | ||
void shouldRemoveHyphen() { | ||
assertThat(lccnNormalizer.apply("2022-890351")).isEqualTo(Optional.of("2022890351")); | ||
} | ||
|
||
@Test | ||
void shouldNormalizeSerialNumber() { | ||
assertThat(lccnNormalizer.apply("2011-89035")).isEqualTo(Optional.of("2011089035")); | ||
assertThat(lccnNormalizer.apply("2020-2")).isEqualTo(Optional.of("2020000002")); | ||
} | ||
|
||
@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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.