Search in sources :

Example 6 with MessageContext

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

the class WebServiceInboundGatewayJavaConfigTests method testWebServiceInboundGatewayJavaConfig.

@Test
public void testWebServiceInboundGatewayJavaConfig() throws Exception {
    MessageContext context = mock(MessageContext.class);
    SoapMessage request = mock(SoapMessage.class);
    SoapMessage response = mock(SoapMessage.class);
    SoapBody soapBody = mock(SoapBody.class);
    String input = "<hello/>";
    Source payloadSource = new StringSource(input);
    StringWriter output = new StringWriter();
    Result payloadResult = new StreamResult(output);
    when(context.getResponse()).thenReturn(response);
    when(response.getPayloadResult()).thenReturn(payloadResult);
    when(response.getSoapBody()).thenReturn(soapBody);
    when(context.getRequest()).thenReturn(request);
    when(request.getPayloadSource()).thenReturn(payloadSource);
    this.messageReceiver.receive(context);
    verify(soapBody).addServerOrReceiverFault(eq("503 Service Unavailable"), any(Locale.class));
    this.wsGateway.start();
    this.messageReceiver.receive(context);
    assertTrue(output.toString().endsWith(input));
    context = mock(MessageContext.class);
    request = mock(SoapMessage.class);
    payloadSource = new StringSource("<order/>");
    when(context.getRequest()).thenReturn(request);
    when(request.getPayloadSource()).thenReturn(payloadSource);
    this.messageReceiver.receive(context);
    Message<?> receive = this.webserviceRequestsQueue.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Element.class));
    Element order = (Element) receive.getPayload();
    assertEquals("order", order.getLocalName());
}
Also used : Locale(java.util.Locale) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) SoapBody(org.springframework.ws.soap.SoapBody) MessageContext(org.springframework.ws.context.MessageContext) StringSource(org.springframework.xml.transform.StringSource) Source(javax.xml.transform.Source) StringSource(org.springframework.xml.transform.StringSource) SoapMessage(org.springframework.ws.soap.SoapMessage) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) Test(org.junit.Test)

Example 7 with MessageContext

use of org.springframework.ws.context.MessageContext in project spring-integration-samples by spring-projects.

the class InboundGatewayTests method testSendAndReceive.

/**
 * Emulate the Spring WS MessageDispatcherServlet by calling the gateway
 * with a DOMSource object representing the payload of the original SOAP
 * 'echoRequest' message.  Expect an 'echoResponse' DOMSource object
 * to be returned in synchronous fashion, which the MessageDispatcherServlet
 * would in turn wrap in a SOAP envelope and return to the client.
 */
@Test
public void testSendAndReceive() throws Exception {
    String xml = "<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>";
    DomPoxMessageFactory messageFactory = new DomPoxMessageFactory();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new InputSource(new StringReader(xml)));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DomPoxMessage request = new DomPoxMessage(document, transformer, "text/xml");
    MessageContext messageContext = new DefaultMessageContext(request, messageFactory);
    gateway.invoke(messageContext);
    Object reply = messageContext.getResponse().getPayloadSource();
    assertThat(reply, is(instanceOf(DOMSource.class)));
    DOMSource replySource = (DOMSource) reply;
    Element element = (Element) replySource.getNode().getFirstChild();
    assertThat(element.getTagName(), equalTo("echoResponse"));
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) Element(org.w3c.dom.Element) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) Document(org.w3c.dom.Document) DomPoxMessage(org.springframework.ws.pox.dom.DomPoxMessage) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) DefaultMessageContext(org.springframework.ws.context.DefaultMessageContext) DefaultMessageContext(org.springframework.ws.context.DefaultMessageContext) MessageContext(org.springframework.ws.context.MessageContext) Test(org.junit.Test)

Aggregations

MessageContext (org.springframework.ws.context.MessageContext)7 Test (org.junit.Test)6 Transformer (javax.xml.transform.Transformer)3 DefaultMessageContext (org.springframework.ws.context.DefaultMessageContext)3 Properties (java.util.Properties)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 MessageHistory (org.springframework.integration.history.MessageHistory)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)2 WebServiceMessage (org.springframework.ws.WebServiceMessage)2 ClientInterceptorAdapter (org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter)2 DomPoxMessageFactory (org.springframework.ws.pox.dom.DomPoxMessageFactory)2 StringResult (org.springframework.xml.transform.StringResult)2 StringSource (org.springframework.xml.transform.StringSource)2 Element (org.w3c.dom.Element)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Locale (java.util.Locale)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1