From d72bb3eccd1a4a2e4886e52f6ca934d5112c3a1a Mon Sep 17 00:00:00 2001 From: struga Date: Wed, 13 Jul 2022 14:31:16 +0300 Subject: [PATCH] Add work with card holders files (#42) --- pom.xml | 4 +- .../flow/lib/constant/OptionFields.java | 3 +- .../entry/CtxToEntryModelConverter.java | 8 +- .../entry/RecCtxToEntryModelConverter.java | 6 +- ...directWithPollingResultIntentResolver.java | 1 - .../flow/lib/service/CardDataService.java | 20 ++++ .../service/CardDataServiceDefaultImpl.java | 35 +++++++ .../CardDataServiceWithHolderNamesImpl.java | 48 +++++++++ .../lib/service/CardHolderNamesService.java | 29 ++++++ .../flow/lib/utils/AdapterProperties.java | 3 + .../flow/lib/flow/config/HandlerConfig.java | 26 ++++- ...ardDataServiceWithHolderNamesImplTest.java | 99 +++++++++++++++++++ src/test/resources/csv/holders.csv | 2 + 13 files changed, 271 insertions(+), 13 deletions(-) create mode 100644 src/main/java/dev/vality/adapter/flow/lib/service/CardDataService.java create mode 100644 src/main/java/dev/vality/adapter/flow/lib/service/CardDataServiceDefaultImpl.java create mode 100644 src/main/java/dev/vality/adapter/flow/lib/service/CardDataServiceWithHolderNamesImpl.java create mode 100644 src/main/java/dev/vality/adapter/flow/lib/service/CardHolderNamesService.java create mode 100644 src/test/java/dev/vality/adapter/flow/lib/service/CardDataServiceWithHolderNamesImplTest.java create mode 100644 src/test/resources/csv/holders.csv diff --git a/pom.xml b/pom.xml index 42db730..535acc3 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ adapter-flow-lib - 0.1.12 + 0.1.13 jar adapter-flow-lib @@ -62,7 +62,7 @@ 5.3.19 2.6.7 1.66-01353ce - 1.1.1 + 1.1.2 diff --git a/src/main/java/dev/vality/adapter/flow/lib/constant/OptionFields.java b/src/main/java/dev/vality/adapter/flow/lib/constant/OptionFields.java index 968c652..89b00f5 100644 --- a/src/main/java/dev/vality/adapter/flow/lib/constant/OptionFields.java +++ b/src/main/java/dev/vality/adapter/flow/lib/constant/OptionFields.java @@ -2,6 +2,7 @@ package dev.vality.adapter.flow.lib.constant; public enum OptionFields { - STAGE + STAGE, + CARD_HOLDER_FROM_FILE, } diff --git a/src/main/java/dev/vality/adapter/flow/lib/converter/entry/CtxToEntryModelConverter.java b/src/main/java/dev/vality/adapter/flow/lib/converter/entry/CtxToEntryModelConverter.java index c023d52..1afc9bd 100644 --- a/src/main/java/dev/vality/adapter/flow/lib/converter/entry/CtxToEntryModelConverter.java +++ b/src/main/java/dev/vality/adapter/flow/lib/converter/entry/CtxToEntryModelConverter.java @@ -1,6 +1,5 @@ package dev.vality.adapter.flow.lib.converter.entry; -import dev.vality.adapter.common.cds.BankCardExtractor; import dev.vality.adapter.common.cds.CdsStorageClient; import dev.vality.adapter.common.cds.model.CardDataProxyModel; import dev.vality.adapter.common.damsel.ProxyProviderPackageCreators; @@ -10,6 +9,7 @@ import dev.vality.adapter.flow.lib.constant.Step; import dev.vality.adapter.flow.lib.constant.TargetStatus; import dev.vality.adapter.flow.lib.model.*; import dev.vality.adapter.flow.lib.serde.TemporaryContextDeserializer; +import dev.vality.adapter.flow.lib.service.CardDataService; import dev.vality.adapter.flow.lib.service.IdGenerator; import dev.vality.adapter.flow.lib.service.TemporaryContextService; import dev.vality.adapter.flow.lib.utils.CallbackUrlExtractor; @@ -37,6 +37,7 @@ public class CtxToEntryModelConverter implements Converter cardHoldersNames; + + public CardHolderNamesService(AdapterProperties properties) throws IOException { + if (properties.getCardHolderNamesFile() != null) { + this.cardHoldersNames = Files.readAllLines(properties.getCardHolderNamesFile().getFile().toPath()) + .stream() + .sorted() + .collect(Collectors.toList()); + } else { + this.cardHoldersNames = List.of(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/dev/vality/adapter/flow/lib/utils/AdapterProperties.java b/src/main/java/dev/vality/adapter/flow/lib/utils/AdapterProperties.java index d553419..c50d339 100644 --- a/src/main/java/dev/vality/adapter/flow/lib/utils/AdapterProperties.java +++ b/src/main/java/dev/vality/adapter/flow/lib/utils/AdapterProperties.java @@ -2,6 +2,7 @@ package dev.vality.adapter.flow.lib.utils; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.io.Resource; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.NotEmpty; @@ -34,4 +35,6 @@ public class AdapterProperties { "tag" ); + private Resource cardHolderNamesFile; + } \ No newline at end of file diff --git a/src/test/java/dev/vality/adapter/flow/lib/flow/config/HandlerConfig.java b/src/test/java/dev/vality/adapter/flow/lib/flow/config/HandlerConfig.java index cecac08..f5e8998 100644 --- a/src/test/java/dev/vality/adapter/flow/lib/flow/config/HandlerConfig.java +++ b/src/test/java/dev/vality/adapter/flow/lib/flow/config/HandlerConfig.java @@ -31,7 +31,9 @@ import dev.vality.damsel.proxy_provider.ProviderProxySrv; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import java.io.IOException; import java.util.List; @Configuration @@ -82,17 +84,30 @@ public class HandlerConfig { temporaryContextService); } + @Bean + public CardHolderNamesService cardHolderNamesService(AdapterProperties properties) throws IOException { + return new CardHolderNamesService(properties); + } + + @Bean + public CardDataServiceWithHolderNamesImpl cardDataService(CdsStorageClient cdsStorageClient, + CardHolderNamesService cardHolderNamesService) { + return new CardDataServiceWithHolderNamesImpl(cdsStorageClient, cardHolderNamesService); + } + @Bean public CtxToEntryModelConverter ctxToEntryModelConverter(CdsStorageClient cdsStorageClient, TemporaryContextDeserializer adapterDeserializer, IdGenerator idGenerator, TemporaryContextService temporaryContextService, - CallbackUrlExtractor callbackUrlExtractor) { + CallbackUrlExtractor callbackUrlExtractor, + CardDataServiceWithHolderNamesImpl cardDataService) { return new CtxToEntryModelConverter(cdsStorageClient, adapterDeserializer, idGenerator, temporaryContextService, - callbackUrlExtractor); + callbackUrlExtractor, + cardDataService); } @Bean @@ -100,6 +115,7 @@ public class HandlerConfig { AdapterProperties adapterProperties = new AdapterProperties(); adapterProperties.setCallbackUrl("http://localhost:8080/adapter/term_url"); adapterProperties.setSuccessRedirectUrl("http://localhost:8080/adapter/term_url"); + adapterProperties.setCardHolderNamesFile(new ClassPathResource("csv/holders.csv")); return adapterProperties; } @@ -107,11 +123,13 @@ public class HandlerConfig { public RecCtxToEntryModelConverter recCtxToEntryModelConverter(CdsStorageClient cdsStorageClient, TemporaryContextDeserializer adapterDeserializer, IdGenerator idGenerator, - TemporaryContextService temporaryContextService) { + TemporaryContextService temporaryContextService, + CardDataServiceWithHolderNamesImpl cardDataService) { return new RecCtxToEntryModelConverter(adapterDeserializer, cdsStorageClient, idGenerator, - temporaryContextService); + temporaryContextService, + cardDataService); } @Bean diff --git a/src/test/java/dev/vality/adapter/flow/lib/service/CardDataServiceWithHolderNamesImplTest.java b/src/test/java/dev/vality/adapter/flow/lib/service/CardDataServiceWithHolderNamesImplTest.java new file mode 100644 index 0000000..8a37e34 --- /dev/null +++ b/src/test/java/dev/vality/adapter/flow/lib/service/CardDataServiceWithHolderNamesImplTest.java @@ -0,0 +1,99 @@ +package dev.vality.adapter.flow.lib.service; + +import dev.vality.adapter.common.cds.model.CardDataProxyModel; +import dev.vality.adapter.flow.lib.constant.OptionFields; +import dev.vality.adapter.flow.lib.flow.AbstractPaymentTest; +import dev.vality.adapter.flow.lib.flow.simple.redirect.config.SimpleRedirectWithPollingDsFlowConfig; +import dev.vality.cds.storage.CardData; +import dev.vality.damsel.domain.BankCard; +import dev.vality.damsel.domain.BankCardExpDate; +import dev.vality.damsel.proxy_provider.PaymentContext; +import dev.vality.damsel.proxy_provider.RecurrentTokenContext; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockitoAnnotations; +import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = SimpleRedirectWithPollingDsFlowConfig.class) +@TestPropertySource(properties = {"server.rest.port=8083", + "error-mapping.file=classpath:fixture/errors.json"}) +class CardDataServiceWithHolderNamesImplTest extends AbstractPaymentTest { + + public static final String TEST_1 = "TEST 1"; + @Autowired + private CardDataServiceWithHolderNamesImpl cardDataServiceWithHolderNames; + + @BeforeEach + public void setUp() throws TException { + MockitoAnnotations.openMocks(this); + doAnswer((Answer) invocationOnMock -> CardDataProxyModel.builder() + .cardholderName(TEST_1) + .build()).when(cdsStorageClient).getCardDataWithListHolders(any(PaymentContext.class), any()); + } + + @Test + void getCardDataProxyModel() { + CardDataProxyModel cardDataProxyModel = cardDataServiceWithHolderNames.getCardDataProxyModel( + new PaymentContext() + .setOptions(Map.of(OptionFields.CARD_HOLDER_FROM_FILE.name(), "true")), + new CardData(), + new BankCard() + .setToken("test") + .setExpDate(new BankCardExpDate())); + + Assertions.assertEquals(TEST_1, cardDataProxyModel.getCardholderName()); + + + cardDataProxyModel = cardDataServiceWithHolderNames.getCardDataProxyModel( + new PaymentContext() + .setOptions(Map.of()), + new CardData(), + new BankCard().setToken("test") + .setExpDate(new BankCardExpDate())); + + Assertions.assertNotEquals(TEST_1, cardDataProxyModel.getCardholderName()); + } + + @Test + void testGetCardDataProxyModel() { + CardDataProxyModel cardDataProxyModel = cardDataServiceWithHolderNames.getCardDataProxyModel( + new RecurrentTokenContext() + .setOptions(Map.of(OptionFields.CARD_HOLDER_FROM_FILE.name(), "true")), + new CardData(), + new BankCard() + .setToken("test") + .setExpDate(new BankCardExpDate())); + + Assertions.assertEquals(TEST_1, cardDataProxyModel.getCardholderName()); + + cardDataProxyModel = cardDataServiceWithHolderNames.getCardDataProxyModel( + new RecurrentTokenContext() + .setOptions(Map.of()), + new CardData(), + new BankCard().setToken("test") + .setExpDate(new BankCardExpDate())); + + Assertions.assertNotEquals(TEST_1, cardDataProxyModel.getCardholderName()); + } + + @Test + void getCardDataProxyModelFromCds() { + CardDataProxyModel cardDataProxyModel = cardDataServiceWithHolderNames.getCardDataProxyModelFromCds( + new PaymentContext().setOptions(Map.of(OptionFields.CARD_HOLDER_FROM_FILE.name(), "true"))); + + Assertions.assertEquals(TEST_1, cardDataProxyModel.getCardholderName()); + } +} \ No newline at end of file diff --git a/src/test/resources/csv/holders.csv b/src/test/resources/csv/holders.csv new file mode 100644 index 0000000..166b2bc --- /dev/null +++ b/src/test/resources/csv/holders.csv @@ -0,0 +1,2 @@ +TEST 1 +TEST 2 \ No newline at end of file