1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.authz.support;
21  
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import junit.framework.Assert;
30  import junit.framework.TestCase;
31  
32  import org.apache.directory.server.core.authz.support.HighestPrecedenceFilter;
33  import org.apache.directory.shared.ldap.aci.ACITuple;
34  import org.apache.directory.shared.ldap.aci.MicroOperation;
35  import org.apache.directory.shared.ldap.aci.ProtectedItem;
36  import org.apache.directory.shared.ldap.aci.UserClass;
37  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
38  
39  
40  /**
41   * Tests {@link HighestPrecedenceFilter}.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev: 613608 $, $Date: 2008-01-20 17:59:10 +0100 (So, 20 Jan 2008) $
45   *
46   */
47  public class HighestPrecedenceFilterTest extends TestCase
48  {
49      private static final Collection<ProtectedItem> PI_EMPTY_COLLECTION = Collections.unmodifiableCollection( new ArrayList<ProtectedItem>() );
50      private static final Collection<UserClass> UC_EMPTY_COLLECTION = Collections.unmodifiableCollection( new ArrayList<UserClass>() );
51      private static final Collection<ACITuple> AT_EMPTY_COLLECTION = Collections.unmodifiableCollection( new ArrayList<ACITuple>() );
52      private static final Set<MicroOperation> MO_EMPTY_SET = Collections.unmodifiableSet( new HashSet<MicroOperation>() );
53  
54  
55      public void testZeroTuple() throws Exception
56      {
57          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
58          Assert.assertEquals( 0, filter.filter( null, AT_EMPTY_COLLECTION, null, null, null, null, null, null, null, null, null,
59              null, null, null ).size() );
60      }
61  
62  
63      public void testOneTuple() throws Exception
64      {
65          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
66          Collection<ACITuple> tuples = new ArrayList<ACITuple>();
67          
68          tuples.add( new ACITuple( UC_EMPTY_COLLECTION, AuthenticationLevel.NONE, PI_EMPTY_COLLECTION, MO_EMPTY_SET, true, 10 ) );
69          tuples = Collections.unmodifiableCollection( tuples );
70          
71          Assert.assertEquals( tuples, filter.filter( null, tuples, null, null, null, null, null, null, null, null, null, null,
72              null, null ) );
73      }
74  
75  
76      public void testMoreThanOneTuples() throws Exception
77      {
78          final int MAX_PRECEDENCE = 10;
79          HighestPrecedenceFilter filter = new HighestPrecedenceFilter();
80          Collection<ACITuple> tuples = new ArrayList<ACITuple>();
81          
82          tuples.add( new ACITuple( UC_EMPTY_COLLECTION, AuthenticationLevel.NONE, PI_EMPTY_COLLECTION, MO_EMPTY_SET, true,
83              MAX_PRECEDENCE ) );
84          tuples.add( new ACITuple( UC_EMPTY_COLLECTION, AuthenticationLevel.NONE, PI_EMPTY_COLLECTION, MO_EMPTY_SET, true,
85              MAX_PRECEDENCE / 2 ) );
86          tuples.add( new ACITuple( UC_EMPTY_COLLECTION, AuthenticationLevel.NONE, PI_EMPTY_COLLECTION, MO_EMPTY_SET, true,
87              MAX_PRECEDENCE ) );
88          tuples.add( new ACITuple( UC_EMPTY_COLLECTION, AuthenticationLevel.NONE, PI_EMPTY_COLLECTION, MO_EMPTY_SET, true,
89              MAX_PRECEDENCE / 3 ) );
90  
91          tuples = filter.filter( null, tuples, null, null, null, null, null, null, null, null, null, null, null, null );
92  
93          for ( ACITuple tuple:tuples )
94          {
95              Assert.assertEquals( MAX_PRECEDENCE, tuple.getPrecedence() );
96          }
97      }
98  }