PlainTextMessageValidator extends DefaultMessageValidator { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, ValidationContext validationContext) throws ValidationException { if (controlMessage == null || controlMessage.getPayload() == null) { log.debug("Skip message payload validation as no control message was defined"); return; } log.debug("Start text message validation"); if (log.isDebugEnabled()) { log.debug("Received message:\n" + receivedMessage); log.debug("Control message:\n" + controlMessage); } try { String resultValue = normalizeWhitespace(receivedMessage.getPayload(String.class).trim()); String controlValue = normalizeWhitespace(context.replaceDynamicContentInString(controlMessage.getPayload(String.class).trim())); controlValue = processIgnoreStatements(controlValue, resultValue); controlValue = processVariableStatements(controlValue, resultValue, context); if (ValidationMatcherUtils.isValidationMatcherExpression(controlValue)) { ValidationMatcherUtils.resolveValidationMatcher("payload", resultValue, controlValue, context); return; } else { validateText(resultValue, controlValue); } } catch (IllegalArgumentException e) { throw new ValidationException("Failed to validate text content", e); } log.info("Text validation successful: All values OK"); }  @Override void validateMessage(Message receivedMessage, Message controlMessage,
                                TestContext context, ValidationContext validationContext); @Override boolean supportsMessageType(String messageType, Message message); boolean isIgnoreWhitespace(); void setIgnoreWhitespace(boolean ignoreWhitespace); boolean isIgnoreNewLineType(); void setIgnoreNewLineType(boolean ignoreNewLineType); static final String IGNORE_NEWLINE_TYPE_PROPERTY; static final String IGNORE_NEWLINE_TYPE_ENV; static final String IGNORE_WHITESPACE_PROPERTY; static final String IGNORE_WHITESPACE_ENV; }
@Test public void testPlainTextValidationWrongValue() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("Hello Citrus!"); try { validator.validateMessage(receivedMessage, controlMessage, context, validationContext); } catch (ValidationException e) { Assert.assertFalse(e.getMessage().contains("only whitespaces!")); Assert.assertTrue(e.getMessage().contains("expected 'Hello Citrus!'")); Assert.assertTrue(e.getMessage().contains("but was 'Hello World!'")); return; } Assert.fail("Missing validation exception due to wrong number of JSON entries"); }
@Test public void testPlainTextValidationLeadingTrailingWhitespace() { Message receivedMessage = new DefaultMessage(" Hello World! "); Message controlMessage = new DefaultMessage("Hello World!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testPlainTextValidationMultiline() { Message receivedMessage = new DefaultMessage("Hello\nWorld!\n"); Message controlMessage = new DefaultMessage("Hello\nWorld!\n"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testPlainTextValidation() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("Hello World!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testPlainTextValidationWithIgnore() { Message receivedMessage = new DefaultMessage(String.format("Hello World, time is %s!", System.currentTimeMillis())); Message controlMessage = new DefaultMessage("Hello World, time is @ignore@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); controlMessage = new DefaultMessage("Hello @ignore@, time is @ignore@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); controlMessage = new DefaultMessage("Hello @ignore@, time is @ignore@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); controlMessage = new DefaultMessage("Hello @ignore@, time is @ignore(100)@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); controlMessage = new DefaultMessage("@ignore(11)@, time is @ignore@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); controlMessage = new DefaultMessage("@ignore@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); receivedMessage = new DefaultMessage(UUID.randomUUID().toString()); controlMessage = new DefaultMessage("@ignore@-@ignore@-@ignore@-@ignore@-@ignore@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); receivedMessage = new DefaultMessage("1a2b3c4d_5e6f7g8h"); controlMessage = new DefaultMessage("1a@ignore(4)@4d_@ignore(6)@8h"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); receivedMessage = new DefaultMessage("Your id is 1a2b3c4d_5e6f7g8h"); controlMessage = new DefaultMessage("Your id is @ignore@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testPlainTextValidationCreateVariable() { Long time = System.currentTimeMillis(); Message receivedMessage = new DefaultMessage(String.format("Hello World, time is %s!", time)); Message controlMessage = new DefaultMessage("Hello World, time is @variable(time)@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); Assert.assertEquals(context.getVariable("time"), time.toString()); controlMessage = new DefaultMessage("Hello @variable('world')@, time is @variable(time)@!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); Assert.assertEquals(context.getVariable("world"), "World"); Assert.assertEquals(context.getVariable("time"), time.toString()); String id = UUID.randomUUID().toString(); receivedMessage = new DefaultMessage(id); controlMessage = new DefaultMessage("@variable('id')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); Assert.assertEquals(context.getVariable("id"), id); receivedMessage = new DefaultMessage("Today is 24.12.2017"); controlMessage = new DefaultMessage("Today is @variable('date')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); Assert.assertEquals(context.getVariable("date"), "24.12.2017"); receivedMessage = new DefaultMessage("Today is 2017-12-24"); controlMessage = new DefaultMessage("Today is @variable('date')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); Assert.assertEquals(context.getVariable("date"), "2017-12-24"); }
@Test public void testPlainTextValidationWithIgnoreFail() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("Hello @ignore@"); try { validator.validateMessage(receivedMessage, controlMessage, context, validationContext); } catch (ValidationException e) { Assert.assertFalse(e.getMessage().contains("only whitespaces!")); Assert.assertTrue(e.getMessage().contains("expected 'Hello World'")); Assert.assertTrue(e.getMessage().contains("but was 'Hello World!'")); return; } Assert.fail("Missing validation exception due to wrong number of JSON entries"); }
@Test public void testPlainTextValidationContains() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("@contains('World!')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test(expectedExceptions = ValidationException.class) public void testPlainTextValidationContainsError() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("@contains('Space!')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testPlainTextValidationVariableSupport() { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("Hello ${world}!"); context.setVariable("world", "World"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }