View Javadoc

1   /*
2    * $Id: CxfCustomHttpHeaderTestCase.java 23030 2011-09-26 18:02:33Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.cxf;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.fail;
17  
18  import org.mule.api.MuleMessage;
19  import org.mule.api.config.MuleProperties;
20  import org.mule.api.context.notification.EndpointMessageNotificationListener;
21  import org.mule.api.context.notification.ServerNotification;
22  import org.mule.api.endpoint.InboundEndpoint;
23  import org.mule.context.notification.EndpointMessageNotification;
24  import org.mule.module.client.MuleClient;
25  import org.mule.tck.AbstractServiceAndFlowTestCase;
26  import org.mule.tck.junit4.rule.DynamicPort;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.concurrent.CountDownLatch;
34  
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.runners.Parameterized.Parameters;
38  
39  public class CxfCustomHttpHeaderTestCase extends AbstractServiceAndFlowTestCase implements EndpointMessageNotificationListener
40  {
41      protected String endpointAddress = null;
42      private List<MuleMessage> notificationMsgList = new ArrayList<MuleMessage>();
43      private CountDownLatch latch = null;
44  
45      @Rule
46      public DynamicPort dynamicPort = new DynamicPort("port1");
47  
48      public CxfCustomHttpHeaderTestCase(ConfigVariant variant, String configResources)
49      {
50          super(variant, configResources);
51      }
52  
53      @Parameters
54      public static Collection<Object[]> parameters()
55      {
56          return Arrays.asList(new Object[][]{
57              {ConfigVariant.SERVICE, "headers-conf-service.xml"},
58              {ConfigVariant.FLOW, "headers-conf-flow.xml"}
59          });
60      }      
61      
62      @Override
63      protected void doSetUp() throws Exception
64      {
65          latch = new CountDownLatch(2);
66          muleContext.registerListener(this);
67          MuleClient client = new MuleClient(muleContext);
68          endpointAddress = ((InboundEndpoint) client.getMuleContext().getRegistry()
69                          .lookupObject("cxfInbound")).getAddress() + "?method=onReceive";
70      }
71  
72      @Override
73      protected void doTearDown() throws Exception
74      {
75          muleContext.unregisterListener(this);
76      }
77  
78      @Test
79      public void testCxf() throws Exception
80      {
81          Object payload = new Object[]{"Test String"};
82          String myProperty = "myProperty";
83  
84          HashMap<String, String> props = new HashMap<String, String>();
85          props.put(MuleProperties.MULE_USER_PROPERTY, "alan");
86          props.put(MuleProperties.MULE_METHOD_PROPERTY, "onReceive");
87          props.put(myProperty, myProperty);
88  
89          MuleClient client = new MuleClient(muleContext);
90          MuleMessage reply = client.send("cxf:" + endpointAddress, payload, props);
91  
92          assertNotNull(reply);
93          assertNotNull(reply.getPayload());
94          assertEquals("Test String Received", reply.getPayloadAsString());
95  
96          // make sure all notifications have trickled in
97          Thread.sleep(3000);
98  
99          // make sure we received the notifications on cxf
100         assertEquals(2, notificationMsgList.size());
101 
102         // MULE_USER should be allowed in
103         assertEquals("alan", notificationMsgList.get(0).getOutboundProperty(MuleProperties.MULE_USER_PROPERTY));
104 
105         // mule properties should be removed
106         assertNull(notificationMsgList.get(0).getOutboundProperty(MuleProperties.MULE_IGNORE_METHOD_PROPERTY));
107 
108         // custom properties should be allowed in
109         assertEquals(myProperty, notificationMsgList.get(0).getOutboundProperty(myProperty));
110     }
111 
112     public void onNotification(ServerNotification notification)
113     {
114         if (notification instanceof EndpointMessageNotification)
115         {
116             String uri = ((EndpointMessageNotification) notification).getEndpoint();
117             if (endpointAddress.equals(uri))
118             {
119                 notificationMsgList.add((MuleMessage) notification.getSource());
120                 latch.countDown();
121             }
122         }
123         else
124         {
125             fail("invalid notification: " + notification);
126         }
127     }
128 }