View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.issues;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  
19  public class ResponseTransformerMule2165TestCase extends FunctionalTestCase
20  {
21  
22      public static final long TIMEOUT = 2000L;
23      public static final String MESSAGE = "a message";
24      // i don't know if this is the "correct" response - it's *one* of the responses in 1.4,
25      // and it seems vaguely reasonable.
26  
27      /* RM
28      Described as:
29      1. Client dispatch = "outbound"
30      2. First Service receiver = "inbound"
31      3. First Service dispatch (first endpoint) = "outbound"
32      4. Second Service receiver = "inbound"
33      5. Response transformer from SecondComponent = "response"
34      Note that because the response transformer is configured locally on the outbound endppoint it only gets called once
35      */
36      public static final String LOCAL_RESPONSE = MESSAGE + " outbound inbound outbound inbound response";
37      // an alternative in 1.4 is " outbound outbound response response" for the global
38      // transformers, which also makes some sense
39      /* RM
40      Described as:
41      1. Client dispatch = "outbound"
42      2. First Service receiver = "inbound"
43      3. First Service dispatch (first endpoint) = "outbound"
44      4. Second Service receiver = "inbound"
45      5. Response transformer from SecondComponent = "response"
46      6. Response from outbound endpoint (to the service) = "response"
47      Note that because the global outbound inpoint is also the inbound endpoint of the bounce service
48      The "response" ResponseTransformer gets called twice
49      */
50      public static final String GLOBAL_RESPONSE = LOCAL_RESPONSE + " response";
51  
52      @Override
53      protected String getConfigResources()
54      {
55          return "issues/response-transformer-mule-2165-test.xml";
56      }
57  
58      protected MuleClient send(String endpoint) throws MuleException
59      {
60          MuleClient client = new MuleClient(muleContext);
61          client.dispatch(endpoint, MESSAGE, null);
62          return client;
63      }
64  
65      protected MuleClient dispatch(String endpoint) throws MuleException
66      {
67          MuleClient client = new MuleClient(muleContext);
68          client.dispatch(endpoint, MESSAGE, null);
69          return client;
70      }
71  
72      protected String request(MuleClient client, String endpoint) throws Exception
73      {
74          MuleMessage message = client.request(endpoint, TIMEOUT);
75          assertNotNull("no response from " + endpoint, message);
76          assertNotNull(message.getPayloadAsString());
77          return message.getPayloadAsString();
78      }
79  
80      protected void testTransformered(String endpoint, String response) throws Exception
81      {
82          String message = request(send("in-" + endpoint), "out-" + endpoint);
83          assertEquals("bad response (" + message + ")  for " + endpoint, response, message);
84      }
85  
86      protected void testTransformeredAsync(String endpoint, String response) throws Exception
87      {
88          String message = request(dispatch("in-" + endpoint), "out-" + endpoint);
89          assertEquals("bad response (" + message + ")  for " + endpoint, response, message);
90      }
91  
92      @Test
93      public void testGlobalNameGlobalTransformer() throws Exception
94      {
95          testTransformered("global-name-global-transformer", GLOBAL_RESPONSE);
96      }
97  
98      @Test
99      public void testGlobalNameUrlTransformer() throws Exception
100     {
101         testTransformered("global-name-url-transformer", GLOBAL_RESPONSE);
102     }
103 
104     @Test
105     public void testGlobalNameLocalTransformer() throws Exception
106     {
107         testTransformered("global-name-local-transformer", LOCAL_RESPONSE);
108     }
109 
110     @Test
111     public void testLocalNameLocalTransformer() throws Exception
112     {
113         testTransformered("local-name-local-transformer", LOCAL_RESPONSE);
114     }
115 
116     @Test
117     public void testLocalNameUrlTransformer() throws Exception
118     {
119         testTransformered("local-name-url-transformer", LOCAL_RESPONSE);
120     }
121 
122 
123     @Test
124     public void testGlobalNameGlobalTransformerAsync() throws Exception
125     {
126         testTransformeredAsync("global-name-global-transformer", GLOBAL_RESPONSE);
127     }
128 
129     @Test
130     public void testGlobalNameUrlTransformerAsync() throws Exception
131     {
132         testTransformeredAsync("global-name-url-transformer", GLOBAL_RESPONSE);
133     }
134 
135     @Test
136     public void testGlobalNameLocalTransformerAsync() throws Exception
137     {
138         testTransformeredAsync("global-name-local-transformer", LOCAL_RESPONSE);
139     }
140 
141     @Test
142     public void testLocalNameLocalTransformerAsync() throws Exception
143     {
144         testTransformeredAsync("local-name-local-transformer", LOCAL_RESPONSE);
145     }
146 
147     @Test
148     public void testLocalNameUrlTransformerAsync() throws Exception
149     {
150         testTransformeredAsync("local-name-url-transformer", LOCAL_RESPONSE);
151     }
152 }