From 08b78615e2347105492bef273990147a52975ebc Mon Sep 17 00:00:00 2001 From: Paulo Gomes da Cruz Junior Date: Thu, 16 Nov 2023 16:20:43 -0800 Subject: [PATCH] feat(FSADT1-973): adding doing business as matcher --- .../ClientDoingBusinessAsRepository.java | 10 +++ .../DoingBusinessAsProcessorMatcher.java | 57 ++++++++++++ .../DoingBusinessAsProcessorMatcherTest.java | 86 +++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 processor/src/main/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcher.java create mode 100644 processor/src/test/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcherTest.java diff --git a/processor/src/main/java/ca/bc/gov/app/repository/legacy/ClientDoingBusinessAsRepository.java b/processor/src/main/java/ca/bc/gov/app/repository/legacy/ClientDoingBusinessAsRepository.java index b932f7c224..a88b803025 100644 --- a/processor/src/main/java/ca/bc/gov/app/repository/legacy/ClientDoingBusinessAsRepository.java +++ b/processor/src/main/java/ca/bc/gov/app/repository/legacy/ClientDoingBusinessAsRepository.java @@ -1,9 +1,11 @@ package ca.bc.gov.app.repository.legacy; import ca.bc.gov.app.entity.legacy.ClientDoingBusinessAsEntity; +import org.springframework.data.r2dbc.repository.Query; import org.springframework.data.repository.reactive.ReactiveCrudRepository; import org.springframework.data.repository.reactive.ReactiveSortingRepository; import org.springframework.stereotype.Repository; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @Repository @@ -12,4 +14,12 @@ public interface ClientDoingBusinessAsRepository extends ReactiveSortingRepository { Mono existsByClientNumber(String clientNumber); + + @Query(""" + SELECT * + FROM THE.CLIENT_DOING_BUSINESS_AS + WHERE + UTL_MATCH.JARO_WINKLER_SIMILARITY(UPPER(DOING_BUSINESS_AS_NAME),UPPER(:companyName)) >= 95 + ORDER BY CLIENT_NUMBER""") + Flux matchBy(String companyName); } diff --git a/processor/src/main/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcher.java b/processor/src/main/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcher.java new file mode 100644 index 0000000000..2cf6c66c0c --- /dev/null +++ b/processor/src/main/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcher.java @@ -0,0 +1,57 @@ +package ca.bc.gov.app.service.processor; + +import static java.util.function.Predicate.not; + +import ca.bc.gov.app.dto.MatcherResult; +import ca.bc.gov.app.dto.SubmissionInformationDto; +import ca.bc.gov.app.entity.legacy.ClientDoingBusinessAsEntity; +import ca.bc.gov.app.entity.legacy.ForestClientEntity; +import ca.bc.gov.app.repository.legacy.ClientDoingBusinessAsRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Component +@RequiredArgsConstructor +@Slf4j +public class DoingBusinessAsProcessorMatcher implements ProcessorMatcher { + + private final ClientDoingBusinessAsRepository doingBusinessAsRepository; + + @Override + public boolean enabled(SubmissionInformationDto submission) { + return "RSP".equalsIgnoreCase(submission.clientType()); + } + + @Override + public String name() { + return "Doing Business As Fuzzy Matcher"; + } + + @Override + public Mono matches(SubmissionInformationDto submission) { + + log.info("{} :: Validating {}", name(), submission.corporationName()); + + return + matchBy(submission.corporationName()) + .map(ClientDoingBusinessAsEntity::getClientNumber) + .collectList() + .filter(not(List::isEmpty)) + .map(values -> + new MatcherResult("corporationName", String.join(",", values)) + ); + } + + private Flux matchBy(String companyName) { + return + doingBusinessAsRepository + .matchBy(companyName) + .doOnNext(entity -> log.info("Found a match {}", entity)); + + } + +} diff --git a/processor/src/test/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcherTest.java b/processor/src/test/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcherTest.java new file mode 100644 index 0000000000..853b413ce9 --- /dev/null +++ b/processor/src/test/java/ca/bc/gov/app/service/processor/DoingBusinessAsProcessorMatcherTest.java @@ -0,0 +1,86 @@ +package ca.bc.gov.app.service.processor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import ca.bc.gov.app.dto.MatcherResult; +import ca.bc.gov.app.dto.SubmissionInformationDto; +import ca.bc.gov.app.entity.legacy.ClientDoingBusinessAsEntity; +import ca.bc.gov.app.entity.legacy.ForestClientEntity; +import ca.bc.gov.app.repository.legacy.ClientDoingBusinessAsRepository; +import ca.bc.gov.app.repository.legacy.ForestClientRepository; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +@DisplayName("Unit Test | Doing Business As Matcher") +class DoingBusinessAsProcessorMatcherTest { + + private final ClientDoingBusinessAsRepository repository = mock(ClientDoingBusinessAsRepository.class); + ProcessorMatcher matcher = new DoingBusinessAsProcessorMatcher(repository); + + @Test + @DisplayName("Name matching") + void shouldMatchName() { + assertEquals("Doing Business As Fuzzy Matcher", matcher.name()); + } + + @ParameterizedTest + @MethodSource("legalName") + @DisplayName("Match or not") + void shouldMatchOrNot( + SubmissionInformationDto dto, + boolean success, + MatcherResult result, + Flux mockData + ) { + + when(repository.matchBy(dto.corporationName())) + .thenReturn(mockData); + + StepVerifier.FirstStep verifier = + matcher + .matches(dto) + .as(StepVerifier::create); + + if (success) { + verifier.verifyComplete(); + } else { + verifier + .expectNext(result) + .verifyComplete(); + } + } + + private static Stream legalName() { + return + Stream.of( + Arguments.of( + new SubmissionInformationDto("James", null,null, null,"C"), + true, + null, + Flux.empty() + ), + Arguments.of( + new SubmissionInformationDto("Marco", null, null, null,"C"), + false, + new MatcherResult("corporationName", String.join(",", "00000000")), + Flux.just(new ForestClientEntity().withClientNumber("00000000")) + ), + Arguments.of( + new SubmissionInformationDto("Lucca", null, null, null,"C"), + false, + new MatcherResult("corporationName", String.join(",", "00000000", "00000001")), + Flux.just(new ClientDoingBusinessAsEntity().withClientNumber("00000000"), + new ClientDoingBusinessAsEntity().withClientNumber("00000001")) + ) + ); + } + +} \ No newline at end of file