How to use dictionary method of com.consol.citrus.dsl.builder.SendMessageBuilder class

Best Citrus code snippet using com.consol.citrus.dsl.builder.SendMessageBuilder.dictionary

Source:SendMessageBuilder.java Github

copy

Full Screen

...27import com.consol.citrus.validation.json.*;28import com.consol.citrus.validation.xml.XpathMessageConstructionInterceptor;29import com.consol.citrus.validation.xml.XpathPayloadVariableExtractor;30import com.consol.citrus.variable.MessageHeaderVariableExtractor;31import com.consol.citrus.variable.dictionary.DataDictionary;32import com.fasterxml.jackson.core.JsonProcessingException;33import com.fasterxml.jackson.databind.ObjectMapper;34import org.springframework.context.ApplicationContext;35import org.springframework.core.io.Resource;36import org.springframework.oxm.Marshaller;37import org.springframework.oxm.XmlMappingException;38import org.springframework.util.Assert;39import org.springframework.util.CollectionUtils;40import org.springframework.xml.transform.StringResult;41import java.io.IOException;42import java.nio.charset.Charset;43import java.util.Map;44/**45 * Action builder creates a send message action with several message payload and header46 * constructing build methods.47 * 48 * @author Christoph Deppisch49 * @since 2.350 */51public class SendMessageBuilder<A extends SendMessageAction, T extends SendMessageBuilder> extends AbstractTestActionBuilder<DelegatingTestAction<TestAction>> {52 /** Self reference for generics support */53 private final T self;54 /** Message type for this action builder */55 private String messageType = Citrus.DEFAULT_MESSAGE_TYPE;56 /** Variable extractors filled within this builder */57 private MessageHeaderVariableExtractor headerExtractor;58 private XpathPayloadVariableExtractor xpathExtractor;59 private JsonPathVariableExtractor jsonPathExtractor;60 /** Message constructing interceptor */61 private XpathMessageConstructionInterceptor xpathMessageConstructionInterceptor;62 private JsonPathMessageConstructionInterceptor jsonPathMessageConstructionInterceptor;63 /** Basic application context */64 private ApplicationContext applicationContext;65 /**66 * Default constructor with test action.67 * @param action68 */69 public SendMessageBuilder(A action) {70 this(new DelegatingTestAction(action));71 }72 /**73 * Default constructor.74 */75 public SendMessageBuilder() {76 this((A) new SendMessageAction());77 }78 /**79 * Constructor using delegate test action.80 * @param action81 */82 public SendMessageBuilder(DelegatingTestAction<TestAction> action) {83 super(action);84 this.self = (T) this;85 }86 /**87 * Sets the message endpoint to send messages to.88 * @param messageEndpoint89 * @return90 */91 public SendMessageBuilder endpoint(Endpoint messageEndpoint) {92 getAction().setEndpoint(messageEndpoint);93 return this;94 }95 /**96 * Sets the message endpoint uri to send messages to.97 * @param messageEndpointUri98 * @return99 */100 public SendMessageBuilder endpoint(String messageEndpointUri) {101 getAction().setEndpointUri(messageEndpointUri);102 return this;103 }104 /**105 * Sets the fork mode for this send action builder.106 * @param forkMode107 * @return108 */109 public T fork(boolean forkMode) {110 getAction().setForkMode(forkMode);111 return self;112 }113 114 /**115 * Sets the message instance to send.116 * @param message117 * @return118 */119 public T message(Message message) {120 StaticMessageContentBuilder staticMessageContentBuilder = StaticMessageContentBuilder.withMessage(message);121 staticMessageContentBuilder.setMessageHeaders(getMessageContentBuilder().getMessageHeaders());122 getAction().setMessageBuilder(staticMessageContentBuilder);123 return self;124 }125 /**126 * Sets the payload data on the message builder implementation.127 * @param payload128 * @return129 */130 protected void setPayload(String payload) {131 MessageContentBuilder messageContentBuilder = getMessageContentBuilder();132 if (messageContentBuilder instanceof PayloadTemplateMessageBuilder) {133 ((PayloadTemplateMessageBuilder) messageContentBuilder).setPayloadData(payload);134 } else if (messageContentBuilder instanceof StaticMessageContentBuilder) {135 ((StaticMessageContentBuilder) messageContentBuilder).getMessage().setPayload(payload);136 } else {137 throw new CitrusRuntimeException("Unable to set payload on message builder type: " + messageContentBuilder.getClass());138 }139 }140 141 /**142 * Sets the message name.143 * @param name144 * @return145 */146 public T name(String name) {147 getMessageContentBuilder().setMessageName(name);148 return self;149 }150 151 /**152 * Adds message payload data to this builder.153 * @param payload154 * @return155 */156 public T payload(String payload) {157 setPayload(payload);158 return self;159 }160 /**161 * Adds message payload resource to this builder.162 * @param payloadResource163 * @return164 */165 public T payload(Resource payloadResource) {166 return payload(payloadResource, FileUtils.getDefaultCharset());167 }168 /**169 * Adds message payload resource to this builder.170 * @param payloadResource171 * @param charset172 * @return173 */174 public T payload(Resource payloadResource, Charset charset) {175 try {176 setPayload(FileUtils.readToString(payloadResource, charset));177 } catch (IOException e) {178 throw new CitrusRuntimeException("Failed to read payload resource", e);179 }180 return self;181 }182 /**183 * Sets payload POJO object which is marshalled to a character sequence using the given object to xml mapper.184 * @param payload185 * @param marshaller186 * @return187 */188 public T payload(Object payload, Marshaller marshaller) {189 StringResult result = new StringResult();190 191 try {192 marshaller.marshal(payload, result);193 } catch (XmlMappingException e) {194 throw new CitrusRuntimeException("Failed to marshal object graph for message payload", e);195 } catch (IOException e) {196 throw new CitrusRuntimeException("Failed to marshal object graph for message payload", e);197 }198 199 setPayload(result.toString());200 return self;201 }202 /**203 * Sets payload POJO object which is mapped to a character sequence using the given object to json mapper.204 * @param payload205 * @param objectMapper206 * @return207 */208 public T payload(Object payload, ObjectMapper objectMapper) {209 try {210 setPayload(objectMapper.writer().writeValueAsString(payload));211 } catch (JsonProcessingException e) {212 throw new CitrusRuntimeException("Failed to map object graph for message payload", e);213 }214 return self;215 }216 /**217 * Sets payload POJO object which is marshalled to a character sequence using the default object to xml or object218 * to json mapper that is available in Spring bean application context.219 *220 * @param payload221 * @return222 */223 public T payloadModel(Object payload) {224 Assert.notNull(applicationContext, "Citrus application context is not initialized!");225 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {226 return payload(payload, applicationContext.getBean(Marshaller.class));227 } else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {228 return payload(payload, applicationContext.getBean(ObjectMapper.class));229 }230 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");231 }232 /**233 * Sets payload POJO object which is marshalled to a character sequence using the given object to xml mapper that234 * is accessed by its bean name in Spring bean application context.235 *236 * @param payload237 * @param mapperName238 * @return239 */240 public T payload(Object payload, String mapperName) {241 Assert.notNull(applicationContext, "Citrus application context is not initialized!");242 if (applicationContext.containsBean(mapperName)) {243 Object mapper = applicationContext.getBean(mapperName);244 if (Marshaller.class.isAssignableFrom(mapper.getClass())) {245 return payload(payload, (Marshaller) mapper);246 } else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {247 return payload(payload, (ObjectMapper) mapper);248 } else {249 throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));250 }251 }252 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");253 }254 /**255 * Adds message header name value pair to this builder's message sending action.256 * @param name257 * @param value258 */259 public T header(String name, Object value) {260 getMessageContentBuilder().getMessageHeaders().put(name, value);261 return self;262 }263 /**264 * Adds message headers to this builder's message sending action.265 * @param headers266 */267 public T headers(Map<String, Object> headers) {268 getMessageContentBuilder().getMessageHeaders().putAll(headers);269 return self;270 }271 /**272 * Adds message header data to this builder's message sending action. Message header data is used in SOAP273 * messages for instance as header XML fragment.274 * @param data275 */276 public T header(String data) {277 getMessageContentBuilder().getHeaderData().add(data);278 return self;279 }280 /**281 * Adds message header data as file resource to this builder's message sending action. Message header data is used in SOAP282 * messages for instance as header XML fragment.283 * @param resource284 */285 public T header(Resource resource) {286 return header(resource, FileUtils.getDefaultCharset());287 }288 /**289 * Adds message header data as file resource to this builder's message sending action. Message header data is used in SOAP290 * messages for instance as header XML fragment.291 * @param resource292 * @param charset293 */294 public T header(Resource resource, Charset charset) {295 try {296 getMessageContentBuilder().getHeaderData().add(FileUtils.readToString(resource, charset));297 } catch (IOException e) {298 throw new CitrusRuntimeException("Failed to read header resource", e);299 }300 return self;301 }302 /**303 * Sets header data POJO object which is marshalled to a character sequence using the given object to xml mapper.304 * @param model305 * @param marshaller306 * @return307 */308 public T headerFragment(Object model, Marshaller marshaller) {309 StringResult result = new StringResult();310 try {311 marshaller.marshal(model, result);312 } catch (XmlMappingException e) {313 throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);314 } catch (IOException e) {315 throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);316 }317 return header(result.toString());318 }319 /**320 * Sets header data POJO object which is mapped to a character sequence using the given object to json mapper.321 * @param model322 * @param objectMapper323 * @return324 */325 public T headerFragment(Object model, ObjectMapper objectMapper) {326 try {327 return header(objectMapper.writer().writeValueAsString(model));328 } catch (JsonProcessingException e) {329 throw new CitrusRuntimeException("Failed to map object graph for message header data", e);330 }331 }332 /**333 * Sets header data POJO object which is marshalled to a character sequence using the default object to xml or object334 * to json mapper that is available in Spring bean application context.335 *336 * @param model337 * @return338 */339 public T headerFragment(Object model) {340 Assert.notNull(applicationContext, "Citrus application context is not initialized!");341 if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {342 return headerFragment(model, applicationContext.getBean(Marshaller.class));343 } else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {344 return headerFragment(model, applicationContext.getBean(ObjectMapper.class));345 }346 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");347 }348 /**349 * Sets header data POJO object which is marshalled to a character sequence using the given object to xml mapper that350 * is accessed by its bean name in Spring bean application context.351 *352 * @param model353 * @param mapperName354 * @return355 */356 public T headerFragment(Object model, String mapperName) {357 Assert.notNull(applicationContext, "Citrus application context is not initialized!");358 if (applicationContext.containsBean(mapperName)) {359 Object mapper = applicationContext.getBean(mapperName);360 if (Marshaller.class.isAssignableFrom(mapper.getClass())) {361 return headerFragment(model, (Marshaller) mapper);362 } else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {363 return headerFragment(model, (ObjectMapper) mapper);364 } else {365 throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));366 }367 }368 throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");369 }370 /**371 * Sets a explicit message type for this send action.372 * @param messageType373 * @return374 */375 public T messageType(MessageType messageType) {376 messageType(messageType.name());377 return self;378 }379 /**380 * Sets a explicit message type for this send action.381 * @param messageType382 * @return383 */384 public T messageType(String messageType) {385 this.messageType = messageType;386 getAction().setMessageType(messageType);387 return self;388 }389 /**390 * Get message builder, if already registered or create a new message builder and register it391 *392 * @return the message builder in use393 */394 protected AbstractMessageContentBuilder getMessageContentBuilder() {395 if (getAction().getMessageBuilder() != null && getAction().getMessageBuilder() instanceof AbstractMessageContentBuilder) {396 return (AbstractMessageContentBuilder) getAction().getMessageBuilder();397 } else {398 PayloadTemplateMessageBuilder messageBuilder = new PayloadTemplateMessageBuilder();399 getAction().setMessageBuilder(messageBuilder);400 return messageBuilder;401 }402 }403 /**404 * Extract message header entry as variable before message is sent.405 * @param headerName406 * @param variable407 * @return408 */409 public T extractFromHeader(String headerName, String variable) {410 if (headerExtractor == null) {411 headerExtractor = new MessageHeaderVariableExtractor();412 getAction().getVariableExtractors().add(headerExtractor);413 }414 415 headerExtractor.getHeaderMappings().put(headerName, variable);416 return self;417 }418 419 /**420 * Extract message element via XPath or JSONPath from payload before message is sent.421 * @param path422 * @param variable423 * @return424 */425 public T extractFromPayload(String path, String variable) {426 if (JsonPathMessageValidationContext.isJsonPathExpression(path)) {427 getJsonPathVariableExtractor().getJsonPathExpressions().put(path, variable);428 } else {429 getXpathVariableExtractor().getXpathExpressions().put(path, variable);430 }431 return self;432 }433 /**434 * Adds XPath manipulating expression that evaluates to message payload before sending.435 * @param expression436 * @param value437 * @return438 */439 public T xpath(String expression, String value) {440 if (xpathMessageConstructionInterceptor == null) {441 xpathMessageConstructionInterceptor = new XpathMessageConstructionInterceptor();442 if (getAction().getMessageBuilder() != null) {443 (getAction().getMessageBuilder()).add(xpathMessageConstructionInterceptor);444 } else {445 PayloadTemplateMessageBuilder messageBuilder = new PayloadTemplateMessageBuilder();446 messageBuilder.getMessageInterceptors().add(xpathMessageConstructionInterceptor);447 getAction().setMessageBuilder(messageBuilder);448 }449 }450 xpathMessageConstructionInterceptor.getXPathExpressions().put(expression, value);451 return self;452 }453 /**454 * Adds JSONPath manipulating expression that evaluates to message payload before sending.455 * @param expression456 * @param value457 * @return458 */459 public T jsonPath(String expression, String value) {460 if (jsonPathMessageConstructionInterceptor == null) {461 jsonPathMessageConstructionInterceptor = new JsonPathMessageConstructionInterceptor();462 if (getAction().getMessageBuilder() != null) {463 (getAction().getMessageBuilder()).add(jsonPathMessageConstructionInterceptor);464 } else {465 PayloadTemplateMessageBuilder messageBuilder = new PayloadTemplateMessageBuilder();466 messageBuilder.getMessageInterceptors().add(jsonPathMessageConstructionInterceptor);467 getAction().setMessageBuilder(messageBuilder);468 }469 }470 jsonPathMessageConstructionInterceptor.getJsonPathExpressions().put(expression, value);471 return self;472 }473 /**474 * Creates new variable extractor and adds it to test action.475 */476 private XpathPayloadVariableExtractor getXpathVariableExtractor() {477 if (xpathExtractor == null) {478 xpathExtractor = new XpathPayloadVariableExtractor();479 getAction().getVariableExtractors().add(xpathExtractor);480 }481 return xpathExtractor;482 }483 /**484 * Creates new variable extractor and adds it to test action.485 */486 private JsonPathVariableExtractor getJsonPathVariableExtractor() {487 if (jsonPathExtractor == null) {488 jsonPathExtractor = new JsonPathVariableExtractor();489 getAction().getVariableExtractors().add(jsonPathExtractor);490 }491 return jsonPathExtractor;492 }493 /**494 * Sets the Spring bean application context.495 * @param applicationContext496 */497 public T withApplicationContext(ApplicationContext applicationContext) {498 this.applicationContext = applicationContext;499 return self;500 }501 /**502 * Sets explicit data dictionary for this receive action.503 * @param dictionary504 * @return505 */506 public T dictionary(DataDictionary dictionary) {507 getAction().setDataDictionary(dictionary);508 return self;509 }510 /**511 * Sets explicit data dictionary by name.512 * @param dictionaryName513 * @return514 */515 @SuppressWarnings("unchecked")516 public T dictionary(String dictionaryName) {517 Assert.notNull(applicationContext, "Citrus application context is not initialized!");518 DataDictionary dictionary = applicationContext.getBean(dictionaryName, DataDictionary.class);519 getAction().setDataDictionary(dictionary);520 return self;521 }522 /**523 * Provides access to receive message action delegate.524 * @return525 */526 protected SendMessageAction getAction() {527 return (SendMessageAction) action.getDelegate();528 }529}...

Full Screen

Full Screen

dictionary

Using AI Code Generation

copy

Full Screen

1.send().message()2.headers()3.dictionary()4.put("citrus_http_status_code", "200")5.put("citrus_http_reason_phrase", "OK")6.put("citrus_http_version", "HTTP/1.1")7.put("citrus_message_id", "1234567890")8.put("citrus_message_timestamp", "2016-10-03T11:15:30+01:00")9.put("citrus_message_type", "text/xml")10.put("citrus_message_version", "1.0")11.put("citrus_message_name", "MyMessage")12.put("citrus_message_prefix", "citrus")13.put("citrus_correlation_id", "0987654321")14.put("citrus_correlation_name", "MyCorrelation")15.put("citrus_correlation_prefix", "citrus")16.put("citrus_endpoint_name", "httpServerEndpoint")17.put("citrus_endpoint_timeout", "5000")18.put("citrus_endpoint_type", "server")19.put("citrus_endpoint_encoding", "UTF-8")20.put("citrus_endpoint_charset", "UTF-8")21.put("citrus_endpoint_protocol", "HTTP")22.put("citrus_endpoint_protocol_version", "1.1")23.put("citrus_endpoint_path", "/services")24.put("citrus_endpoint_host", "localhost")25.put("citrus_endpoint_port", "8080")26.put("citrus_endpoint_secure", "false")27.put("citrus_endpoint_authentication", "none")28.put("citrus_endpoint_username", "http")29.put("citrus_endpoint_password", "citrus")30.put("citrus_endpoint_requestMethod", "POST")31.put("citrus_endpoint_requestPath", "/services")32.put("citrus_endpoint_requestQuery", "")33.put("citrus_endpoint_requestFragment", "")34.put("citrus_endpoint_requestHeaders", "")35.put("citrus_endpoint_requestCookies", "")36.put("citrus_endpoint_requestContentLength", "0")37.put("citrus_endpoint_requestContentType", "text/plain")38.put("citrus_endpoint_requestCharacterEncoding", "UTF-8")39.put("citrus_endpoint_request

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful