How to use convertOutboundMessage method of com.consol.citrus.http.message.HttpMessageConverter class

Best Citrus code snippet using com.consol.citrus.http.message.HttpMessageConverter.convertOutboundMessage

Source:HttpMessageConverter.java Github

copy

Full Screen

...48 @Override49 public HttpEntity<?> convertOutbound(Message message,50 HttpEndpointConfiguration endpointConfiguration,51 TestContext context) {52 HttpMessage httpMessage = convertOutboundMessage(message);53 HttpHeaders httpHeaders = createHttpHeaders(httpMessage, endpointConfiguration);54 Object payload = httpMessage.getPayload();55 if (httpMessage.getStatusCode() != null) {56 return new ResponseEntity<>(payload, httpHeaders, httpMessage.getStatusCode());57 } else {58 for (Cookie cookie : httpMessage.getCookies()) {59 httpHeaders.add(60 HttpHeaders.COOKIE,61 cookie.getName() + "=" + context.replaceDynamicContentInString(cookie.getValue()));62 }63 }64 HttpMethod method = determineHttpMethod(endpointConfiguration, httpMessage);65 return createHttpEntity(httpHeaders, payload, method);66 }67 @Override68 public HttpMessage convertInbound(HttpEntity<?> message,69 HttpEndpointConfiguration endpointConfiguration,70 TestContext context) {71 Map<String, Object> mappedHeaders = endpointConfiguration.getHeaderMapper().toHeaders(message.getHeaders());72 HttpMessage httpMessage = new HttpMessage(extractMessageBody(message), convertHeaderTypes(mappedHeaders));73 for (Map.Entry<String, String> customHeader : getCustomHeaders(message.getHeaders(), mappedHeaders).entrySet()) {74 httpMessage.setHeader(customHeader.getKey(), customHeader.getValue());75 }76 if (message instanceof ResponseEntity<?>) {77 httpMessage.status(((ResponseEntity<?>) message).getStatusCode());78 // We've no information here about the HTTP Version in this context.79 // Because HTTP/2 is not supported anyways currently, this should be acceptable.80 httpMessage.version("HTTP/1.1");81 if (endpointConfiguration.isHandleCookies()) {82 httpMessage.setCookies(cookieConverter.convertCookies(message));83 }84 }85 return httpMessage;86 }87 @Override88 public void convertOutbound(HttpEntity externalMessage,89 Message internalMessage,90 HttpEndpointConfiguration endpointConfiguration,91 TestContext context) {92 throw new UnsupportedOperationException("HttpMessageConverter does not support predefined HttpEntity objects");93 }94 /**95 * Message headers consist of standard HTTP message headers and custom headers.96 * This method assumes that all header entries that were not initially mapped97 * by header mapper implementations are custom headers.98 *99 * @param httpHeaders all message headers in their pre nature.100 * @param mappedHeaders the previously mapped header entries (all standard headers).101 * @return The map of custom headers102 */103 private Map<String, String> getCustomHeaders(HttpHeaders httpHeaders, Map<String, Object> mappedHeaders) {104 Map<String, String> customHeaders = new HashMap<>();105 for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {106 if (!mappedHeaders.containsKey(header.getKey())) {107 customHeaders.put(header.getKey(), StringUtils.collectionToCommaDelimitedString(header.getValue()));108 }109 }110 return customHeaders;111 }112 /**113 * Checks for collection typed header values and convert them to comma delimited String.114 * We need this for further header processing e.g when forwarding headers to JMS queues.115 *116 * @param headers the http request headers.117 */118 private Map<String, Object> convertHeaderTypes(Map<String, Object> headers) {119 Map<String, Object> convertedHeaders = new HashMap<>();120 for (Map.Entry<String, Object> header : headers.entrySet()) {121 if (header.getValue() instanceof Collection<?>) {122 Collection<?> value = (Collection<?>)header.getValue();123 convertedHeaders.put(header.getKey(), StringUtils.collectionToCommaDelimitedString(value));124 } else if (header.getValue() instanceof MediaType) {125 convertedHeaders.put(header.getKey(), header.getValue().toString());126 } else {127 convertedHeaders.put(header.getKey(), header.getValue());128 }129 }130 return convertedHeaders;131 }132 /**133 * Creates HttpHeaders based on the outbound message and the endpoint configurations header mapper.134 * @param httpMessage The HttpMessage to copy the headers from135 * @param endpointConfiguration The endpoint configuration to get th header mapper from136 */137 private HttpHeaders createHttpHeaders(HttpMessage httpMessage,138 HttpEndpointConfiguration endpointConfiguration) {139 HttpHeaders httpHeaders = new HttpHeaders();140 endpointConfiguration141 .getHeaderMapper()142 .fromHeaders(143 new org.springframework.messaging.MessageHeaders(httpMessage.getHeaders()),144 httpHeaders);145 Map<String, Object> messageHeaders = httpMessage.getHeaders();146 for (Map.Entry<String, Object> header : messageHeaders.entrySet()) {147 if (!header.getKey().startsWith(MessageHeaders.PREFIX) &&148 !MessageHeaderUtils.isSpringInternalHeader(header.getKey()) &&149 !httpHeaders.containsKey(header.getKey())) {150 httpHeaders.add(header.getKey(), header.getValue().toString());151 }152 }153 if (httpHeaders.getFirst(HttpMessageHeaders.HTTP_CONTENT_TYPE) == null) {154 httpHeaders.add(HttpMessageHeaders.HTTP_CONTENT_TYPE, composeContentTypeHeaderValue(endpointConfiguration));155 }156 return httpHeaders;157 }158 /**159 *160 * @param endpointConfiguration The HttpEndpointConfiguration to get the default request method from161 * @param httpMessage The HttpMessage to override the default with if necessary162 * @return The HttpMethod of the message to send163 */164 private HttpMethod determineHttpMethod(HttpEndpointConfiguration endpointConfiguration,165 HttpMessage httpMessage) {166 HttpMethod method = endpointConfiguration.getRequestMethod();167 if (httpMessage.getRequestMethod() != null) {168 method = httpMessage.getRequestMethod();169 }170 return method;171 }172 /**173 * Composes a HttpEntity based on the given parameters174 * @param httpHeaders The headers to set175 * @param payload The payload to set176 * @param method The HttpMethod to use177 * @return The composed HttpEntitiy178 */179 private HttpEntity<?> createHttpEntity(HttpHeaders httpHeaders, Object payload, HttpMethod method) {180 if (httpMethodSupportsBody(method)) {181 return new HttpEntity<>(payload, httpHeaders);182 } else {183 return new HttpEntity<>(httpHeaders);184 }185 }186 /**187 * Converts the outbound Message object into a HttpMessage188 * @param message The message to convert189 * @return The converted message as HttpMessage190 */191 private HttpMessage convertOutboundMessage(Message message) {192 HttpMessage httpMessage;193 if (message instanceof HttpMessage) {194 httpMessage = (HttpMessage) message;195 } else {196 httpMessage = new HttpMessage(message);197 }198 return httpMessage;199 }200 /**201 * Determines whether the given message type supports a message body202 * @param method The HttpMethod to evaluate203 * @return Whether a message body is supported204 */205 private boolean httpMethodSupportsBody(HttpMethod method) {...

Full Screen

Full Screen

convertOutboundMessage

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.http.message;2import com.consol.citrus.exceptions.CitrusRuntimeException;3import com.consol.citrus.message.DefaultMessage;4import com.consol.citrus.message.Message;5import com.consol.citrus.message.MessageConverter;6import com.consol.citrus.message.MessageType;7import com.consol.citrus.util.FileUtils;8import com.consol.citrus.validation.json.JsonTextMessageValidator;9import com.consol.citrus.validation.xml.XmlMessageValidationContext;10import com.consol.citrus.validation.xml.XmlTextMessageValidator;11import org.slf4j.Logger

Full Screen

Full Screen

convertOutboundMessage

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.http.message;2import org.testng.Assert;3import org.testng.annotations.Test;4import com.consol.citrus.message.Message;5import com.consol.citrus.message.MessageType;6public class HttpMessageConverterTest {7 public void testConvertOutboundMessage() {8 HttpMessageConverter converter = new HttpMessageConverter();9 Message message = converter.convertOutboundMessage(null, null, null, null, null);10 Assert.assertEquals(message.getMessageType(), MessageType.PLAINTEXT);11 Assert.assertEquals(message.getPayload(String.class), "");12 message = converter.convertOutboundMessage(null, null, null, "Hello World!", null);13 Assert.assertEquals(message.getMessageType(), MessageType.PLAINTEXT);14 Assert.assertEquals(message.getPayload(String.class), "Hello World!");15 }16 public void testConvertInboundMessage() {17 HttpMessageConverter converter = new HttpMessageConverter();18 Message message = converter.convertInboundMessage(null, null, null, null, null, null);19 Assert.assertEquals(message.getMessageType(), MessageType.PLAINTEXT);20 Assert.assertEquals(message.getPayload(String.class), "");21 message = converter.convertInboundMessage(null, null, null, null, "Hello World!", null);22 Assert.assertEquals(message.getMessageType(), MessageType.PLAINTEXT);23 Assert.assertEquals(message.getPayload(String.class), "Hello World!");24 }25}26message.setHeader("Content-Type", "application/json");27message = converter.convertOutboundMessage(null, null, null, "Hello World!", null);28Assert.assertEquals(message.getMessageType(), MessageType.JSON);29Assert.assertEquals(message.getPayload(String.class), "Hello World!");

Full Screen

Full Screen

convertOutboundMessage

Using AI Code Generation

copy

Full Screen

1convertOutboundMessage("text/plain", "application/json", "UTF-8");2convertInboundMessage("application/json", "text/xml", "UTF-8");3convertInboundMessage("text/xml", "application/json", "UTF-8");4convertOutboundMessage("application/json", "text/xml", "UTF-8");5convertOutboundMessage("text/plain", "application/json", "UTF-8");6convertInboundMessage("application/json", "text/xml", "UTF-8");

Full Screen

Full Screen

convertOutboundMessage

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.design.TestDesigner2import com.consol.citrus.dsl.design.TestDesignerRunner3import com.consol.citrus.dsl.design.TestRunner4import com.consol.citrus.dsl.runner.TestRunnerSupport5import com.consol.citrus.dsl.runner.TestRunnerSupportRunner6import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner7import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner8import com.consol.citrus.dsl.testng.TestNGCitrusTestRunnerSupport9import com.consol.citrus.dsl.testng.TestNGTestRunner10import com.consol.citrus.dsl.testng.TestNGTestRunnerSupport11import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunner12import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupport13import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunner14import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunner15import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunner16import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunner17import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunnerRunner18import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunnerRunnerRunner19import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunnerRunnerRunnerRunner20import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunnerRunnerRunnerRunnerRunner21import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunnerRunnerRunnerRunnerRunnerRunnerRunnerRunner22import com.consol.citrus.dsl.testng.TestNGTestRunnerSupportRunnerSupportRunnerRunner

Full Screen

Full Screen

convertOutboundMessage

Using AI Code Generation

copy

Full Screen

1convertOutboundMessage(new HttpMessageConverter()2 .json()3 .charset(StandardCharsets.UTF_8)4 .indent(true)5 .objectMapper(objectMapper)6 .schemaValidation(true)7 .schemaValidationRootType(Flight.class)8 .schemaValidationSchema(schema)9 .schemaValidationErrorHandler(new LoggingSchemaValidationErrorHandler())10 .schemaValidationSchemaResolver(new ClassPathResource("xsd/flights.xsd")));11convertInboundMessage(new HttpMessageConverter()12 .json()13 .charset(StandardCharsets.UTF_8)14 .indent(true)15 .objectMapper(objectMapper)16 .schemaValidation(true)17 .schemaValidationRootType(Flight.class)18 .schemaValidationSchema(schema)19 .schemaValidationErrorHandler(new LoggingSchemaValidationErrorHandler())20 .schemaValidationSchemaResolver(new ClassPathResource("xsd/flights.xsd")));21convertOutboundMessage(new HttpMessageConverter()22 .xml()23 .charset(StandardCharsets.UTF_8)24 .indent(true)25 .schemaValidation(true)26 .schemaValidationRootType(Flight.class)27 .schemaValidationSchema(schema)28 .schemaValidationErrorHandler(new LoggingSchemaValidationErrorHandler())29 .schemaValidationSchemaResolver(new ClassPathResource("xsd/flights.xsd")));30convertInboundMessage(new HttpMessageConverter()31 .xml()32 .charset(StandardCharsets.UTF_8)33 .indent(true)34 .schemaValidation(true)35 .schemaValidationRootType(Flight.class)36 .schemaValidationSchema(schema)37 .schemaValidationErrorHandler(new LoggingSchemaValidationErrorHandler())38 .schemaValidationSchemaResolver(new ClassPathResource("xsd/flights.xsd")));39convertOutboundMessage(new HttpMessageConverter()40 .soap()41 .charset(StandardCharsets.UTF_8)42 .indent(true)

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