View Javadoc

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