GroovyScriptMessageValidator extends AbstractMessageValidator<ScriptValidationContext> { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, ScriptValidationContext validationContext) throws ValidationException { try { String validationScript = validationContext.getValidationScript(context); if (StringUtils.hasText(validationScript)) { log.debug("Start groovy message validation ..."); GroovyClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() { public GroovyClassLoader run() { return new GroovyClassLoader(GroovyScriptMessageValidator.class.getClassLoader()); } }); Class<?> groovyClass = loader.parseClass(TemplateBasedScriptBuilder.fromTemplateResource(scriptTemplateResource) .withCode(validationScript) .build()); if (groovyClass == null) { throw new CitrusRuntimeException("Failed to load groovy validation script resource"); } GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); ((GroovyScriptExecutor) groovyObject).validate(receivedMessage, context); log.info("Groovy message validation successful: All values OK"); } } catch (CompilationFailedException | InstantiationException | IllegalAccessException e) { throw new CitrusRuntimeException(e); } catch (AssertionError e) { throw new ValidationException("Groovy script validation failed with assertion error:\n" + e.getMessage(), e); } } GroovyScriptMessageValidator();  GroovyScriptMessageValidator(ClassPathResource scriptTemplateResource); @Override void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, ScriptValidationContext validationContext); @Override ScriptValidationContext findValidationContext(List<ValidationContext> validationContexts); @Override boolean supportsMessageType(String messageType, Message message);  }
@Test public void testGroovyScriptValidation() throws ValidationException { String validationScript = "assert headers.operation == 'unitTesting'\n" + "assert payload == 'This is plain text!'\n" + "assert payload.contains('!')"; ScriptValidationContext validationContext = new ScriptValidationContext.Builder() .scriptType(ScriptTypes.GROOVY) .script(validationScript) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testGroovyScriptValidationVariableSupport() throws ValidationException { String validationScript = "assert headers.operation == 'unitTesting'\n" + "assert payload == '${plainText}'\n" + "assert payload.contains('!')"; context.setVariable("plainText", "This is plain text!"); ScriptValidationContext validationContext = new ScriptValidationContext.Builder() .scriptType(ScriptTypes.GROOVY) .script(validationScript) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testGroovyScriptValidationWrongValue() throws ValidationException { String validationScript = "assert headers.operation == 'somethingElse'\n" + "assert payload == 'This is plain text!'\n" + "assert payload.contains('!')"; ScriptValidationContext validationContext = new ScriptValidationContext.Builder() .scriptType(ScriptTypes.GROOVY) .script(validationScript) .build(); try { validator.validateMessage(message, new DefaultMessage(), context, validationContext); } catch (ValidationException e) { Assert.assertTrue(e.getCause() instanceof AssertionError); return; } Assert.fail("Missing validation exception due to wrong value"); }
@Test public void testTestContextSupport() throws ValidationException { String validationScript = "context.setVariable('operation', 'unitTesting')\n" + "context.setVariable('text', 'This is plain text!')"; ScriptValidationContext validationContext = new ScriptValidationContext.Builder() .scriptType(ScriptTypes.GROOVY) .script(validationScript) .build(); Assert.assertNull(context.getVariables().get("operation")); Assert.assertNull(context.getVariables().get("text")); validator.validateMessage(message, new DefaultMessage(), context, validationContext); Assert.assertNotNull(context.getVariables().get("operation")); Assert.assertNotNull(context.getVariables().get("text")); Assert.assertEquals(context.getVariable("operation"), "unitTesting"); Assert.assertEquals(context.getVariable("text"), "This is plain text!"); }