View Javadoc

1   /*
2    * $Id$
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.processor;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.RequestContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.processor.InterceptingMessageProcessor;
18  import org.mule.endpoint.AbstractMessageProcessorTestCase;
19  import org.mule.tck.security.TestSecurityFilter;
20  
21  import org.junit.Test;
22  
23  public class SecurityFilterMessageProcessorTestCase extends AbstractMessageProcessorTestCase
24  {
25  
26      @Test
27      public void testFilterPass() throws Exception
28      {
29          TestSecurityFilter securityFilter = new TestSecurityFilter(true);
30          InboundEndpoint endpoint = createTestInboundEndpoint(null, securityFilter,
31              MessageExchangePattern.REQUEST_RESPONSE, null);
32          InterceptingMessageProcessor mp = new SecurityFilterMessageProcessor(securityFilter);
33          TestListener listner = new TestListener();
34          mp.setListener(listner);
35  
36          MuleEvent inEvent = createTestInboundEvent(endpoint);
37          MuleEvent resultEvent = mp.process(inEvent);
38          assertNotNull(listner.sensedEvent);
39          assertSame(inEvent, listner.sensedEvent);
40          assertEquals(inEvent, resultEvent);
41  
42      }
43  
44      @Test
45      public void testFilterFail() throws Exception
46      {
47          TestSecurityFilter securityFilter = new TestSecurityFilter(false);
48          InboundEndpoint endpoint = createTestInboundEndpoint(null, securityFilter,
49              MessageExchangePattern.REQUEST_RESPONSE, null);
50          InterceptingMessageProcessor mp = new SecurityFilterMessageProcessor(securityFilter);
51          TestListener listner = new TestListener();
52          mp.setListener(listner);
53  
54          MuleEvent inEvent = createTestInboundEvent(endpoint);
55  
56          // Need event in RequestContext :-(
57          RequestContext.setEvent(inEvent);
58  
59          MuleEvent resultEvent = mp.process(inEvent);
60          assertNull(listner.sensedEvent);
61  
62          assertNotNull(resultEvent);
63          assertEquals(TestSecurityFilter.SECURITY_EXCEPTION_MESSAGE, resultEvent.getMessageAsString());
64          assertNotNull(resultEvent.getMessage().getExceptionPayload());
65          assertTrue(resultEvent.getMessage().getExceptionPayload().getException() instanceof TestSecurityFilter.StaticMessageUnauthorisedException);
66      }
67  
68  }