1   /*
2    *   @(#) $Id: MaxValueCountFilterTest.java 292096 2005-09-28 02:26:16Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.ldap.server.authz.support;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import javax.naming.directory.Attribute;
28  import javax.naming.directory.Attributes;
29  import javax.naming.directory.BasicAttribute;
30  import javax.naming.directory.BasicAttributes;
31  
32  import junit.framework.Assert;
33  import junit.framework.TestCase;
34  
35  import org.apache.ldap.common.aci.ACITuple;
36  import org.apache.ldap.common.aci.AuthenticationLevel;
37  import org.apache.ldap.common.aci.ProtectedItem;
38  import org.apache.ldap.common.aci.ProtectedItem.MaxValueCountItem;
39  
40  /***
41   * Tests {@link MaxValueCountFilter}.
42   *
43   * @author The Apache Directory Project
44   * @version $Rev: 292096 $, $Date: 2005-09-27 22:26:16 -0400 (Tue, 27 Sep 2005) $
45   */
46  public class MaxValueCountFilterTest extends TestCase
47  {
48      private static final Collection EMPTY_COLLECTION =
49          Collections.unmodifiableCollection( new ArrayList() );
50      private static final Set EMPTY_SET =
51          Collections.unmodifiableSet( new HashSet() );
52      
53      private static final Collection PROTECTED_ITEMS = new ArrayList();
54      private static final Attributes ENTRY = new BasicAttributes();
55      private static final Attributes FULL_ENTRY = new BasicAttributes();
56      
57      static
58      {
59          Collection mvcItems = new ArrayList();
60          mvcItems.add( new MaxValueCountItem( "testAttr", 2 ) );
61          PROTECTED_ITEMS.add( new ProtectedItem.MaxValueCount( mvcItems ) );
62          
63          ENTRY.put( "testAttr", "1" );
64  
65          Attribute attr = new BasicAttribute( "testAttr" );
66          attr.add( "1" );
67          attr.add( "2" );
68          FULL_ENTRY.put( attr );
69      }
70      
71      public void testWrongScope() throws Exception
72      {
73          MaxValueCountFilter filter = new MaxValueCountFilter();
74          Collection tuples = new ArrayList();
75          tuples.add( new ACITuple(
76                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
77                  EMPTY_SET, true, 0 ) );
78          
79          tuples = Collections.unmodifiableCollection( tuples );
80          
81          Assert.assertEquals(
82                  tuples, filter.filter(
83                          tuples, OperationScope.ATTRIBUTE_TYPE, null, null, null,
84                          null, null, null, null, null, null, null ) );
85  
86          Assert.assertEquals(
87                  tuples, filter.filter(
88                          tuples, OperationScope.ENTRY, null, null, null,
89                          null, null, null, null, null, null, null ) );
90      }
91      
92      public void testZeroTuple() throws Exception
93      {
94          MaxValueCountFilter filter = new MaxValueCountFilter();
95          
96          Assert.assertEquals(
97                  0, filter.filter(
98                          EMPTY_COLLECTION, OperationScope.ATTRIBUTE_TYPE_AND_VALUE,
99                          null, null, null, null, null, null, null, null, null, null ).size() );
100     }
101     
102     public void testDenialTuple() throws Exception
103     {
104         MaxValueCountFilter filter = new MaxValueCountFilter();
105         Collection tuples = new ArrayList();
106         tuples.add( new ACITuple(
107                 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
108                 EMPTY_SET, false, 0 ) );
109         
110         tuples = Collections.unmodifiableCollection( tuples );
111         
112         Assert.assertEquals(
113                 tuples, filter.filter(
114                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
115                         null, null, null, "testAttr", null, ENTRY, null ) );
116         Assert.assertEquals(
117                 tuples, filter.filter(
118                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
119                         null, null, null, "testAttr", null, FULL_ENTRY, null ) );
120     }
121 
122 
123     public void testGrantTuple() throws Exception
124     {
125         MaxValueCountFilter filter = new MaxValueCountFilter();
126         Collection tuples = new ArrayList();
127         tuples.add( new ACITuple(
128                 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
129                 EMPTY_SET, true, 0 ) );
130         
131         Assert.assertEquals(
132                 1, filter.filter(
133                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
134                         null, null, null, "testAttr", null, ENTRY, null ).size() );
135 
136         Assert.assertEquals(
137                 0, filter.filter(
138                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
139                         null, null, null, "testAttr", null, FULL_ENTRY, null ).size() );
140     }
141 }