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.MicroOperationFilter;
33  import org.apache.directory.server.core.authz.support.OperationScope;
34  import org.apache.directory.shared.ldap.aci.ACITuple;
35  import org.apache.directory.shared.ldap.aci.MicroOperation;
36  import org.apache.directory.shared.ldap.aci.ProtectedItem;
37  import org.apache.directory.shared.ldap.aci.UserClass;
38  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
39  
40  
41  /**
42   * Tests {@link MicroOperationFilter}.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   * @version $Rev: 613608 $, $Date: 2008-01-20 17:59:10 +0100 (So, 20 Jan 2008) $
46   */
47  public class MicroOperationFilterTest extends TestCase
48  {
49      private static final Collection<ACITuple> EMPTY_ACI_TUPLE_COLLECTION = Collections.unmodifiableCollection( new ArrayList<ACITuple>() );
50      private static final Collection<UserClass> EMPTY_USER_CLASS_COLLECTION = Collections.unmodifiableCollection( new ArrayList<UserClass>() );
51      private static final Collection<ProtectedItem> EMPTY_PROTECTED_ITEM_COLLECTION = Collections.unmodifiableCollection( new ArrayList<ProtectedItem>() );
52  
53      private static final Set<MicroOperation> USER_OPERATIONS_A = new HashSet<MicroOperation>();
54      private static final Set<MicroOperation> USER_OPERATIONS_B = new HashSet<MicroOperation>();
55      private static final Set<MicroOperation> TUPLE_OPERATIONS = new HashSet<MicroOperation>();
56  
57      static
58      {
59          USER_OPERATIONS_A.add( MicroOperation.ADD );
60          USER_OPERATIONS_A.add( MicroOperation.BROWSE );
61          USER_OPERATIONS_B.add( MicroOperation.COMPARE );
62          USER_OPERATIONS_B.add( MicroOperation.DISCLOSE_ON_ERROR );
63          TUPLE_OPERATIONS.add( MicroOperation.ADD );
64          TUPLE_OPERATIONS.add( MicroOperation.BROWSE );
65          TUPLE_OPERATIONS.add( MicroOperation.EXPORT );
66      }
67  
68  
69      public void testZeroTuple() throws Exception
70      {
71          MicroOperationFilter filter = new MicroOperationFilter();
72  
73          Assert.assertEquals( 0, filter.filter( null, EMPTY_ACI_TUPLE_COLLECTION, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, 
74              null, null, null, null, null, null, null, null, null, null, null ).size() );
75      }
76  
77  
78      public void testOneTuple() throws Exception
79      {
80          MicroOperationFilter filter = new MicroOperationFilter();
81          Collection<ACITuple> tuples = new ArrayList<ACITuple>();
82          
83          tuples.add( new ACITuple( EMPTY_USER_CLASS_COLLECTION, AuthenticationLevel.NONE, EMPTY_PROTECTED_ITEM_COLLECTION, 
84              TUPLE_OPERATIONS, true, 0 ) );
85  
86          Assert.assertEquals( 1, filter.filter( null, tuples, OperationScope.ENTRY, null, null, null, null, null, null, null,
87              null, null, USER_OPERATIONS_A, null ).size() );
88          Assert.assertEquals( 0, filter.filter( null, tuples, OperationScope.ENTRY, null, null, null, null, null, null, null,
89              null, null, USER_OPERATIONS_B, null ).size() );
90      }
91  }