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

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

Source:CitrusWebSocketDispatcherServletTest.java Github

copy

Full Screen

...18import com.consol.citrus.endpoint.adapter.TimeoutProducingEndpointAdapter;19import com.consol.citrus.http.controller.HttpMessageController;20import com.consol.citrus.http.interceptor.DelegatingHandlerInterceptor;21import com.consol.citrus.http.interceptor.LoggingHandlerInterceptor;22import com.consol.citrus.http.message.HttpMessageConverter;23import com.consol.citrus.testng.AbstractTestNGUnitTest;24import com.consol.citrus.websocket.endpoint.WebSocketEndpoint;25import com.consol.citrus.websocket.endpoint.WebSocketEndpointConfiguration;26import com.consol.citrus.websocket.handler.CitrusWebSocketHandler;27import com.consol.citrus.websocket.handler.WebSocketUrlHandlerMapping;28import com.consol.citrus.websocket.server.WebSocketServer;29import org.eclipse.jetty.server.Server;30import org.eclipse.jetty.server.handler.ContextHandler;31import org.eclipse.jetty.util.DecoratedObjectFactory;32import org.eclipse.jetty.webapp.WebAppContext;33import org.mockito.Mockito;34import org.springframework.beans.factory.annotation.Autowired;35import org.springframework.context.support.GenericApplicationContext;36import org.springframework.web.socket.server.support.DefaultHandshakeHandler;37import org.testng.Assert;38import org.testng.annotations.BeforeClass;39import org.testng.annotations.Test;40import java.util.*;41import static org.mockito.Mockito.*;42/**43 * @author Christoph Deppisch44 * @since 2.345 */46public class CitrusWebSocketDispatcherServletTest extends AbstractTestNGUnitTest {47 private WebSocketServer httpServer = Mockito.mock(WebSocketServer.class);48 private CitrusWebSocketDispatcherServlet servlet;49 @Autowired50 private HttpMessageController httpMessageController;51 @Autowired52 private DelegatingHandlerInterceptor handlerInterceptor;53 @Autowired54 private WebSocketUrlHandlerMapping urlHandlerMapping;55 @Autowired56 private DefaultHandshakeHandler handshakeHandler;57 @BeforeClass58 public void setUp() {59 servlet = new CitrusWebSocketDispatcherServlet(httpServer);60 }61 @Test62 public void testNoBeansInContext() throws Exception {63 reset(httpServer);64 when(httpServer.getWebSockets()).thenReturn(new ArrayList<WebSocketEndpoint>());65 GenericApplicationContext applicationContext = new GenericApplicationContext();66 applicationContext.refresh();67 servlet.initStrategies(applicationContext);68 }69 @Test70 public void testConfigureHandlerInterceptor() throws Exception {71 List<Object> interceptors = new ArrayList<Object>();72 interceptors.add(new LoggingHandlerInterceptor());73 reset(httpServer);74 when(httpServer.getInterceptors()).thenReturn(interceptors);75 when(httpServer.getEndpointAdapter()).thenReturn(null);76 when(httpServer.getMessageConverter()).thenReturn(new HttpMessageConverter());77 when(httpServer.getWebSockets()).thenReturn(new ArrayList<WebSocketEndpoint>());78 servlet.initStrategies(applicationContext);79 Assert.assertEquals(handlerInterceptor.getInterceptors().size(), 1L);80 Assert.assertEquals(handlerInterceptor.getInterceptors().get(0), interceptors.get(0));81 Assert.assertNotNull(httpMessageController.getEndpointConfiguration().getMessageConverter());82 Assert.assertEquals(httpMessageController.getEndpointAdapter().getClass(), EmptyResponseEndpointAdapter.class);83 }84 @Test85 public void testConfigureMessageController() throws Exception {86 reset(httpServer);87 when(httpServer.getInterceptors()).thenReturn(null);88 when(httpServer.getEndpointAdapter()).thenReturn(new TimeoutProducingEndpointAdapter());89 when(httpServer.getMessageConverter()).thenReturn(new HttpMessageConverter());90 when(httpServer.getWebSockets()).thenReturn(new ArrayList<WebSocketEndpoint>());91 servlet.initStrategies(applicationContext);92 Assert.assertEquals(handlerInterceptor.getInterceptors().size(), 0L);93 Assert.assertEquals(httpMessageController.getEndpointAdapter().getClass(), TimeoutProducingEndpointAdapter.class);94 Assert.assertNotNull(httpMessageController.getEndpointConfiguration().getMessageConverter());95 }96 @Test97 public void testConfigureWebSockerHandler() throws Exception {98 WebSocketEndpoint wsEndpoint = Mockito.mock(WebSocketEndpoint.class);99 WebSocketEndpointConfiguration wsEndpointConfig = Mockito.mock(WebSocketEndpointConfiguration.class);100 String wsId = "wsId";101 String endpointUri = "someEndpointUri";102 List<WebSocketEndpoint> webSockets = new ArrayList<>();103 webSockets.add(wsEndpoint);104 WebAppContext.Context servletContext = Mockito.mock(WebAppContext.Context.class);105 ContextHandler contextHandler = Mockito.mock(ContextHandler.class);106 DecoratedObjectFactory objectFactory = Mockito.mock(DecoratedObjectFactory.class);107 reset(httpServer, servletContext, contextHandler);108 when(httpServer.getInterceptors()).thenReturn(null);109 when(httpServer.getEndpointAdapter()).thenReturn(null);110 when(httpServer.getMessageConverter()).thenReturn(new HttpMessageConverter());111 when(httpServer.getWebSockets()).thenReturn(webSockets);112 when(wsEndpoint.getEndpointConfiguration()).thenReturn(wsEndpointConfig);113 when(wsEndpoint.getName()).thenReturn(wsId);114 wsEndpoint.setWebSocketHandler(isA(CitrusWebSocketHandler.class));115 when(wsEndpointConfig.getEndpointUri()).thenReturn(endpointUri);116 when(servletContext.getContextHandler()).thenReturn(contextHandler);117 when(servletContext.getAttribute(DecoratedObjectFactory.ATTR)).thenReturn(objectFactory);118 when(contextHandler.getServer()).thenReturn(new Server());119 handshakeHandler.setServletContext(servletContext);120 servlet.initStrategies(applicationContext);121 Map<String, ?> urlMap = urlHandlerMapping.getUrlMap();122 Assert.assertEquals(urlMap.size(), 1);123 Assert.assertTrue(urlMap.containsKey(endpointUri));124 Assert.assertNotNull(urlMap.get(endpointUri));...

Full Screen

Full Screen

Source:CitrusDispatcherServlet.java Github

copy

Full Screen

...20import com.consol.citrus.http.interceptor.*;21import com.consol.citrus.http.message.DelegatingHttpEntityMessageConverter;22import com.consol.citrus.http.server.HttpServer;23import org.springframework.context.ApplicationContext;24import org.springframework.http.converter.HttpMessageConverter;25import org.springframework.integration.http.support.DefaultHttpHeaderMapper;26import org.springframework.util.AntPathMatcher;27import org.springframework.web.context.request.WebRequestInterceptor;28import org.springframework.web.servlet.DispatcherServlet;29import org.springframework.web.servlet.HandlerInterceptor;30import org.springframework.web.servlet.handler.MappedInterceptor;31import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;32import org.springframework.web.util.UrlPathHelper;33import java.util.ArrayList;34import java.util.List;35/**36 * Citrus dispatcher servlet extends Spring's message dispatcher servlet and just37 * adds optional configuration settings for default mapping strategies and so on.38 *39 * @author Christoph Deppisch40 * @since 1.441 */42public class CitrusDispatcherServlet extends DispatcherServlet {43 /** Http server hosting the servlet */44 private HttpServer httpServer;45 /** Default bean names used in default configuration */46 protected static final String LOGGING_INTERCEPTOR_BEAN_NAME = "citrusLoggingInterceptor";47 protected static final String HANDLER_INTERCEPTOR_BEAN_NAME = "citrusHandlerInterceptor";48 protected static final String MESSAGE_CONTROLLER_BEAN_NAME = "citrusHttpMessageController";49 protected static final String MESSAGE_CONVERTER_BEAN_NAME = "citrusHttpMessageConverter";50 /**51 * Default constructor using http server instance that52 * holds this servlet.53 * @param httpServer54 */55 public CitrusDispatcherServlet(HttpServer httpServer) {56 this.httpServer = httpServer;57 }58 @Override59 protected void initStrategies(ApplicationContext context) {60 super.initStrategies(context);61 configureHandlerInterceptor(context);62 configureMessageController(context);63 configureMessageConverter(context);64 }65 /**66 * Post process handler interceptors.67 * @param context68 */69 protected void configureHandlerInterceptor(ApplicationContext context) {70 if (context.containsBean(HANDLER_INTERCEPTOR_BEAN_NAME)) {71 DelegatingHandlerInterceptor handlerInterceptor = context.getBean(HANDLER_INTERCEPTOR_BEAN_NAME, DelegatingHandlerInterceptor.class);72 handlerInterceptor.setInterceptors(adaptInterceptors(httpServer.getInterceptors(), context));73 }74 }75 /**76 * Post process message controller.77 * @param context78 */79 protected void configureMessageController(ApplicationContext context) {80 if (context.containsBean(MESSAGE_CONTROLLER_BEAN_NAME)) {81 HttpMessageController messageController = context.getBean(MESSAGE_CONTROLLER_BEAN_NAME, HttpMessageController.class);82 EndpointAdapter endpointAdapter = httpServer.getEndpointAdapter();83 HttpEndpointConfiguration endpointConfiguration = new HttpEndpointConfiguration();84 endpointConfiguration.setMessageConverter(httpServer.getMessageConverter());85 endpointConfiguration.setHeaderMapper(DefaultHttpHeaderMapper.inboundMapper());86 endpointConfiguration.setHandleAttributeHeaders(httpServer.isHandleAttributeHeaders());87 endpointConfiguration.setHandleCookies(httpServer.isHandleCookies());88 endpointConfiguration.setDefaultStatusCode(httpServer.getDefaultStatusCode());89 messageController.setEndpointConfiguration(endpointConfiguration);90 if (endpointAdapter != null) {91 messageController.setEndpointAdapter(endpointAdapter);92 }93 }94 }95 /**96 * Post process message converter.97 * @param context98 */99 protected void configureMessageConverter(ApplicationContext context) {100 if (context.containsBean(MESSAGE_CONVERTER_BEAN_NAME)) {101 HttpMessageConverter messageConverter = context.getBean(MESSAGE_CONVERTER_BEAN_NAME, HttpMessageConverter.class);102 if (messageConverter instanceof DelegatingHttpEntityMessageConverter) {103 ((DelegatingHttpEntityMessageConverter) messageConverter).setBinaryMediaTypes(httpServer.getBinaryMediaTypes());104 }105 }106 }107 /**108 * Adapts object list to handler interceptors.109 * @param interceptors110 * @param context111 * @return112 */113 private List<HandlerInterceptor> adaptInterceptors(List<Object> interceptors, ApplicationContext context) {114 List<HandlerInterceptor> handlerInterceptors = new ArrayList<HandlerInterceptor>();115 if (context.containsBean(LOGGING_INTERCEPTOR_BEAN_NAME)) {...

Full Screen

Full Screen

Source:CitrusDispatcherServletTest.java Github

copy

Full Screen

...18import com.consol.citrus.endpoint.adapter.TimeoutProducingEndpointAdapter;19import com.consol.citrus.http.controller.HttpMessageController;20import com.consol.citrus.http.interceptor.DelegatingHandlerInterceptor;21import com.consol.citrus.http.interceptor.LoggingHandlerInterceptor;22import com.consol.citrus.http.message.HttpMessageConverter;23import com.consol.citrus.http.server.HttpServer;24import com.consol.citrus.testng.AbstractTestNGUnitTest;25import org.mockito.Mockito;26import org.springframework.beans.factory.annotation.Autowired;27import org.springframework.context.support.GenericApplicationContext;28import org.testng.Assert;29import org.testng.annotations.BeforeClass;30import org.testng.annotations.Test;31import java.util.ArrayList;32import java.util.List;33import static org.mockito.Mockito.*;34/**35 * @author Christoph Deppisch36 * @since 1.437 */38public class CitrusDispatcherServletTest extends AbstractTestNGUnitTest {39 private HttpServer httpServer = Mockito.mock(HttpServer.class);40 private CitrusDispatcherServlet servlet;41 @Autowired42 private HttpMessageController httpMessageController;43 @Autowired44 private DelegatingHandlerInterceptor handlerInterceptor;45 @BeforeClass46 public void setUp() {47 servlet = new CitrusDispatcherServlet(httpServer);48 }49 @Test50 public void testNoBeansInContext() throws Exception {51 reset(httpServer);52 GenericApplicationContext applicationContext = new GenericApplicationContext();53 applicationContext.refresh();54 servlet.initStrategies(applicationContext);55 }56 @Test57 public void testConfigureHandlerInterceptor() throws Exception {58 List<Object> interceptors = new ArrayList<Object>();59 interceptors.add(new LoggingHandlerInterceptor());60 reset(httpServer);61 when(httpServer.getInterceptors()).thenReturn(interceptors);62 when(httpServer.getEndpointAdapter()).thenReturn(null);63 when(httpServer.isHandleAttributeHeaders()).thenReturn(false);64 when(httpServer.isHandleCookies()).thenReturn(false);65 when(httpServer.getMessageConverter()).thenReturn(new HttpMessageConverter());66 servlet.initStrategies(applicationContext);67 Assert.assertEquals(handlerInterceptor.getInterceptors().size(), 2L);68 Assert.assertEquals(handlerInterceptor.getInterceptors().get(0).getClass(), LoggingHandlerInterceptor.class);69 Assert.assertEquals(handlerInterceptor.getInterceptors().get(1), interceptors.get(0));70 Assert.assertNotNull(httpMessageController.getEndpointConfiguration().getMessageConverter());71 Assert.assertFalse(httpMessageController.getEndpointConfiguration().isHandleAttributeHeaders());72 Assert.assertFalse(httpMessageController.getEndpointConfiguration().isHandleCookies());73 Assert.assertEquals(httpMessageController.getEndpointAdapter().getClass(), EmptyResponseEndpointAdapter.class);74 }75 @Test76 public void testConfigureMessageController() throws Exception {77 reset(httpServer);78 when(httpServer.getInterceptors()).thenReturn(null);79 when(httpServer.getEndpointAdapter()).thenReturn(new TimeoutProducingEndpointAdapter());80 when(httpServer.isHandleAttributeHeaders()).thenReturn(true);81 when(httpServer.isHandleCookies()).thenReturn(true);82 when(httpServer.getMessageConverter()).thenReturn(new HttpMessageConverter());83 servlet.initStrategies(applicationContext);84 Assert.assertEquals(handlerInterceptor.getInterceptors().size(), 1L);85 Assert.assertEquals(handlerInterceptor.getInterceptors().get(0).getClass(), LoggingHandlerInterceptor.class);86 Assert.assertEquals(httpMessageController.getEndpointAdapter().getClass(), TimeoutProducingEndpointAdapter.class);87 Assert.assertNotNull(httpMessageController.getEndpointConfiguration().getMessageConverter());88 Assert.assertTrue(httpMessageController.getEndpointConfiguration().isHandleAttributeHeaders());89 Assert.assertTrue(httpMessageController.getEndpointConfiguration().isHandleCookies());90 }91}...

Full Screen

Full Screen

HttpMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.http.message;2import java.io.IOException;3import java.io.InputStream;4import java.util.HashMap;5import java.util.Map;6import org.springframework.http.HttpHeaders;7import org.springframework.http.HttpInputMessage;8import org.springframework.http.HttpOutputMessage;9import org.springframework.http.MediaType;10import org.springframework.http.converter.HttpMessageConverter;11import org.springframework.http.converter.StringHttpMessageConverter;12import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;13import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;14import org.springframework.util.FileCopyUtils;15public class HttpMessageConverter {16 public static void main(String[] args) throws IOException {17 StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();18 Jaxb2RootElementHttpMessageConverter jaxbConverter = new Jaxb2RootElementHttpMessageConverter();19 MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();20 HttpMessageConverter[] converters = new HttpMessageConverter[] { stringConverter, jaxbConverter,21 jacksonConverter };22 HttpInputMessage inputMessage = new HttpInputMessage() {23 public HttpHeaders getHeaders() {24 HttpHeaders headers = new HttpHeaders();25 headers.setContentType(MediaType.APPLICATION_JSON);26 return headers;27 }28 public InputStream getBody() throws IOException {29 return this.getClass().getResourceAsStream("/com/consol/citrus/http/message/input.json");30 }31 };32 HttpOutputMessage outputMessage = new HttpOutputMessage() {33 public HttpHeaders getHeaders() {34 HttpHeaders headers = new HttpHeaders();35 headers.setContentType(MediaType.APPLICATION_JSON);36 return headers;37 }38 public OutputStream getBody() throws IOException {39 return new ByteArrayOutputStream();40 }41 };42 Object object = null;43 for (HttpMessageConverter converter : converters) {44 if (converter.canRead(Object.class, inputMessage.getHeaders().getContentType())) {45 object = converter.read(Object.class, inputMessage);46 break;

Full Screen

Full Screen

HttpMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.http;2import org.springframework.http.MediaType;3import org.springframework.http.converter.FormHttpMessageConverter;4import org.springframework.http.converter.HttpMessageConverter;5import org.springframework.http.converter.StringHttpMessageConverter;6import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;7import org.springframework.http.converter.xml.SourceHttpMessageConverter;8import org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter;9import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;10import org.springframework.util.Assert;11import org.springframework.util.CollectionUtils;12import org.springframework.util.StringUtils;13import org.springframework.xml.transform.StringResult;14import org.springframework.xml.transform.StringSource;15import org.w3c.dom.Document;16import org.xml.sax.InputSource;17import org.xml.sax.SAXException;18import org.xml.sax.SAXParseException;19import javax.xml.bind.JAXBElement;20import javax.xml.bind.JAXBException;21import javax.xml.bind.Unmarshaller;22import javax.xml.bind.util.ValidationEventCollector;23import javax.xml.parsers.DocumentBuilder;24import javax.xml.parsers.DocumentBuilderFactory;25import javax.xml.parsers.ParserConfigurationException;26import javax.xml.transform.*;27import javax.xml.transform.dom.DOMSource;28import javax.xml.transform.sax.SAXSource;29import javax.xml.transform.stream.StreamSource;30import java.io.IOException;31import java.io.StringReader;32import java.util.*;33public class HttpMessageConverter {34 private List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();35 private String defaultCharset = "UTF-8";36 public HttpMessageConverter() {37 messageConverters.add(new StringHttpMessageConverter());38 messageConverters.add(new FormHttpMessageConverter());39 messageConverters.add(new XmlAwareFormHttpMessageConverter());40 messageConverters.add(new Jaxb2RootElementHttpMessageConverter());41 messageConverters.add(new SourceHttpMessageConverter());42 messageConverters.add(new MappingJackson2HttpMessageConverter());43 }44 public HttpMessageConverter(List<HttpMessageConverter<?>> messageConverters) {45 this.messageConverters = messageConverters;46 }47 public List<HttpMessageConverter<?>> getMessageConverters() {48 return messageConverters;49 }

Full Screen

Full Screen

HttpMessageConverter

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.http.message.HttpMessageConverter;2import com.consol.citrus.message.Message;3import com.consol.citrus.message.MessageType;4import com.consol.citrus.message.builder.DefaultMessageBuilder;5import java.io.IOException;6import java.util.HashMap;7import java.util.Map;8public class 3 {9 public static void main(String[] args) throws IOException {10 HttpMessageConverter messageConverter = new HttpMessageConverter();11 Map<String, Object> header = new HashMap<String, Object>();12 header.put("Content-Type", "application/json");13 header.put("Accept", "application/json");14 String body = "{\"name\":\"John\"}";15 Message message = messageConverter.convertOutbound(new DefaultMessageBuilder(body).setHeader(header).build(), MessageType.JSON);16 System.out.println(message.toString());17 }18}19import com.consol.citrus.http.message.HttpMessageConverter;20import com.consol.citrus.message.Message;21import com.consol.citrus.message.MessageType;22import com.consol.citrus.message.builder.DefaultMessageBuilder;23import java.io.IOException;24import java.util.HashMap;25import java.util.Map;26public class 4 {27 public static void main(String[] args) throws IOException {28 HttpMessageConverter messageConverter = new HttpMessageConverter();29 Map<String, Object> header = new HashMap<String, Object>();30 header.put("Content-Type", "application/xml");31 header.put("Accept", "application/xml");32 String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><name>John</name>";33 Message message = messageConverter.convertOutbound(new DefaultMessageBuilder(body).setHeader(header).build(), MessageType.XML);34 System.out.println(message.toString());35 }36}37import com.consol.citrus.http.message.HttpMessageConverter;38import com

Full Screen

Full Screen

HttpMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import org.testng.annotations.Test;3import com.consol.citrus.http.message.HttpMessageConverter;4import com.consol.citrus.http.message.HttpMessageConverter;5public class Test3 {6 public void test3() {7 String xml = "<root><name>John</name><age>25</age></root>";8 String json = HttpMessageConverter.convertXmlToJson(xml);9 System.out.println(json);10 }11}12{"root":{"name":"John","age":"25"}}13package com.consol.citrus;14import org.testng.annotations.Test;15import com.consol.citrus.http.message.HttpMessageConverter;16import com.consol.citrus.http.message.HttpMessageConverter;17public class Test4 {18 public void test4() {19 String json = "{\"root\":{\"name\":\"John\",\"age\":\"25\"}}";20 String xml = HttpMessageConverter.convertJsonToXml(json);21 System.out.println(xml);22 }23}24package com.consol.citrus;25import org.testng.annotations.Test;26import com.consol.citrus.http.message.HttpMessageConverter;27import com.consol.citrus.http.message.HttpMessageConverter;28public class Test5 {29 public void test5() {30 String xml = "<root><name>John</name><age>25</age></root>";31 String json = HttpMessageConverter.convertXmlToJson(xml);32 System.out.println(json);33 String xml1 = HttpMessageConverter.convertJsonToXml(json);34 System.out.println(xml1);35 }36}37{"root":{"name":"John","age":"25"}}38package com.consol.citrus;39import org.testng.annotations.Test

Full Screen

Full Screen

HttpMessageConverter

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.message.Message;2import com.consol.citrus.http.message.HttpMessageConverter;3import com.consol.citrus.http.message.HttpMessage;4public class 3 {5 public static void main(String[] args) {6 HttpMessageConverter httpMessageConverter = new HttpMessageConverter();7 HttpMessage httpMessage = new HttpMessage();8 httpMessage.setPayload("Hello");9 httpMessage.setHeader("Hello", "World");10 String message = httpMessageConverter.convertOutbound(httpMessage, Message.class);11 System.out.println(message);12 }13}

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