add init card model with optional exp date (#34)

* add init card model with optional exp date

* bump

Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
Gregory 2022-08-10 17:39:47 +03:00 committed by GitHub
parent 48a84735ab
commit cde1b30665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 16 deletions

View File

@ -12,7 +12,7 @@
</parent> </parent>
<artifactId>adapter-common-lib</artifactId> <artifactId>adapter-common-lib</artifactId>
<version>1.1.3</version> <version>1.1.4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>adapter-common-lib</name> <name>adapter-common-lib</name>

View File

@ -9,6 +9,7 @@ import dev.vality.damsel.domain.BankCard;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -26,23 +27,10 @@ public class BankCardExtractor {
public static CardDataProxyModel initCardDataProxyModel(BankCard bankCard, public static CardDataProxyModel initCardDataProxyModel(BankCard bankCard,
CardData cardData, CardData cardData,
List<String> cardHoldersNames) { List<String> cardHoldersNames) {
String cardHolder; String cardHolder = extractCardHolder(bankCard, cardData, cardHoldersNames);
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)
.toUpperCase();
}
if (!bankCard.isSetExpDate() && !cardData.isSetExpDate()) { if (!bankCard.isSetExpDate() && !cardData.isSetExpDate()) {
throw new CdsStorageExpDateException("Expiration date not found"); throw new CdsStorageExpDateException("Expiration date not found");
} }
return CardDataProxyModel.builder() return CardDataProxyModel.builder()
.cardholderName(cardHolder) .cardholderName(cardHolder)
.pan(cardData.getPan()) .pan(cardData.getPan())
@ -51,7 +39,45 @@ public class BankCardExtractor {
.build(); .build();
} }
private static String extractCardHolder(BankCard bankCard, CardData cardData, List<String> cardHoldersNames) {
if (bankCard.isSetCardholderName()) {
return bankCard.getCardholderName();
} else if (cardData.isSetCardholderName()) {
return cardData.getCardholderName();
} else if (!CollectionUtils.isEmpty(cardHoldersNames)) {
return getCardHolderFromList(cardHoldersNames, bankCard.getToken());
} else {
return (FAKER_NAME.firstName() + StringUtils.SPACE + FAKER_NAME.lastName())
.replaceAll(NAME_REGEXP, StringUtils.EMPTY)
.toUpperCase();
}
}
private static String getCardHolderFromList(List<String> cardHoldersNames, String cardToken) { private static String getCardHolderFromList(List<String> cardHoldersNames, String cardToken) {
return cardHoldersNames.get(Math.abs(cardToken.hashCode() % cardHoldersNames.size())); return cardHoldersNames.get(Math.abs(cardToken.hashCode() % cardHoldersNames.size()));
} }
public static CardDataProxyModel initCardDataProxyModelWithOptionalExpDate(BankCard bankCard, CardData cardData) {
String cardHolder = extractCardHolder(bankCard, cardData, null);
return CardDataProxyModel.builder()
.cardholderName(cardHolder)
.pan(cardData.getPan())
.expMonth(getExpMonth(bankCard, cardData))
.expYear(getExpYear(bankCard, cardData))
.build();
}
private static byte getExpMonth(BankCard bankCard, CardData cardData) {
if (bankCard.isSetExpDate()) {
return bankCard.getExpDate().getMonth();
}
return cardData.isSetExpDate() ? cardData.getExpDate().getMonth() : 0;
}
private static short getExpYear(BankCard bankCard, CardData cardData) {
if (bankCard.isSetExpDate()) {
return bankCard.getExpDate().getYear();
}
return cardData.isSetExpDate() ? cardData.getExpDate().getYear() : 0;
}
} }

View File

@ -25,6 +25,23 @@ class BankCardExtractorTest {
new CardData(), new CardData(),
List.of(TEST_1, TEST_2)); List.of(TEST_1, TEST_2));
assertEquals(cardDataProxyModel.getCardholderName(), TEST_1); assertEquals(TEST_1, cardDataProxyModel.getCardholderName());
}
@Test
void initCardDataProxyModelWithOptionalExpDate() {
String pan = "pan";
CardDataProxyModel cardDataProxyModel =
BankCardExtractor.initCardDataProxyModelWithOptionalExpDate(
new BankCard()
.setToken("test")
.setCardholderName(TEST_1),
new CardData()
.setPan(pan));
assertEquals(TEST_1, cardDataProxyModel.getCardholderName());
assertEquals(pan, cardDataProxyModel.getPan());
assertEquals(0, cardDataProxyModel.getExpMonth());
assertEquals(0, cardDataProxyModel.getExpYear());
} }
} }