diff --git a/core/src/main/java/io/bisq/core/payment/ReceiptPredicates.java b/core/src/main/java/io/bisq/core/payment/ReceiptPredicates.java index 6c1a67b89e..060ad5071c 100644 --- a/core/src/main/java/io/bisq/core/payment/ReceiptPredicates.java +++ b/core/src/main/java/io/bisq/core/payment/ReceiptPredicates.java @@ -31,7 +31,7 @@ class ReceiptPredicates { return arePaymentMethodsEqual; } - boolean isSameOrSpecificBank(Offer offer, PaymentAccount account) { + boolean isOfferRequireSameOrSpecificBank(Offer offer, PaymentAccount account) { PaymentMethod paymentMethod = offer.getPaymentMethod(); boolean isSameOrSpecificBank = paymentMethod.equals(PaymentMethod.SAME_BANK) || paymentMethod.equals(PaymentMethod.SPECIFIC_BANKS); diff --git a/core/src/main/java/io/bisq/core/payment/ReceiptValidator.java b/core/src/main/java/io/bisq/core/payment/ReceiptValidator.java index bf84800f13..07820193e1 100644 --- a/core/src/main/java/io/bisq/core/payment/ReceiptValidator.java +++ b/core/src/main/java/io/bisq/core/payment/ReceiptValidator.java @@ -13,11 +13,14 @@ class ReceiptValidator { private final PaymentAccount account; private final Offer offer; - ReceiptValidator(Offer offer, PaymentAccount account) { - this.account = account; + this(offer, account, new ReceiptPredicates()); + } + + ReceiptValidator(Offer offer, PaymentAccount account, ReceiptPredicates predicates) { this.offer = offer; - this.predicates = new ReceiptPredicates(); + this.account = account; + this.predicates = predicates; } boolean isValid() { @@ -38,14 +41,14 @@ class ReceiptValidator { // We have same country if (predicates.isSepaRelated(offer, account)) { return isEqualPaymentMethods; - } else if (predicates.isSameOrSpecificBank(offer, account)) { - return isValidForSameOrSpecificBankAccount(); + } else if (predicates.isOfferRequireSameOrSpecificBank(offer, account)) { + return isValidWhenOfferRequireSameOrSpecificBank(); } else { return isValidByType(); } } - private boolean isValidForSameOrSpecificBankAccount() { + private boolean isValidWhenOfferRequireSameOrSpecificBank() { final List acceptedBanksForOffer = offer.getAcceptedBankIds(); Preconditions.checkNotNull(acceptedBanksForOffer, "offer.getAcceptedBankIds() must not be null"); diff --git a/core/src/test/java/io/bisq/core/payment/ReceiptPredicatesTest.java b/core/src/test/java/io/bisq/core/payment/ReceiptPredicatesTest.java index 09ee3a7499..5336c131f0 100644 --- a/core/src/test/java/io/bisq/core/payment/ReceiptPredicatesTest.java +++ b/core/src/test/java/io/bisq/core/payment/ReceiptPredicatesTest.java @@ -53,7 +53,7 @@ public class ReceiptPredicatesTest { Offer offer = mock(Offer.class); when(offer.getPaymentMethod()).thenReturn(PaymentMethod.SAME_BANK); - assertTrue(predicates.isSameOrSpecificBank(offer, mock(NationalBankAccount.class))); + assertTrue(predicates.isOfferRequireSameOrSpecificBank(offer, mock(NationalBankAccount.class))); } @Test diff --git a/core/src/test/java/io/bisq/core/payment/ReceiptValidatorTest.java b/core/src/test/java/io/bisq/core/payment/ReceiptValidatorTest.java new file mode 100644 index 0000000000..ad086dc720 --- /dev/null +++ b/core/src/test/java/io/bisq/core/payment/ReceiptValidatorTest.java @@ -0,0 +1,170 @@ +package io.bisq.core.payment; + +import io.bisq.core.offer.Offer; +import io.bisq.core.payment.payload.PaymentMethod; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({SpecificBanksAccount.class, SameBankAccount.class, NationalBankAccount.class, + WesternUnionAccount.class, PaymentMethod.class}) +public class ReceiptValidatorTest { + private ReceiptValidator validator; + private PaymentAccount account; + private Offer offer; + private ReceiptPredicates predicates; + + @Before + public void setUp() { + this.predicates = mock(ReceiptPredicates.class); + this.account = mock(CountryBasedPaymentAccount.class); + this.offer = mock(Offer.class); + this.validator = new ReceiptValidator(offer, account, predicates); + } + + @Test + public void testIsValidWhenCurrencyDoesNotMatch() { + when(predicates.isMatchingCurrency(offer, account)).thenReturn(false); + + assertFalse(validator.isValid()); + verify(predicates).isMatchingCurrency(offer, account); + } + + @Test + public void testIsValidWhenNotCountryBasedAccount() { + account = mock(PaymentAccount.class); + assertFalse(account instanceof CountryBasedPaymentAccount); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + + assertTrue(new ReceiptValidator(offer, account, predicates).isValid()); + } + + @Test + public void testIsValidWhenNotMatchingCodes() { + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(false); + + assertFalse(validator.isValid()); + verify(predicates).isMatchingCountryCodes(offer, account); + } + + @Test + public void testIsValidWhenSepaRelated() { + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(false); + when(predicates.isSepaRelated(offer, account)).thenReturn(true); + + assertFalse(validator.isValid()); + verify(predicates).isSepaRelated(offer, account); + } + + @Test + public void testIsValidWhenSpecificBankAccountAndOfferRequireSpecificBank() { + account = mock(SpecificBanksAccount.class); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(true); + + assertFalse(new ReceiptValidator(offer, account, predicates).isValid()); + verify((SpecificBanksAccount) account).getAcceptedBanks(); + } + + @Test + public void testIsValidWhenSameBankAccountAndOfferRequireSpecificBank() { + account = mock(SameBankAccount.class); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(true); + + assertFalse(new ReceiptValidator(offer, account, predicates).isValid()); + verify((BankAccount) account).getBankId(); + } + + @Test + public void testIsValidWhenSpecificBankAccount() { + account = mock(SpecificBanksAccount.class); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(false); + + assertFalse(new ReceiptValidator(offer, account, predicates).isValid()); + verify((SpecificBanksAccount) account).getAcceptedBanks(); + } + + @Test + public void testIsValidWhenSameBankAccount() { + account = mock(SameBankAccount.class); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(false); + + assertFalse(new ReceiptValidator(offer, account, predicates).isValid()); + verify((SameBankAccount) account).getBankId(); + } + + @Test + public void testIsValidWhenNationalBankAccount() { + account = mock(NationalBankAccount.class); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(false); + + assertTrue(new ReceiptValidator(offer, account, predicates).isValid()); + } + + @Test + public void testIsValidWhenWesternUnionAccount(){ + account = mock(WesternUnionAccount.class); + + PaymentMethod.WESTERN_UNION = mock(PaymentMethod.class); + + when(offer.getPaymentMethod()).thenReturn(PaymentMethod.WESTERN_UNION); + + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(false); + + assertTrue(new ReceiptValidator(offer, account, predicates).isValid()); + verify(offer).getPaymentMethod(); + } + + @Test + public void testIsValidWhenWesternIrregularAccount(){ + when(predicates.isMatchingCurrency(offer, account)).thenReturn(true); + when(predicates.isEqualPaymentMethods(offer, account)).thenReturn(true); + when(predicates.isMatchingCountryCodes(offer, account)).thenReturn(true); + when(predicates.isSepaRelated(offer, account)).thenReturn(false); + when(predicates.isOfferRequireSameOrSpecificBank(offer, account)).thenReturn(false); + + assertFalse(validator.isValid()); + } +}