From 527ec41f4663e714131b63e34463aa9b22fb8ff3 Mon Sep 17 00:00:00 2001 From: Iwein Fuld Date: Wed, 11 Mar 2009 11:46:54 +0000 Subject: [PATCH] IN PROGRESS - issue INT-162: Provide Spring-WS source adapters http://jira.springframework.org/browse/INT-162 Added tests, created INT-593 for silent delivery failure on timeout --- org.springframework.integration.ws/.classpath | 2 + org.springframework.integration.ws/ivy.xml | 5 +- .../ws/SimpleWebServiceInboundGateway.java | 3 + .../SimpleWebServiceInboundGatewayTests.java | 131 ++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 org.springframework.integration.ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java diff --git a/org.springframework.integration.ws/.classpath b/org.springframework.integration.ws/.classpath index 21945b94f9..3287c3bcf3 100644 --- a/org.springframework.integration.ws/.classpath +++ b/org.springframework.integration.ws/.classpath @@ -15,6 +15,8 @@ + + diff --git a/org.springframework.integration.ws/ivy.xml b/org.springframework.integration.ws/ivy.xml index ea88cf8bd4..6934ec9aa9 100644 --- a/org.springframework.integration.ws/ivy.xml +++ b/org.springframework.integration.ws/ivy.xml @@ -23,7 +23,6 @@ - @@ -31,6 +30,10 @@ + + + + \ No newline at end of file diff --git a/org.springframework.integration.ws/src/main/java/org/springframework/integration/ws/SimpleWebServiceInboundGateway.java b/org.springframework.integration.ws/src/main/java/org/springframework/integration/ws/SimpleWebServiceInboundGateway.java index 70019d476c..8de019dcbf 100644 --- a/org.springframework.integration.ws/src/main/java/org/springframework/integration/ws/SimpleWebServiceInboundGateway.java +++ b/org.springframework.integration.ws/src/main/java/org/springframework/integration/ws/SimpleWebServiceInboundGateway.java @@ -29,6 +29,7 @@ import org.w3c.dom.Document; import org.springframework.integration.core.Message; import org.springframework.integration.gateway.SimpleMessagingGateway; import org.springframework.integration.message.MessageBuilder; +import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.context.MessageContext; import org.springframework.ws.server.endpoint.MessageEndpoint; @@ -52,7 +53,9 @@ public class SimpleWebServiceInboundGateway extends SimpleMessagingGateway imple } public void invoke(MessageContext messageContext) throws Exception { + Assert.notNull(messageContext,"'messageContext' is required; it must not be null."); WebServiceMessage request = messageContext.getRequest(); + Assert.notNull(request, "Invalid message context: request was null."); MessageBuilder builder = MessageBuilder.withPayload( (this.extractPayload) ? request.getPayloadSource() : request); String[] propertyNames = messageContext.getPropertyNames(); diff --git a/org.springframework.integration.ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java b/org.springframework.integration.ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java new file mode 100644 index 0000000000..a98d3f6526 --- /dev/null +++ b/org.springframework.integration.ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java @@ -0,0 +1,131 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.ws; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.stubbing.Answer; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.core.Message; +import org.springframework.integration.core.MessageChannel; +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.context.MessageContext; + +/** + * + * @author Iwein Fuld + * + */ + +@RunWith(MockitoJUnit44Runner.class) +public class SimpleWebServiceInboundGatewayTests { + + private SimpleWebServiceInboundGateway gateway = new SimpleWebServiceInboundGateway(); + + @Mock + private MessageContext context; + + @Mock + private WebServiceMessage request; + + @Mock + private WebServiceMessage response; + + @Mock + private MessageChannel requestChannel; + + private MessageChannel replyChannel = new DirectChannel(); + + private String input = ""; + + private Source payloadSource = new StreamSource(new StringReader(input)); + + private StringWriter output = new StringWriter(); + + private Result payloadResult = new StreamResult(output); + + @Before + public void setup() { + gateway.setRequestChannel(requestChannel); + gateway.setReplyChannel(replyChannel); + when(context.getResponse()).thenReturn(response); + when(response.getPayloadResult()).thenReturn(payloadResult); + when(context.getRequest()).thenReturn(request); + } + + @Test + public void invokePoxSourceWithReply() throws Exception { + when(requestChannel.send(isA(Message.class))).thenAnswer( + withReplyTo(replyChannel)); + when(request.getPayloadSource()).thenReturn(payloadSource); + gateway.invoke(context); + verify(requestChannel).send(messageWithPayload(payloadSource)); + assertTrue(output.toString().endsWith(input)); + } + + @Test(timeout = 5000) + public void invokePoxSourceTimeout() throws Exception { + //this is tells the story of a message silently dropped on timeout see INT-593 + gateway.setRequestTimeout(10); + gateway.setReplyTimeout(10); + when(requestChannel.send(isA(Message.class), anyLong())).thenReturn(false); + when(request.getPayloadSource()).thenReturn(payloadSource); + gateway.invoke(context); + verify(requestChannel).send(messageWithPayload(payloadSource), + anyLong()); + verify(requestChannel, never()).send(isA(Message.class)); + } + + private Message messageWithPayload(final Object payload) { + return argThat(new BaseMatcher>() { + + public boolean matches(Object candidate) { + return ((Message) candidate).getPayload().equals(payload); + } + + public void describeTo(Description description) { + description.appendText("A message with payload: " + payload); + } + }); + } + + private Answer withReplyTo(final MessageChannel replyChannel) { + return new Answer() { + public Boolean answer(InvocationOnMock invocation) throws Throwable { + replyChannel.send((Message) invocation.getArguments()[0]); + return true; + } + }; + } +}