WeekdayValidationMatcher implements ValidationMatcher, ControlExpressionParser { @Override public void validate(String fieldName, String value, List<String> controlParameters, TestContext context) throws ValidationException { SimpleDateFormat dateFormat; String weekday = controlParameters.get(0); String formatString = "dd.MM.yyyy"; if (controlParameters.size() == 2) { formatString = controlParameters.get(1); } try { dateFormat = new SimpleDateFormat(formatString); } catch (PatternSyntaxException e) { throw new ValidationException(this.getClass().getSimpleName() + " failed for field '" + fieldName + "' " + ". Found invalid date format", e); } try { Calendar cal = Calendar.getInstance(); cal.setTime(dateFormat.parse(value)); if (cal.get(Calendar.DAY_OF_WEEK) == Weekday.valueOf(weekday).getConstantValue()) { LOG.info("Weekday validation matcher successful - All values OK"); } else { throw new ValidationException(this.getClass().getSimpleName() + " failed for field '" + fieldName + "'" + ". Received invalid week day '" + value + "', expected date to be a '" + weekday + "'"); } } catch (ParseException e) { throw new ValidationException(this.getClass().getSimpleName() + " failed for field '" + fieldName + "'" + ". Received invalid date format for value '" + value + "', expected date format is '" + formatString + "'", e); } }  @Override void validate(String fieldName, String value, List<String> controlParameters, TestContext context); @Override List<String> extractControlValues(String controlExpression, Character delimiter);  }
@Test public void testValidationMatcher() { matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("MONDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.TUESDAY).getTime()), Arrays.asList("TUESDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.WEDNESDAY).getTime()), Arrays.asList("WEDNESDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.THURSDAY).getTime()), Arrays.asList("THURSDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.FRIDAY).getTime()), Arrays.asList("FRIDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.SATURDAY).getTime()), Arrays.asList("SATURDAY"), context); matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.SUNDAY).getTime()), Arrays.asList("SUNDAY"), context); try { matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("SUNDAY"), context); Assert.fail("Missing validation matcher failed exception"); } catch (CitrusRuntimeException e) { Assert.assertTrue(e.getMessage().endsWith("expected date to be a 'SUNDAY'")); } }
@Test public void testCustomDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("MONDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.TUESDAY).getTime()), Arrays.asList("TUESDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.WEDNESDAY).getTime()), Arrays.asList("WEDNESDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.THURSDAY).getTime()), Arrays.asList("THURSDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.FRIDAY).getTime()), Arrays.asList("FRIDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.SATURDAY).getTime()), Arrays.asList("SATURDAY", "yyyy-MM-dd"), context); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.SUNDAY).getTime()), Arrays.asList("SUNDAY", "yyyy-MM-dd"), context); try { matcher.validate("fieldName", dateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("SUNDAY", "yyyy-MM-dd"), context); Assert.fail("Missing validation matcher failed exception"); } catch (CitrusRuntimeException e) { Assert.assertTrue(e.getMessage().endsWith("expected date to be a 'SUNDAY'")); } }
@Test(expectedExceptions = {CitrusRuntimeException.class}) public void testInvalidDefaultDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); matcher.validate("fieldName", dateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("MONDAY"), context); }
@Test(expectedExceptions = {CitrusRuntimeException.class}) public void testInvalidCustomDateFormat() { matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("MONDAY", "dd-MM-yyyy"), context); }
@Test(expectedExceptions = {IllegalArgumentException.class}) public void testInvalidDateFormatSyntax() { matcher.validate("fieldName", defaultDateFormat.format(getNext(Calendar.MONDAY).getTime()), Arrays.asList("MONDAY","ABC"), context); }