INT-1752 added test cases to validate 'rest-template' attribute support

This commit is contained in:
Oleg Zhurakousky
2011-01-29 11:34:26 -05:00
parent c4fe6f1c47
commit fc00a926c0
7 changed files with 68 additions and 13 deletions

View File

@@ -23,7 +23,7 @@ import org.springframework.beans.factory.xml.ParserContext;
* @author Oleg Zhurakousky
* @since 2.0.2
*/
public class HttpParsingUtils {
class HttpAdapterParsingUtils {
private static final String[] REST_TEMPLATE_ATTRIBUTES = {
"request-factory", "error-handler", "message-converters"

View File

@@ -50,8 +50,8 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){
HttpParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addPropertyReference("restTemplate", restTemplate);
HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addConstructorArgReference("restTemplate");
}
else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");

View File

@@ -48,12 +48,11 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
"org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler");
builder.addConstructorArgValue(element.getAttribute("url"));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){
HttpParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addPropertyReference("restTemplate", restTemplate);
HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addConstructorArgReference("restTemplate");
}
else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
@@ -83,8 +82,6 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-request-payload", "extractPayload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expected-response-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "request-timeout", "sendTimeout");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel", "outputChannel");
List<Element> uriVariableElements = DomUtils.getChildElementsByTagName(element, "uri-variable");
if (!CollectionUtils.isEmpty(uriVariableElements)) {

View File

@@ -94,7 +94,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private final Map<String, Expression> uriVariableExpressions = new HashMap<String, Expression>();
private volatile RestTemplate restTemplate = new RestTemplate();
private final RestTemplate restTemplate;
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
@@ -110,15 +110,20 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
* Create a handler that will send requests to the provided URI.
*/
public HttpRequestExecutingMessageHandler(String uri) {
this(uri, null);
}
/**
* Create a handler that will send requests to the provided URI using a provided RestTemplate
* @param uri
* @param resrTemplate
*/
public HttpRequestExecutingMessageHandler(String uri, RestTemplate restTemplate) {
Assert.hasText(uri, "URI is required");
this.restTemplate = (restTemplate == null ? new RestTemplate() : restTemplate);
this.restTemplate.getMessageConverters().add(0, new SerializingHttpMessageConverter());
this.uri = uri;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
/**
* Specify the {@link HttpMethod} for requests. The default method will be POST.
*/

View File

@@ -13,6 +13,10 @@
<si:channel id="requests"/>
<outbound-channel-adapter id="minimalConfig" url="http://localhost/test1" channel="requests"/>
<outbound-channel-adapter id="restTemplateConfig" url="http://localhost/test1" channel="requests" rest-template="restTemplate"/>
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
<outbound-channel-adapter id="fullConfig"
url="http://localhost/test2/{foo}"

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/integration/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<si:channel id="requests"/>
<outbound-channel-adapter id="minimalConfig" url="http://localhost/test1"
error-handler="testErrorHandler" rest-template="restTemplate" channel="requests"/>
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
<beans:bean id="testErrorHandler"
class="org.springframework.integration.http.config.HttpOutboundChannelAdapterParserTests$StubErrorHandler"/>
</beans:beans>

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.http.config;
import static junit.framework.Assert.assertNotSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -29,7 +30,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequestFactory;
@@ -37,10 +40,12 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
/**
* @author Mark Fisher
@@ -54,14 +59,23 @@ public class HttpOutboundChannelAdapterParserTests {
@Autowired @Qualifier("fullConfig")
private AbstractEndpoint fullConfigEndpoint;
@Autowired @Qualifier("restTemplateConfig")
private AbstractEndpoint restTemplateConfig;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private RestTemplate restTemplate;
@Test
public void minimalConfig() {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.minimalConfigEndpoint);
RestTemplate rTemplate =
TestUtils.getPropertyValue(this.minimalConfigEndpoint, "handler.restTemplate", RestTemplate.class);
assertNotSame(restTemplate, rTemplate);
HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(false, handlerAccessor.getPropertyValue("expectReply"));
@@ -115,6 +129,18 @@ public class HttpOutboundChannelAdapterParserTests {
assertTrue(ObjectUtils.containsElement(mappedRequestHeaders, "requestHeader1"));
assertTrue(ObjectUtils.containsElement(mappedRequestHeaders, "requestHeader2"));
}
@Test
public void restTemplateConfig() {
RestTemplate rTemplate =
TestUtils.getPropertyValue(this.restTemplateConfig, "handler.restTemplate", RestTemplate.class);
assertEquals(restTemplate, rTemplate);
}
@Test(expected=BeanDefinitionParsingException.class)
public void failWithRestTemplateAndRestAttributes() {
new ClassPathXmlApplicationContext("HttpOutboundChannelAdapterParserTests-fail-context.xml", this.getClass());
}
public static class StubErrorHandler implements ResponseErrorHandler {