Search in sources :

Example 6 with WebServiceMessageFactory

use of org.springframework.ws.WebServiceMessageFactory in project spring-integration by spring-projects.

the class WebServiceOutboundGatewayWithHeaderMapperTests method process.

private Message<?> process(Object payload, String gatewayName, String channelName, final boolean soap) throws Exception {
    AbstractWebServiceOutboundGateway gateway = TestUtils.getPropertyValue(this.context.getBean(gatewayName), "handler", AbstractWebServiceOutboundGateway.class);
    if (!soap) {
        WebServiceTemplate template = TestUtils.getPropertyValue(gateway, "webServiceTemplate", WebServiceTemplate.class);
        template.setMessageFactory(new DomPoxMessageFactory());
    }
    WebServiceMessageSender messageSender = Mockito.mock(WebServiceMessageSender.class);
    WebServiceConnection wsConnection = Mockito.mock(WebServiceConnection.class);
    Mockito.when(messageSender.createConnection(Mockito.any(URI.class))).thenReturn(wsConnection);
    Mockito.when(messageSender.supports(Mockito.any(URI.class))).thenReturn(true);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        WebServiceMessage wsMessage = (WebServiceMessage) args[0];
        // }
        if (soap) {
            SoapHeader soapHeader = ((SoapMessage) wsMessage).getSoapHeader();
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("foo")));
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("foobar")));
            assertNotNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("abaz")));
            assertNull(soapHeader.getAttributeValue(QNameUtils.parseQNameString("bar")));
        }
        return null;
    }).when(wsConnection).send(Mockito.any(WebServiceMessage.class));
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        WebServiceMessageFactory factory = (WebServiceMessageFactory) args[0];
        String responseMessage = factory instanceof SoapMessageFactory ? responseSoapMessage : responseNonSoapMessage;
        WebServiceMessage wsMessage = factory.createWebServiceMessage(new ByteArrayInputStream(responseMessage.getBytes()));
        if (soap) {
            ((SoapMessage) wsMessage).getSoapHeader().addAttribute(QNameUtils.parseQNameString("bar"), "bar");
            ((SoapMessage) wsMessage).getSoapHeader().addAttribute(QNameUtils.parseQNameString("baz"), "baz");
        }
        // }
        return wsMessage;
    }).when(wsConnection).receive(Mockito.any(WebServiceMessageFactory.class));
    gateway.setMessageSender(messageSender);
    MessageChannel inputChannel = context.getBean(channelName, MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload(payload).setHeader("foo", "foo").setHeader("foobar", "foobar").setHeader("abaz", "abaz").setHeader("bar", "bar").setHeader(WebServiceHeaders.SOAP_ACTION, "someAction").build();
    inputChannel.send(message);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    return outputChannel.receive(0);
}
Also used : WebServiceMessageFactory(org.springframework.ws.WebServiceMessageFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate) URI(java.net.URI) SoapMessage(org.springframework.ws.soap.SoapMessage) WebServiceMessage(org.springframework.ws.WebServiceMessage) SoapMessageFactory(org.springframework.ws.soap.SoapMessageFactory) WebServiceMessageSender(org.springframework.ws.transport.WebServiceMessageSender) MessageChannel(org.springframework.messaging.MessageChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) SoapHeader(org.springframework.ws.soap.SoapHeader) AbstractWebServiceOutboundGateway(org.springframework.integration.ws.AbstractWebServiceOutboundGateway) WebServiceConnection(org.springframework.ws.transport.WebServiceConnection)

Example 7 with WebServiceMessageFactory

use of org.springframework.ws.WebServiceMessageFactory in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGatewayTests method createMockMessageSender.

public static WebServiceMessageSender createMockMessageSender(final String mockResponseMessage) throws Exception {
    WebServiceMessageSender messageSender = Mockito.mock(WebServiceMessageSender.class);
    WebServiceConnection wsConnection = Mockito.mock(WebServiceConnection.class);
    Mockito.when(messageSender.createConnection(Mockito.any(URI.class))).thenReturn(wsConnection);
    Mockito.when(messageSender.supports(Mockito.any(URI.class))).thenReturn(true);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        WebServiceMessageFactory factory = (WebServiceMessageFactory) args[0];
        return factory.createWebServiceMessage(new ByteArrayInputStream(mockResponseMessage.getBytes()));
    }).when(wsConnection).receive(Mockito.any(WebServiceMessageFactory.class));
    return messageSender;
}
Also used : WebServiceMessageSender(org.springframework.ws.transport.WebServiceMessageSender) WebServiceMessageFactory(org.springframework.ws.WebServiceMessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) WebServiceConnection(org.springframework.ws.transport.WebServiceConnection) URI(java.net.URI)

Aggregations

WebServiceMessageFactory (org.springframework.ws.WebServiceMessageFactory)7 Test (org.junit.Test)5 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 Marshaller (org.springframework.oxm.Marshaller)2 Unmarshaller (org.springframework.oxm.Unmarshaller)2 WebServiceMessage (org.springframework.ws.WebServiceMessage)2 WebServiceConnection (org.springframework.ws.transport.WebServiceConnection)2 WebServiceMessageSender (org.springframework.ws.transport.WebServiceMessageSender)2 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 AbstractWebServiceOutboundGateway (org.springframework.integration.ws.AbstractWebServiceOutboundGateway)1 MessageChannel (org.springframework.messaging.MessageChannel)1