Skip to content

Commit

Permalink
Allow secure account number to be used with any account number class
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Apr 2, 2021
1 parent 5811e8f commit 6af76f2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class AccountNumberComplete extends BaseRawData implements AccountNumber {
final String accountNumberString = parseAccountNumber(trimToEmpty(rawAccountNumber));
accountNumber = new DisposableStringData(accountNumberString);

// Secure information (metadata) that has no part of the actual account in it
final boolean passesLuhnCheck = luhnCheck();
final MajorIndustryIdentifier majorIndustryIdentifier =
MajorIndustryIdentifier.from(accountNumberString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import static java.util.Objects.requireNonNull;

final class AccountNumberSecure implements AccountNumber {
public final class AccountNumberSecure implements AccountNumber {

private static final long serialVersionUID = 2002490292247684624L;

Expand All @@ -21,6 +21,22 @@ final class AccountNumberSecure implements AccountNumber {
private final boolean isPrimaryAccountNumberValid;
private final boolean isExceedsMaximumLength;

/**
* Captures secure information (metadata) that has no part of the actual account number in it.
*
* @param accountNumber Account number with metadata
*/
public AccountNumberSecure(final AccountNumber accountNumber) {
this(
requireNonNull(accountNumber, "No account number provided").getCardBrand(),
accountNumber.getMajorIndustryIdentifier(),
accountNumber.passesLuhnCheck(),
accountNumber.getAccountNumberLength(),
accountNumber.isLengthValid(),
accountNumber.isPrimaryAccountNumberValid(),
accountNumber.exceedsMaximumLength());
}

AccountNumberSecure(
final CardBrand cardBrand,
final MajorIndustryIdentifier majorIndustryIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static us.fatehi.creditcardnumber.AccountNumbers.accountNumber;
import static us.fatehi.test.utility.AccountNumbersTestUtility.equivalent;

import org.junit.jupiter.api.Test;

import us.fatehi.creditcardnumber.AccountNumber;
import us.fatehi.creditcardnumber.AccountNumberSecure;
import us.fatehi.creditcardnumber.CardBrand;
import us.fatehi.creditcardnumber.MajorIndustryIdentifier;

Expand Down Expand Up @@ -64,6 +66,14 @@ public void pan1() {
assertThat(pan.isPrimaryAccountNumberValid(), is(true));
assertThat(pan.getAccountNumberLength(), is(16));
check(rawAccountNumber, pan, rawAccountNumber);

final AccountNumber securePan1 = pan.toSecureAccountNumber();
assertThat(pan != securePan1, is(true));
assertThat(equivalent(pan, securePan1), is(true));

final AccountNumber securePan2 = new AccountNumberSecure(pan);
assertThat(pan != securePan2, is(true));
assertThat(equivalent(pan, securePan2), is(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.nullValue;
import static us.fatehi.test.utility.AccountNumbersTestUtility.equivalent;

import org.junit.jupiter.api.Test;

import us.fatehi.creditcardnumber.AccountNumber;
import us.fatehi.creditcardnumber.AccountNumberSecure;
import us.fatehi.creditcardnumber.AccountNumbers;
import us.fatehi.creditcardnumber.CardBrand;
import us.fatehi.creditcardnumber.MajorIndustryIdentifier;
Expand Down Expand Up @@ -49,8 +51,13 @@ public void secureAccountNumber() {
assertThat(pan.getLastFourDigits(), is(nullValue()));
assertThat(pan.getIssuerIdentificationNumber(), is(nullValue()));

final AccountNumber securePan = pan.toSecureAccountNumber();
assertThat(pan == securePan, is(true));
final AccountNumber securePan1 = pan.toSecureAccountNumber();
assertThat(pan == securePan1, is(true));
assertThat(equivalent(pan, securePan1), is(true));

final AccountNumber securePan2 = new AccountNumberSecure(pan);
assertThat(pan != securePan2, is(true));
assertThat(equivalent(pan, securePan2), is(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package us.fatehi.test.utility;

import us.fatehi.creditcardnumber.AccountNumber;

public class AccountNumbersTestUtility {

public static boolean equivalent(final AccountNumber pan1, final AccountNumber pan2) {
if (pan1 == pan2) {
return true;
}
if (pan1 == null || pan2 == null) {
return false;
}

// Compare metadata
if (pan1.getCardBrand() != pan2.getCardBrand()) {
return false;
}
if (pan1.getMajorIndustryIdentifier() != pan2.getMajorIndustryIdentifier()) {
return false;
}
if (pan1.passesLuhnCheck() != pan2.passesLuhnCheck()) {
return false;
}
if (pan1.getAccountNumberLength() != pan2.getAccountNumberLength()) {
return false;
}
if (pan1.isLengthValid() != pan2.isLengthValid()) {
return false;
}
if (pan1.isPrimaryAccountNumberValid() != pan2.isPrimaryAccountNumberValid()) {
return false;
}
if (pan1.exceedsMaximumLength() != pan2.exceedsMaximumLength()) {
return false;
}

return true;
}

private AccountNumbersTestUtility() {
// Prevent instantation
}
}

0 comments on commit 6af76f2

Please sign in to comment.