|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.http.config; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; |
| 22 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; |
| 23 | +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; |
| 24 | + |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; |
| 31 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 32 | +import org.springframework.http.HttpMethod; |
| 33 | +import org.springframework.http.MediaType; |
| 34 | +import org.springframework.messaging.Message; |
| 35 | +import org.springframework.messaging.MessageChannel; |
| 36 | +import org.springframework.messaging.PollableChannel; |
| 37 | +import org.springframework.messaging.support.GenericMessage; |
| 38 | +import org.springframework.test.annotation.DirtiesContext; |
| 39 | +import org.springframework.test.context.ContextConfiguration; |
| 40 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 41 | +import org.springframework.test.web.client.MockRestServiceServer; |
| 42 | +import org.springframework.web.client.RestTemplate; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Oleg Zhurakousky |
| 46 | + * @author Artem Bilan |
| 47 | + * |
| 48 | + * see https://jira.springsource.org/browse/INT-2397 |
| 49 | + */ |
| 50 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 51 | +@ContextConfiguration |
| 52 | +@DirtiesContext |
| 53 | +public class HttpOutboundGatewayWithMethodExpressionTests { |
| 54 | + |
| 55 | + @Autowired |
| 56 | + private MessageChannel defaultChannel; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private MessageChannel requestChannel; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private PollableChannel replyChannel; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private RestTemplate restTemplate; |
| 66 | + |
| 67 | + private MockRestServiceServer mockServer; |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setup() { |
| 71 | + this.mockServer = MockRestServiceServer.createServer(this.restTemplate); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testDefaultMethod() throws Exception { |
| 76 | + this.mockServer.expect(requestTo("/testApps/httpMethod")) |
| 77 | + .andExpect(method(HttpMethod.POST)) |
| 78 | + .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN)); |
| 79 | + |
| 80 | + this.defaultChannel.send(new GenericMessage<String>("Hello")); |
| 81 | + Message<?> message = this.replyChannel.receive(5000); |
| 82 | + assertNotNull(message); |
| 83 | + assertEquals("POST", message.getPayload()); |
| 84 | + |
| 85 | + this.mockServer.verify(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testExplicitlySetMethod() throws Exception { |
| 90 | + this.mockServer.expect(requestTo("/testApps/httpMethod")) |
| 91 | + .andExpect(method(HttpMethod.GET)) |
| 92 | + .andRespond(withSuccess(HttpMethod.GET.name(), MediaType.TEXT_PLAIN)); |
| 93 | + |
| 94 | + this.requestChannel.send(new GenericMessage<String>("GET")); |
| 95 | + Message<?> message = replyChannel.receive(5000); |
| 96 | + assertNotNull(message); |
| 97 | + assertEquals("GET", message.getPayload()); |
| 98 | + |
| 99 | + this.mockServer.verify(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test(expected = BeanDefinitionParsingException.class) |
| 103 | + public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception { |
| 104 | + new ClassPathXmlApplicationContext( |
| 105 | + "http-outbound-gateway-with-httpmethod-expression-fail.xml", getClass()) |
| 106 | + .close(); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments