View Javadoc

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