Best Citrus code snippet using com.consol.citrus.dsl.builder.ReceiveMessageBuilder.HeaderValidationContext
Source:ReceiveMessageBuilder.java
...25import com.consol.citrus.validation.HeaderValidator;26import com.consol.citrus.validation.MessageValidator;27import com.consol.citrus.validation.builder.*;28import com.consol.citrus.validation.callback.ValidationCallback;29import com.consol.citrus.validation.context.HeaderValidationContext;30import com.consol.citrus.validation.context.ValidationContext;31import com.consol.citrus.validation.json.*;32import com.consol.citrus.validation.script.ScriptValidationContext;33import com.consol.citrus.validation.xml.*;34import com.consol.citrus.variable.MessageHeaderVariableExtractor;35import com.consol.citrus.variable.dictionary.DataDictionary;36import com.fasterxml.jackson.core.JsonProcessingException;37import com.fasterxml.jackson.databind.ObjectMapper;38import org.springframework.context.ApplicationContext;39import org.springframework.context.ApplicationContextAware;40import org.springframework.core.io.Resource;41import org.springframework.oxm.Marshaller;42import org.springframework.oxm.XmlMappingException;43import org.springframework.util.Assert;44import org.springframework.util.CollectionUtils;45import org.springframework.xml.transform.StringResult;46import java.io.IOException;47import java.nio.charset.Charset;48import java.util.Map;49import java.util.stream.Stream;50/**51 * Receive message action builder offers configuration methods for a receive test action. Build options52 * include construction of control message payload and headers as well as value extraction.53 * 54 * @author Christoph Deppisch55 * @since 2.356 */57public class ReceiveMessageBuilder<A extends ReceiveMessageAction, T extends ReceiveMessageBuilder> extends AbstractTestActionBuilder<DelegatingTestAction<TestAction>> {58 /** Self reference for generics support */59 private final T self;60 /** Message type for this action builder */61 private String messageType;62 /** Validation context used in this action builder */63 private HeaderValidationContext headerValidationContext = new HeaderValidationContext();64 private XmlMessageValidationContext xmlMessageValidationContext = new XmlMessageValidationContext();65 private JsonMessageValidationContext jsonMessageValidationContext = new JsonMessageValidationContext();66 /** JSON validation context used in this action builder */67 private JsonPathMessageValidationContext jsonPathValidationContext;68 /** Script validation context used in this action builder */69 private ScriptValidationContext scriptValidationContext;70 /** Variable extractors filled within this action builder */71 private MessageHeaderVariableExtractor headerExtractor;72 private XpathPayloadVariableExtractor xpathExtractor;73 private JsonPathVariableExtractor jsonPathExtractor;74 /** Basic application context */75 private ApplicationContext applicationContext;76 /**77 * Default constructor using test action, basic application context and position handle.78 * @param action79 */80 public ReceiveMessageBuilder(A action) {81 this(new DelegatingTestAction(action));82 }83 /**84 * Default constructor.85 */86 public ReceiveMessageBuilder() {87 this((A) new ReceiveMessageAction());88 }89 /**90 * Constructor using delegate test action.91 * @param action92 */93 public ReceiveMessageBuilder(DelegatingTestAction<TestAction> action) {94 super(action);95 this.self = (T) this;96 }97 /**98 * Sets the message endpoint to receive messages from.99 * @param messageEndpoint100 * @return101 */102 public ReceiveMessageBuilder endpoint(Endpoint messageEndpoint) {103 getAction().setEndpoint(messageEndpoint);104 return this;105 }106 /**107 * Sets the message endpoint uri to receive messages from.108 * @param messageEndpointUri109 * @return110 */111 public ReceiveMessageBuilder endpoint(String messageEndpointUri) {112 getAction().setEndpointUri(messageEndpointUri);113 return this;114 }115 /**116 * Adds a custom timeout to this message receiving action.117 * @param receiveTimeout118 * @return119 */120 public T timeout(long receiveTimeout) {121 getAction().setReceiveTimeout(receiveTimeout);122 return self;123 }124 125 /**126 * Expect a control message in this receive action.127 * @param controlMessage128 * @return129 */130 public T message(Message controlMessage) {131 StaticMessageContentBuilder staticMessageContentBuilder = StaticMessageContentBuilder.withMessage(controlMessage);132 staticMessageContentBuilder.setMessageHeaders(getMessageContentBuilder().getMessageHeaders());133 getAction().setMessageBuilder(staticMessageContentBuilder);134 return self;135 }136 /**137 * Sets the payload data on the message builder implementation.138 * @param payload139 * @return140 */141 protected void setPayload(String payload) {142 MessageContentBuilder messageContentBuilder = getMessageContentBuilder();143 if (messageContentBuilder instanceof PayloadTemplateMessageBuilder) {144 ((PayloadTemplateMessageBuilder) messageContentBuilder).setPayloadData(payload);145 } else if (messageContentBuilder instanceof StaticMessageContentBuilder) {146 ((StaticMessageContentBuilder) messageContentBuilder).getMessage().setPayload(payload);147 } else {148 throw new CitrusRuntimeException("Unable to set payload on message builder type: " + messageContentBuilder.getClass());149 }150 }151 /**152 * Sets the message name.153 * @param name154 * @return155 */156 public T name(String name) {157 getMessageContentBuilder().setMessageName(name);158 return self;159 }160 161 /**162 * Expect this message payload data in received message.163 * @param payload164 * @return165 */166 public T payload(String payload) {167 setPayload(payload);168 return self;169 }170 171 /**172 * Expect this message payload data in received message.173 * @param payloadResource174 * @return175 */176 public T payload(Resource payloadResource) {177 return payload(payloadResource, FileUtils.getDefaultCharset());178 }179 /**180 * Expect this message payload data in received message.181 * @param payloadResource182 * @param charset183 * @return184 */185 public T payload(Resource payloadResource, Charset charset) {186 try {187 setPayload(FileUtils.readToString(payloadResource, charset));188 } catch (IOException e) {189 throw new CitrusRuntimeException("Failed to read payload resource", e);190 }191 return self;192 }193 /**194 * Expect this message payload as model object which is marshalled to a character sequence195 * using the default object to xml mapper before validation is performed.196 * @param payload197 * @param marshaller198 * @return199 */200 public T payload(Object payload, Marshaller marshaller) {201 StringResult result = new StringResult();202 try {203 marshaller.marshal(payload, result);204 } catch (XmlMappingException e) {205 throw new CitrusRuntimeException("Failed to marshal object graph for message payload", e);206 } catch (IOException e) {207 throw new CitrusRuntimeException("Failed to marshal object graph for message payload", e);208 }209 setPayload(result.toString());210 return self;211 }212 /**213 * Expect this message payload as model object which is mapped to a character sequence214 * using the default object to json mapper before validation is performed.215 * @param payload216 * @param objectMapper217 * @return218 */219 public T payload(Object payload, ObjectMapper objectMapper) {220 try {221 setPayload(objectMapper.writer().writeValueAsString(payload));222 } catch (JsonProcessingException e) {223 throw new CitrusRuntimeException("Failed to map object graph for message payload", e);224 }225 return self;226 }227 /**228 * Expect this message payload as model object which is marshalled to a character sequence using the default object to xml mapper that229 * is available in Spring bean application context.230 *231 * @param payload232 * @return233 */234 public T payloadModel(Object payload) {235 Assert.notNull(applicationContext, "Citrus application context is not initialized!");236 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {237 return payload(payload, applicationContext.getBean(Marshaller.class));238 } else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {239 return payload(payload, applicationContext.getBean(ObjectMapper.class));240 }241 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");242 }243 /**244 * Expect this message payload as model object which is marshalled to a character sequence using the given object to xml mapper that245 * is accessed by its bean name in Spring bean application context.246 *247 * @param payload248 * @param mapperName249 * @return250 */251 public T payload(Object payload, String mapperName) {252 Assert.notNull(applicationContext, "Citrus application context is not initialized!");253 if (applicationContext.containsBean(mapperName)) {254 Object mapper = applicationContext.getBean(mapperName);255 if (Marshaller.class.isAssignableFrom(mapper.getClass())) {256 return payload(payload, (Marshaller) mapper);257 } else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {258 return payload(payload, (ObjectMapper) mapper);259 } else {260 throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));261 }262 }263 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");264 }265 266 /**267 * Expect this message header entry in received message.268 * @param name269 * @param value270 * @return271 */272 public T header(String name, Object value) {273 getMessageContentBuilder().getMessageHeaders().put(name, value);274 return self;275 }276 /**277 * Expect this message header entries in received message.278 * @param headers279 * @return280 */281 public T headers(Map<String, Object> headers) {282 getMessageContentBuilder().getMessageHeaders().putAll(headers);283 return self;284 }285 286 /**287 * Expect this message header data in received message. Message header data is used in 288 * SOAP messages as XML fragment for instance.289 * @param data290 * @return291 */292 public T header(String data) {293 getMessageContentBuilder().getHeaderData().add(data);294 return self;295 }296 /**297 * Expect this message header data as model object which is marshalled to a character sequence using the default object to xml mapper that298 * is available in Spring bean application context.299 *300 * @param model301 * @return302 */303 public T headerFragment(Object model) {304 Assert.notNull(applicationContext, "Citrus application context is not initialized!");305 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {306 return headerFragment(model, applicationContext.getBean(Marshaller.class));307 } else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {308 return headerFragment(model, applicationContext.getBean(ObjectMapper.class));309 }310 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");311 }312 /**313 * Expect this message header data as model object which is marshalled to a character sequence using the given object to xml mapper that314 * is accessed by its bean name in Spring bean application context.315 *316 * @param model317 * @param mapperName318 * @return319 */320 public T headerFragment(Object model, String mapperName) {321 Assert.notNull(applicationContext, "Citrus application context is not initialized!");322 if (applicationContext.containsBean(mapperName)) {323 Object mapper = applicationContext.getBean(mapperName);324 if (Marshaller.class.isAssignableFrom(mapper.getClass())) {325 return headerFragment(model, (Marshaller) mapper);326 } else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {327 return headerFragment(model, (ObjectMapper) mapper);328 } else {329 throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));330 }331 }332 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");333 }334 /**335 * Expect this message header data as model object which is marshalled to a character sequence336 * using the default object to xml mapper before validation is performed.337 * @param model338 * @param marshaller339 * @return340 */341 public T headerFragment(Object model, Marshaller marshaller) {342 StringResult result = new StringResult();343 try {344 marshaller.marshal(model, result);345 } catch (XmlMappingException e) {346 throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);347 } catch (IOException e) {348 throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);349 }350 return header(result.toString());351 }352 /**353 * Expect this message header data as model object which is mapped to a character sequence354 * using the default object to json mapper before validation is performed.355 * @param model356 * @param objectMapper357 * @return358 */359 public T headerFragment(Object model, ObjectMapper objectMapper) {360 try {361 return header(objectMapper.writer().writeValueAsString(model));362 } catch (JsonProcessingException e) {363 throw new CitrusRuntimeException("Failed to map object graph for message header data", e);364 }365 }366 /**367 * Expect this message header data in received message from file resource. Message header data is used in 368 * SOAP messages as XML fragment for instance.369 * @param resource370 * @return371 */372 public T header(Resource resource) {373 return header(resource, FileUtils.getDefaultCharset());374 }375 /**376 * Expect this message header data in received message from file resource. Message header data is used in377 * SOAP messages as XML fragment for instance.378 * @param resource379 * @param charset380 * @return381 */382 public T header(Resource resource, Charset charset) {383 try {384 getMessageContentBuilder().getHeaderData().add(FileUtils.readToString(resource, charset));385 } catch (IOException e) {386 throw new CitrusRuntimeException("Failed to read header resource", e);387 }388 return self;389 }390 /**391 * Validate header names with case insensitive keys.392 * @param value393 * @return394 */395 public T headerNameIgnoreCase(boolean value) {396 headerValidationContext.setHeaderNameIgnoreCase(value);397 return self;398 }399 400 /**401 * Adds script validation.402 * @param validationScript403 * @return404 */405 public T validateScript(String validationScript) {406 getScriptValidationContext().setValidationScript(validationScript);407 return self;408 }409 410 /**411 * Reads validation script file resource and sets content as validation script.412 * @param scriptResource413 * @return414 */415 public T validateScript(Resource scriptResource) {416 return validateScript(scriptResource, FileUtils.getDefaultCharset());417 }418 /**419 * Reads validation script file resource and sets content as validation script.420 * @param scriptResource421 * @param charset422 * @return423 */424 public T validateScript(Resource scriptResource, Charset charset) {425 try {426 validateScript(FileUtils.readToString(scriptResource, charset));427 } catch (IOException e) {428 throw new CitrusRuntimeException("Failed to read script resource file", e);429 }430 return self;431 }432 /**433 * Adds script validation file resource.434 * @param fileResourcePath435 * @return436 */437 public T validateScriptResource(String fileResourcePath) {438 getScriptValidationContext().setValidationScriptResourcePath(fileResourcePath);439 return self;440 }441 442 /**443 * Adds custom validation script type.444 * @param type445 * @return446 */447 public T validateScriptType(String type) {448 getScriptValidationContext().setScriptType(type);449 return self;450 }451 /**452 * Sets a explicit message type for this receive action.453 * @param messageType454 * @return455 */456 public T messageType(MessageType messageType) {457 messageType(messageType.name());458 return self;459 }460 461 /**462 * Sets a explicit message type for this receive action.463 * @param messageType464 * @return465 */466 public T messageType(String messageType) {467 this.messageType = messageType;468 getAction().setMessageType(messageType);469 if (getAction().getValidationContexts().isEmpty()) {470 getAction().getValidationContexts().add(headerValidationContext);471 getAction().getValidationContexts().add(xmlMessageValidationContext);472 getAction().getValidationContexts().add(jsonMessageValidationContext);473 }474 return self;475 }476 477 /**478 * Sets schema validation enabled/disabled for this message.479 * @param enabled480 * @return481 */482 public T schemaValidation(boolean enabled) {483 xmlMessageValidationContext.setSchemaValidation(enabled);484 jsonMessageValidationContext.setSchemaValidation(enabled);485 return self;486 }487 /**488 * Validates XML namespace with prefix and uri.489 * @param prefix490 * @param namespaceUri491 * @return492 */493 public T validateNamespace(String prefix, String namespaceUri) {494 xmlMessageValidationContext.getControlNamespaces().put(prefix, namespaceUri);495 return self;496 }497 498 /**499 * Adds message element validation.500 * @param path501 * @param controlValue502 * @return503 */504 public T validate(String path, Object controlValue) {505 if (JsonPathMessageValidationContext.isJsonPathExpression(path)) {506 getJsonPathValidationContext().getJsonPathExpressions().put(path, controlValue);507 } else {508 getXPathValidationContext().getXpathExpressions().put(path, controlValue);509 }510 return self;511 }512 513 /**514 * Adds ignore path expression for message element.515 * @param path516 * @return517 */518 public T ignore(String path) {519 if (messageType.equalsIgnoreCase(MessageType.XML.name())520 || messageType.equalsIgnoreCase(MessageType.XHTML.name())) {521 xmlMessageValidationContext.getIgnoreExpressions().add(path);522 } else if (messageType.equalsIgnoreCase(MessageType.JSON.name())) {523 jsonMessageValidationContext.getIgnoreExpressions().add(path);524 }525 return self;526 }527 528 /**529 * Adds XPath message element validation.530 * @param xPathExpression531 * @param controlValue532 * @return533 */534 public T xpath(String xPathExpression, Object controlValue) {535 validate(xPathExpression, controlValue);536 return self;537 }538 /**539 * Adds JsonPath message element validation.540 * @param jsonPathExpression541 * @param controlValue542 * @return543 */544 public T jsonPath(String jsonPathExpression, Object controlValue) {545 validate(jsonPathExpression, controlValue);546 return self;547 }548 549 /**550 * Sets explicit schema instance name to use for schema validation.551 * @param schemaName552 * @return553 */554 public T xsd(String schemaName) {555 xmlMessageValidationContext.setSchema(schemaName);556 return self;557 }558 /**559 * Sets explicit schema instance name to use for schema validation.560 * @param schemaName The name of the schema bean561 */562 public T jsonSchema(String schemaName) {563 jsonMessageValidationContext.setSchema(schemaName);564 return self;565 }566 567 /**568 * Sets explicit xsd schema repository instance to use for validation.569 * @param schemaRepository570 * @return571 */572 public T xsdSchemaRepository(String schemaRepository) {573 xmlMessageValidationContext.setSchemaRepository(schemaRepository);574 return self;575 }576 /**577 * Sets explicit json schema repository instance to use for validation.578 * @param schemaRepository The name of the schema repository bean579 * @return580 */581 public T jsonSchemaRepository(String schemaRepository) {582 jsonMessageValidationContext.setSchemaRepository(schemaRepository);583 return self;584 }585 586 /**587 * Adds explicit namespace declaration for later path validation expressions.588 * @param prefix589 * @param namespaceUri590 * @return591 */592 public T namespace(String prefix, String namespaceUri) {593 getXpathVariableExtractor().getNamespaces().put(prefix, namespaceUri);594 xmlMessageValidationContext.getNamespaces().put(prefix, namespaceUri);595 return self;596 }597 598 /**599 * Sets default namespace declarations on this action builder.600 * @param namespaceMappings601 * @return602 */603 public T namespaces(Map<String, String> namespaceMappings) {604 getXpathVariableExtractor().getNamespaces().putAll(namespaceMappings);605 xmlMessageValidationContext.getNamespaces().putAll(namespaceMappings);606 return self;607 }608 609 /**610 * Sets message selector string.611 * @param messageSelector612 * @return613 */614 public T selector(String messageSelector) {615 getAction().setMessageSelector(messageSelector);616 return self;617 }618 619 /**620 * Sets message selector elements.621 * @param messageSelector622 * @return623 */624 public T selector(Map<String, Object> messageSelector) {625 getAction().setMessageSelectorMap(messageSelector);626 return self;627 }628 629 /**630 * Sets explicit message validators for this receive action.631 * @param validators632 * @return633 */634 public T validator(MessageValidator<? extends ValidationContext> ... validators) {635 Stream.of(validators).forEach(getAction()::addValidator);636 return self;637 }638 639 /**640 * Sets explicit message validators by name.641 * @param validatorNames642 * @return643 */644 @SuppressWarnings("unchecked")645 public T validator(String ... validatorNames) {646 Assert.notNull(applicationContext, "Citrus application context is not initialized!");647 for (String validatorName : validatorNames) {648 getAction().addValidator(applicationContext.getBean(validatorName, MessageValidator.class));649 }650 return self;651 }652 /**653 * Sets explicit header validator for this receive action.654 * @param validators655 * @return656 */657 public T headerValidator(HeaderValidator... validators) {658 Stream.of(validators).forEach(headerValidationContext::addHeaderValidator);659 return self;660 }661 /**662 * Sets explicit header validators by name.663 * @param validatorNames664 * @return665 */666 @SuppressWarnings("unchecked")667 public T headerValidator(String ... validatorNames) {668 Assert.notNull(applicationContext, "Citrus application context is not initialized!");669 for (String validatorName : validatorNames) {670 headerValidationContext.addHeaderValidator(applicationContext.getBean(validatorName, HeaderValidator.class));671 }672 return self;673 }674 /**675 * Sets explicit data dictionary for this receive action.676 * @param dictionary677 * @return678 */679 public T dictionary(DataDictionary dictionary) {680 getAction().setDataDictionary(dictionary);681 return self;682 }683 /**684 * Sets explicit data dictionary by name.685 * @param dictionaryName686 * @return687 */688 @SuppressWarnings("unchecked")689 public T dictionary(String dictionaryName) {690 Assert.notNull(applicationContext, "Citrus application context is not initialized!");691 DataDictionary dictionary = applicationContext.getBean(dictionaryName, DataDictionary.class);692 getAction().setDataDictionary(dictionary);693 return self;694 }695 696 /**697 * Extract message header entry as variable.698 * @param headerName699 * @param variable700 * @return701 */702 public T extractFromHeader(String headerName, String variable) {703 if (headerExtractor == null) {704 headerExtractor = new MessageHeaderVariableExtractor();705 getAction().getVariableExtractors().add(headerExtractor);706 }707 708 headerExtractor.getHeaderMappings().put(headerName, variable);709 return self;710 }711 712 /**713 * Extract message element via XPath or JSONPath from message payload as new test variable.714 * @param path715 * @param variable716 * @return717 */718 public T extractFromPayload(String path, String variable) {719 if (JsonPathMessageValidationContext.isJsonPathExpression(path)) {720 getJsonPathVariableExtractor().getJsonPathExpressions().put(path, variable);721 } else {722 getXpathVariableExtractor().getXpathExpressions().put(path, variable);723 }724 return self;725 }726 727 /**728 * Adds validation callback to the receive action for validating 729 * the received message with Java code.730 * @param callback731 * @return732 */733 public T validationCallback(ValidationCallback callback) {734 if (callback instanceof ApplicationContextAware) {735 ((ApplicationContextAware) callback).setApplicationContext(applicationContext);736 }737 getAction().setValidationCallback(callback);738 return self;739 }740 /**741 * Sets the Spring bean application context.742 * @param applicationContext743 */744 public T withApplicationContext(ApplicationContext applicationContext) {745 this.applicationContext = applicationContext;746 return self;747 }748 /**749 * Get message builder, if already registered or create a new message builder and register it750 *751 * @return the message builder in use752 */753 protected AbstractMessageContentBuilder getMessageContentBuilder() {754 if (getAction().getMessageBuilder() != null && getAction().getMessageBuilder() instanceof AbstractMessageContentBuilder) {755 return (AbstractMessageContentBuilder) getAction().getMessageBuilder();756 } else {757 PayloadTemplateMessageBuilder messageBuilder = new PayloadTemplateMessageBuilder();758 getAction().setMessageBuilder(messageBuilder);759 return messageBuilder;760 }761 }762 /**763 * Creates new variable extractor and adds it to test action.764 */765 private XpathPayloadVariableExtractor getXpathVariableExtractor() {766 if (xpathExtractor == null) {767 xpathExtractor = new XpathPayloadVariableExtractor();768 getAction().getVariableExtractors().add(xpathExtractor);769 }770 return xpathExtractor;771 }772 /**773 * Creates new variable extractor and adds it to test action.774 */775 private JsonPathVariableExtractor getJsonPathVariableExtractor() {776 if (jsonPathExtractor == null) {777 jsonPathExtractor = new JsonPathVariableExtractor();778 getAction().getVariableExtractors().add(jsonPathExtractor);779 }780 return jsonPathExtractor;781 }782 /**783 * Gets the validation context as XML validation context an raises exception if existing validation context is784 * not a XML validation context.785 * @return786 */787 private XpathMessageValidationContext getXPathValidationContext() {788 if (xmlMessageValidationContext instanceof XpathMessageValidationContext) {789 return ((XpathMessageValidationContext)xmlMessageValidationContext);790 } else {791 XpathMessageValidationContext xPathContext = new XpathMessageValidationContext();792 xPathContext.setNamespaces(xmlMessageValidationContext.getNamespaces());793 xPathContext.setControlNamespaces(xmlMessageValidationContext.getControlNamespaces());794 xPathContext.setIgnoreExpressions(xmlMessageValidationContext.getIgnoreExpressions());795 xPathContext.setSchema(xmlMessageValidationContext.getSchema());796 xPathContext.setSchemaRepository(xmlMessageValidationContext.getSchemaRepository());797 xPathContext.setSchemaValidation(xmlMessageValidationContext.isSchemaValidationEnabled());798 xPathContext.setDTDResource(xmlMessageValidationContext.getDTDResource());799 getAction().getValidationContexts().remove(xmlMessageValidationContext);800 getAction().getValidationContexts().add(xPathContext);801 xmlMessageValidationContext = xPathContext;802 return xPathContext;803 }804 }805 /**806 * Creates new script validation context if not done before and gets the script validation context.807 */808 private ScriptValidationContext getScriptValidationContext() {809 if (scriptValidationContext == null) {810 scriptValidationContext = new ScriptValidationContext(messageType.toString());811 getAction().getValidationContexts().add(scriptValidationContext);812 }813 return scriptValidationContext;814 }815 /**816 * Creates new JSONPath validation context if not done before and gets the validation context.817 */818 private JsonPathMessageValidationContext getJsonPathValidationContext() {819 if (jsonPathValidationContext == null) {820 jsonPathValidationContext = new JsonPathMessageValidationContext();821 getAction().getValidationContexts().add(jsonPathValidationContext);822 }823 return jsonPathValidationContext;824 }825 /**826 * Provides access to receive message action delegate.827 * @return828 */829 protected ReceiveMessageAction getAction() {830 return (ReceiveMessageAction) action.getDelegate();831 }832 /**833 * Sets the message type.834 * @param messageType835 */836 protected void setMessageType(MessageType messageType) {837 this.messageType = messageType.name();838 }839 /**840 * Sets the message type.841 * @param messageType842 */843 protected void setMessageType(String messageType) {844 this.messageType = messageType;845 }846 /**847 * Sets the xpath extractor.848 * @param xpathExtractor849 */850 protected void setXpathExtractor(XpathPayloadVariableExtractor xpathExtractor) {851 this.xpathExtractor = xpathExtractor;852 }853 /**854 * Sets the jsonPath extractor.855 * @param jsonPathExtractor856 */857 protected void setJsonPathExtractor(JsonPathVariableExtractor jsonPathExtractor) {858 this.jsonPathExtractor = jsonPathExtractor;859 }860 /**861 * Sets the header extractor.862 * @param headerExtractor863 */864 protected void setHeaderExtractor(MessageHeaderVariableExtractor headerExtractor) {865 this.headerExtractor = headerExtractor;866 }867 /**868 * Sets the script message validator.869 * @param scriptValidationContext870 */871 protected void setScriptValidationContext(ScriptValidationContext scriptValidationContext) {872 this.scriptValidationContext = scriptValidationContext;873 }874 /**875 * Sets the script message validator.876 * @param jsonPathValidationContext877 */878 protected void setJsonPathValidationContext(JsonPathMessageValidationContext jsonPathValidationContext) {879 this.jsonPathValidationContext = jsonPathValidationContext;880 }881 /**882 * Sets the XML validation context.883 * @param validationContext884 */885 protected void setXmlMessageValidationContext(XmlMessageValidationContext validationContext) {886 this.xmlMessageValidationContext = validationContext;887 }888 /**889 * Sets the JSON validation context.890 * @param validationContext891 */892 protected void setJsonMessageValidationContext(JsonMessageValidationContext validationContext) {893 this.jsonMessageValidationContext = validationContext;894 }895 /**896 * Sets the header validation context.897 * @param validationContext898 */899 protected void setHeaderValidationContext(HeaderValidationContext validationContext) {900 this.headerValidationContext = validationContext;901 }902}...
HeaderValidationContext
Using AI Code Generation
1import com.consol.citrus.dsl.builder.ReceiveMessageBuilder2import com.consol.citrus.dsl.builder.SendMessageBuilder3import com.consol.citrus.dsl.runner.TestRunner4import com.consol.citrus.dsl.runner.TestRunnerSupport5class MyTest extends TestRunnerSupport {6 def "My Test"() {7 def request = new ReceiveMessageBuilder()8 .messageType("text/plain")9 .headerValidationContext()10 .ignoreHeaders("citrus_jms_messageId", "citrus_jms_correlationId")11 .build()12 def response = new SendMessageBuilder()13 .messageType("text/plain")14 .header("citrus_jms_messageId", "123456")15 .header("citrus_jms_correlationId", "654321")16 .header("citrus_jms_timestamp", "123456789")17 .build()18 run(request, response)19 }20}21import com.consol.citrus.dsl.builder.ReceiveMessageBuilder22import com.consol.citrus.dsl.builder.SendMessageBuilder23import com.consol.citrus.dsl.runner.TestRunner24import com.consol.citrus.dsl.runner.TestRunnerSupport25class MyTest extends TestRunnerSupport {26 def "My Test"() {27 def request = new ReceiveMessageBuilder()28 .messageType("text/plain")29 .header("citrus_jms_messageId", "123456")30 .header("citrus_jms_correlationId", "654321")31 .header("citrus_jms_timestamp", "123456789")32 .build()33 def response = new SendMessageBuilder()34 .messageType("text/plain")35 .headerValidationContext()36 .ignoreHeaders("citrus_jms_messageId", "citrus_jms_correlationId")37 .build()38 run(request, response)39 }40}41import com.consol.citrus.dsl.builder.ReceiveMessageBuilder42import com.consol.citrus.dsl.builder.SendSoapMessageBuilder43import com.consol.citrus.dsl.runner
HeaderValidationContext
Using AI Code Generation
1import com.consol.citrus.dsl.builder.ReceiveMessageBuilder2import com.consol.citrus.dsl.runner.TestRunner3import com.consol.citrus.message.MessageType4class HeaderValidationContextTest {5 def "test HeaderValidationContext"() {6 def runner = new TestRunner()7 runner.receive()8 .messageType(MessageType.PLAINTEXT)9 .headerValidationContext({ ctx ->10 ctx.ignore("citrus_jms_messageId")11 })12 .payload("Hello Citrus!")13 runner.run()14 }15}16import com.consol.citrus.dsl.builder.ReceiveMessageBuilder17import com.consol.citrus.dsl.runner.TestRunner18import com.consol.citrus.message.MessageType19class HeaderValidationContextTest {20 def "test HeaderValidationContext"() {21 def runner = new TestRunner()22 runner.receive()23 .messageType(MessageType.XML)24 .headerValidationContext({ ctx ->25 ctx.ignore("citrus_jms_messageId")26 })27 .payload("<Message>Hello Citrus!</Message>")28 runner.run()29 }30}31import com.consol.citrus
HeaderValidationContext
Using AI Code Generation
1receive()2 .messageType(MessageType.PLAINTEXT)3 .headerValidationContext(headerValidationContext -> headerValidationContext4 .ignoreHeaders("ignoreHeader1", "ignoreHeader2")5 .validateHeader("validateHeader1", "validateHeader1Value")6 .validateHeader("validateHeader2", "validateHeader2Value")7 .validateHeader("validateHeader3", "validateHeader3Value")8 .validateHeader("validateHeader4", "validateHeader4Value")9 .validateHeader("validateHeader5", "validateHeader5Value")10 .validateHeader("validateHeader6", "validateHeader6Value")11 .validateHeader("validateHeader7", "validateHeader7Value")12 .validateHeader("validateHeader8", "validateHeader8Value")13 .validateHeader("validateHeader9", "validateHeader9Value")14 .validateHeader("validateHeader10", "validateHeader10Value")15 .validateHeader("validateHeader11", "validateHeader11Value")16 .validateHeader("validateHeader12", "validateHeader12Value")17 .validateHeader("validateHeader13", "validateHeader13Value")18 .validateHeader("validateHeader14", "validateHeader14Value")19 .validateHeader("validateHeader15", "validateHeader15Value")20 .validateHeader("validateHeader16", "validateHeader16Value")21 .validateHeader("validateHeader17", "validateHeader17Value")22 .validateHeader("validateHeader18", "validateHeader18Value")23 .validateHeader("validateHeader19", "validateHeader19Value")24 .validateHeader("validateHeader20", "validateHeader20Value")25 .validateHeader("validateHeader21", "validateHeader21Value")26 .validateHeader("validateHeader22", "validateHeader22Value")27 .validateHeader("validateHeader23", "validateHeader23Value")28 .validateHeader("validateHeader24", "validateHeader24Value")29 .validateHeader("validateHeader25", "validateHeader25Value")30 .validateHeader("validateHeader26", "validateHeader26Value")31 .validateHeader("validateHeader27", "validateHeader27Value")32 .validateHeader("validateHeader28", "validateHeader28Value")33 .validateHeader("validateHeader29", "validateHeader29Value")34 .validateHeader("validateHeader30", "validateHeader30Value")35 .validateHeader("validateHeader31", "
HeaderValidationContext
Using AI Code Generation
1public void testReceiveMessageBuilderWithHeaderValidationContext() {2 run(new TestRunner() {3 public void execute() {4 variable("correlationId", "1234567890");5 receive("myQueue")6 .headerValidationContext(new HeaderValidationContext() {7 public void validateHeader(String name, Object value, Message message) {8 if (name.equals("correlationId")) {9 assertEquals("1234567890", value);10 }11 }12 });13 }14 });15}16public void testReceiveMessageBuilderWithHeaderValidationContext() {17 run(new TestRunner() {18 public void execute() {19 variable("correlationId", "1234567890");20 receive("myQueue")21 .headerValidationContext((name, value, message) -> {22 if (name.equals("correlationId")) {23 assertEquals("1234567890", value);24 }25 });26 }27 });28}29public void testReceiveMessageBuilderWithHeaderValidationContext() {30 run(new TestRunner() {31 public void execute() {32 variable("correlationId", "1234567890");33 receive("myQueue")34 .headerValidationContext((name, value, message) -> {35 if (name.equals("correlationId")) {36 assertEquals("1234567890", value);37 }38 });39 }40 });41}42public void testReceiveMessageBuilderWithHeaderValidationContext() {43 run(new TestRunner() {44 public void execute() {45 variable("correlationId", "1234567890");46 receive("myQueue")47 .headerValidationContext((name, value, message) -> {48 if (name.equals("correlationId")) {49 assertEquals("1234567890", value);50 }51 });52 }53 });54}55public void testReceiveMessageBuilderWithHeaderValidationContext() {
HeaderValidationContext
Using AI Code Generation
1package com.consol.citrus.dsl.design;2import com.consol.citrus.dsl.builder.HeaderValidationContext;3import com.consol.citrus.dsl.builder.HttpActionBuilder;4import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder;5import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder.HttpResponseActionBuilder;6import com.consol.citrus.dsl.builder.ReceiveMessageBuilder;7import com.consol.citrus.dsl.builder.SendMessageBuilder;8import com.consol.citrus.http.message.HttpMessage;9import com.consol.citrus.http.message.HttpMessageHeaders;10import com.consol.citrus.testng.CitrusParameters;11import org.testng.annotations.DataProvider;12import org.testng.annotations.Test;13import java.util.Collections;14import static com.consol.citrus.actions.EchoAction.Builder.echo;15import static com.consol.citrus.actions.FailAction.Builder.fail;16import static com.consol.citrus.actions.SendMessageAction.Builder.send;17import static com.consol.citrus.container.Sequence.Builder.sequential;18import static com.consol.citrus.dsl.builder.Builder.*;19import static com.consol.citrus.http.actions.HttpActionBuilder.http;20import static com.consol.citrus.http.actions.HttpServerResponseActionBuilder.httpServerResponse;21import static com.consol.citrus.http.actions.HttpServerResponseActionBuilder.httpServerResponseBuilder;22public class HttpJavaITest extends AbstractTestDesigner {23 public void httpGet() {24 http()25 .client("httpClient")26 .send()27 .get("/api/test");28 http()29 .server("httpServer")30 .receive()31 .get("/api/test");32 http()33 .server("httpServer")34 .send()35 .response(HttpStatus
HeaderValidationContext
Using AI Code Generation
1The `HeaderValidationContext` is used by the `header()` method of the `ReceiveMessageBuilder` and `SendMessageBuilder`:2The `HeaderValidationContext` is available in the `validation()` method of the `ReceiveMessageBuilder`:3The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `ReceiveMessageBuilder`:4The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `SendMessageBuilder`:5The `HeaderValidationContext` is available in the `validation()` method of the `SendMessageBuilder`:6The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `SendMessageBuilder`:7The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `SendMessageBuilder`:8The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `SendMessageBuilder`:9The `HeaderValidationContext` is available in the `header()` method of the `validation()` method of the `
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!!