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.directory.Attribute;
35  import javax.naming.directory.Attributes;
36  import javax.naming.directory.BasicAttribute;
37  import javax.naming.directory.BasicAttributes;
38  import javax.naming.directory.DirContext;
39  import javax.naming.directory.ModificationItem;
40  import javax.naming.directory.SearchControls;
41  import javax.naming.directory.SearchResult;
42  import javax.naming.ldap.LdapContext;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  
47  /**
48   * Testcases for the SubentryInterceptor. Investigation on handling Subtree Refinement
49   * Selection Membership upon entry modifications. As we allow any LDAP filter to be
50   * specified as specificationFilter in subtreeSpecifications, any modification on
51   * entries can cause changes on subentry operational attributes.
52   *
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   * @version $Rev$
55   */
56  @RunWith ( CiRunner.class )
57  public class SubentryServiceEntryModificationHandlingIT
58  {
59      public static DirectoryService service;
60  
61  
62      public Attributes getTestEntry( String cn )
63      {
64          Attributes entry = new BasicAttributes( true );
65          Attribute objectClass = new BasicAttribute( "objectClass" );
66          objectClass.add( "top" );
67          objectClass.add( "person" );
68          entry.put( objectClass );
69          entry.put( "cn", cn );
70          entry.put( "sn", cn );
71          return entry;
72      }
73  
74  
75      public Attributes getCollectiveAttributeTestSubentryWithLDAPFilter( String cn, String sn )
76      {
77          Attributes subentry = new BasicAttributes( true );
78          Attribute objectClass = new BasicAttribute( "objectClass" );
79          objectClass.add( "top" );
80          objectClass.add( SchemaConstants.SUBENTRY_OC );
81          objectClass.add( "collectiveAttributeSubentry" );
82          subentry.put( objectClass );
83          subentry.put( "subtreeSpecification", "{ specificationFilter (sn=" + sn + ") }" );
84          subentry.put( "c-o", "Test Org" );
85          subentry.put( "cn", cn );
86          return subentry;
87      }
88  
89  
90      public void addAdministrativeRoles() throws Exception
91      {
92          LdapContext sysRoot = getSystemContext( service );
93          Attribute attribute = new BasicAttribute( "administrativeRole" );
94          attribute.add( "autonomousArea" );
95          attribute.add( "collectiveAttributeSpecificArea" );
96          ModificationItem item = new ModificationItem( DirContext.ADD_ATTRIBUTE, attribute );
97          sysRoot.modifyAttributes( "", new ModificationItem[]
98              { item } );
99      }
100 
101 
102     public Map<String, Attributes> getAllEntries() throws Exception
103     {
104         LdapContext sysRoot = getSystemContext( service );
105         Map<String, Attributes> resultMap = new HashMap<String, Attributes>();
106         SearchControls controls = new SearchControls();
107         controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
108         controls.setReturningAttributes( new String[]
109             { "+", "*" } );
110         NamingEnumeration<SearchResult> results = sysRoot.search( "", "(objectClass=*)", controls );
111         
112         while ( results.hasMore() )
113         {
114             SearchResult result = results.next();
115             resultMap.put( result.getName(), result.getAttributes() );
116         }
117         
118         return resultMap;
119     }
120     
121 
122     @Test
123     public void testTrackingOfEntryModificationsInSubentryServiceModifyRoutine() throws Exception
124     {
125         LdapContext sysRoot = getSystemContext( service );
126         addAdministrativeRoles();
127         sysRoot.createSubcontext( "cn=collectiveAttributeTestSubentry",
128             getCollectiveAttributeTestSubentryWithLDAPFilter( "collectiveAttributeTestSubentry", "testEntry" ) );
129         sysRoot.createSubcontext( "cn=testEntry", getTestEntry( "testEntry" ) );
130 
131         //----------------------------------------------------------------------
132 
133         Map<String, Attributes> results = getAllEntries();
134         Attributes testEntry = results.get( "cn=testEntry,ou=system" );
135 
136         Attribute collectiveAttributeSubentries = testEntry.get( "collectiveAttributeSubentries" );
137 
138         assertNotNull( collectiveAttributeSubentries );
139 
140         //----------------------------------------------------------------------
141 
142         Attribute attr = new BasicAttribute( "sn", "changedSn");
143         ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
144         ModificationItem[] mods = new ModificationItem[] { mod };
145         
146         sysRoot.modifyAttributes( "cn=testEntry", mods );
147 
148         results = getAllEntries();
149         testEntry = ( Attributes ) results.get( "cn=testEntry,ou=system" );
150 
151         collectiveAttributeSubentries = testEntry.get( "collectiveAttributeSubentries" );
152 
153         assertNull( collectiveAttributeSubentries );
154     }
155 
156 }