PaymentDetailsDTOValidator implements Validator { @Override public void validate(final Object target, final Errors errors) { final PaymentDetailsWsDTO paymentDetails = (PaymentDetailsWsDTO) target; if (StringUtils.isNotBlank(paymentDetails.getStartMonth()) && StringUtils.isNotBlank(paymentDetails.getStartYear()) && StringUtils.isNotBlank(paymentDetails.getExpiryMonth()) && StringUtils.isNotBlank(paymentDetails.getExpiryYear())) { final Calendar start = Calendar.getInstance(); start.set(Calendar.DAY_OF_MONTH, 0); start.set(Calendar.MONTH, Integer.parseInt(paymentDetails.getStartMonth()) - 1); start.set(Calendar.YEAR, Integer.parseInt(paymentDetails.getStartYear()) - 1); final Calendar expiration = Calendar.getInstance(); expiration.set(Calendar.DAY_OF_MONTH, 0); expiration.set(Calendar.MONTH, Integer.parseInt(paymentDetails.getExpiryMonth()) - 1); expiration.set(Calendar.YEAR, Integer.parseInt(paymentDetails.getExpiryYear()) - 1); if (start.after(expiration)) { errors.rejectValue("startMonth", "payment.startDate.invalid"); } } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "accountHolderName", FIELD_REQUIRED_MESSAGE_ID); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cardType.code", FIELD_REQUIRED_MESSAGE_ID); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cseToken", FIELD_REQUIRED_MESSAGE_ID); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "expiryMonth", FIELD_REQUIRED_MESSAGE_ID); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "expiryYear", FIELD_REQUIRED_MESSAGE_ID); paymentAddressValidator.validate(paymentDetails, errors); }  @SuppressWarnings("squid:S3740") @Override boolean supports(final Class clazz); @Override void validate(final Object target, final Errors errors); @Required void setPaymentAddressValidator(final Validator paymentAddressValidator);  }
@Test public void shouldNotFailOnValid() { updatePaymentDetails("Bo Nielsen", createCardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", "01", "2017", "01", "2019"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("No errors expected", 0, errors.getErrorCount()); }
@Test public void shouldFailOnMissingExpiryYear() { updatePaymentDetails("Bo Nielsen", createCardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", null, null, "01", null); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain expiryYear", codes[0].contains("expiryYear")); }
@Test public void shouldFailOnMissingExpiryMonth() { updatePaymentDetails("Bo Nielsen", createCardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", null, null, null, "2016"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain expiryMonth", codes[0].contains("expiryMonth")); }
@Test public void shouldFailOnMissingCseToken() { updatePaymentDetails("Bo Nielsen", createCardTypeWsDTO(), null, null, null, "01", "2016"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain cseToken", codes[0].contains("cseToken")); }
@Test public void shouldFailOnMissingCardTypeCode() { updatePaymentDetails("Bo Nielsen", new CardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", null, null, "01", "2016"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain cardType.code", codes[0].contains("cardType.code")); }
@Test public void shouldFailOnMissingAccountHolderName() { updatePaymentDetails(null, createCardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", null, null, "01", "2016"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain accountHolderName", codes[0].contains("accountHolderName")); }
@Test public void shouldFailOnStartDateAfterExpiration() { updatePaymentDetails("Bo Nielsen", createCardTypeWsDTO(), "ksdjhfskdfhsdkhfsdkfhsdkf", "04", "2016", "01", "2016"); testObj.validate(paymentDetailsWsDTO, errors); Assert.assertEquals("Only errors is expected", 1, errors.getErrorCount()); String[] codes = errors.getAllErrors().get(0).getCodes(); Assert.assertTrue("The first code is expected to contain startMonth", codes[0].contains("startMonth")); }