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