PaymentDetailsFormValidator implements Validator { @Override public void validate(final Object object, final Errors errors) { final PaymentDetailsForm form = (PaymentDetailsForm) object; if (isDeliveryAddressIsNull(form)) { errors.reject(GLOBAL_MISSING_DELIVERY_ADDRESS); } if (StringUtils.isBlank(form.getPaymentMethod())) { errors.reject(WORLDPAY_PAYMENT_METHOD_INVALID); } if (!errors.hasErrors() && isAPMPaymentMethodSelected(form.getPaymentMethod())) { final WorldpayAPMConfigurationModel apmConfiguration = apmConfigurationLookupService.getAPMConfigurationForCode(form.getPaymentMethod()); if (!apmAvailabilityService.isAvailable(apmConfiguration, cartService.getSessionCart())) { errors.rejectValue(FIELD_PAYMENT_METHOD, "worldpay.paymentMethod.notAvailable", "Payment method is not available"); } else if (apmConfiguration.getBank() && StringUtils.isBlank(form.getShopperBankCode())) { errors.reject(WORLDPAY_PAYMENT_METHOD_NO_SHOPPER_BANK_CODE, "Bank not selected for payment method"); } } validateTermsCheck(errors, form); if (FALSE.equals(form.getUseDeliveryAddress())) { validateField(errors, FIELD_BILLING_ADDRESS_FIRST_NAME, "address.firstName.invalid"); validateField(errors, FIELD_BILLING_ADDRESS_LAST_NAME, "address.lastName.invalid"); validateField(errors, FIELD_BILLING_ADDRESS_LINE1, "address.line1.invalid"); validateField(errors, FIELD_BILLING_ADDRESS_TOWN_CITY, "address.townCity.invalid"); validateField(errors, FIELD_BILLING_ADDRESS_POSTCODE, "address.postcode.invalid"); validateField(errors, FIELD_BILLING_ADDRESS_COUNTRY_ISO, "address.country.invalid"); } }  @Override boolean supports(final Class<?> aClass); @Override void validate(final Object object, final Errors errors);  }
@Test public void whenCheckTermsAreNotCheckedAddError() { when(paymentDetailsFormMock.isTermsCheck()).thenReturn(false); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock).reject(CHECKOUT_ERROR_TERMS_NOT_ACCEPTED); }
@Test public void validateChecksBillingAddressWhenShippingNotSameAsBilling() { when(paymentDetailsFormMock.getUseDeliveryAddress()).thenReturn(false); testObj.validate(paymentDetailsFormMock, errorsMock); verifyBillingAddressValidation(times(1)); }
@Test public void validateBillingAddressNotValidatedWhenShippingSameAsBilling() { testObj.validate(paymentDetailsFormMock, errorsMock); verifyBillingAddressValidation(never()); verify(checkoutCartMock).getDeliveryAddress(); }
@Test public void validateDoesNotCheckApmAvailabilityWhenPaymentMethodValidationFails() { when(errorsMock.hasErrors()).thenReturn(true); testObj.validate(paymentDetailsFormMock, errorsMock); verify(apmConfigurationLookupServiceMock, never()).getAPMConfigurationForCode(anyString()); verify(apmAvailabilityServiceMock, never()).isAvailable(any(WorldpayAPMConfigurationModel.class), any(CartModel.class)); verify(errorsMock, never()).rejectValue(eq(FIELD_PAYMENT_METHOD), anyString(), anyString()); }
@Test public void validateDoesNotCheckApmAvailabilityWhenPaymentMethodIsONLINE() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(ONLINE.getMethodCode()); testObj.validate(paymentDetailsFormMock, errorsMock); verify(apmConfigurationLookupServiceMock, never()).getAPMConfigurationForCode(anyString()); verify(apmAvailabilityServiceMock, never()).isAvailable(any(WorldpayAPMConfigurationModel.class), any(CartModel.class)); verify(errorsMock, never()).rejectValue(eq(FIELD_PAYMENT_METHOD), anyString(), anyString()); }
@Test public void validateChecksApmAvailabilityWhenPaymentMethodIsNotCreditCardAndNoErrorsOnPaymentMethodValidation() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(APM_PAYMENT_METHOD); when(apmConfigurationLookupServiceMock.getAPMConfigurationForCode(APM_PAYMENT_METHOD)).thenReturn(apmConfigurationMock); when(cartServiceMock.getSessionCart()).thenReturn(cartMock); testObj.validate(paymentDetailsFormMock, errorsMock); verify(apmConfigurationLookupServiceMock).getAPMConfigurationForCode(APM_PAYMENT_METHOD); verify(apmAvailabilityServiceMock).isAvailable(apmConfigurationMock, cartMock); verify(errorsMock).rejectValue(eq(FIELD_PAYMENT_METHOD), anyString(), anyString()); }
@Test public void validateRejectsFieldIfShopperBankCodeIsNotSelectedWhenIsBankApm() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(APM_PAYMENT_METHOD); when(cartServiceMock.getSessionCart()).thenReturn(cartMock); when(apmConfigurationLookupServiceMock.getAPMConfigurationForCode(APM_PAYMENT_METHOD)).thenReturn(apmConfigurationMock); when(apmAvailabilityServiceMock.isAvailable(apmConfigurationMock, cartMock)).thenReturn(true); when(apmConfigurationMock.getBank()).thenReturn(true); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock).reject(eq(WORLDPAY_PAYMENT_METHOD_NO_SHOPPER_BANK_CODE), anyString()); }
@Test public void validateDoesNotRaiseErrorWhenShopperBankSelectedForBankApm() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(APM_PAYMENT_METHOD); when(paymentDetailsFormMock.getShopperBankCode()).thenReturn(BANK_CODE); when(cartServiceMock.getSessionCart()).thenReturn(cartMock); when(apmConfigurationLookupServiceMock.getAPMConfigurationForCode(APM_PAYMENT_METHOD)).thenReturn(apmConfigurationMock); when(apmAvailabilityServiceMock.isAvailable(apmConfigurationMock, cartMock)).thenReturn(true); when(apmConfigurationMock.getBank()).thenReturn(true); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock, never()).rejectValue(eq(FIELD_SHOPPER_BANK_CODE), anyString(), anyString()); }
@Test public void validateAddErrorWhenUseBillingAsAddressAndCartDoesNotHaveDeliveryAddress() { when(checkoutFacadeMock.getCheckoutCart().getDeliveryAddress()).thenReturn(null); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock).reject(GLOBAL_MISSING_DELIVERY_ADDRESS); }
@Test public void validateAddGlobalErrorWhenNoPaymentMethodIsSelectedIsEmpty() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(""); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock).reject(WORLDPAY_PAYMENT_METHOD_INVALID); }
@Test public void validateAddGlobalErrorWhenNoPaymentMethodIsSelectedAndValueIsNull() { when(paymentDetailsFormMock.getPaymentMethod()).thenReturn(null); testObj.validate(paymentDetailsFormMock, errorsMock); verify(errorsMock).reject(WORLDPAY_PAYMENT_METHOD_INVALID); }