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.test.config;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.endpoint.EndpointBuilder;
11  import org.mule.api.endpoint.InboundEndpoint;
12  import org.mule.api.lifecycle.InitialisationException;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.processor.SecurityFilterMessageProcessor;
15  import org.mule.security.AbstractAuthenticationFilter;
16  import org.mule.security.filters.MuleEncryptionEndpointSecurityFilter;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  
19  import java.util.List;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  /**
27   * Test configuration of security filters
28   */
29  public class SecurityFilterTestCase extends FunctionalTestCase
30  {
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/test/config/security-filter-config.xml";
36      }
37  
38      @Test
39      public void testConfig() throws Exception
40      {
41          EndpointBuilder epb = muleContext.getRegistry().lookupEndpointBuilder("testEndpoint1");
42          assertNotNull(epb);
43          InboundEndpoint iep = epb.buildInboundEndpoint();
44          List<MessageProcessor> mps =iep.getMessageProcessors();
45          int count = 0;
46          SecurityFilterMessageProcessor securityMp = null;
47          for (MessageProcessor mp : mps)
48          {
49              if (mp instanceof SecurityFilterMessageProcessor)
50              {
51                  count++;
52                  securityMp = (SecurityFilterMessageProcessor) mp;
53              }
54          }
55          assertEquals(1, count);
56          assertEquals(CustomSecurityFilter.class, securityMp.getFilter().getClass());
57  
58          epb = muleContext.getRegistry().lookupEndpointBuilder("testEndpoint2");
59          assertNotNull(epb);
60          iep = epb.buildInboundEndpoint();
61          mps =iep.getMessageProcessors();
62          count = 0;
63          securityMp = null;
64          for (MessageProcessor mp : mps)
65          {
66              if (mp instanceof SecurityFilterMessageProcessor)
67              {
68                  count++;
69                  securityMp = (SecurityFilterMessageProcessor) mp;
70              }
71          }
72          assertEquals(1, count);
73          assertEquals(MuleEncryptionEndpointSecurityFilter.class, securityMp.getFilter().getClass());        
74      }
75  
76      /**
77       * Custom security filter class that does nothing at all
78       */
79      public static class CustomSecurityFilter extends AbstractAuthenticationFilter
80      {
81          @Override
82          protected void authenticateInbound(MuleEvent event)
83          {
84          }
85  
86          @Override
87          protected void authenticateOutbound(MuleEvent event)
88          {
89          }
90  
91          @Override
92          protected void doInitialise() throws InitialisationException
93          {
94          }
95      }
96  
97  }