INT-1111. Added support for enriching headers when mapping gateway methods

This commit is contained in:
Oleg Zhurakousky
2010-05-01 21:45:55 +00:00
parent 4b0ab1cf2f
commit 31970fcf11
9 changed files with 273 additions and 64 deletions

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration">
<int:gateway id="gateway"
service-interface="org.springframework.integration.gateway.HeaderEnrichedGatewayTests$SampleGateway">
<int:method name="sendString" request-channel="input">
<int:header name="foo" value="#{stringValue}"/>
<int:header name="bar" value="bar"/>
</int:method>
<int:method name="sendInteger" request-channel="input">
<int:header name="foo" value="foo"/>
<int:header name="bar" value="bar"/>
</int:method>
<int:method name="sendStringWithParameterHeaders" request-channel="input">
<int:header name="foo" value="foo"/>
<int:header name="bar" value="bar"/>
</int:method>
</int:gateway>
<bean id="stringValue" class="java.lang.String">
<constructor-arg value="foo"/>
</bean>
<bean id="foo" class="org.springframework.integration.gateway.HeaderEnrichedGatewayTests$Foo">
<property name="bar" value="#{stringValue}"/>
</bean>
<int:channel id="input"/>
</beans>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration">
<int:gateway id="faildGateway"
service-interface="org.springframework.integration.gateway.HeaderEnrichedGatewayTests$SampleGateway">
<int:method name="sendString" request-channel="input">
<int:header name="foo" value="foo"/>
<int:header name="$bar" value="bar"/>
</int:method>
</int:gateway>
<int:channel id="input"/>
</beans>

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2002-2010 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.gateway;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.message.MessageHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HeaderEnrichedGatewayTests {
@Autowired
@Qualifier("gateway")
private SampleGateway gateway;
@Autowired
@Qualifier("input")
private DirectChannel input;
private Object testPayload;
@Test
public void validateStaticHeaderMappings() throws Exception {
MessageHandler handler = Mockito.mock(MessageHandler.class);
input.subscribe(handler);
this.prepareHandlerForTest(handler);
testPayload = "hello";
gateway.sendString((String) testPayload);
Mockito.verify(handler, Mockito.times(1)).handleMessage(Mockito.any(Message.class));
this.prepareHandlerForTest(handler);
testPayload = 123;
gateway.sendInteger((Integer) testPayload);
Mockito.verify(handler, Mockito.times(1)).handleMessage(Mockito.any(Message.class));
this.prepareHandlerForTest(handler);
testPayload = "withAnnotatedHeaders";
gateway.sendStringWithParameterHeaders((String) testPayload, "headerA", "headerB");
Mockito.verify(handler, Mockito.times(1)).handleMessage(Mockito.any(Message.class));
}
@Test(expected=BeanDefinitionStoreException.class)
public void validateFailedGatewayHeaders() throws Exception {
new ClassPathXmlApplicationContext("HeaderEnrichedGatewayTests-failed-context.xml", HeaderEnrichedGatewayTests.class);
}
public static class Foo{
private String bar;
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
/*
*
*/
@SuppressWarnings("unchecked")
private void prepareHandlerForTest(MessageHandler handler){
Mockito.reset(handler);
Mockito.doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Message message = (Message) invocation.getArguments()[0];
assertEquals(testPayload, message.getPayload());
assertEquals("foo", message.getHeaders().get("foo"));
assertEquals("bar", message.getHeaders().get("bar"));
assertNull(message.getHeaders().get(MessageHeaders.PREFIX + "baz"));
if (message.getPayload().equals("withAnnotatedHeaders")){
assertEquals("headerA", message.getHeaders().get("headerA"));
assertEquals("headerB", message.getHeaders().get("headerB"));
}
return null;
}})
.when(handler).handleMessage(Mockito.any(Message.class));
}
/*
*
*/
public static interface SampleGateway {
public void sendString(String value);
public void sendInteger(Integer value);
public void sendStringWithParameterHeaders(String value,
@Header("headerA") String headerA, @Header("headerB") String headerB);
}
}

View File

@@ -28,6 +28,7 @@ import org.junit.Test;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Headers;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.handler.ArgumentArrayMessageMapper;
import org.springframework.integration.message.MessageBuilder;
@@ -189,6 +190,26 @@ public class ArgumentArrayMessageMapperToMessageTests {
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] { "abc", "def" });
}
@Test
public void toMessageWithPayloadAndStaticHeaders() throws Exception {
Method method = TestService.class.getMethod("sendPayload", String.class);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("foo", "foo");
headers.put("bar", "bar");
headers.put(MessageHeaders.PREFIX + "baz", "hello");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, headers);
Message<?> message = mapper.toMessage(new Object[] { "test" });
assertEquals("test", message.getPayload());
assertEquals("foo", message.getHeaders().get("foo"));
assertEquals("bar", message.getHeaders().get("bar"));
}
@Test(expected=IllegalArgumentException.class)
public void toMessageWithPayloadAndIllegalHeader() throws Exception {
Method method = TestService.class.getMethod("sendPayloadAndIllegalHeader", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test", "foo"});
}
private static interface TestService {
@@ -196,6 +217,8 @@ public class ArgumentArrayMessageMapperToMessageTests {
void sendPayload(String payload);
void sendPayloadAndHeader(String payload, @Header("foo") String foo);
void sendPayloadAndIllegalHeader(String payload, @Header(MessageHeaders.PREFIX + "foo") String foo);
void sendPayloadAndOptionalHeader(String payload, @Header(value="foo", required=false) String foo);