Best Citrus code snippet using com.consol.citrus.validation.context.HeaderValidationContext
Source:ReceiveMessageActionParserTest.java
...18import com.consol.citrus.endpoint.Endpoint;19import com.consol.citrus.testng.AbstractActionParserTest;20import com.consol.citrus.validation.*;21import com.consol.citrus.validation.builder.PayloadTemplateMessageBuilder;22import com.consol.citrus.validation.context.HeaderValidationContext;23import com.consol.citrus.validation.json.*;24import com.consol.citrus.validation.script.GroovyScriptMessageBuilder;25import com.consol.citrus.validation.script.ScriptValidationContext;26import com.consol.citrus.validation.xml.*;27import com.consol.citrus.variable.MessageHeaderVariableExtractor;28import org.testng.Assert;29import org.testng.annotations.Test;30/**31 * @author Christoph Deppisch32 */33public class ReceiveMessageActionParserTest extends AbstractActionParserTest<ReceiveMessageAction> {34 @Test35 public void testReceiveMessageActionParser() {36 assertActionCount(18);37 assertActionClassAndName(ReceiveMessageAction.class, "receive");38 39 PayloadTemplateMessageBuilder messageBuilder;40 GroovyScriptMessageBuilder groovyMessageBuilder;41 42 // 1st action43 ReceiveMessageAction action = getNextTestActionFromTest();44 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());45 Assert.assertNull(action.getMessageSelector());46 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));47 Assert.assertNull(action.getEndpointUri());48 49 Assert.assertEquals(action.getValidationContexts().size(), 3);50 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);51 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);52 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);53 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);54 messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder();55 56 Assert.assertNull(messageBuilder.getPayloadResourcePath());57 Assert.assertNotNull(messageBuilder.getPayloadData());58 Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<TestMessage>Hello Citrus</TestMessage>");59 Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 1);60 Assert.assertEquals(messageBuilder.getMessageHeaders().get("operation"), "Test");61 Assert.assertEquals(messageBuilder.getMessageInterceptors().size(), 0);62 Assert.assertNull(action.getDataDictionary());63 Assert.assertEquals(action.getVariableExtractors().size(), 0);64 // 2nd action65 action = getNextTestActionFromTest();66 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());67 Assert.assertNull(action.getMessageSelector());68 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));69 Assert.assertNull(action.getEndpointUri());70 Assert.assertEquals(action.getValidationContexts().size(), 3);71 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);72 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);73 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);74 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);75 messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder();76 Assert.assertNull(messageBuilder.getPayloadResourcePath());77 Assert.assertNotNull(messageBuilder.getPayloadData());78 Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?><TestMessage xmlns=\"http://citrusframework.org/test\">Hello Citrus</TestMessage>");79 Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 1);80 Assert.assertEquals(messageBuilder.getMessageHeaders().get("operation"), "Test");81 Assert.assertEquals(messageBuilder.getHeaderData().size(), 1);82 Assert.assertEquals(messageBuilder.getHeaderData().get(0).trim(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Header xmlns=\"http://citrusframework.org/test\">\n <operation>hello</operation>\n</Header>");83 Assert.assertEquals(messageBuilder.getMessageInterceptors().size(), 0);84 Assert.assertNull(action.getDataDictionary());85 Assert.assertEquals(action.getVariableExtractors().size(), 0);86 // 3rd action87 action = getNextTestActionFromTest();88 Assert.assertEquals(action.getMessageSelectorMap().size(), 1);89 Assert.assertEquals(action.getMessageSelectorMap().get("operation"), "Test");90 Assert.assertNull(action.getMessageSelector());91 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));92 Assert.assertNull(action.getEndpointUri());93 94 Assert.assertEquals(action.getValidationContexts().size(), 3);95 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);96 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);97 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);98 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);99 messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder();100 101 Assert.assertNotNull(messageBuilder.getPayloadResourcePath());102 Assert.assertEquals(messageBuilder.getPayloadResourcePath(), "classpath:com/consol/citrus/actions/test-request-payload.xml");103 Assert.assertNull(messageBuilder.getPayloadData());104 Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 0);105 Assert.assertEquals(messageBuilder.getMessageInterceptors().size(), 0);106 107 // 4th action108 action = getNextTestActionFromTest();109 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());110 Assert.assertEquals(action.getMessageSelector(), "operation = 'Test'");111 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));112 Assert.assertNull(action.getEndpointUri());113 114 // 5th action115 action = getNextTestActionFromTest();116 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());117 Assert.assertNull(action.getMessageSelector());118 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));119 Assert.assertNull(action.getEndpointUri());120 121 Assert.assertEquals(action.getValidationContexts().size(), 3);122 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);123 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);124 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);125 Assert.assertTrue(action.getMessageBuilder() instanceof GroovyScriptMessageBuilder);126 groovyMessageBuilder = (GroovyScriptMessageBuilder)action.getMessageBuilder();127 128 Assert.assertNull(groovyMessageBuilder.getScriptResourcePath());129 Assert.assertNotNull(groovyMessageBuilder.getScriptData());130 Assert.assertEquals(groovyMessageBuilder.getScriptData().trim(), "println '<TestMessage>Hello Citrus</TestMessage>'");131 Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().size(), 2);132 Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().get("header1"), "Test");133 Assert.assertEquals(groovyMessageBuilder.getMessageHeaders().get("header2"), "Test");134 135 // 6th action136 action = getNextTestActionFromTest();137 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());138 Assert.assertNull(action.getMessageSelector());139 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("myMessageEndpoint", Endpoint.class));140 Assert.assertNull(action.getEndpointUri());141 142 Assert.assertEquals(action.getValidationContexts().size(), 3);143 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);144 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);145 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);146 Assert.assertTrue(action.getMessageBuilder() instanceof GroovyScriptMessageBuilder);147 groovyMessageBuilder = (GroovyScriptMessageBuilder)action.getMessageBuilder();148 149 Assert.assertNotNull(groovyMessageBuilder.getScriptResourcePath());150 Assert.assertEquals(groovyMessageBuilder.getScriptResourcePath(), "classpath:com/consol/citrus/script/example.groovy");151 Assert.assertNull(groovyMessageBuilder.getScriptData());152 // 7th action153 action = getNextTestActionFromTest();154 Assert.assertEquals(action.getVariableExtractors().size(), 2);155 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor);156 MessageHeaderVariableExtractor headerVariableExtractor = (MessageHeaderVariableExtractor)action.getVariableExtractors().get(0);157 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof DefaultPayloadVariableExtractor);158 DefaultPayloadVariableExtractor variableExtractor = (DefaultPayloadVariableExtractor)action.getVariableExtractors().get(1);159 160 Assert.assertEquals(variableExtractor.getNamespaces().size(), 0L);161 Assert.assertEquals(headerVariableExtractor.getHeaderMappings().size(), 1);162 Assert.assertEquals(headerVariableExtractor.getHeaderMappings().get("operation"), "operation");163 Assert.assertEquals(variableExtractor.getPathExpressions().size(), 1);164 Assert.assertEquals(variableExtractor.getPathExpressions().get("/TestMessage/text()"), "text");165 Assert.assertNotNull(action.getDataDictionary());166 // 8th action167 action = getNextTestActionFromTest();168 Assert.assertEquals(action.getValidationContexts().size(), 3);169 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);170 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);171 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);172 XmlMessageValidationContext xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1);173 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);174 messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder();175 176 Assert.assertNull(messageBuilder.getPayloadResourcePath());177 Assert.assertNotNull(messageBuilder.getPayloadData());178 Assert.assertEquals(messageBuilder.getPayloadData().trim(), "<ns:TestMessage xmlns:ns=\"http://www.consol.com\">Hello Citrus</ns:TestMessage>");179 180 Assert.assertEquals(messageBuilder.getMessageInterceptors().size(), 1);181 Assert.assertTrue(messageBuilder.getMessageInterceptors().get(0) instanceof XpathMessageConstructionInterceptor);182 XpathMessageConstructionInterceptor messageConstructionInterceptor = (XpathMessageConstructionInterceptor)messageBuilder.getMessageInterceptors().get(0);183 184 Assert.assertEquals(messageConstructionInterceptor.getXPathExpressions().size(), 1);185 Assert.assertEquals(messageConstructionInterceptor.getXPathExpressions().get("/ns:TestMessage/"), "newValue");186 187 Assert.assertEquals(xmlValidationContext.isSchemaValidationEnabled(), false);188 189 Assert.assertEquals(xmlValidationContext.getIgnoreExpressions().size(), 1);190 Assert.assertEquals(xmlValidationContext.getIgnoreExpressions().iterator().next(), "/ns:TestMessage/ns:ignore");191 Assert.assertEquals(xmlValidationContext.getNamespaces().size(), 1);192 Assert.assertEquals(xmlValidationContext.getNamespaces().get("ns"), "http://www.consol.com");193 194 // 9th action195 action = getNextTestActionFromTest();196 Assert.assertEquals(action.getValidationContexts().size(), 4);197 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);198 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);199 Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext);200 Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext);201 xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1);202 XpathMessageValidationContext xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2);203 Assert.assertNull(action.getEndpoint());204 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");205 Assert.assertEquals(xmlValidationContext.isSchemaValidationEnabled(), true);206 Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 2);207 Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("/TestMessage/text"), "Hello Citrus");208 Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true");209 // 10th action210 action = getNextTestActionFromTest();211 Assert.assertEquals(action.getValidationContexts().size(), 4);212 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);213 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);214 Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext);215 Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext);216 xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1);217 xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2);218 Assert.assertNull(action.getEndpoint());219 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");220 Assert.assertEquals(xmlValidationContext.isSchemaValidationEnabled(), true);221 Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 2);222 Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("/TestMessage/text"), "Hello Citrus");223 Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true");224 // 11th action225 action = getNextTestActionFromTest();226 Assert.assertEquals(action.getValidationContexts().size(), 5);227 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);228 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);229 Assert.assertTrue(action.getValidationContexts().get(2) instanceof XpathMessageValidationContext);230 Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonMessageValidationContext);231 Assert.assertTrue(action.getValidationContexts().get(4) instanceof ScriptValidationContext);232 xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1);233 xPathValidationContext = (XpathMessageValidationContext)action.getValidationContexts().get(2);234 ScriptValidationContext scriptValidationContext = (ScriptValidationContext)action.getValidationContexts().get(4);235 Assert.assertNull(action.getEndpoint());236 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");237 Assert.assertEquals(xmlValidationContext.isSchemaValidationEnabled(), true);238 Assert.assertEquals(xPathValidationContext.getXpathExpressions().size(), 1);239 Assert.assertEquals(xPathValidationContext.getXpathExpressions().get("boolean:/TestMessage/foo"), "true");240 Assert.assertEquals(scriptValidationContext.getScriptType(), "groovy");241 Assert.assertEquals(scriptValidationContext.getValidationScript().trim(), "assert true");242 // 12th action243 action = getNextTestActionFromTest();244 Assert.assertEquals(action.getValidationContexts().size(), 4);245 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);246 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);247 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);248 Assert.assertTrue(action.getValidationContexts().get(3) instanceof ScriptValidationContext);249 xmlValidationContext = (XmlMessageValidationContext)action.getValidationContexts().get(1);250 scriptValidationContext = (ScriptValidationContext)action.getValidationContexts().get(3);251 Assert.assertNull(action.getEndpoint());252 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");253 Assert.assertEquals(xmlValidationContext.isSchemaValidationEnabled(), true);254 Assert.assertEquals(scriptValidationContext.getScriptType(), "groovy");255 Assert.assertEquals(scriptValidationContext.getValidationScriptResourcePath(), "classpath:com/consol/citrus/actions/test-validation-script.groovy");256 // 13th action257 action = getNextTestActionFromTest();258 Assert.assertEquals(action.getValidationContexts().size(), 4);259 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);260 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);261 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);262 Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonPathMessageValidationContext);263 JsonPathMessageValidationContext jsonPathValidationContext = (JsonPathMessageValidationContext)action.getValidationContexts().get(3);264 Assert.assertNull(action.getEndpoint());265 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");266 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);267 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().size(), 2);268 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$.json.text"), "Hello Citrus");269 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$..foo.bar"), "true");270 // 14th action271 action = getNextTestActionFromTest();272 Assert.assertEquals(action.getValidationContexts().size(), 4);273 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);274 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);275 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);276 Assert.assertTrue(action.getValidationContexts().get(3) instanceof JsonPathMessageValidationContext);277 jsonPathValidationContext = (JsonPathMessageValidationContext)action.getValidationContexts().get(3);278 Assert.assertNull(action.getEndpoint());279 Assert.assertEquals(action.getEndpointUri(), "channel:myMessageEndpoint");280 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);281 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().size(), 2);282 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$.json.text"), "Hello Citrus");283 Assert.assertEquals(jsonPathValidationContext.getJsonPathExpressions().get("$..foo.bar"), "true");284 // 15th action285 action = getNextTestActionFromTest();286 Assert.assertEquals(action.getValidationContexts().size(), 3);287 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);288 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);289 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);290 JsonMessageValidationContext jsonValidationContext = (JsonMessageValidationContext)action.getValidationContexts().get(2);291 Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);292 messageBuilder = (PayloadTemplateMessageBuilder)action.getMessageBuilder();293 Assert.assertNull(messageBuilder.getPayloadResourcePath());294 Assert.assertNotNull(messageBuilder.getPayloadData());295 Assert.assertEquals(messageBuilder.getPayloadData().trim(), "{ \"FooMessage\": { \"foo\": \"Hello World!\" }, { \"bar\": \"@ignore@\" }}");296 Assert.assertEquals(messageBuilder.getMessageInterceptors().size(), 1);297 Assert.assertTrue(messageBuilder.getMessageInterceptors().get(0) instanceof JsonPathMessageConstructionInterceptor);298 JsonPathMessageConstructionInterceptor jsonMessageConstructionInterceptor = (JsonPathMessageConstructionInterceptor)messageBuilder.getMessageInterceptors().get(0);299 Assert.assertEquals(jsonMessageConstructionInterceptor.getJsonPathExpressions().size(), 1);300 Assert.assertEquals(jsonMessageConstructionInterceptor.getJsonPathExpressions().get("$.FooMessage.foo"), "newValue");301 Assert.assertEquals(jsonValidationContext.getIgnoreExpressions().size(), 1);302 Assert.assertEquals(jsonValidationContext.getIgnoreExpressions().iterator().next(), "$.FooMessage.bar");303 // 16th action304 action = getNextTestActionFromTest();305 Assert.assertEquals(action.getVariableExtractors().size(), 2);306 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor);307 headerVariableExtractor = (MessageHeaderVariableExtractor)action.getVariableExtractors().get(0);308 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof DefaultPayloadVariableExtractor);309 DefaultPayloadVariableExtractor jsonVariableExtractor = (DefaultPayloadVariableExtractor) action.getVariableExtractors().get(1);310 Assert.assertEquals(headerVariableExtractor.getHeaderMappings().size(), 1);311 Assert.assertEquals(headerVariableExtractor.getHeaderMappings().get("operation"), "operation");312 Assert.assertEquals(jsonVariableExtractor.getPathExpressions().size(), 1);313 Assert.assertEquals(jsonVariableExtractor.getPathExpressions().get("$.message.text"), "text");314 // 17th action315 action = getNextTestActionFromTest();316 Assert.assertEquals(action.getValidators().size(), 1);317 Assert.assertEquals(action.getValidators().get(0), beanDefinitionContext.getBean("myValidator", MessageValidator.class));318 HeaderValidationContext headerValidationContext = (HeaderValidationContext) action.getValidationContexts().get(0);319 Assert.assertEquals(headerValidationContext.getValidatorNames().size(), 1);320 Assert.assertEquals(headerValidationContext.getValidatorNames().get(0), "myHeaderValidator");321 // 18th action322 action = getNextTestActionFromTest();323 Assert.assertEquals(action.getValidators().size(), 2);324 Assert.assertEquals(action.getValidators().get(0), beanDefinitionContext.getBean("myValidator", MessageValidator.class));325 Assert.assertEquals(action.getValidators().get(1), beanDefinitionContext.getBean("defaultPlaintextMessageValidator", MessageValidator.class));326 headerValidationContext = (HeaderValidationContext) action.getValidationContexts().get(0);327 Assert.assertEquals(headerValidationContext.getValidatorNames().size(), 2);328 Assert.assertEquals(headerValidationContext.getValidatorNames().get(0), "myHeaderValidator");329 Assert.assertEquals(headerValidationContext.getValidatorNames().get(1), "defaultHeaderValidator");330 }331}...
Source:HttpReceiveResponseActionParserTest.java
...22import com.consol.citrus.message.MessageHeaders;23import com.consol.citrus.testng.AbstractActionParserTest;24import com.consol.citrus.validation.DefaultPayloadVariableExtractor;25import com.consol.citrus.validation.builder.PayloadTemplateMessageBuilder;26import com.consol.citrus.validation.context.HeaderValidationContext;27import com.consol.citrus.validation.json.JsonMessageValidationContext;28import com.consol.citrus.validation.xml.XmlMessageValidationContext;29import org.springframework.beans.factory.BeanDefinitionStoreException;30import org.testng.Assert;31import org.testng.annotations.Test;32public class HttpReceiveResponseActionParserTest extends AbstractActionParserTest<ReceiveMessageAction> {33 @Test34 public void testHttpRequestActionParser() {35 assertActionCount(4);36 assertActionClassAndName(ReceiveMessageAction.class, "http:receive-response");37 PayloadTemplateMessageBuilder messageBuilder;38 HttpMessageContentBuilder httpMessageContentBuilder;39 ReceiveMessageAction action = getNextTestActionFromTest();40 Assert.assertEquals(action.getValidationContexts().size(), 1);41 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);42 httpMessageContentBuilder = ((HttpMessageContentBuilder)action.getMessageBuilder());43 Assert.assertNotNull(httpMessageContentBuilder);44 Assert.assertEquals(httpMessageContentBuilder.getDelegate().getClass(), PayloadTemplateMessageBuilder.class);45 messageBuilder = (PayloadTemplateMessageBuilder)httpMessageContentBuilder.getDelegate();46 Assert.assertNull(messageBuilder.getPayloadData());47 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().size(), 2L);48 Assert.assertNotNull(httpMessageContentBuilder.getMessage().getHeaders().get(MessageHeaders.ID));49 Assert.assertNotNull(httpMessageContentBuilder.getMessage().getHeaders().get(MessageHeaders.TIMESTAMP));50 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("httpClient", HttpClient.class));51 Assert.assertNull(action.getEndpointUri());52 Assert.assertEquals(action.getVariableExtractors().size(), 0);53 action = getNextTestActionFromTest();54 Assert.assertEquals(action.getValidationContexts().size(), 1);55 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);56 httpMessageContentBuilder = ((HttpMessageContentBuilder)action.getMessageBuilder());57 Assert.assertNotNull(httpMessageContentBuilder);58 Assert.assertEquals(httpMessageContentBuilder.getDelegate().getClass(), PayloadTemplateMessageBuilder.class);59 messageBuilder = (PayloadTemplateMessageBuilder)httpMessageContentBuilder.getDelegate();60 Assert.assertNull(messageBuilder.getPayloadData());61 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().size(), 5L);62 Assert.assertNotNull(httpMessageContentBuilder.getMessage().getHeaders().get(MessageHeaders.ID));63 Assert.assertNotNull(httpMessageContentBuilder.getMessage().getHeaders().get(MessageHeaders.TIMESTAMP));64 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().get(HttpMessageHeaders.HTTP_STATUS_CODE), "404");65 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().get(HttpMessageHeaders.HTTP_REASON_PHRASE), "NOT_FOUND");66 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().get(HttpMessageHeaders.HTTP_VERSION), "HTTP/1.1");67 Assert.assertNull(action.getEndpoint());68 Assert.assertEquals(action.getEndpointUri(), "http://localhost:8080/test");69 action = getNextTestActionFromTest();70 Assert.assertEquals(action.getValidationContexts().size(), 3);71 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);72 Assert.assertTrue(action.getValidationContexts().get(1) instanceof XmlMessageValidationContext);73 Assert.assertTrue(action.getValidationContexts().get(2) instanceof JsonMessageValidationContext);74 httpMessageContentBuilder = ((HttpMessageContentBuilder)action.getMessageBuilder());75 Assert.assertNotNull(httpMessageContentBuilder);76 Assert.assertEquals(httpMessageContentBuilder.getDelegate().getClass(), PayloadTemplateMessageBuilder.class);77 messageBuilder = (PayloadTemplateMessageBuilder)httpMessageContentBuilder.getDelegate();78 Assert.assertEquals(messageBuilder.getPayloadData(), "<user><id>1001</id><name>new_user</name></user>");79 Assert.assertEquals(httpMessageContentBuilder.getMessage().getHeaders().get("userId"), "1001");80 Assert.assertEquals(action.getEndpoint(), beanDefinitionContext.getBean("httpClient", HttpClient.class));81 Assert.assertNull(action.getEndpointUri());82 Assert.assertEquals(action.getVariableExtractors().size(), 1L);83 Assert.assertEquals(((DefaultPayloadVariableExtractor)action.getVariableExtractors().get(0)).getPathExpressions().size(), 1L);84 Assert.assertEquals(((DefaultPayloadVariableExtractor)action.getVariableExtractors().get(0)).getPathExpressions().get("$.user.id"), "userId");85 action = getNextTestActionFromTest();86 Assert.assertEquals(action.getValidationContexts().size(), 1);87 Assert.assertTrue(action.getValidationContexts().get(0) instanceof HeaderValidationContext);88 httpMessageContentBuilder = ((HttpMessageContentBuilder)action.getMessageBuilder());89 Assert.assertNotNull(httpMessageContentBuilder);90 Assert.assertEquals(httpMessageContentBuilder.getDelegate().getClass(), PayloadTemplateMessageBuilder.class);91 messageBuilder = (PayloadTemplateMessageBuilder)httpMessageContentBuilder.getDelegate();92 Assert.assertNull(messageBuilder.getPayloadData());93 Assert.assertNull(action.getEndpoint());94 Assert.assertEquals(action.getEndpointUri(), "http://localhost:8080/test");95 Assert.assertEquals(action.getActor(), beanDefinitionContext.getBean("testActor", TestActor.class));96 }97 @Test98 public void testHttpRequestActionParserFailed() {99 try {100 createApplicationContext("failed");101 Assert.fail("Missing bean creation exception due to invalid attributes");...
Source:DefaultMessageHeaderValidator.java
...17import com.consol.citrus.context.TestContext;18import com.consol.citrus.endpoint.resolver.DynamicEndpointUriResolver;19import com.consol.citrus.exceptions.ValidationException;20import com.consol.citrus.message.*;21import com.consol.citrus.validation.context.HeaderValidationContext;22import org.springframework.beans.factory.NoSuchBeanDefinitionException;23import org.springframework.beans.factory.annotation.Autowired;24import org.springframework.util.CollectionUtils;25import java.util.*;26/**27 * Basic header message validator provides message header validation. Subclasses only have to add28 * specific logic for message payload validation. This validator is based on a control message.29 * 30 * @author Christoph Deppisch31 */32public class DefaultMessageHeaderValidator extends AbstractMessageValidator<HeaderValidationContext> {33 /** List of special header validators */34 @Autowired(required = false)35 private List<HeaderValidator> validators = new ArrayList<>();36 @Override37 public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, HeaderValidationContext validationContext) {38 Map<String, Object> controlHeaders = controlMessage.getHeaders();39 Map<String, Object> receivedHeaders = receivedMessage.getHeaders();40 if (CollectionUtils.isEmpty(controlHeaders)) { return; }41 log.debug("Start message header validation ...");42 for (Map.Entry<String, Object> entry : controlHeaders.entrySet()) {43 if (MessageHeaderUtils.isSpringInternalHeader(entry.getKey()) ||44 entry.getKey().startsWith(MessageHeaders.MESSAGE_PREFIX) ||45 entry.getKey().equals(DynamicEndpointUriResolver.ENDPOINT_URI_HEADER_NAME) ||46 entry.getKey().equals(DynamicEndpointUriResolver.REQUEST_PATH_HEADER_NAME) ||47 entry.getKey().equals(DynamicEndpointUriResolver.QUERY_PARAM_HEADER_NAME)) {48 continue;49 }50 final String headerName = getHeaderName(entry.getKey(), receivedHeaders, context, validationContext);51 if (!receivedHeaders.containsKey(headerName)) {52 throw new ValidationException("Validation failed: Header element '" + headerName + "' is missing");53 }54 Object controlValue = entry.getValue();55 validationContext.getValidators()56 .stream()57 .filter(validator -> validator.supports(headerName, Optional.ofNullable(controlValue).map(Object::getClass).orElse(null)))58 .findFirst()59 .orElse(60 validationContext.getValidatorNames()61 .stream()62 .map(beanName -> {63 try {64 return context.getApplicationContext().getBean(beanName, HeaderValidator.class);65 } catch (NoSuchBeanDefinitionException e) {66 log.warn("Failed to resolve header validator for name: " + beanName);67 return null;68 }69 })70 .filter(Objects::nonNull)71 .findFirst()72 .orElse(73 validators.stream()74 .filter(validator -> validator.supports(headerName, Optional.ofNullable(controlValue).map(Object::getClass).orElse(null)))75 .findFirst()76 .orElse(new DefaultHeaderValidator())77 )78 ).validateHeader(headerName, receivedHeaders.get(headerName), controlValue, context, validationContext);79 }80 log.info("Message header validation successful: All values OK");81 }82 /**83 * Get header name from control message but also check if header expression is a variable or function. In addition to that find case insensitive header name in84 * received message when feature is activated.85 *86 * @param name87 * @param receivedHeaders88 * @param context89 * @param validationContext90 * @return91 */92 private String getHeaderName(String name, Map<String, Object> receivedHeaders, TestContext context, HeaderValidationContext validationContext) {93 String headerName = context.resolveDynamicValue(name);94 if (!receivedHeaders.containsKey(headerName) &&95 validationContext.isHeaderNameIgnoreCase()) {96 String key = headerName;97 log.debug(String.format("Finding case insensitive header for key '%s'", key));98 headerName = receivedHeaders99 .entrySet()100 .parallelStream()101 .filter(item -> item.getKey().equalsIgnoreCase(key))102 .map(Map.Entry::getKey)103 .findFirst()104 .orElseThrow(() -> new ValidationException("Validation failed: No matching header for key '" + key + "'"));105 log.info(String.format("Found matching case insensitive header name: %s", headerName));106 }107 return headerName;108 }109 @Override110 public boolean supportsMessageType(String messageType, Message message) {111 return true;112 }113 @Override114 protected Class<HeaderValidationContext> getRequiredValidationContextType() {115 return HeaderValidationContext.class;116 }117 /**118 * Adds header validator.119 * @param validator120 */121 public void addHeaderValidator(HeaderValidator validator) {122 this.validators.add(validator);123 }124 /**125 * Gets the validators.126 *127 * @return128 */129 public List<HeaderValidator> getValidators() {...
HeaderValidationContext
Using AI Code Generation
1import com.consol.citrus.dsl.runner.TestRunner;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.validation.context.HeaderValidationContext;4import org.testng.annotations.Test;5public class 4 extends TestNGCitrusTestDesigner {6public void 4() {7TestRunner runner = createTestRunner();8runner.http(builder -> builder.client("httpClient")9.send()10.get("/test")11.header("Accept", "application/json"));12runner.http(builder -> builder.client("httpClient")13.receive()14.response(HttpStatus.OK)15.header("Content-Type", "application/json")16.validationContext(HeaderValidationContext.Builder.headerValidationContext()17.ignore("Content-Type").build()));18}19}
HeaderValidationContext
Using AI Code Generation
1import com.consol.citrus.validation.context.HeaderValidationContext;2import com.consol.citrus.validation.xml.XmlMessageValidationContext;3import com.consol.citrus.message.MessageType;4import com.consol.citrus.dsl.builder.ReceiveMessageActionBuilder;5import com.consol.citrus.dsl.builder.SendMessageActionBuilder;6import com.consol.citrus.dsl.builder.SendSoapMessageActionBuilder;7import com.consol.citrus.dsl.builder.ReceiveSoapMessageActionBuilder;8import com.consol.citrus.dsl.builder.DelegatingTestActionBuilder;9import com.consol.citrus.dsl.builder.HttpActionBuilder;10import com.consol.citrus.dsl.builder.HttpServerActionBuilder;11import com.consol.citrus.dsl.builder.HttpClientActionBuilder;12import com.consol.citrus.dsl.builder.HttpActionBuilder.HttpActionBuilderSupport;13import com.consol.citrus.dsl.builder.HttpServerActionBuilder.HttpServerActionBuilderSupport;14import com.consol.citrus.dsl.builder.HttpClientActionBuilder.HttpClientActionBuilderSupport;15@CitrusXmlTest(name = "4")16public class 4 {17 public void 4() {18 description("4");19 variable("var1", "value1");20 variable("var2", "value2");21 variable("var3", "value3");22 variable("var4", "value4");23 parallel(24 sequential(25 http(httpServerActionBuilder -> httpServerActionBuilder26 .server("httpServer")27 .receive()28 .post()29 .header("Operation", "sayHello")30 http(httpClientActionBuilder -> httpClientActionBuilder31 .client("httpClient")32 .send()33 .post()34 .header("Operation", "sayHello")35 http(httpClientActionBuilder -> httpClientActionBuilder36 .client("httpClient")37 .receive()38 .response(HttpStatus.OK)39 sequential(40 http(httpServerAction
HeaderValidationContext
Using AI Code Generation
1public class HeaderValidationContextTest {2 public void headerValidationContextTest() {3 run(new TestAction() {4 public void doExecute(TestContext context) {5 HeaderValidationContext validationContext = new HeaderValidationContext();6 validationContext.addHeader("operation", "add");7 validationContext.addHeader("action", "multiply");8 validationContext.addHeader("type", "double");9 context.setValidationContext("headerValidationContext", validationContext);10 }11 });12 run(new HttpSendActionBuilder()13 .client("httpClient")14 .send()15 .header("operation", "add")16 .header("action", "multiply")17 .header("type", "double")18 .payload("<mathRequest><number1>10</number1><number2>5</number2></mathRequest>"));19 run(new HttpReceiveActionBuilder()20 .client("httpClient")21 .receive()22 .validationContext("headerValidationContext")23 .payload("<mathResponse><result>15</result></mathResponse>"));24 }25}26public class JsonMessageValidationContextTest {27 public void jsonMessageValidationContextTest() {28 run(new TestAction() {29 public void doExecute(TestContext context) {30 JsonMessageValidationContext validationContext = new JsonMessageValidationContext();31 validationContext.setJsonPathExpressions(Arrays.asList("$.operation", "$.action"));32 context.setValidationContext("jsonMessageValidationContext", validationContext);33 }34 });35 run(new HttpSendActionBuilder()36 .client("httpClient")37 .send()38 .header("operation", "add")39 .header("action", "multiply")40 .header("type", "double")41 .payload("<mathRequest><number1>10</number1><number2>5</number2></mathRequest>"));42 run(new HttpReceiveActionBuilder()43 .client("httpClient")44 .receive()45 .validationContext("jsonMessageValidationContext")46 .payload("{\"operation\":\"add\",\"action\":\"multiply\",\"result\":15}"));47 }48}
HeaderValidationContext
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.validation.context.HeaderValidationContext;6import org.springframework.http.HttpStatus;7import org.testng.annotations.Test;8public class HeaderValidationContextTest extends TestNGCitrusTestDesigner {9 public void HeaderValidationContext() {10 http(httpActionBuilder -> httpActionBuilder.client("httpClient")11 .send()12 .get("/hello")13 );14 http(httpActionBuilder -> httpActionBuilder.client("httpClient")15 .receive()16 .response(HttpStatus.OK)17 .messageType(MessageType.PLAINTEXT)18 .validateHeader("Content-Type", "text/plain")19 .validateHeader("Content-Length", "5")20 .extractFromHeader("Content-Length", "length")21 .extractFromHeader("Content-Type", "type")22 );23 echo("Length: ${length}");24 echo("Type: ${type}");25 http(httpActionBuilder -> httpActionBuilder.client("httpClient")26 .send()27 .get("/hello")28 );29 http(httpActionBuilder -> httpActionBuilder.client("httpClient")30 .receive()31 .response(HttpStatus.OK)32 .messageType(MessageType.PLAINTEXT)33 .validateHeader("Content-Type", "text/plain")34 .validateHeader("Content-Length", "5")35 .extractFromHeader("Content-Length", "length")36 .extractFromHeader("Content-Type", "type")37 );38 echo("Length: ${length}");39 echo("Type: ${type}");40 http(httpActionBuilder -> httpActionBuilder.client("httpClient")41 .send()42 .get("/hello")43 );44 http(httpActionBuilder -> httpActionBuilder.client("httpClient")45 .receive()46 .response(HttpStatus.OK)47 .messageType(MessageType.PLAINTEXT)48 .headerValidator(new HeaderValidationContext()49 .validate("Content-Type", "text/plain")50 .validate("Content-Length", "5")51 .extractFromHeader("Content-Length", "length")52 .extractFromHeader("Content-Type", "type")53 );54 echo("Length: ${length}");55 echo("Type: ${type}");56 }57}
HeaderValidationContext
Using AI Code Generation
1public class HeaderValidationContextTest extends TestNGCitrusTestRunner {2 public void testHeaderValidationContext() {3 variable("headerName", "citrus:randomNumber(5)");4 variable("headerValue", "citrus:randomNumber(5)");5 variable("headerName1", "citrus:randomNumber(5)");6 variable("headerValue1", "citrus:randomNumber(5)");7 http().client("httpClient")8 .send()9 .post("/test")10 .header("${headerName}", "${headerValue}")11 .header("${headerName1}", "${headerValue1}");12 http().client("httpClient")13 .receive()14 .response(HttpStatus.OK)15 .header("${headerName}", "${headerValue}")16 .header("${headerName1}", "${headerValue1}");17 }18}19public class MessageValidationContextTest extends TestNGCitrusTestRunner {20 public void testMessageValidationContext() {21 variable("message", "citrus:randomNumber(5)");22 variable("message1", "citrus:randomNumber(5)");23 http().client("httpClient")24 .send()25 .post("/test")26 .payload("${message}");27 http().client("httpClient")28 .receive()29 .response(HttpStatus.OK)30 .payload("${message1}");31 }32}33public class PayloadValidationContextTest extends TestNGCitrusTestRunner {34 public void testPayloadValidationContext() {35 variable("payload", "citrus:randomNumber(5)");36 variable("payload1", "citrus:randomNumber(5)");37 http().client("httpClient")38 .send()39 .post("/test")40 .payload("${payload}");41 http().client("httpClient")42 .receive()43 .response(HttpStatus.OK)44 .payload("${payload1}");45 }46}47public class TextValidationContextTest extends TestNGCitrusTestRunner {48 public void testTextValidationContext()
HeaderValidationContext
Using AI Code Generation
1public class HeaderValidationContext {2 public static void main(String[] args) {3 HeaderValidationContext context = new HeaderValidationContext();4 context.setHeaderName("id");5 context.setHeaderValue("12345");6 }7}8public class HeaderValidationContextBuilder {9 public static void main(String[] args) {10 HeaderValidationContextBuilder context = new HeaderValidationContextBuilder();11 context.setHeaderName("id");12 context.setHeaderValue("12345");13 }14}15public class HttpActionBuilder {16 public static void main(String[] args) {17 HttpActionBuilder http = new HttpActionBuilder();18 http.client("httpclient");19 http.send();20 http.receive();21 }22}23public class HttpActionBuilder {24 public static void main(String[] args) {25 HttpActionBuilder http = new HttpActionBuilder();26 http.client("httpclient");27 http.send();28 http.receive();29 }30}31public class HttpActionBuilder {32 public static void main(String[] args) {33 HttpActionBuilder http = new HttpActionBuilder();34 http.client("httpclient");35 http.send();36 http.receive();37 }38}39public class HttpActionBuilder {40 public static void main(String[] args) {41 HttpActionBuilder http = new HttpActionBuilder();42 http.client("httpclient");43 http.send();44 http.receive();45 }46}47public class HttpActionBuilder {48 public static void main(String[] args) {49 HttpActionBuilder http = new HttpActionBuilder();50 http.client("httpclient");51 http.send();52 http.receive();53 }54}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!