Best Citrus code snippet using com.consol.citrus.jms.message.JmsMessageConverter.convertOutbound
Source:JmsMessageConverter.java
...32 * @author Christoph Deppisch33 */34public class JmsMessageConverter implements MessageConverter<javax.jms.Message, javax.jms.Message, JmsEndpointConfiguration> {35 @Override36 public javax.jms.Message convertOutbound(Message message, JmsEndpointConfiguration endpointConfiguration, TestContext context) {37 Connection connection = null;38 Session session = null;39 try {40 connection = endpointConfiguration.getConnectionFactory().createConnection();41 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);42 return createJmsMessage(message, session, endpointConfiguration, context);43 } catch (JMSException e) {44 throw new CitrusRuntimeException("Failed to create JMS message", e);45 } finally {46 JmsUtils.closeSession(session);47 JmsUtils.closeConnection(connection);48 }49 }50 @Override51 public void convertOutbound(javax.jms.Message jmsMessage, Message message, JmsEndpointConfiguration endpointConfiguration, TestContext context) {52 Map<String, Object> headers = message.getHeaders();53 if (headers != null) {54 endpointConfiguration.getHeaderMapper().fromHeaders(new MessageHeaders(headers), jmsMessage);55 }56 }57 @Override58 public Message convertInbound(javax.jms.Message jmsMessage, JmsEndpointConfiguration endpointConfiguration, TestContext context) {59 if (jmsMessage == null) {60 return null;61 }62 try {63 MessageHeaders headers = endpointConfiguration.getHeaderMapper().toHeaders(jmsMessage);64 Object payload;65 if (jmsMessage instanceof TextMessage) {66 payload = ((TextMessage) jmsMessage).getText();67 } else if (jmsMessage instanceof BytesMessage) {68 byte[] bytes = new byte[(int) ((BytesMessage) jmsMessage).getBodyLength()];69 ((BytesMessage) jmsMessage).readBytes(bytes);70 payload = bytes;71 } else if (jmsMessage instanceof MapMessage) {72 Map<String, Object> map = new HashMap<String, Object>();73 Enumeration en = ((MapMessage) jmsMessage).getMapNames();74 while (en.hasMoreElements()) {75 String key = (String) en.nextElement();76 map.put(key, ((MapMessage) jmsMessage).getObject(key));77 }78 payload = map;79 } else if (jmsMessage instanceof ObjectMessage) {80 payload = ((ObjectMessage) jmsMessage).getObject();81 } else {82 payload = jmsMessage;83 }84 if (payload instanceof Message) {85 Message nestedMessage = (Message) payload;86 for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {87 if (!headerEntry.getKey().startsWith(com.consol.citrus.message.MessageHeaders.MESSAGE_PREFIX)) {88 nestedMessage.setHeader(headerEntry.getKey(), headerEntry.getValue());89 }90 }91 return nestedMessage;92 } else {93 JmsMessage message = new JmsMessage(payload);94 for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {95 if (!headerEntry.getKey().startsWith(com.consol.citrus.message.MessageHeaders.MESSAGE_PREFIX)) {96 message.setHeader(headerEntry.getKey(), headerEntry.getValue());97 }98 }99 return message;100 }101 } catch (JMSException e) {102 throw new CitrusRuntimeException("Failed to convert jms message", e);103 }104 }105 /**106 * Creates JMS message instance from internal message representation. According to message payload type the JMS session107 * creates related JMS message type such as TextMessage, MapMessage, ObjectMessage or BytesMessage.108 *109 * @param message110 * @param session111 * @param endpointConfiguration112 * @param context113 * @return114 */115 public javax.jms.Message createJmsMessage(Message message, Session session, JmsEndpointConfiguration endpointConfiguration, TestContext context) {116 try {117 Object payload = message.getPayload();118 javax.jms.Message jmsMessage;119 if (endpointConfiguration.isUseObjectMessages()) {120 jmsMessage = session.createObjectMessage(message);121 } else if (payload instanceof javax.jms.Message) {122 jmsMessage = (javax.jms.Message) payload;123 } else if (payload instanceof String) {124 jmsMessage = session.createTextMessage((String) payload);125 } else if (payload instanceof byte[]) {126 jmsMessage = session.createBytesMessage();127 ((BytesMessage)jmsMessage).writeBytes((byte[]) payload);128 } else if (payload instanceof Map) {129 jmsMessage = session.createMapMessage();130 Map<?, ?> map = ((Map) payload);131 for (Map.Entry entry : map.entrySet()) {132 if (!(entry.getKey() instanceof String)) {133 throw new CitrusRuntimeException("Cannot convert non-String key of type [" + entry.getKey() + "] to JMS MapMessage entry");134 }135 ((MapMessage)jmsMessage).setObject((String) entry.getKey(), entry.getValue());136 }137 } else if (payload instanceof Serializable) {138 jmsMessage = session.createObjectMessage((Serializable) payload);139 } else {140 throw new CitrusRuntimeException("Cannot convert object of type [" + payload + "] to JMS message. Supported message " +141 "payloads are: String, byte array, Map<String,?>, Serializable object.");142 }143 convertOutbound(jmsMessage, message, endpointConfiguration, context);144 return jmsMessage;145 } catch (JMSException e) {146 throw new CitrusRuntimeException("Failed to convert jms message", e);147 }148 }149}...
convertOutbound
Using AI Code Generation
1import com.consol.citrus.dsl.design.TestDesigner;2import com.consol.citrus.dsl.design.TestDesigner;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.message.builder.ObjectMappingPayloadBuilder;6import com.consol.citrus.ws.addressing.SoapAddressingHeaders;7import com.consol.citrus.ws.addressing.SoapAddressingHeadersBuilder;8import com.consol.citrus.ws.message.SoapMessage;9import com.consol.citrus.ws.message.SoapMessageHeaders;10import com.consol.citrus.ws.message.SoapMessageHeadersBuilder;11import com.consol.citrus.ws.server.WebServiceServer;12import com.consol.citrus.ws.server.WebServiceServerBuilder;13import org.springframework.beans.factory.annotation.Autowired;14import org.springframework.beans.factory.annotation.Qualifier;15import org.springframework.core.io.ClassPathResource;16import org.springframework.oxm.jaxb.Jaxb2Marshaller;17import org.springframework.xml.transform.StringSource;18import org.testng.annotations.Test;19import javax.xml.transform.Source;20import javax.xml.transform.stream.StreamSource;21import java.io.IOException;22import java.util.HashMap;23import java.util.Map;24import static com.consol.citrus.actions.SendMessageAction.Builder.send;25import static com.consol.citrus.actions.ReceiveMessageAction.Builder.receive;26import static com.consol.citrus.container.Sequence.Builder.sequential;27import static com.consol.citrus.dsl.builder.Builder.*;28import static com.consol.citrus.dsl.builder.Builder.echo;29import static com.consol.citrus.dsl.builder.Builder.variable;30import static com.consol.citrus.ws.actions.SoapActionBuilder.soap;31public class SoapServerIT extends TestNGCitrusTestDesigner {32 @Qualifier("jaxb2Marshaller")33 private Jaxb2Marshaller jaxb2Marshaller;34 public void testSoapServer() {35 WebServiceServerBuilder serverBuilder = WebServiceServerBuilder.webServiceServer();36 serverBuilder.autoStart(true);37 serverBuilder.endpointAdapter(webServiceEndpointAdapter());38 serverBuilder.interceptor(webServiceInterceptor());39 serverBuilder.interceptor(webServiceInterceptor());
convertOutbound
Using AI Code Generation
1import com.consol.citrus.dsl.endpoint.CitrusEndpoints;2import com.consol.citrus.message.MessageType;3import com.consol.citrus.ws.client.WebServiceClient;4import com.consol.citrus.ws.message.SoapMessage;5import com.consol.citrus.ws.message.converter.SoapMessageConverter;6import com.consol.citrus.ws.message.converter.SoapAttachmentMessageConverter;7import com.consol.citrus.ws.message.converter.SoapMessageConverter;8import com.consol.citrus
convertOutbound
Using AI Code Generation
1package com.consol.citrus.samples.jms;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;4import com.consol.citrus.dsl.runner.TestRunner;5import com.consol.citrus.jms.message.JmsMessageConverter;6import org.junit.Test;7import org.springframework.beans.factory.annotation.Autowired;8import org.springframework.beans.factory.annotation.Qualifier;9import org.springframework.jms.core.JmsTemplate;10import org.springframework.jms.core.MessageCreator;11import javax.jms.JMSException;12import javax.jms.Message;13import javax.jms.Session;14public class JmsConsumerIT extends JUnit4CitrusTestDesigner {15 @Qualifier("jmsTemplate")16 private JmsTemplate jmsTemplate;17 private JmsMessageConverter jmsMessageConverter;18 public void jmsConsumer() {19 variable("message", "Hello Citrus!");20 send("jms:queue:messages.in", "${message}");21 receive("jms:queue:messages.out")22 .messageType(String.class)23 .messageConverter(jmsMessageConverter)24 .payload("<Message>Hello Citrus!</Message>");25 }26 public void jmsConsumerWithJmsTemplate() {27 variable("message", "Hello Citrus!");28 jmsTemplate.send("messages.in", new
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!!