View Javadoc

1   /*
2    * $Id: SecurityFilterMessageProcessorTestCase.java 22377 2011-07-11 12:41:42Z 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.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  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.fail;
28  
29  public class SecurityFilterMessageProcessorTestCase extends AbstractMessageProcessorTestCase
30  {
31      @Test
32      public void testFilterPass() throws Exception
33      {
34          TestSecurityFilter securityFilter = new TestSecurityFilter(true);
35          InboundEndpoint endpoint = createTestInboundEndpoint(null, securityFilter,
36              MessageExchangePattern.REQUEST_RESPONSE, null);
37          InterceptingMessageProcessor mp = new SecurityFilterMessageProcessor(securityFilter);
38          TestListener listner = new TestListener();
39          mp.setListener(listner);
40  
41          MuleEvent inEvent = createTestInboundEvent(endpoint);
42          MuleEvent resultEvent = mp.process(inEvent);
43          assertNotNull(listner.sensedEvent);
44          assertSame(inEvent, listner.sensedEvent);
45          assertEquals(inEvent, resultEvent);
46  
47      }
48  
49      @Test
50      public void testFilterFail() throws Exception
51      {
52          TestSecurityFilter securityFilter = new TestSecurityFilter(false);
53          InboundEndpoint endpoint = createTestInboundEndpoint(null, securityFilter,
54              MessageExchangePattern.REQUEST_RESPONSE, null);
55          InterceptingMessageProcessor mp = new SecurityFilterMessageProcessor(securityFilter);
56          TestListener listner = new TestListener();
57          mp.setListener(listner);
58  
59          MuleEvent inEvent = createTestInboundEvent(endpoint);
60  
61          // Need event in RequestContext :-(
62          RequestContext.setEvent(inEvent);
63  
64          try
65          {
66              mp.process(inEvent);
67              fail("Exception expected");
68          }
69          catch (TestSecurityFilter.StaticMessageUnauthorisedException e)
70          {
71              // expected
72          }
73  
74          assertNull(listner.sensedEvent);
75      }
76  }