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.subtree;
21  
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.directory.server.core.DefaultDirectoryService;
26  import org.apache.directory.server.core.DirectoryService;
27  import org.apache.directory.server.core.entry.DefaultServerAttribute;
28  import org.apache.directory.server.core.entry.ServerAttribute;
29  import org.apache.directory.server.core.subtree.RefinementEvaluator;
30  import org.apache.directory.server.core.subtree.RefinementLeafEvaluator;
31  import org.apache.directory.server.schema.registries.OidRegistry;
32  import org.apache.directory.server.schema.registries.Registries;
33  import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
34  import org.apache.directory.shared.ldap.filter.EqualityNode;
35  import org.apache.directory.shared.ldap.filter.ExprNode;
36  import org.apache.directory.shared.ldap.filter.FilterParser;
37  import org.apache.directory.shared.ldap.filter.NotNode;
38  import org.apache.directory.shared.ldap.schema.AttributeType;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  
44  import static org.junit.Assert.fail;
45  import static org.junit.Assert.assertFalse;
46  import static org.junit.Assert.assertTrue;
47  
48  
49  /**
50   * Unit test cases for testing the evaluator for refinements.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev: 655126 $
54   */
55  public class RefinementEvaluatorTest
56  {
57      /** the registries */
58      private static Registries registries;
59      
60      /** the refinement evaluator to test */
61      private static RefinementEvaluator evaluator;
62  
63      /** The ObjectClass AttributeType */
64      private static AttributeType OBJECT_CLASS;
65  
66      /** The CN AttributeType */
67      private static AttributeType CN;
68  
69      /** A reference to the directory service */
70      private static DirectoryService service;
71  
72      
73      /**
74       * Initializes the global registries.
75       * @throws javax.naming.NamingException if there is a failure loading the schema
76       */
77      @BeforeClass public static void init() throws NamingException
78      {
79          service = new DefaultDirectoryService();
80          registries = service.getRegistries();
81      }
82  
83  
84      /**
85       * Initializes registries and creates the leaf evalutator
86       * @throws Exception if there are schema initialization problems
87       */
88      @Before public void setUp() throws Exception
89      {
90          OidRegistry registry = registries.getOidRegistry();
91          RefinementLeafEvaluator leafEvaluator = new RefinementLeafEvaluator( registry );
92          evaluator = new RefinementEvaluator( leafEvaluator );
93          
94          OBJECT_CLASS = registries.getAttributeTypeRegistry().lookup( "objectClass" );
95          CN = registries.getAttributeTypeRegistry().lookup( "cn" );
96      }
97  
98  
99      /**
100      * Sets evaluator and registries to null.
101      */
102     @After public void tearDown()
103     {
104         evaluator = null;
105     }
106 
107 
108     /**
109      * Test cases for various bad combinations of arguments
110      * @throws Exception if something goes wrong
111      */
112     @Test public void testForBadArguments() throws Exception
113     {
114         try
115         {
116             assertFalse( evaluator.evaluate( null, new DefaultServerAttribute( "objectClass", OBJECT_CLASS ) ) );
117             fail( "should never get here due to an IAE" );
118         }
119         catch ( IllegalArgumentException iae )
120         {
121         }
122 
123         try
124         {
125             assertFalse( evaluator.evaluate( new EqualityNode( "", new ClientStringValue( "" ) ), null ) );
126             fail( "should never get here due to an IAE" );
127         }
128         catch ( IllegalArgumentException iae )
129         {
130         }
131 
132         try
133         {
134             assertFalse( evaluator.evaluate( new EqualityNode( "", new ClientStringValue( "" ) ), new DefaultServerAttribute( "cn", CN ) ) );
135             fail( "should never get here due to an IAE" );
136         }
137         catch ( IllegalArgumentException iae )
138         {
139         }
140     }
141 
142 
143     @Test public void testMatchByName() throws Exception
144     {
145         ServerAttribute objectClasses = null;
146 
147         // positive test
148         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
149         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
150 
151         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person", "blah" );
152         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
153 
154         // negative tests
155         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
156         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "blah" ) ), objectClasses ) );
157 
158         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "blah" );
159         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
160     }
161 
162 
163     @Test public void testMatchByOID() throws Exception
164     {
165         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
166         
167         // positive test
168         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.6" ) ), objectClasses ) );
169 
170         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person", "blah" );
171         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.6" ) ), objectClasses ) );
172 
173         // negative tests
174         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
175         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.5" ) ), objectClasses ) );
176 
177         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "blah" );
178         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.5" ) ), objectClasses ) );
179     }
180 
181 
182     @Test public void testComplexOrRefinement() throws Exception
183     {
184         ExprNode refinement = null;
185         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
186         String refStr = "(|(objectClass=person)(objectClass=organizationalUnit))";
187         
188         refinement = FilterParser.parse( refStr );
189 
190         assertTrue( evaluator.evaluate( refinement, objectClasses ) );
191         
192         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "organizationalUnit" );
193         assertTrue( evaluator.evaluate( refinement, objectClasses ) );
194         
195         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "domain" );
196         assertFalse( evaluator.evaluate( refinement, objectClasses ) );
197     }
198 
199 
200     @Test public void testComplexAndRefinement() throws Exception
201     {
202         ExprNode refinement = null;
203         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
204         objectClasses.add( "organizationalUnit" );
205         String refStr = "(&(objectClass=person)(objectClass=organizationalUnit))";
206         
207         refinement = FilterParser.parse( refStr );
208 
209         assertTrue( evaluator.evaluate( refinement, objectClasses ) );
210         
211         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "organizationalUnit" );
212         assertFalse( evaluator.evaluate( refinement, objectClasses ) );
213         
214         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
215         assertFalse( evaluator.evaluate( refinement, objectClasses ) );
216         
217         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "domain" );
218         assertFalse( evaluator.evaluate( refinement, objectClasses ) );
219     }
220 
221 
222     @Test public void testComplexNotRefinement() throws Exception
223     {
224         ExprNode refinement = null;
225         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
226         String refStr = "(!(objectClass=person))";
227 
228         refinement = FilterParser.parse( refStr );
229 
230         assertFalse( evaluator.evaluate( refinement, objectClasses ) );
231         
232         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "organizationalUnit" );
233         assertTrue( evaluator.evaluate( refinement, objectClasses ) );
234         
235         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "domain" );
236         assertTrue( evaluator.evaluate( refinement, objectClasses ) );
237 
238         try
239         {
240             assertFalse( evaluator.evaluate( new NotNode(), new DefaultServerAttribute( "objectClass", OBJECT_CLASS ) ) );
241             fail( "should never get here due to an IAE" );
242         }
243         catch ( IllegalArgumentException iae )
244         {
245         }
246     }
247 }