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  
21  package org.apache.directory.server.core.subtree;
22  
23  
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.integ.CiRunner;
26  import static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
27  import org.apache.directory.shared.ldap.constants.SchemaConstants;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertNull;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  
33  import javax.naming.NamingEnumeration;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.BasicAttribute;
38  import javax.naming.directory.BasicAttributes;
39  import javax.naming.directory.DirContext;
40  import javax.naming.directory.ModificationItem;
41  import javax.naming.directory.SearchControls;
42  import javax.naming.directory.SearchResult;
43  import javax.naming.ldap.LdapContext;
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  
48  /**
49   * Testcases for the SubentryInterceptor. Investigation on handling Subtree Refinement
50   * Selection Membership upon objectClass attribute value changes.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev$
54   */
55  @RunWith ( CiRunner.class )
56  public class SubentryServiceObjectClassChangeHandlingIT 
57  {
58      public static DirectoryService service;
59  
60  
61      public Attributes getTestEntry( String cn )
62      {
63          Attributes entry = new BasicAttributes( true );
64          Attribute objectClass = new BasicAttribute( "objectClass" );
65          objectClass.add( "top" );
66          objectClass.add( "person" );
67          entry.put( objectClass );
68          entry.put( "cn", cn );
69          entry.put( "sn", cn );
70          return entry;
71      }
72  
73  
74      public Attributes getModsForIntroducingNewOC() throws NamingException
75      {
76          Attributes changes = new BasicAttributes( true );
77          Attribute objectClass = new BasicAttribute( "objectClass" );
78          objectClass.add( "organizationalPerson" );
79          changes.put( objectClass );
80          changes.put( "ou", "Test Organizational Unit" );
81          return changes;
82      }
83  
84  
85      public Attributes getCollectiveAttributeTestSubentry( String cn )
86      {
87          Attributes subentry = new BasicAttributes( true );
88          Attribute objectClass = new BasicAttribute( "objectClass" );
89          objectClass.add( "top" );
90          objectClass.add( SchemaConstants.SUBENTRY_OC );
91          objectClass.add( "collectiveAttributeSubentry" );
92          subentry.put( objectClass );
93          subentry.put( "subtreeSpecification", "{ specificationFilter item:organizationalPerson }" );
94          subentry.put( "c-o", "Test Org" );
95          subentry.put( "cn", cn );
96          return subentry;
97      }
98  
99  
100     public void addAdministrativeRoles() throws Exception
101     {
102         LdapContext sysRoot = getSystemContext( service );
103         Attribute attribute = new BasicAttribute( "administrativeRole" );
104         attribute.add( "autonomousArea" );
105         attribute.add( "collectiveAttributeSpecificArea" );
106         ModificationItem item = new ModificationItem( DirContext.ADD_ATTRIBUTE, attribute );
107         sysRoot.modifyAttributes( "", new ModificationItem[]
108             { item } );
109     }
110 
111 
112     public Map<String, Attributes> getAllEntries() throws Exception
113     {
114         LdapContext sysRoot = getSystemContext( service );
115         Map<String, Attributes> resultMap = new HashMap<String, Attributes>();
116         SearchControls controls = new SearchControls();
117         controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
118         controls.setReturningAttributes( new String[]
119             { "+", "*" } );
120         NamingEnumeration<SearchResult> results = sysRoot.search( "", "(objectClass=*)", controls );
121         
122         while ( results.hasMore() )
123         {
124             SearchResult result = results.next();
125             resultMap.put( result.getName(), result.getAttributes() );
126         }
127         return resultMap;
128     }
129     
130 
131     @Test
132     public void testTrackingOfOCChangesInSubentryServiceModifyRoutine() throws Exception
133     {
134         LdapContext sysRoot = getSystemContext( service );
135         addAdministrativeRoles();
136         sysRoot.createSubcontext( "cn=collectiveAttributeTestSubentry",
137             getCollectiveAttributeTestSubentry( "collectiveAttributeTestSubentry" ) );
138         sysRoot.createSubcontext( "cn=testEntry", getTestEntry( "testEntry" ) );
139 
140         //----------------------------------------------------------------------
141 
142         Map<String, Attributes> results = getAllEntries();
143         Attributes testEntry = results.get( "cn=testEntry,ou=system" );
144 
145         Attribute collectiveAttributeSubentries = testEntry.get( "collectiveAttributeSubentries" );
146         
147         assertNull( collectiveAttributeSubentries );
148 
149         //----------------------------------------------------------------------
150 
151         sysRoot.modifyAttributes( "cn=testEntry", DirContext.ADD_ATTRIBUTE, getModsForIntroducingNewOC() );
152 
153         results = getAllEntries();
154         testEntry = ( Attributes ) results.get( "cn=testEntry,ou=system" );
155 
156         collectiveAttributeSubentries = testEntry.get( "collectiveAttributeSubentries" );
157         assertNotNull( collectiveAttributeSubentries );
158     }
159 
160 }