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.jndi;
21  
22  
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.integ.CiRunner;
25  import static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
26  import org.junit.Test;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertTrue;
27  import org.junit.runner.RunWith;
28  
29  import javax.naming.directory.Attribute;
30  import javax.naming.directory.Attributes;
31  import javax.naming.directory.BasicAttribute;
32  import javax.naming.directory.BasicAttributes;
33  import javax.naming.directory.DirContext;
34  import javax.naming.ldap.LdapContext;
35  
36  
37  /**
38   * Tests the use of extensible objects.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev: 691024 $
42   */
43  @RunWith ( CiRunner.class )
44  public class ExtensibleObjectIT
45  {
46      public static DirectoryService service;
47  
48  
49      @Test
50      public void testExtensibleObjectModify() throws Exception
51      {
52          LdapContext sysRoot = getSystemContext( service );
53          Attributes attributes = new BasicAttributes( true );
54          Attribute attribute = new BasicAttribute( "objectClass" );
55          attribute.add( "top" );
56          attribute.add( "organizationalUnit" );
57          attributes.put( attribute );
58          attributes.put( "ou", "testing00" );
59          DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
60          assertNotNull( ctx );
61  
62          ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
63          assertNotNull( ctx );
64  
65          attributes = ctx.getAttributes( "" );
66          assertNotNull( attributes );
67          assertEquals( "testing00", attributes.get( "ou" ).get() );
68          attribute = attributes.get( "objectClass" );
69          assertNotNull( attribute );
70          assertTrue( attribute.contains( "top" ) );
71          assertTrue( attribute.contains( "organizationalUnit" ) );
72  
73          Attributes newattribs = new BasicAttributes( true );
74          Attribute freeform = new BasicAttribute( "cn" );
75          freeform.add( "testing" );
76          newattribs.put( freeform );
77          Attribute objectClass = new BasicAttribute( "objectClass" );
78          objectClass.add( "top" );
79          objectClass.add( "extensibleObject" );
80          objectClass.add( "organizationalUnit" );
81          newattribs.put( objectClass );
82          ctx.modifyAttributes( "", DirContext.REPLACE_ATTRIBUTE, newattribs );
83  
84          attributes = ctx.getAttributes( "" );
85          assertNotNull( attributes );
86          assertEquals( "testing00", attributes.get( "ou" ).get() );
87          attribute = attributes.get( "objectClass" );
88          assertNotNull( attribute );
89          assertTrue( attribute.contains( "top" ) );
90          assertTrue( attribute.contains( "organizationalUnit" ) );
91          assertTrue( attribute.contains( "extensibleObject" ) );
92          attribute = attributes.get( "cn" );
93          assertTrue( attribute.contains( "testing" ) );
94      }
95  
96  
97      @Test
98      public void testExtensibleObjectAdd() throws Exception
99      {
100         LdapContext sysRoot = getSystemContext( service );
101         Attributes attributes = new BasicAttributes( true );
102         Attribute attribute = new BasicAttribute( "objectClass" );
103         attribute.add( "top" );
104         attribute.add( "extensibleObject" );
105         attribute.add( "organizationalUnit" );
106         attributes.put( attribute );
107         attributes.put( "ou", "testing00" );
108 
109         // WARNING: extensible objects cannot accept any arbitrary 
110         // attribute.  The attribute must be defined by the schema
111         // at a bare minimum or the addition will be rejected
112 
113         // here's an attribute that is not on the MAY or MUST list for 
114         // an organizationalUnit - it's our test for extensible objects
115         attributes.put( "employeeType", "testing" );
116 
117         DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
118         assertNotNull( ctx );
119 
120         ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
121         assertNotNull( ctx );
122 
123         attributes = ctx.getAttributes( "" );
124         assertNotNull( attributes );
125         assertEquals( "testing00", attributes.get( "ou" ).get() );
126         attribute = attributes.get( "objectClass" );
127         assertNotNull( attribute );
128         assertTrue( attribute.contains( "top" ) );
129         assertTrue( attribute.contains( "extensibleObject" ) );
130         assertTrue( attribute.contains( "organizationalUnit" ) );
131         attribute = attributes.get( "employeeType" );
132         assertTrue( attribute.contains( "testing" ) );
133     }
134 }