1
2
3
4
5
6
7 package org.mule.module.cxf;
8
9 import org.mule.api.MuleMessage;
10 import org.mule.api.config.MuleProperties;
11 import org.mule.api.context.notification.EndpointMessageNotificationListener;
12 import org.mule.api.context.notification.ServerNotification;
13 import org.mule.api.endpoint.InboundEndpoint;
14 import org.mule.context.notification.EndpointMessageNotification;
15 import org.mule.module.client.MuleClient;
16 import org.mule.tck.junit4.FunctionalTestCase;
17 import org.mule.tck.junit4.rule.DynamicPort;
18
19 import java.util.HashMap;
20 import java.util.concurrent.CountDownLatch;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29
30 public class CxfCustomHttpHeaderTestCase extends FunctionalTestCase implements EndpointMessageNotificationListener
31 {
32 protected String endpointAddress = null;
33 private MuleMessage notificationMsg = null;
34 private CountDownLatch latch = null;
35
36 @Rule
37 public DynamicPort dynamicPort = new DynamicPort("port1");
38
39 @Override
40 protected String getConfigResources()
41 {
42 return "headers-conf.xml";
43 }
44
45 @Override
46 protected void doSetUp() throws Exception
47 {
48 latch = new CountDownLatch(1);
49 muleContext.registerListener(this);
50 MuleClient client = new MuleClient(muleContext);
51 endpointAddress = ((InboundEndpoint) client.getMuleContext().getRegistry()
52 .lookupObject("cxfInbound")).getAddress() + "?method=onReceive";
53 }
54
55 @Override
56 protected void doTearDown() throws Exception
57 {
58 muleContext.unregisterListener(this);
59 }
60
61 @Test
62 public void testCxf() throws Exception
63 {
64 Object payload = new Object[]{"Test String"};
65 String myProperty = "myProperty";
66
67 HashMap<String, String> props = new HashMap<String, String>();
68 props.put(MuleProperties.MULE_USER_PROPERTY, "alan");
69 props.put(MuleProperties.MULE_METHOD_PROPERTY, "onReceive");
70 props.put(myProperty, myProperty);
71
72 MuleClient client = new MuleClient(muleContext);
73 MuleMessage reply = client.send("cxf:" + endpointAddress, payload, props);
74
75 assertNotNull(reply);
76 assertNotNull(reply.getPayload());
77 assertEquals("Test String Received", reply.getPayloadAsString());
78
79
80 Thread.sleep(3000);
81
82
83 assertNotNull(notificationMsg);
84
85
86 assertEquals("alan", notificationMsg.getOutboundProperty(MuleProperties.MULE_USER_PROPERTY));
87
88
89 assertNull(notificationMsg.getOutboundProperty(MuleProperties.MULE_IGNORE_METHOD_PROPERTY));
90
91
92 assertEquals(myProperty, notificationMsg.getOutboundProperty(myProperty));
93 }
94
95 public void onNotification(ServerNotification notification)
96 {
97 if (notification instanceof EndpointMessageNotification)
98 {
99 String uri = ((EndpointMessageNotification) notification).getEndpoint();
100 if (endpointAddress.equals(uri))
101 {
102 notificationMsg = (MuleMessage) notification.getSource();
103 latch.countDown();
104 }
105 }
106 else
107 {
108 fail("invalid notification: " + notification);
109 }
110 }
111 }