View Javadoc

1   /*
2    * $Id: BridgeTestCase.java 22419 2011-07-15 03:41:06Z dirk.olmes $
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.test.integration.construct;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  import org.mule.transport.NullPayload;
21  import org.mule.util.concurrent.Latch;
22  
23  import java.util.Collections;
24  import java.util.Map;
25  import java.util.concurrent.TimeUnit;
26  
27  import org.apache.commons.lang.RandomStringUtils;
28  import org.apache.commons.lang.math.RandomUtils;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class BridgeTestCase extends FunctionalTestCase
34  {
35      private MuleClient muleClient;
36  
37      public BridgeTestCase()
38      {
39          setDisposeContextPerClass(true);
40      }
41  
42      @Override
43      protected void doSetUp() throws Exception
44      {
45          super.doSetUp();
46          muleClient = new MuleClient(muleContext);
47      }
48  
49      @Override
50      protected String getConfigResources()
51      {
52          return "org/mule/test/integration/construct/bridge-config.xml";
53      }
54  
55      @Test
56      public void testSynchronous() throws Exception
57      {
58          doTestMathsService("vm://synchronous-bridge.in");
59      }
60  
61      @Test
62      public void testAsynchronous() throws Exception
63      {
64          final MuleMessage result = muleClient.send("vm://asynchronous-bridge.in", "foobar", null);
65          assertEquals(NullPayload.getInstance(), result.getPayload());
66      }
67  
68      @Test
69      public void testTransformers() throws Exception
70      {
71          doTestStringMassager("vm://transforming-bridge.in");
72      }
73  
74      @Test
75      public void testEndpointReferences() throws Exception
76      {
77          doTestMathsService("vm://endpoint-ref-bridge.in");
78      }
79  
80      @Test
81      public void testChildEndpoints() throws Exception
82      {
83          doTestMathsService("vm://child-endpoint-bridge.in");
84      }
85  
86      @Test
87      public void testExceptionHandler() throws Exception
88      {
89          doTestMathsService("vm://exception-bridge.in");
90      }
91  
92      @Test
93      public void testVmTransacted() throws Exception
94      {
95          doTestMathsService("vm://transacted-bridge.in");
96      }
97  
98      @Test
99      public void testInheritance() throws Exception
100     {
101         doTestMathsService("vm://concrete-child-bridge.in");
102     }
103 
104     @Test
105     public void testHeterogeneousTransports() throws Exception
106     {
107         doJmsBasedTest("jms://myDlq", "dlq-file-picker");
108     }
109 
110     @Test
111     public void testJmsTransactions() throws Exception
112     {
113         doJmsBasedTest("jms://myQueue", "topic-listener");
114     }
115 
116     @Test
117     public void testDynamicEndpoint() throws Exception
118     {
119         doTestMathsService("vm://child-dynamic-endpoint-bridge.in", Collections.singletonMap("bridgeTarget", "maths-service.in"));
120     }
121 
122     @Test
123     public void testDynamicAddress() throws Exception
124     {
125         doTestMathsService("vm://address-dynamic-endpoint-bridge.in", Collections.singletonMap("bridgeTarget", "maths-service.in"));
126     }
127 
128     private void doJmsBasedTest(final String jmsDestinationUri, final String ftcName) throws Exception, MuleException, InterruptedException
129     {
130         final FunctionalTestComponent ftc = getFunctionalTestComponent(ftcName);
131         final Latch latch = new Latch();
132         ftc.setEventCallback(new EventCallback()
133         {
134             @Override
135             public void eventReceived(final MuleEventContext context, final Object component) throws Exception
136             {
137                 latch.countDown();
138             }
139         });
140 
141         final String payload = RandomStringUtils.randomAlphabetic(10);
142         muleClient.dispatch(jmsDestinationUri, payload, null);
143         latch.await(getTestTimeoutSecs(), TimeUnit.SECONDS);
144         assertEquals(1, ftc.getReceivedMessagesCount());
145         assertEquals(payload, byteArrayOrStringtoString(ftc.getReceivedMessage(1)));
146     }
147 
148     private void doTestMathsService(final String url) throws MuleException
149     {
150         doTestMathsService(url, null);
151     }
152 
153     private void doTestMathsService(final String url, final Map<?, ?> messageProperties) throws MuleException
154     {
155         final int a = RandomUtils.nextInt(100);
156         final int b = RandomUtils.nextInt(100);
157         final int result = (Integer) muleClient.send(url, new int[] { a, b }, messageProperties).getPayload();
158         assertEquals(a + b, result);
159     }
160 
161     private void doTestStringMassager(final String url) throws Exception, MuleException
162     {
163         final String payload = RandomStringUtils.randomAlphabetic(10);
164         final String result = muleClient.send(url, payload.getBytes(), null).getPayloadAsString();
165         assertEquals(payload + "barbaz", result);
166     }
167 
168     private String byteArrayOrStringtoString(final Object o)
169     {
170         if (o instanceof String)
171         {
172             return (String) o;
173         }
174 
175         return new String((byte[]) o);
176     }
177 }