ReceiveMessageActionParser extends AbstractMessageActionParser { protected List<VariableExtractor> getVariableExtractors(Element element) { List<VariableExtractor> variableExtractors = new ArrayList<>(); parseExtractHeaderElements(element, variableExtractors); Element extractElement = DomUtils.getChildElementByTagName(element, "extract"); if (extractElement != null) { Map<String, String> extractFromPath = new HashMap<>(); List<Element> messageValueElements = DomUtils.getChildElementsByTagName(extractElement, "message"); messageValueElements.addAll(DomUtils.getChildElementsByTagName(extractElement, "body")); VariableExtractorParserUtil.parseMessageElement(messageValueElements, extractFromPath); if (!CollectionUtils.isEmpty(extractFromPath)) { VariableExtractorParserUtil.addPayloadVariableExtractors(element, variableExtractors, extractFromPath); } } return variableExtractors; }  @Override BeanDefinition parse(Element element, ParserContext parserContext);  }
@Test public void testReceiveMessageActionParser() { assertActionCount(18); assertActionClassAndName(ReceiveMessageAction.class, "receive"); PayloadTemplateMessageBuilder messageBuilder; GroovyScriptMessageBuilder groovyMessageBuilder; ReceiveMessageAction action = getNextTestActionFromTest(); Assert.assertTrue(action.getMessageSelectorMap().isEmpty()); Assert.assertNull(action.getMessageSelector()); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder(); Assert.assertNull(messageBuilder.getPayloadResourcePath()); Assert.assertNotNull(messageBuilder.getPayloadData()); Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<TestMessage>Hello Citrus</TestMessage>"); Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 1); Assert.assertEquals(messageBuilder.getMessageHeaders().get("operation"), "Test"); Assert.assertEquals(action.getMessageProcessors().size(), 0); Assert.assertNull(action.getDataDictionary()); Assert.assertEquals(action.getMessageProcessors().size(), 0); action = getNextTestActionFromTest(); Assert.assertTrue(action.getMessageSelectorMap().isEmpty()); Assert.assertNull(action.getMessageSelector()); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder(); Assert.assertNull(messageBuilder.getPayloadResourcePath()); Assert.assertNotNull(messageBuilder.getPayloadData()); Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?><TestMessage xmlns=\"http: Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 1); Assert.assertEquals(messageBuilder.getMessageHeaders().get("operation"), "Test"); Assert.assertEquals(messageBuilder.getHeaderData().size(), 1); Assert.assertEquals(messageBuilder.getHeaderData().get(0).trim(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Header xmlns=\"http: Assert.assertEquals(action.getMessageProcessors().size(), 0); Assert.assertNull(action.getDataDictionary()); Assert.assertEquals(action.getMessageProcessors().size(), 0); action = getNextTestActionFromTest(); Assert.assertEquals(action.getMessageSelectorMap().size(), 1); Assert.assertEquals(action.getMessageSelectorMap().get("operation"), "Test"); Assert.assertNull(action.getMessageSelector()); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder(); Assert.assertNotNull(messageBuilder.getPayloadResourcePath()); Assert.assertEquals(messageBuilder.getPayloadResourcePath(), "classpath:com/consol/citrus/actions/test-request-payload.xml"); Assert.assertNull(messageBuilder.getPayloadData()); Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 0); Assert.assertEquals(action.getMessageProcessors().size(), 0); action = getNextTestActionFromTest(); Assert.assertTrue(action.getMessageSelectorMap().isEmpty()); Assert.assertEquals(action.getMessageSelector(), "operation = 'Test'"); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); action = getNextTestActionFromTest(); Assert.assertTrue(action.getMessageSelectorMap().isEmpty()); Assert.assertNull(action.getMessageSelector()); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getMessageBuilder() instanceof GroovyScriptMessageBuilder); groovyMessageBuilder = (GroovyScriptMessageBuilder)action.getMessageBuilder(); Assert.assertNull(groovyMessageBuilder.getScriptResourcePath()); Assert.assertNotNull(groovyMessageBuilder.getScriptData()); Assert.assertEquals(groovyMessageBuilder.getScriptData().trim(), "println '<TestMessage>Hello Citrus</TestMessage>'"); Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().size(), 2); Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().get("header1"), "Test"); Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().get("header2"), "Test"); action = getNextTestActionFromTest(); Assert.assertTrue(action.getMessageSelectorMap().isEmpty()); Assert.assertNull(action.getMessageSelector()); Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class)); Assert.assertNull(action.getEndpointUri()); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getMessageBuilder() instanceof GroovyScriptMessageBuilder); groovyMessageBuilder = (GroovyScriptMessageBuilder)action.getMessageBuilder(); Assert.assertNotNull(groovyMessageBuilder.getScriptResourcePath()); Assert.assertEquals(groovyMessageBuilder.getScriptResourcePath(), "classpath:com/consol/citrus/script/example.groovy"); Assert.assertNull(groovyMessageBuilder.getScriptData()); action = getNextTestActionFromTest(); Assert.assertEquals(action.getVariableExtractors().size(), 2); Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor); MessageHeaderVariableExtractor headerVariableExtractor = (MessageHeaderVariableExtractor)action.getVariableExtractors().get(0); Assert.assertTrue(action.getVariableExtractors().get(1) instanceof DelegatingPayloadVariableExtractor); DelegatingPayloadVariableExtractor variableExtractor = (DelegatingPayloadVariableExtractor)action.getVariableExtractors().get(1); Assert.assertEquals(variableExtractor.getNamespaces().size(), 0L); Assert.assertEquals(headerVariableExtractor.getHeaderMappings().size(), 1); Assert.assertEquals(headerVariableExtractor.getHeaderMappings().get("operation"), "operation"); Assert.assertEquals(variableExtractor.getPathExpressions().size(), 1); Assert.assertEquals(variableExtractor.getPathExpressions().get("/TestMessage/text()"), "text"); Assert.assertNotNull(action.getDataDictionary()); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); XmlMessageValidationContext xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder(); Assert.assertNull(messageBuilder.getPayloadResourcePath()); Assert.assertNotNull(messageBuilder.getPayloadData()); Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<ns:TestMessage xmlns:ns=\"http: Assert.assertEquals(action.getMessageProcessors().size(), 1); Assert.assertTrue(action.getMessageProcessors().get(0) instanceof DelegatingPathExpressionProcessor); DelegatingPathExpressionProcessor messageProcessor = (DelegatingPathExpressionProcessor)action.getMessageProcessors().get(0); Assert.assertEquals(messageProcessor.getPathExpressions().size(), 1); Assert.assertEquals(messageProcessor.getPathExpressions().get("/ns:TestMessage/"), "newValue"); Assert.assertFalse(xmlValidationContext.isSchemaValidationEnabled()); Assert.assertEquals(xmlValidationContext.getIgnoreExpressions().size(), 1); Assert.assertEquals(xmlValidationContext.getIgnoreExpressions().iterator().next(), "/ns:TestMessage/ns:ignore"); Assert.assertEquals(xmlValidationContext.getNamespaces().size(), 1); Assert.assertEquals(xmlValidationContext.getNamespaces().get("ns"), "http: action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 4); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext); xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1); XpathMessageValidationContext xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(xmlValidationContext.isSchemaValidationEnabled()); Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 2); Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("/TestMessage/text"), "Hello Citrus"); Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 4); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext); xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1); xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(xmlValidationContext.isSchemaValidationEnabled()); Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 2); Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("/TestMessage/text"), "Hello Citrus"); Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 5); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(4) instanceof ScriptValidationContext); xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1); xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2); ScriptValidationContext scriptValidationContext = (ScriptValidationContext)action.getValidationContexts().get(4); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(xmlValidationContext.isSchemaValidationEnabled()); Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 1); Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true"); Assert.assertEquals(scriptValidationContext.getScriptType(), "groovy"); Assert.assertEquals(scriptValidationContext.getValidationScript().trim(), "assert true"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 4); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof ScriptValidationContext); xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1); scriptValidationContext = (ScriptValidationContext)action.getValidationContexts().get(3); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(xmlValidationContext.isSchemaValidationEnabled()); Assert.assertEquals(scriptValidationContext.getScriptType(), "groovy"); Assert.assertEquals(scriptValidationContext.getValidationScriptResourcePath(), "classpath:com/consol/citrus/actions/test-validation-script.groovy"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 4); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonPathMessageValidationContext); JsonPathMessageValidationContext jsonPathValidationContext = (JsonPathMessageValidationContext)action.getValidationContexts().get(3); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().size(), 2); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$.json.text"), "Hello Citrus"); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$..foo.bar"), "true"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 4); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonPathMessageValidationContext); jsonPathValidationContext = (JsonPathMessageValidationContext)action.getValidationContexts().get(3); Assert.assertNull(action.getEndpoint()); Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint"); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().size(), 2); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$.json.text"), "Hello Citrus"); Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$..foo.bar"), "true"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidationContexts().size(), 3); Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext); Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext); Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext); JsonMessageValidationContext jsonValidationContext = (JsonMessageValidationContext)action.getValidationContexts().get(2); Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder); messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder(); Assert.assertNull(messageBuilder.getPayloadResourcePath()); Assert.assertNotNull(messageBuilder.getPayloadData()); Assert.assertEquals(messageBuilder.getPayloadData().trim(), "{ \"FooMessage\": { \"foo\": \"Hello World!\" }, { \"bar\": \"@ignore@\" }}"); Assert.assertEquals(action.getMessageProcessors().size(), 1); Assert.assertTrue(action.getMessageProcessors().get(0) instanceof DelegatingPathExpressionProcessor); DelegatingPathExpressionProcessor jsonMessageProcessor = (DelegatingPathExpressionProcessor)action.getMessageProcessors().get(0); Assert.assertEquals(jsonMessageProcessor.getPathExpressions().size(), 1); Assert.assertEquals(jsonMessageProcessor.getPathExpressions().get("$.FooMessage.foo"), "newValue"); Assert.assertEquals(jsonValidationContext.getIgnoreExpressions().size(), 1); Assert.assertEquals(jsonValidationContext.getIgnoreExpressions().iterator().next(), "$.FooMessage.bar"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getVariableExtractors().size(), 2); Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor); headerVariableExtractor = (MessageHeaderVariableExtractor)action.getVariableExtractors().get(0); Assert.assertTrue(action.getVariableExtractors().get(1) instanceof DelegatingPayloadVariableExtractor); DelegatingPayloadVariableExtractor jsonVariableExtractor = (DelegatingPayloadVariableExtractor) action.getVariableExtractors().get(1); Assert.assertEquals(headerVariableExtractor.getHeaderMappings().size(), 1); Assert.assertEquals(headerVariableExtractor.getHeaderMappings().get("operation"), "operation"); Assert.assertEquals(jsonVariableExtractor.getPathExpressions().size(), 1); Assert.assertEquals(jsonVariableExtractor.getPathExpressions().get("$.message.text"), "text"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidators().size(), 1); Assert.assertEquals(action.getValidators().get(0), beanDefinitionContext.getBean("myValidator", MessageValidator.class)); HeaderValidationContext headerValidationContext = (HeaderValidationContext) action.getValidationContexts().get(0); Assert.assertEquals(headerValidationContext.getValidatorNames().size(), 1); Assert.assertEquals(headerValidationContext.getValidatorNames().get(0), "myHeaderValidator"); action = getNextTestActionFromTest(); Assert.assertEquals(action.getValidators().size(), 2); Assert.assertEquals(action.getValidators().get(0), beanDefinitionContext.getBean("myValidator", MessageValidator.class)); Assert.assertEquals(action.getValidators().get(1), beanDefinitionContext.getBean("defaultMessageValidator", MessageValidator.class)); headerValidationContext = (HeaderValidationContext) action.getValidationContexts().get(0); Assert.assertEquals(headerValidationContext.getValidatorNames().size(), 2); Assert.assertEquals(headerValidationContext.getValidatorNames().get(0), "myHeaderValidator"); Assert.assertEquals(headerValidationContext.getValidatorNames().get(1), "defaultHeaderValidator"); }