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

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

Source:SendMessageBuilder.java Github

copy

Full Screen

...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 */...

Full Screen

Full Screen

jsonPath

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.builder.SendMessageBuilder2import com.consol.citrus.dsl.builder.HttpActionBuilder3import com.consol.citrus.dsl.builder.HttpClientActionBuilder4import com.consol.citrus.dsl.builder.HttpServerActionBuilder5import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder6import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder7import com.consol.citrus.dsl.builder.HttpServerActionBuilder8import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder9import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder10import com.consol.citrus.dsl.builder.HttpClientActionBuilder11import com.consol.citrus.dsl.builder.HttpActionBuilder12import com.consol.citrus.dsl.builder.SendMessageBuilder13import com.consol.citrus.dsl.builder.HttpServerActionBuilder14import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder15import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder16import com.consol.citrus.dsl.builder.HttpServerActionBuilder17import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder18import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder19import com.consol.citrus.dsl.builder.HttpClientActionBuilder20import com.consol.citrus.dsl.builder.HttpActionBuilder21import com.consol.citrus.dsl.builder.SendMessageBuilder22import com.consol.citrus.dsl.builder.HttpServerActionBuilder23import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder24import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder25import com.consol.citrus.dsl.builder.HttpServerActionBuilder26import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder27import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder28import com.consol.citrus.dsl.builder.HttpClientActionBuilder29import com.consol.citrus.dsl.builder.HttpActionBuilder30import com.consol.citrus.dsl.builder.SendMessageBuilder31import com.consol.citrus.dsl.builder.HttpServerActionBuilder32import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder33import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder34import com.consol.citrus.dsl.builder.HttpServerActionBuilder35import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder36import com.consol.c

Full Screen

Full Screen

jsonPath

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.builder.SendMessageBuilder2import com.consol.citrus.dsl.runner.TestRunner3import com.consol.citrus.dsl.runner.TestRunnerSupport4import com.consol.citrus.message.MessageType5import static com.consol.citrus.actions.EchoAction.Builder.echo6class JsonPathTest {7 void test() {8 runner().http(action -> action.client("httpClient")9 .send()10 .post()11 .messageType(MessageType.JSON)12 .payload("{\"id\":\"123456789\",\"name\":\"citrus\"}")13 .header("Content-Type", "application/json")14 .jsonPath(action -> action.variable("citrusId", "$.id")15 .variable("citrusName", "$.name")16 .jsonPath("$.id")17 .jsonPath("$.name")18 .echo(echo -> echo.message("Id: ${citrusId}"))19 .echo(echo -> echo.message("Name: ${citrusName}"))20 }21 private TestRunner runner() {22 return new TestRunnerSupport()23 }24}

Full Screen

Full Screen

jsonPath

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.builder.SendMessageBuilder2import com.consol.citrus.dsl.runner.TestRunner3import com.consol.citrus.dsl.runner.TestRunnerSupport4import org.springframework.http.HttpStatus5import org.springframework.http.MediaType6import static com.consol.citrus.http.actions.HttpActionBuilder.http7class JsonPathTest implements TestRunnerSupport {8 def void testJsonPath() {9 runner().http(builder -> builder.client("httpClient")10 .send()11 .post()12 .contentType(MediaType.APPLICATION_JSON_VALUE)13 .payload("{\"name\": \"citrus\", \"id\": 12345}")14 .header("operation", "sayHello")15 .header("citrus_http_method", "POST")16 .header("citrus_http_path", "/test")17 .header("citrus_http_query", "param1=value1&param2=value2")18 .header("citrus_http_version", "HTTP/1.1")19 .receive()20 .response(HttpStatus.OK)21 .messageType(MessageType.PLAINTEXT)22 .payload("{\"responseMessage\": \"Hello citrus\"}")23 .extractFromPayload("$..responseMessage", "responseMessage")24 }25 TestRunner runner() {26 return Citrus.newInstance().createTestRunner()27 }28}29import com.consol.citrus.dsl.builder.HttpActionBuilder30import com.consol.citrus.dsl.runner.TestRunner31import com.consol.citrus.dsl.runner.TestRunnerSupport32import org.springframework.http.HttpStatus33import org.springframework.http.MediaType34import static com.consol.citrus.http.actions.HttpActionBuilder.http35class JsonPathTest implements TestRunnerSupport {36 def void testJsonPath() {37 runner().http(builder -> builder.client("httpClient")38 .send()39 .post()40 .contentType(MediaType.APPLICATION_JSON_VALUE)41 .payload("{\"name\": \"citrus\", \"id\": 12345}")42 .header("operation", "sayHello")43 .header("citrus_http_method", "POST")44 .header("citrus_http_path", "/test")45 .header("citrus_http_query", "param1=value1&param2=value2")46 .header("citrus_http_version", "HTTP/1.1")47 .receive()48 .response(HttpStatus.OK)

Full Screen

Full Screen

jsonPath

Using AI Code Generation

copy

Full Screen

1 .payload("{\"message\":\"Hello Citrus!\"}")2 .header("operation", "greeting")3 .header("citrus_jms_messageSelector", "operation = 'greeting'")4 .header("citrus_jms_replyDestination", "jms:queue:replyQueue")5 .extractFromPayload("$..[?(@.message == 'Hello Citrus!')]", "extractedMessage");6 .payload("{\"message\":\"Hello Citrus!\"}")7 .header("operation", "greeting")8 .header("citrus_jms_messageSelector", "operation = 'greeting'")9 .header("citrus_jms_replyDestination", "jms:queue:replyQueue")10 .payload("{\"message\":\"Hello Citrus!\"}")11 .header("operation", "greeting")12 .header("citrus_jms_messageSelector", "operation = 'greeting'")13 .header("citrus_jms_replyDestination", "jms:queue:replyQueue")14send(soap()15 .client("citrus:soapClient")16 .header("operation", "greeting")17 .header("citrus_jms_messageSelector", "operation = 'greeting'")18 .header("citrus_jms_replyDestination", "jms:queue:replyQueue"))19receive(soap

Full Screen

Full Screen

jsonPath

Using AI Code Generation

copy

Full Screen

1 .payload("{\"name\": \"Apple\", \"color\": \"Red\"}")2 .header("Content-Type", "application/json")3 .extractFromPayload("$.id", "id");4 .payload("{\"name\": \"Apple\", \"color\": \"Red\"}")5 .header("Content-Type", "application/json")6 .extractFromPayload("$.name", "name");7 .payload("{\"name\": \"Apple\", \"color\": \"Red\"}")8 .header("Content-Type", "application/json")9 .extractFromPayload("$.color", "color");10 .payload("{\"name\": \"Apple\", \"color\": \"Red\"}")11 .header("Content-Type", "application/json")12 .extractFromPayload("$.price", "price");13 .payload("{\"name\": \"Apple\", \"color\": \"Red\"}")14 .header("Content-Type", "application/json")15 .extractFromPayload("$.quantity", "quantity");16 .payload("{\"name\": \"Orange\", \"color\": \"Orange\"}")17 .header("Content-Type", "application/json")18 .extractFromPayload("$.id", "id");

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