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.RefinementLeafEvaluator;
30  import org.apache.directory.server.schema.registries.OidRegistry;
31  import org.apache.directory.server.schema.registries.Registries;
32  import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
33  import org.apache.directory.shared.ldap.filter.EqualityNode;
34  import org.apache.directory.shared.ldap.filter.GreaterEqNode;
35  import org.apache.directory.shared.ldap.schema.AttributeType;
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import static org.junit.Assert.assertFalse;
41  import static org.junit.Assert.assertTrue;
42  import static org.junit.Assert.fail;
43  
44  
45  /**
46   * Unit test cases for testing the evaluator for refinement leaf nodes.
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   * @version $Rev: 655126 $
50   */
51  public class RefinementLeafEvaluatorTest
52  {
53      /** The ObjectClass AttributeType */
54      private static AttributeType OBJECT_CLASS;
55  
56      /** A reference to the directory service */
57      private static DirectoryService service;
58      
59      /** the registries */
60      private static Registries registries;
61  
62      /** the refinement leaf evaluator to test */
63      private RefinementLeafEvaluator evaluator;
64  
65  
66      /**
67       * Initializes the global registries.
68       * @throws javax.naming.NamingException if there is a failure loading the schema
69       */
70      @BeforeClass public static void init() throws NamingException
71      {
72          service = new DefaultDirectoryService();
73          registries = service.getRegistries();
74          OBJECT_CLASS = registries.getAttributeTypeRegistry().lookup( "objectClass" );
75      }
76      
77  
78      /**
79       * Initializes registries and creates the leaf evalutator
80       * @throws Exception if there are schema initialization problems
81       */
82      @Before public void setUp() throws Exception
83      {
84          OidRegistry registry = registries.getOidRegistry();
85          evaluator = new RefinementLeafEvaluator( registry );
86      }
87  
88  
89      /**
90       * Sets evaluator and registries to null.
91       */
92      @After public void tearDown()
93      {
94          evaluator = null;
95      }
96  
97  
98      /**
99       * Test cases for various bad combinations of arguments
100      * @throws Exception if something goes wrongg
101      */
102     @Test public void testForBadArguments() throws Exception
103     {
104         ServerAttribute objectClasses = null;
105 
106         try
107         {
108             assertFalse( evaluator.evaluate( null, null ) );
109             fail( "should never get here due to an IAE" );
110         }
111         catch ( IllegalArgumentException iae )
112         {
113         }
114 
115         try
116         {
117             assertFalse( evaluator.evaluate( new GreaterEqNode( "", new ClientStringValue( "" ) ), objectClasses ) );
118             fail( "should never get here due to an NE" );
119         }
120         catch ( NamingException ne )
121         {
122         }
123 
124         try
125         {
126             assertFalse( evaluator.evaluate( new EqualityNode( "", new ClientStringValue( "" ) ), objectClasses ) );
127             fail( "should never get here due to an NE" );
128         }
129         catch ( NamingException ne )
130         {
131         }
132 
133         try
134         {
135             assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "" ) ), objectClasses ) );
136             fail( "should never get here due to an IAE" );
137         }
138         catch ( IllegalArgumentException iae )
139         {
140         }
141 
142         try
143         {
144             objectClasses = new DefaultServerAttribute( "cn", OBJECT_CLASS );
145             assertFalse( evaluator.evaluate( new EqualityNode( "cn", new ClientStringValue( "" ) ), objectClasses ) );
146             fail( "should never get here due to an IAE" );
147         }
148         catch ( NamingException ne )
149         {
150             assertTrue( true );
151         }
152     }
153 
154 
155     @Test public void testMatchByName() throws Exception
156     {
157         // positive test
158         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
159         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
160 
161         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS );
162         objectClasses.add( "person" );
163         objectClasses.add( "blah" );
164         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
165 
166         // negative tests
167         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
168         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "blah" ) ), objectClasses ) );
169 
170         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "blah" );
171         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "person" ) ), objectClasses ) );
172     }
173 
174 
175     @Test public void testMatchByOID() throws Exception
176     {
177         ServerAttribute objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
178 
179         // positive test
180         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.6" ) ), objectClasses ) );
181 
182         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS );
183         objectClasses.add( "person" );
184         objectClasses.add( "blah" );
185         assertTrue( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.6" ) ), objectClasses ) );
186 
187         // negative tests
188         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "person" );
189         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.5" ) ), objectClasses ) );
190 
191         objectClasses = new DefaultServerAttribute( "objectClass", OBJECT_CLASS, "blah" );
192         assertFalse( evaluator.evaluate( new EqualityNode( "objectClass", new ClientStringValue( "2.5.6.5" ) ), objectClasses ) );
193     }
194 }