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
This commit is contained in:
Iwein Fuld
2009-03-11 11:46:54 +00:00
parent 630f10d036
commit 527ec41f46
4 changed files with 140 additions and 1 deletions

View File

@@ -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();

View File

@@ -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 = "<hello/>";
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<Message<?>>() {
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<Boolean> withReplyTo(final MessageChannel replyChannel) {
return new Answer<Boolean>() {
public Boolean answer(InvocationOnMock invocation) throws Throwable {
replyChannel.send((Message<?>) invocation.getArguments()[0]);
return true;
}
};
}
}