implemented unit test for ReceiptValidator

This commit is contained in:
Sergey Rozhnov 2018-01-30 12:39:13 +04:00
parent cadaac15e9
commit 0561e8e4cb
4 changed files with 181 additions and 8 deletions

View File

@ -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);

View File

@ -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<String> acceptedBanksForOffer = offer.getAcceptedBankIds();
Preconditions.checkNotNull(acceptedBanksForOffer, "offer.getAcceptedBankIds() must not be null");

View File

@ -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

View File

@ -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());
}
}