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.routing;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.MuleSession;
14  import org.mule.api.endpoint.OutboundEndpoint;
15  import org.mule.api.service.Service;
16  import org.mule.tck.MuleTestUtils;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  
19  import com.mockobjects.dynamic.Mock;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  
27  public class IdempotentMessageFilterTestCase extends AbstractMuleContextTestCase
28  {
29  
30      @Test
31      public void testIdempotentReceiver() throws Exception
32      {
33          OutboundEndpoint endpoint1 = getTestOutboundEndpoint("Test1Provider", "test://Test1Provider?exchangePattern=one-way");
34          Mock session = MuleTestUtils.getMockSession();
35          Service service = getTestService();
36          session.matchAndReturn("getFlowConstruct", service);
37  
38  
39          IdempotentMessageFilter ir = new IdempotentMessageFilter();
40          ir.setIdExpression("#[header:id]");
41          ir.setFlowConstruct(service);
42          ir.setThrowOnUnaccepted(false);
43  
44          MuleMessage okMessage = new DefaultMuleMessage("OK", muleContext);
45          okMessage.setOutboundProperty("id", "1");
46          MuleEvent event = new DefaultMuleEvent(okMessage, endpoint1, (MuleSession) session.proxy());
47  
48          // This one will process the event on the target endpoint
49          event = ir.process(event);
50          assertNotNull(event);
51  
52           // This will not process, because the ID is a duplicate
53          okMessage = new DefaultMuleMessage("OK", muleContext);
54          okMessage.setOutboundProperty("id", "1");
55          event = new DefaultMuleEvent(okMessage, endpoint1, (MuleSession) session.proxy());
56          event = ir.process(event);
57          assertNull(event);
58      }
59  
60  }