From cd97e117566e1487476cc4674e86b2f6e09a5f2b Mon Sep 17 00:00:00 2001 From: struga Date: Wed, 13 Jul 2022 12:13:27 +0300 Subject: [PATCH] Add method for load card holder from custom list (#32) --- pom.xml | 2 +- .../adapter/common/cds/BankCardExtractor.java | 13 ++++++++ .../adapter/common/cds/CdsStorageClient.java | 12 ++++++-- .../common/cds/BankCardExtractorTest.java | 30 +++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/test/java/dev/vality/adapter/common/cds/BankCardExtractorTest.java diff --git a/pom.xml b/pom.xml index 8a2a76a..030fac6 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ adapter-common-lib - 1.1.1 + 1.1.2 jar adapter-common-lib diff --git a/src/main/java/dev/vality/adapter/common/cds/BankCardExtractor.java b/src/main/java/dev/vality/adapter/common/cds/BankCardExtractor.java index 03b8756..87b32a3 100644 --- a/src/main/java/dev/vality/adapter/common/cds/BankCardExtractor.java +++ b/src/main/java/dev/vality/adapter/common/cds/BankCardExtractor.java @@ -10,6 +10,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; +import java.util.List; import java.util.Locale; @NoArgsConstructor(access = AccessLevel.PRIVATE) @@ -19,11 +20,19 @@ public class BankCardExtractor { private static final String NAME_REGEXP = "[^a-zA-Z +]"; public static CardDataProxyModel initCardDataProxyModel(BankCard bankCard, CardData cardData) { + return initCardDataProxyModel(bankCard, cardData, null); + } + + public static CardDataProxyModel initCardDataProxyModel(BankCard bankCard, + CardData cardData, + List cardHoldersNames) { String cardHolder; if (bankCard.isSetCardholderName()) { cardHolder = bankCard.getCardholderName(); } else if (cardData.isSetCardholderName()) { cardHolder = cardData.getCardholderName(); + } else if (cardHoldersNames != null && cardHoldersNames.size() > 0) { + cardHolder = getCardHolderFromList(cardHoldersNames, bankCard.getToken()); } else { cardHolder = (FAKER_NAME.firstName() + StringUtils.SPACE + FAKER_NAME.lastName()) .replaceAll(NAME_REGEXP, StringUtils.EMPTY) @@ -41,4 +50,8 @@ public class BankCardExtractor { .expYear(bankCard.isSetExpDate() ? bankCard.getExpDate().getYear() : cardData.getExpDate().getYear()) .build(); } + + private static String getCardHolderFromList(List cardHoldersNames, String cardToken) { + return cardHoldersNames.get(Math.abs(cardToken.hashCode() % cardHoldersNames.size())); + } } diff --git a/src/main/java/dev/vality/adapter/common/cds/CdsStorageClient.java b/src/main/java/dev/vality/adapter/common/cds/CdsStorageClient.java index cf47aa8..00da56e 100644 --- a/src/main/java/dev/vality/adapter/common/cds/CdsStorageClient.java +++ b/src/main/java/dev/vality/adapter/common/cds/CdsStorageClient.java @@ -17,6 +17,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.thrift.TException; import org.springframework.cache.annotation.Cacheable; +import java.util.List; + import static dev.vality.adapter.common.damsel.ProxyProviderPackageExtractors.*; @Slf4j @@ -47,9 +49,7 @@ public class CdsStorageClient { } public CardDataProxyModel getCardData(PaymentContext context) { - CardData cardData = getCardData(extractBankCardToken(extractPaymentResource(context))); - BankCard bankCard = extractBankCard(context.getPaymentInfo()); - return BankCardExtractor.initCardDataProxyModel(bankCard, cardData); + return getCardDataWithListHolders(context, null); } public CardDataProxyModel getCardData(Withdrawal withdrawal) { @@ -80,6 +80,12 @@ public class CdsStorageClient { return BankCardExtractor.initCardDataProxyModel(bankCard, getCardData(bankCard.getToken())); } + public CardDataProxyModel getCardDataWithListHolders(PaymentContext context, List cardHoldersNames) { + CardData cardData = getCardData(extractBankCardToken(extractPaymentResource(context))); + BankCard bankCard = extractBankCard(context.getPaymentInfo()); + return BankCardExtractor.initCardDataProxyModel(bankCard, cardData, cardHoldersNames); + } + public SessionData getSessionData(PaymentContext context) { DisposablePaymentResource disposablePaymentResource = extractDisposablePaymentResource(context); if (!disposablePaymentResource.isSetPaymentSessionId()) { diff --git a/src/test/java/dev/vality/adapter/common/cds/BankCardExtractorTest.java b/src/test/java/dev/vality/adapter/common/cds/BankCardExtractorTest.java new file mode 100644 index 0000000..4487946 --- /dev/null +++ b/src/test/java/dev/vality/adapter/common/cds/BankCardExtractorTest.java @@ -0,0 +1,30 @@ +package dev.vality.adapter.common.cds; + +import dev.vality.adapter.common.cds.model.CardDataProxyModel; +import dev.vality.cds.storage.CardData; +import dev.vality.damsel.domain.BankCard; +import dev.vality.damsel.domain.BankCardExpDate; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class BankCardExtractorTest { + + public static final String TEST_2 = "test_2"; + public static final String TEST_1 = "test_1"; + + @Test + void initCardDataProxyModel() { + CardDataProxyModel cardDataProxyModel = + BankCardExtractor.initCardDataProxyModel( + new BankCard() + .setToken("test") + .setExpDate(new BankCardExpDate()), + new CardData(), + List.of(TEST_1, TEST_2)); + + assertEquals(cardDataProxyModel.getCardholderName(), TEST_1); + } +} \ No newline at end of file