WorldpayCSE { public static Set<Integer> validate(WPCardData data) { return new WPCardValidator().validateCardData(data); } WorldpayCSE(); void setPublicKey(String publicKey); WPPublicKey getPublicKey(); void setPublicKey(WPPublicKey publicKey); static boolean isValidPublicKey(String stringKey); String encrypt(WPCardData cardData); static Set<Integer> validate(WPCardData data); static final String RSA_1_5; static final String A_256_GCM; static final String API_VERSION; static final String LIB_VERSION; static final String CHANNEL; }
@Test public void testValidateLongValues() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("123456789012345678901234"); cardData.setCvc("12333"); cardData.setExpiryMonth("1212"); cardData.setExpiryYear("197021"); cardData.setCardHolderName("1234567890123456789012345678901"); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_NUMBER)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CVC)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_MONTH)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_YEAR)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_HOLDER_NAME)); Assert.assertEquals(5, errors.size()); }
@Test public void testValidateLogicValues() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221110"); cardData.setCvc("123"); cardData.setExpiryMonth("12"); cardData.setExpiryYear("1970"); cardData.setCardHolderName("John Smith"); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_DATE)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_NUMBER_BY_LUHN)); Assert.assertEquals(2, errors.size()); }
@Test public void testValidateLogicValues2() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("8444333322221110"); cardData.setCvc("123"); cardData.setExpiryMonth("13"); cardData.setExpiryYear("2020"); cardData.setCardHolderName("John Smith"); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_MONTH_OUT_RANGE)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_NUMBER_BY_LUHN)); Assert.assertEquals(2, errors.size()); }
@Test public void testValidateNonDigitalValue() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444oooo22221111"); cardData.setCvc("1a3"); cardData.setExpiryMonth("a1"); cardData.setExpiryYear("19o1"); cardData.setCardHolderName("John Smith"); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_NUMBER)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CVC)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_MONTH)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_YEAR)); Assert.assertEquals(4, errors.size()); }
@Test public void testExpiryDatePreviousMonth() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221111"); cardData.setCvc("123"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1)); String year = Integer.toString(cal.get(Calendar.YEAR)); cardData.setExpiryMonth(month); cardData.setExpiryYear(year); cardData.setCardHolderName("John Smith"); Set<Integer> errors = WorldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_DATE)); Assert.assertEquals(1, errors.size()); }
@Test public void testExpiryDateThisMonth() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221111"); cardData.setCvc("123"); Calendar cal = Calendar.getInstance(); String currentMonth = String.format("%02d", (cal.get(Calendar.MONTH) + 1)); String currentYear = Integer.toString(cal.get(Calendar.YEAR)); cardData.setExpiryMonth(currentMonth); cardData.setExpiryYear(currentYear); cardData.setCardHolderName("John Smith"); Set<Integer> errors = WorldpayCSE.validate(cardData); Assert.assertEquals(0, errors.size()); }
@Test public void testExpiryDateNextMonth() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221111"); cardData.setCvc("123"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, 1); String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1)); String year = Integer.toString(cal.get(Calendar.YEAR)); cardData.setExpiryMonth(month); cardData.setExpiryYear(year); cardData.setCardHolderName("John Smith"); Set<Integer> errors = WorldpayCSE.validate(cardData); Assert.assertEquals(0, errors.size()); }
@Test public void testExpiryDatePreviousYear() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221111"); cardData.setCvc("123"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1); String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1)); String year = Integer.toString(cal.get(Calendar.YEAR)); cardData.setExpiryMonth(month); cardData.setExpiryYear(year); cardData.setCardHolderName("John Smith"); Set<Integer> errors = WorldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_DATE)); Assert.assertEquals(1, errors.size()); }
@Test public void testExpiryDateNextYear() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("4444333322221111"); cardData.setCvc("123"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 1); String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1)); String year = Integer.toString(cal.get(Calendar.YEAR)); cardData.setExpiryMonth(month); cardData.setExpiryYear(year); cardData.setCardHolderName("John Smith"); Set<Integer> errors = WorldpayCSE.validate(cardData); Assert.assertEquals(0, errors.size()); }
@Test public void testValidateOk() throws Exception { WPCardData cardData = getWPCardData(); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertEquals(0, errors.size()); }
@Test public void testValidateEmptyValues() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber(""); cardData.setCvc(""); cardData.setExpiryMonth(""); cardData.setExpiryYear(""); cardData.setCardHolderName(""); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.EMPTY_CARD_NUMBER)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.EMPTY_EXPIRY_MONTH)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.EMPTY_EXPIRY_YEAR)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.EMPTY_CARD_HOLDER_NAME)); Assert.assertEquals(4, errors.size()); }
@Test public void testValidateShortValues() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("1234123"); cardData.setCvc("1"); cardData.setExpiryMonth("2"); cardData.setExpiryYear("20"); cardData.setCardHolderName("J"); Set<Integer> errors = worldpayCSE.validate(cardData); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CARD_NUMBER)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_CVC)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_MONTH)); Assert.assertTrue(errors.contains(WPValidationErrorCodes.INVALID_EXPIRY_YEAR)); Assert.assertEquals(4, errors.size()); }