PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/http/src/test/java/org/switchyard/component/http/HttpGatewayTest.java

https://github.com/objectiser/switchyard-components
Java | 280 lines | 221 code | 37 blank | 22 comment | 0 complexity | e554339be85634679001f492d7ba6b97 MD5 | raw file
  1. /*
  2. * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors.
  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. * http://www.apache.org/licenses/LICENSE-2.0
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package org.switchyard.component.http;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.PrintStream;
  17. import java.net.InetSocketAddress;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import javax.xml.namespace.QName;
  21. import org.junit.After;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.Ignore;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.jboss.com.sun.net.httpserver.HttpContext;
  28. import org.jboss.com.sun.net.httpserver.HttpExchange;
  29. import org.jboss.com.sun.net.httpserver.HttpHandler;
  30. import org.jboss.com.sun.net.httpserver.HttpServer;
  31. import org.switchyard.Exchange;
  32. import org.switchyard.Message;
  33. import org.switchyard.ServiceDomain;
  34. import org.switchyard.component.http.composer.HttpRequestBindingData;
  35. import org.switchyard.component.http.composer.HttpComposition;
  36. import org.switchyard.component.http.composer.HttpContextMapper;
  37. import org.switchyard.component.http.config.model.HttpBindingModel;
  38. import org.switchyard.component.test.mixins.http.HTTPMixIn;
  39. import org.switchyard.config.model.ModelPuller;
  40. import org.switchyard.config.model.composite.CompositeModel;
  41. import org.switchyard.config.model.composite.CompositeReferenceModel;
  42. import org.switchyard.config.model.composite.CompositeServiceModel;
  43. import org.switchyard.metadata.BaseService;
  44. import org.switchyard.metadata.InOnlyOperation;
  45. import org.switchyard.metadata.InOnlyService;
  46. import org.switchyard.metadata.InOutOperation;
  47. import org.switchyard.metadata.InOutService;
  48. import org.switchyard.metadata.ServiceOperation;
  49. import org.switchyard.test.Invoker;
  50. import org.switchyard.test.MockHandler;
  51. import org.switchyard.test.SwitchYardRunner;
  52. import org.switchyard.test.SwitchYardTestCaseConfig;
  53. /**
  54. * Contains tests for HTTP Gateway.
  55. *
  56. * @author Magesh Kumar B <mageshbk@jboss.com> (C) 2012 Red Hat Inc.
  57. */
  58. @RunWith(SwitchYardRunner.class)
  59. @SwitchYardTestCaseConfig(mixins = HTTPMixIn.class)
  60. public class HttpGatewayTest {
  61. private static final String METHOD_NAME = "sayHello";
  62. private static final String INPUT = "magesh";
  63. private static final String OUTPUT = "response to " + INPUT;
  64. private static final QName STRING_QNAME = new QName("java:java.lang.String");
  65. private static ModelPuller<CompositeModel> _puller;
  66. private ServiceDomain _domain;
  67. private HTTPMixIn httpMixIn;
  68. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}SampleHttpConsumerService")
  69. private Invoker _consumerService;
  70. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}InvalidHttpConsumerService")
  71. private Invoker _consumerService2;
  72. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}AuthHttpConsumerService")
  73. private Invoker _consumerService3;
  74. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}NtlmHttpConsumerService")
  75. private Invoker _consumerService4;
  76. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}TimeoutHttpConsumerService")
  77. private Invoker _consumerService5;
  78. @org.switchyard.test.ServiceOperation("{urn:http:test:1.1}OneWayHttpConsumerService")
  79. private Invoker _inOnlyConsumerService;
  80. private InboundHandler _httpInbound;
  81. private InboundHandler _httpInbound2;
  82. private OutboundHandler _httpOutbound;
  83. private OutboundHandler _httpOutbound2;
  84. private OutboundHandler _httpOutbound3;
  85. private OutboundHandler _httpOutbound4;
  86. private OutboundHandler _httpOutbound5;
  87. private final MockHandler mockService = new MockHandler().forwardInToOut();
  88. @Before
  89. public void setUp() throws Exception {
  90. _puller = new ModelPuller<CompositeModel>();
  91. CompositeModel composite = _puller.pull("/HelloSwitchYard.xml", getClass());
  92. composite.assertModelValid();
  93. CompositeServiceModel compositeService = composite.getServices().get(0);
  94. HttpBindingModel config = (HttpBindingModel)compositeService.getBindings().get(0);
  95. // Massive hack for Test Runner. Register both a service and a reference binding.
  96. _domain.registerService(config.getServiceName(), new InOutService(), mockService);
  97. _domain.registerServiceReference(config.getServiceName(), new HelloInterface());
  98. _httpInbound = new InboundHandler(config, _domain);
  99. _httpInbound.start();
  100. compositeService = composite.getServices().get(1);
  101. HttpBindingModel config2 = (HttpBindingModel)compositeService.getBindings().get(0);
  102. _domain.registerService(config2.getServiceName(), new InOnlyService(), new MockHandler());
  103. _domain.registerServiceReference(config2.getServiceName(), new HelloInterface());
  104. _httpInbound2 = new InboundHandler(config2, _domain);
  105. _httpInbound2.start();
  106. CompositeReferenceModel compositeReference = composite.getReferences().get(0);
  107. HttpBindingModel configRef = (HttpBindingModel)compositeReference.getBindings().get(0);
  108. _httpOutbound = new OutboundHandler(configRef, null);
  109. _domain.registerService(configRef.getServiceName(), new HelloInterface(), _httpOutbound);
  110. _httpOutbound.start();
  111. compositeReference = composite.getReferences().get(1);
  112. HttpBindingModel configRef2 = (HttpBindingModel)compositeReference.getBindings().get(0);
  113. _httpOutbound2 = new OutboundHandler(configRef2, null);
  114. _domain.registerService(configRef2.getServiceName(), new HelloInterface(), _httpOutbound2);
  115. _httpOutbound2.start();
  116. compositeReference = composite.getReferences().get(2);
  117. HttpBindingModel configRef3 = (HttpBindingModel)compositeReference.getBindings().get(0);
  118. _httpOutbound3 = new OutboundHandler(configRef3, null);
  119. _domain.registerService(configRef3.getServiceName(), new HelloInterface(), _httpOutbound3);
  120. _httpOutbound3.start();
  121. compositeReference = composite.getReferences().get(3);
  122. HttpBindingModel configRef4 = (HttpBindingModel)compositeReference.getBindings().get(0);
  123. _httpOutbound4 = new OutboundHandler(configRef4, null);
  124. _domain.registerService(configRef4.getServiceName(), new HelloInterface(), _httpOutbound4);
  125. _httpOutbound4.start();
  126. compositeReference = composite.getReferences().get(4);
  127. HttpBindingModel configRef5 = (HttpBindingModel)compositeReference.getBindings().get(0);
  128. _httpOutbound5 = new OutboundHandler(configRef5, null);
  129. _domain.registerService(configRef5.getServiceName(), new HelloInterface(), _httpOutbound5);
  130. _httpOutbound5.start();
  131. }
  132. @After
  133. public void tearDown() throws Exception {
  134. _httpInbound.stop();
  135. _httpInbound2.stop();
  136. _httpOutbound.stop();
  137. _httpOutbound2.stop();
  138. _httpOutbound3.stop();
  139. _httpOutbound4.stop();
  140. _httpOutbound5.stop();
  141. }
  142. @Test
  143. public void httpGatewayServiceTest() throws Exception {
  144. String response = httpMixIn.sendString("http://localhost:8080/http", INPUT, HTTPMixIn.HTTP_POST);
  145. //Test CSR - Composing a Service Request
  146. Assert.assertEquals(String.class, mockService.getMessages().peek().getMessage().getContent().getClass());
  147. Assert.assertEquals(1, mockService.getMessages().size());
  148. Assert.assertEquals(INPUT, response);
  149. }
  150. @Test
  151. public void httpOneWayStatusTest() throws Exception {
  152. int status = httpMixIn.sendStringAndGetStatus("http://localhost:8080/oneway", INPUT, HTTPMixIn.HTTP_POST);
  153. Assert.assertEquals(202, status);
  154. }
  155. @Test
  156. public void httpGatewayReferenceTest() throws Exception {
  157. Message responseMsg = _consumerService.operation(METHOD_NAME).sendInOut(INPUT);
  158. //Test CRR - Composing an Reference Response
  159. Assert.assertEquals(String.class, responseMsg.getContent().getClass());
  160. Assert.assertEquals(INPUT, responseMsg.getContent());
  161. }
  162. @Test
  163. public void httpStatus() throws Exception {
  164. MockHandler handler = new MockHandler();
  165. Exchange ex = _consumerService.operation(METHOD_NAME).createExchange(handler);
  166. Message requestMsg = ex.createMessage().setContent(INPUT);
  167. requestMsg.getContext().setProperty("SomeRequestHeader", "BAR");
  168. ex.send(requestMsg);
  169. handler.waitForOKMessage();
  170. Assert.assertEquals(200, ex.getContext().getProperty(HttpContextMapper.HTTP_RESPONSE_STATUS).getValue());
  171. }
  172. @Test
  173. public void httpFault() throws Exception {
  174. MockHandler handler = new MockHandler();
  175. Exchange ex = _consumerService2.operation(METHOD_NAME).createExchange(handler);
  176. Message requestMsg = ex.createMessage().setContent(INPUT);
  177. ex.send(requestMsg);
  178. handler.waitForFaultMessage();
  179. Assert.assertEquals(404, ex.getContext().getProperty(HttpContextMapper.HTTP_RESPONSE_STATUS).getValue());
  180. }
  181. @Test
  182. public void httpTimeout() throws Exception {
  183. HttpServer httpServer = HttpServer.create(new InetSocketAddress(8090), 10);
  184. httpServer.setExecutor(null); // creates a default executor
  185. httpServer.start();
  186. HttpContext httpContext = httpServer.createContext("/forever", new HttpHandler() {
  187. public void handle(HttpExchange exchange) {
  188. try {
  189. Thread.sleep(10000);
  190. } catch (InterruptedException ie) {
  191. //Ignore
  192. }}});
  193. try {
  194. Message responseMsg = _consumerService5.operation(METHOD_NAME).sendInOut(INPUT);
  195. } catch (Exception e) {
  196. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  197. e.printStackTrace(new PrintStream(baos));
  198. Assert.assertTrue(baos.toString().contains("SocketTimeoutException: Read timed out"));
  199. }
  200. httpServer.stop(0);
  201. }
  202. @Test
  203. public void authentication() throws Exception {
  204. HttpServer httpServer = HttpServer.create(new InetSocketAddress(8100), 10);
  205. httpServer.setExecutor(null); // creates a default executor
  206. httpServer.start();
  207. HttpContext context = httpServer.createContext("/basic-secured-endpoint", new StandaloneHandler());
  208. context.setAuthenticator(new HttpBasicAuthenticator());
  209. Message responseMsg = _consumerService3.operation(METHOD_NAME).sendInOut(INPUT);
  210. Assert.assertEquals(OUTPUT, responseMsg.getContent(String.class));
  211. httpServer.stop(0);
  212. }
  213. @Ignore // Exclusively for Magesh ;)
  214. @Test
  215. public void ntlmAuthentication() throws Exception {
  216. Message responseMsg = _consumerService4.operation(METHOD_NAME).sendInOut(INPUT);
  217. Assert.assertEquals(OUTPUT, responseMsg.getContent(String.class));
  218. }
  219. private static class HelloInterface extends BaseService {
  220. private static Set<ServiceOperation> _operations = new HashSet<ServiceOperation>(2);
  221. static {
  222. _operations.add(new InOutOperation(METHOD_NAME, STRING_QNAME, STRING_QNAME));
  223. _operations.add(new InOnlyOperation("oneWay", STRING_QNAME));
  224. }
  225. public HelloInterface() {
  226. super(_operations);
  227. }
  228. }
  229. private static class StandaloneHandler implements HttpHandler {
  230. public void handle(HttpExchange exchange) {
  231. try {
  232. HttpRequestBindingData httpRequest = new HttpRequestBindingData();
  233. httpRequest.setBodyFromStream(exchange.getRequestBody());
  234. Assert.assertEquals(INPUT, httpRequest.getBodyAsString());
  235. httpRequest.setBody(OUTPUT);
  236. exchange.sendResponseHeaders(200, httpRequest.getBodyBytes().available());
  237. httpRequest.writeBodyToStream(exchange.getResponseBody());
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. }
  243. }