1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.http.functional;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import org.mule.api.MuleEventContext;
19 import org.mule.api.MuleMessage;
20 import org.mule.tck.AbstractServiceAndFlowTestCase;
21 import org.mule.tck.functional.EventCallback;
22 import org.mule.tck.functional.FunctionalTestComponent;
23 import org.mule.tck.junit4.rule.DynamicPort;
24 import org.mule.transport.http.HttpConstants;
25
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.HttpMethod;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.NameValuePair;
35 import org.apache.commons.httpclient.methods.GetMethod;
36 import org.apache.commons.httpclient.methods.PostMethod;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runners.Parameterized.Parameters;
40
41 public class HttpPersistentQueueTestCase extends AbstractServiceAndFlowTestCase
42 {
43 private CountDownLatch messageDidArrive = new CountDownLatch(1);
44 private int port = -1;
45
46 @Rule
47 public DynamicPort dynamicPort = new DynamicPort("port1");
48
49 public HttpPersistentQueueTestCase(ConfigVariant variant, String configResources)
50 {
51 super(variant, configResources);
52 }
53
54 @Parameters
55 public static Collection<Object[]> parameters()
56 {
57 return Arrays.asList(new Object[][]{
58 {ConfigVariant.SERVICE, "http-persistent-queue-service.xml"},
59 {ConfigVariant.FLOW, "http-persistent-queue-flow.xml"}
60 });
61 }
62
63 @Override
64 protected void doSetUp() throws Exception
65 {
66 super.doSetUp();
67
68 FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("PersistentQueueAsync");
69 assertNotNull(testComponent);
70 testComponent.setEventCallback(new Callback(messageDidArrive));
71 port = dynamicPort.getNumber();
72 }
73
74 @Test
75 public void testPersistentMessageDeliveryWithGet() throws Exception
76 {
77 GetMethod method = new GetMethod("http://localhost:" + port + "/services/Echo?foo=bar");
78 method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
79 doTestPersistentMessageDelivery(method);
80 }
81
82 @Test
83 public void testPersistentMessageDeliveryWithPost() throws Exception
84 {
85 PostMethod method = new PostMethod("http://localhost:" + port + "/services/Echo");
86 method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
87 method.addParameter(new NameValuePair("foo", "bar"));
88 doTestPersistentMessageDelivery(method);
89 }
90
91 private void doTestPersistentMessageDelivery(HttpMethod httpMethod) throws Exception
92 {
93 HttpClient client = new HttpClient();
94 int rc = client.executeMethod(httpMethod);
95
96 assertEquals(HttpStatus.SC_OK, rc);
97 assertTrue(messageDidArrive.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
98 }
99
100 private static class Callback implements EventCallback
101 {
102 private CountDownLatch messageDidArrive;
103
104 public Callback(CountDownLatch latch)
105 {
106 super();
107 messageDidArrive = latch;
108 }
109
110 public void eventReceived(MuleEventContext context, Object component) throws Exception
111 {
112 MuleMessage message = context.getMessage();
113
114 Object httpMethod = message.getInboundProperty("http.method");
115 if (HttpConstants.METHOD_GET.equals(httpMethod))
116 {
117 assertEquals("/services/Echo?foo=bar", message.getPayloadAsString());
118 }
119 else if (HttpConstants.METHOD_POST.equals(httpMethod))
120 {
121 assertEquals("foo=bar", message.getPayloadAsString());
122 }
123 else
124 {
125 fail("invalid HTTP method : " + httpMethod);
126 }
127
128 assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_CONNECTION));
129 assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_KEEP_ALIVE));
130
131 messageDidArrive.countDown();
132 }
133 }
134
135 }