View Javadoc

1   /*
2    * $Id: ResponseAggregatorTestCase.java 22666 2011-08-15 06:35:36Z 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.test.usecases.routing.response;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.routing.requestreply.AbstractAsyncRequestReplyRequester;
18  import org.mule.tck.AbstractServiceAndFlowTestCase;
19  import org.mule.tck.SensingNullMessageProcessor;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Map;
24  
25  import org.junit.Test;
26  import org.junit.runners.Parameterized.Parameters;
27  import org.mule.util.store.SimpleMemoryObjectStore;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertTrue;
32  
33  public class ResponseAggregatorTestCase extends AbstractServiceAndFlowTestCase
34  {
35      @Parameters
36      public static Collection<Object[]> parameters()
37      {
38          return Arrays.asList(new Object[][]{
39              {ConfigVariant.SERVICE, "org/mule/test/usecases/routing/response/response-router-service.xml"},
40              {ConfigVariant.FLOW, "org/mule/test/usecases/routing/response/response-router-flow.xml"}});
41      }
42  
43      public ResponseAggregatorTestCase(ConfigVariant variant, String configResources)
44      {
45          super(variant, configResources);
46      }
47  
48      @Test
49      public void testSyncResponse() throws Exception
50      {
51          MuleClient client = new MuleClient(muleContext);
52          MuleMessage message = client.send("http://localhost:28081", "request", null);
53          assertNotNull(message);
54          assertEquals("Received: request", new String(message.getPayloadAsBytes()));
55      }
56  
57      @Test
58      public void testResponseEventsCleanedUp() throws Exception
59      {
60          RelaxedAsyncReplyMP mp = new RelaxedAsyncReplyMP();
61  
62          try
63          {
64              MuleEvent event = getTestEvent("message1");
65              final MuleMessage message = event.getMessage();
66              final String id = message.getUniqueId();
67              message.setCorrelationId(id);
68              message.setCorrelationGroupSize(1);
69  
70              SensingNullMessageProcessor listener = getSensingNullMessageProcessor();
71              mp.setListener(listener);
72              mp.setReplySource(listener.getMessageSource());
73  
74              mp.process(event);
75  
76              Map<String, MuleEvent> responseEvents = mp.getResponseEvents();
77              assertTrue("Response events should be cleaned up.", responseEvents.isEmpty());
78          }
79          finally
80          {
81              mp.stop();
82          }
83      }
84  
85      /**
86       * This class opens up the access to responseEvents map for testing
87       */
88      private static final class RelaxedAsyncReplyMP extends AbstractAsyncRequestReplyRequester
89      {
90          private RelaxedAsyncReplyMP() throws MuleException
91          {
92              store = new SimpleMemoryObjectStore();
93              name = "asyncReply";
94              start();
95          }
96  
97          public Map<String, MuleEvent> getResponseEvents()
98          {
99              return responseEvents;
100         }
101     }
102 }