View Javadoc

1   /*
2    * $Id: VmExceptionStrategyRequestResponseTestCase.java 23154 2011-10-12 06:00:08Z 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.transport.vm.functional.transactions;
12  
13  import org.hamcrest.core.Is;
14  import org.hamcrest.core.IsInstanceOf;
15  import org.hamcrest.core.IsNull;
16  import org.junit.Before;
17  import org.junit.Test;
18  import org.mule.api.MuleEventContext;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.lifecycle.Callable;
21  import org.mule.api.transformer.TransformerException;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.message.DefaultExceptionPayload;
24  import org.mule.message.ExceptionMessage;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.junit4.FunctionalTestCase;
27  import org.mule.transformer.AbstractTransformer;
28  import org.mule.transport.NullPayload;
29  import org.mule.util.concurrent.Latch;
30  
31  import java.util.concurrent.TimeUnit;
32  
33  import static org.junit.Assert.assertThat;
34  import static org.junit.Assert.fail;
35  
36  public class VmExceptionStrategyRequestResponseTestCase extends FunctionalTestCase
37  {
38  
39      public static final int TIMEOUT = 3000;
40      public static final int TINY_TIMEOUT = 300;
41      public static final String ORIGINAL_MESSAGE = "some message";
42      private static Latch outboundComponentLatch;
43      private static Latch deadLetterQueueLatch;
44      private static boolean outboundComponentReached;
45  
46      @Override
47      protected String getConfigResources()
48      {
49          return "vm/vm-exception-strategy-config-request-response.xml";
50      }
51  
52      @Before
53      public void setUp() throws Exception
54      {
55          outboundComponentLatch = new Latch();
56          deadLetterQueueLatch = new Latch();
57          outboundComponentReached = false;
58      }
59  
60      @Test
61      public void testDeadLetterQueueWithInboundEndpointException() throws Exception
62      {
63          MuleClient muleClient = new MuleClient(muleContext);
64          MuleMessage response = muleClient.send("vm://in1", ORIGINAL_MESSAGE, null);
65          if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
66              fail("dead letter queue must be reached");
67          }
68          assertThat(outboundComponentReached, Is.is(false));
69          assertThat(response, IsNull.<Object>notNullValue());
70          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
71          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
72          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
73          assertThat(muleClient.request("vm://out1", TINY_TIMEOUT), IsNull.<Object>nullValue());
74      }
75  
76      @Test
77      public void testDeadLetterQueueWithInboundEndpointResponseException() throws Exception
78      {
79          MuleClient muleClient = new MuleClient(muleContext);
80          MuleMessage response = muleClient.send("vm://in2", ORIGINAL_MESSAGE, null);
81          //TODO PLG - ES - fix this, dlq is failing because transaction was already commited by next flow despite is called using one-way with vm
82          /*if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
83              fail("dead letter queue must be reached");
84          }*/
85          assertThat(response, IsNull.<Object>notNullValue());
86          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
87          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
88          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
89          assertThat(muleClient.request("vm://out2", TINY_TIMEOUT), IsNull.<Object>nullValue());
90          if (!outboundComponentLatch.await(TINY_TIMEOUT, TimeUnit.MILLISECONDS))
91          {
92              fail("outbound component not reached");
93          }
94      }
95  
96      @Test
97      public void testDeadLetterQueueWithComponentException() throws Exception
98      {
99          MuleClient muleClient = new MuleClient(muleContext);
100         MuleMessage response = muleClient.send("vm://in3", ORIGINAL_MESSAGE, null);
101         if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
102             fail("dead letter queue must be reached");
103         }
104         assertThat(outboundComponentReached, Is.is(false));
105         assertThat(response, IsNull.<Object>notNullValue());
106         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
107         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
108         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
109         assertThat(muleClient.request("vm://out3", TINY_TIMEOUT), IsNull.<Object>nullValue());
110     }
111 
112     @Test
113     public void testDeadLetterQueueWithOutboundEndpointException() throws Exception
114     {
115         MuleClient muleClient = new MuleClient(muleContext);
116         MuleMessage response = muleClient.send("vm://in4", ORIGINAL_MESSAGE, null);
117         if (!deadLetterQueueLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
118             fail("dead letter queue must be reached");
119         }
120         assertThat(outboundComponentReached, Is.is(false));
121         assertThat(response, IsNull.<Object>notNullValue());
122         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
123         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
124         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
125         assertThat(muleClient.request("vm://out4", TINY_TIMEOUT), IsNull.<Object>nullValue());
126     }
127 
128     @Test
129     public void testDeadLetterQueueWithOutboundEndpointResponseException() throws Exception
130     {
131         MuleClient muleClient = new MuleClient(muleContext);
132         MuleMessage response = muleClient.send("vm://in5", ORIGINAL_MESSAGE, null);
133         //TODO PLG - ES - fix this issue, the response must have an exception since there was a failire in the flow. It seems that response chain was not executed
134         /*assertThat(response, IsNull.<Object>notNullValue());
135         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
136         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
137         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));*/
138         assertThat(muleClient.request("vm://out5", TINY_TIMEOUT), IsNull.<Object>nullValue());
139         if (!outboundComponentLatch.await(TINY_TIMEOUT, TimeUnit.MILLISECONDS))
140         {
141             fail("outbound component not reached");
142         }
143         //TODO PLG - ES - fix this issue. dead letter queue component is not reached
144         /*if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
145             fail("dead letter queue must be reached");
146         }*/
147     }
148 
149     public static class FailingTransformer extends AbstractTransformer
150     {
151 
152         @Override
153         protected Object doTransform(Object src, String enc) throws TransformerException
154         {
155             throw new TransformerException(CoreMessages.failedToBuildMessage(), this);
156         }
157     }
158 
159     public static class DeadLetterQueueComponent implements Callable
160     {
161 
162         public Object onCall(MuleEventContext eventContext) throws Exception
163         {
164             deadLetterQueueLatch.release();
165             MuleMessage message = eventContext.getMessage();
166             assertThat(message, IsNull.<Object>notNullValue());
167             assertThat(message.getExceptionPayload(), IsNull.<Object>nullValue());
168             assertThat(message.getPayload(), IsInstanceOf.instanceOf(ExceptionMessage.class));
169             return eventContext.getMessage();
170         }
171     }
172 
173     public static class OutboundComponent implements Callable
174     {
175 
176         public Object onCall(MuleEventContext eventContext) throws Exception
177         {
178             outboundComponentLatch.release();
179             return eventContext.getMessage();
180         }
181     }
182 }