View Javadoc

1   /*
2    * $Id: ResponseTransformerMule2165TestCase.java 22489 2011-07-21 09:37:30Z justin.calleja $
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.issues;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import java.util.Arrays;
17  import java.util.Collection;
18  
19  import org.junit.Test;
20  import org.junit.runners.Parameterized.Parameters;
21  import org.mule.api.MuleException;
22  import org.mule.api.MuleMessage;
23  import org.mule.module.client.MuleClient;
24  import org.mule.tck.AbstractServiceAndFlowTestCase;
25  
26  public class ResponseTransformerMule2165TestCase extends AbstractServiceAndFlowTestCase
27  {
28  
29  
30      public static final long TIMEOUT = 2000L;
31      public static final String MESSAGE = "a message";
32      // i don't know if this is the "correct" response - it's *one* of the responses in 1.4,
33      // and it seems vaguely reasonable.
34  
35      /* RM
36      Described as:
37      1. Client dispatch = "outbound"
38      2. First Service receiver = "inbound"
39      3. First Service dispatch (first endpoint) = "outbound"
40      4. Second Service receiver = "inbound"
41      5. Response transformer from SecondComponent = "response"
42      Note that because the response transformer is configured locally on the outbound endppoint it only gets called once
43      */
44      public static final String LOCAL_RESPONSE = MESSAGE + " outbound inbound outbound inbound response";
45      // an alternative in 1.4 is " outbound outbound response response" for the global
46      // transformers, which also makes some sense
47      /* RM
48      Described as:
49      1. Client dispatch = "outbound"
50      2. First Service receiver = "inbound"
51      3. First Service dispatch (first endpoint) = "outbound"
52      4. Second Service receiver = "inbound"
53      5. Response transformer from SecondComponent = "response"
54      6. Response from outbound endpoint (to the service) = "response"
55      Note that because the global outbound inpoint is also the inbound endpoint of the bounce service
56      The "response" ResponseTransformer gets called twice
57      */
58      public static final String GLOBAL_RESPONSE = LOCAL_RESPONSE + " response";
59  
60      public ResponseTransformerMule2165TestCase(ConfigVariant variant, String configResources)
61      {
62          super(variant, configResources);
63      }
64      
65      @Parameters
66      public static Collection<Object[]> parameters()
67      {
68          return Arrays.asList(new Object[][]{
69              {ConfigVariant.SERVICE, "issues/response-transformer-mule-2165-test-service.xml"},
70              {ConfigVariant.FLOW, "issues/response-transformer-mule-2165-test-flow.xml"}});
71      }
72  
73      protected MuleClient send(String endpoint) throws MuleException
74      {
75          MuleClient client = new MuleClient(muleContext);
76          client.dispatch(endpoint, MESSAGE, null);
77          return client;
78      }
79  
80      protected MuleClient dispatch(String endpoint) throws MuleException
81      {
82          MuleClient client = new MuleClient(muleContext);
83          client.dispatch(endpoint, MESSAGE, null);
84          return client;
85      }
86  
87      protected String request(MuleClient client, String endpoint) throws Exception
88      {
89          MuleMessage message = client.request(endpoint, TIMEOUT);
90          assertNotNull("no response from " + endpoint, message);
91          assertNotNull(message.getPayloadAsString());
92          return message.getPayloadAsString();
93      }
94  
95      protected void testTransformered(String endpoint, String response) throws Exception
96      {
97          String message = request(send("in-" + endpoint), "out-" + endpoint);
98          assertEquals("bad response (" + message + ")  for " + endpoint, response, message);
99      }
100 
101     protected void testTransformeredAsync(String endpoint, String response) throws Exception
102     {
103         String message = request(dispatch("in-" + endpoint), "out-" + endpoint);
104         assertEquals("bad response (" + message + ")  for " + endpoint, response, message);
105     }
106 
107     @Test
108     public void testGlobalNameGlobalTransformer() throws Exception
109     {
110         testTransformered("global-name-global-transformer", GLOBAL_RESPONSE);
111     }
112 
113     @Test
114     public void testGlobalNameUrlTransformer() throws Exception
115     {
116         testTransformered("global-name-url-transformer", GLOBAL_RESPONSE);
117     }
118 
119     @Test
120     public void testGlobalNameLocalTransformer() throws Exception
121     {
122         testTransformered("global-name-local-transformer", LOCAL_RESPONSE);
123     }
124 
125     @Test
126     public void testLocalNameLocalTransformer() throws Exception
127     {
128         testTransformered("local-name-local-transformer", LOCAL_RESPONSE);
129     }
130 
131     @Test
132     public void testLocalNameUrlTransformer() throws Exception
133     {
134         testTransformered("local-name-url-transformer", LOCAL_RESPONSE);
135     }
136 
137 
138     @Test
139     public void testGlobalNameGlobalTransformerAsync() throws Exception
140     {
141         testTransformeredAsync("global-name-global-transformer", GLOBAL_RESPONSE);
142     }
143 
144     @Test
145     public void testGlobalNameUrlTransformerAsync() throws Exception
146     {
147         testTransformeredAsync("global-name-url-transformer", GLOBAL_RESPONSE);
148     }
149 
150     @Test
151     public void testGlobalNameLocalTransformerAsync() throws Exception
152     {
153         testTransformeredAsync("global-name-local-transformer", LOCAL_RESPONSE);
154     }
155 
156     @Test
157     public void testLocalNameLocalTransformerAsync() throws Exception
158     {
159         testTransformeredAsync("local-name-local-transformer", LOCAL_RESPONSE);
160     }
161 
162     @Test
163     public void testLocalNameUrlTransformerAsync() throws Exception
164     {
165         testTransformeredAsync("local-name-url-transformer", LOCAL_RESPONSE);
166     }
167 }