View Javadoc

1   /*
2    * $Id: CoreNamespaceRoutersFlowTestCase.java 22481 2011-07-21 04:21:24Z 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.config.spring;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.mule.api.MessagingException;
18  import org.mule.api.MuleEvent;
19  import org.mule.api.MuleException;
20  import org.mule.api.MuleMessage;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.construct.Flow;
23  import org.mule.routing.IdempotentMessageFilter;
24  import org.mule.routing.IdempotentSecureHashMessageFilter;
25  import org.mule.routing.outbound.AbstractOutboundRouter;
26  import org.mule.tck.junit4.FunctionalTestCase;
27  import org.mule.util.store.InMemoryObjectStore;
28  import org.mule.util.store.TextFileObjectStore;
29  
30  import java.util.List;
31  
32  import org.junit.Test;
33  
34  public class CoreNamespaceRoutersFlowTestCase extends FunctionalTestCase
35  {
36      
37      @Override
38      public String getConfigResources()
39      {
40          return "core-namespace-routers-flow.xml";
41      }
42  
43      @Test
44      public void testIdempotentSecureHashReceiverRouter() throws Exception
45      {
46          MessageProcessor router = lookupMessageProcessorFromFlow("IdempotentSecureHashReceiverRouter");
47          assertTrue(router instanceof IdempotentSecureHashMessageFilter);
48  
49          IdempotentSecureHashMessageFilter filter = (IdempotentSecureHashMessageFilter)router;
50          assertEquals("SHA-128", filter.getMessageDigestAlgorithm());
51          assertNotNull(filter.getStore());
52          assertTrue(filter.getStore() instanceof InMemoryObjectStore);
53  
54          InMemoryObjectStore<String> store = (InMemoryObjectStore<String>)filter.getStore();
55          assertEquals(1001, store.getEntryTTL());
56          assertEquals(1001, store.getExpirationInterval());
57          assertEquals(1001, store.getMaxEntries());
58          assertEquals("xyz", store.getName());
59          assertNotNull(store.getScheduler());
60      }
61  
62      @Test
63      public void testIdempotentReceiverRouter() throws Exception
64      {
65          MessageProcessor router = lookupMessageProcessorFromFlow("IdempotentReceiverRouter");
66          assertTrue(router instanceof IdempotentMessageFilter);
67  
68          IdempotentMessageFilter filter = (IdempotentMessageFilter)router;
69          assertEquals("#[message:id]-#[message:correlationId]", filter.getIdExpression());
70          assertNotNull(filter.getStore());
71          assertTrue(filter.getStore() instanceof TextFileObjectStore);
72  
73          TextFileObjectStore store = (TextFileObjectStore)filter.getStore();
74          assertEquals(-1, store.getEntryTTL());
75          assertEquals(1000, store.getExpirationInterval());
76          assertEquals(10000000, store.getMaxEntries());
77          assertEquals("foo", store.getDirectory());
78          assertNotNull(store.getName());
79          assertNotNull(store.getScheduler());
80      }
81  
82      @Test
83      public void testCustomRouter() throws Exception
84      {
85          MessageProcessor router = lookupCustomRouterFromFlow("CustomRouter");
86          assertTrue(router instanceof CustomRouter);
87      }
88  
89      protected MessageProcessor lookupCustomRouterFromFlow(String serviceName) throws Exception
90      {
91          Flow service = lookupFlow(serviceName);
92          return service.getMessageProcessors().get(0);
93      }
94  
95      protected MessageProcessor lookupMessageProcessorFromFlow(String serviceName) throws Exception
96      {
97          Flow service = lookupFlow(serviceName);
98          List<MessageProcessor> routers = 
99              service.getMessageProcessors();
100         assertEquals(1, routers.size());
101         return routers.get(0);
102     }
103 
104     protected Flow lookupFlow(String serviceName)
105     {
106         Flow service = muleContext.getRegistry().lookupObject(serviceName);
107         assertNotNull(service);
108         return service;
109     }
110     
111     public static class CustomRouter extends AbstractOutboundRouter
112     {
113         public boolean isMatch(MuleMessage message) throws MuleException
114         {
115             return true;
116         }
117 
118         @Override
119         protected MuleEvent route(MuleEvent event) throws MessagingException
120         {
121             return event;
122         }
123     }
124 }