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.transport.vm.functional.transactions;
8   
9   import org.hamcrest.core.Is;
10  import org.hamcrest.core.IsInstanceOf;
11  import org.hamcrest.core.IsNull;
12  import org.junit.Before;
13  import org.junit.Test;
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.lifecycle.Callable;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.message.DefaultExceptionPayload;
20  import org.mule.message.ExceptionMessage;
21  import org.mule.module.client.MuleClient;
22  import org.mule.tck.junit4.FunctionalTestCase;
23  import org.mule.transformer.AbstractTransformer;
24  import org.mule.transport.NullPayload;
25  import org.mule.util.concurrent.Latch;
26  
27  import static edu.emory.mathcs.backport.java.util.concurrent.TimeUnit.MILLISECONDS;
28  import static org.junit.Assert.assertThat;
29  import static org.junit.Assert.fail;
30  
31  public class VmExceptionStrategyOneWayTestCase extends FunctionalTestCase
32  {
33  
34      public static final int TIMEOUT = 3000;
35      public static final int TINY_TIMEOUT = 300;
36      public static final String ORIGINAL_MESSAGE = "some message";
37      private static Latch outboundComponentLatch;
38      private static Latch deadLetterQueueLatch;
39      private static boolean outboundComponentReached;
40  
41      @Override
42      protected String getConfigResources()
43      {
44          return "vm/vm-exception-strategy-config-one-way.xml";
45      }
46  
47      @Before
48      public void setUp() throws Exception
49      {
50          deadLetterQueueLatch = new Latch();
51          outboundComponentLatch = new Latch();
52          outboundComponentReached = false;
53      }
54  
55      @Test
56      public void testDeadLetterQueueWithInboundEndpointException() throws Exception
57      {
58          MuleClient muleClient = new MuleClient(muleContext);
59          MuleMessage response = muleClient.send("vm://in1", ORIGINAL_MESSAGE, null);
60          if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
61              fail("dead letter queue must be reached");
62          }
63          assertThat(outboundComponentReached, Is.is(false));
64          assertThat(response, IsNull.<Object>notNullValue());
65          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
66          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
67          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
68      }
69  
70      @Test
71      public void testDeadLetterQueueWithInboundEndpointResponseException() throws Exception
72      {
73          MuleClient muleClient = new MuleClient(muleContext);
74          MuleMessage response = muleClient.send("vm://in2", ORIGINAL_MESSAGE, null);
75          //TODO PLG - ES - fix this, DLQ call fails since tx was resolved already
76          /*if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
77              fail("dead letter queue must be reached");
78          }*/
79          assertThat(response, IsNull.<Object>notNullValue());
80          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
81          assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
82          assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
83          if (!outboundComponentLatch.await(TINY_TIMEOUT, MILLISECONDS))
84          {
85              fail("outbound component not reached");
86          }
87      }
88  
89      @Test
90      public void testDeadLetterQueueWithComponentException() throws Exception
91      {
92          MuleClient muleClient = new MuleClient(muleContext);
93          MuleMessage response = muleClient.send("vm://in3", ORIGINAL_MESSAGE, null);
94          if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
95              fail("dead letter queue must be reached");
96          }
97          assertThat(outboundComponentReached, Is.is(false));
98          assertThat(response, IsNull.<Object>notNullValue());
99          assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
100         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
101         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
102     }
103 
104     @Test
105     public void testDeadLetterQueueWithOutboundEndpointException() throws Exception
106     {
107         MuleClient muleClient = new MuleClient(muleContext);
108         MuleMessage response = muleClient.send("vm://in4", ORIGINAL_MESSAGE, null);
109         if (!deadLetterQueueLatch.await(TIMEOUT, MILLISECONDS)) {
110             fail("dead letter queue must be reached");
111         }
112         assertThat(outboundComponentReached, Is.is(false));
113         assertThat(response, IsNull.<Object>notNullValue());
114         assertThat(response.getPayload(),IsInstanceOf.instanceOf(NullPayload.class));
115         assertThat(response.getExceptionPayload(), IsNull.<Object>notNullValue());
116         assertThat(response.getExceptionPayload(), IsInstanceOf.instanceOf(DefaultExceptionPayload.class));
117     }
118 
119     public static class FailingTransformer extends AbstractTransformer
120     {
121 
122         @Override
123         protected Object doTransform(Object src, String enc) throws TransformerException
124         {
125             throw new TransformerException(CoreMessages.failedToBuildMessage(), this);
126         }
127     }
128 
129     public static class DeadLetterQueueComponent implements Callable
130     {
131 
132         public Object onCall(MuleEventContext eventContext) throws Exception
133         {
134             deadLetterQueueLatch.release();
135             MuleMessage message = eventContext.getMessage();
136             assertThat(message, IsNull.<Object>notNullValue());
137             assertThat(message.getExceptionPayload(), IsNull.<Object>nullValue());
138             assertThat(message.getPayload(), IsInstanceOf.instanceOf(ExceptionMessage.class));
139             return eventContext.getMessage();
140         }
141     }
142 
143     public static class OutboundComponent implements Callable
144     {
145 
146         public Object onCall(MuleEventContext eventContext) throws Exception
147         {
148             outboundComponentLatch.release();
149             outboundComponentReached = true;
150             return eventContext.getMessage();
151         }
152     }
153 }