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.entry.DefaultServerEntry;
25  import org.apache.directory.server.core.integ.CiRunner;
26  import static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
27  import static org.apache.directory.server.core.integ.IntegrationUtils.getUserAddLdif;
28  import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeValueException;
29  import org.apache.directory.shared.ldap.exception.LdapNoSuchAttributeException;
30  import org.apache.directory.shared.ldap.ldif.LdifEntry;
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertNull;
35  import static org.junit.Assert.assertNotNull;
36  import static org.junit.Assert.fail;
37  import org.junit.Test;
38  import org.junit.runner.RunWith;
39  
40  import javax.naming.NamingException;
41  import javax.naming.directory.Attribute;
42  import javax.naming.directory.Attributes;
43  import javax.naming.directory.BasicAttribute;
44  import javax.naming.directory.BasicAttributes;
45  import javax.naming.directory.DirContext;
46  import javax.naming.ldap.LdapContext;
47  
48  
49  /**
50   * Tests the methods on JNDI contexts that are analogous to entry modify
51   * operations in LDAP.
52   *
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   * @version $Rev: 691024 $
55   */
56  @RunWith ( CiRunner.class )
57  public class ModifyContextIT
58  {
59      public static DirectoryService service;
60  
61  
62      /**
63       * @todo put this into an ldif annotation
64       *
65       * @throws NamingException on error
66       */
67      protected void createData() throws Exception
68      {
69          LdifEntry akarasulu = getUserAddLdif();
70          service.getAdminSession().add( 
71              new DefaultServerEntry( service.getRegistries(), akarasulu.getEntry() ) ); 
72  
73          LdapContext sysRoot = getSystemContext( service );
74  
75          /*
76           * create ou=testing00,ou=system
77           */
78          Attributes attributes = new BasicAttributes( true );
79          Attribute attribute = new BasicAttribute( "objectClass" );
80          attribute.add( "top" );
81          attribute.add( "organizationalUnit" );
82          attributes.put( attribute );
83          attributes.put( "ou", "testing00" );
84          DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
85          assertNotNull( ctx );
86  
87          ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
88          assertNotNull( ctx );
89  
90          attributes = ctx.getAttributes( "" );
91          assertNotNull( attributes );
92          assertEquals( "testing00", attributes.get( "ou" ).get() );
93          attribute = attributes.get( "objectClass" );
94          assertNotNull( attribute );
95          assertTrue( attribute.contains( "top" ) );
96          assertTrue( attribute.contains( "organizationalUnit" ) );
97  
98          /*
99           * create ou=testing01,ou=system
100          */
101         attributes = new BasicAttributes( true );
102         attribute = new BasicAttribute( "objectClass" );
103         attribute.add( "top" );
104         attribute.add( "organizationalUnit" );
105         attributes.put( attribute );
106         attributes.put( "ou", "testing01" );
107         ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
108         assertNotNull( ctx );
109 
110         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
111         assertNotNull( ctx );
112 
113         attributes = ctx.getAttributes( "" );
114         assertNotNull( attributes );
115         assertEquals( "testing01", attributes.get( "ou" ).get() );
116         attribute = attributes.get( "objectClass" );
117         assertNotNull( attribute );
118         assertTrue( attribute.contains( "top" ) );
119         assertTrue( attribute.contains( "organizationalUnit" ) );
120 
121         /*
122          * create ou=testing02,ou=system
123          */
124         attributes = new BasicAttributes( true );
125         attribute = new BasicAttribute( "objectClass" );
126         attribute.add( "top" );
127         attribute.add( "organizationalUnit" );
128         attributes.put( attribute );
129         attributes.put( "ou", "testing02" );
130         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
131         assertNotNull( ctx );
132 
133         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
134         assertNotNull( ctx );
135 
136         attributes = ctx.getAttributes( "" );
137         assertNotNull( attributes );
138         assertEquals( "testing02", attributes.get( "ou" ).get() );
139         attribute = attributes.get( "objectClass" );
140         assertNotNull( attribute );
141         assertTrue( attribute.contains( "top" ) );
142         assertTrue( attribute.contains( "organizationalUnit" ) );
143 
144         /*
145          * create ou=subtest,ou=testing01,ou=system
146          */
147         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
148 
149         attributes = new BasicAttributes( true );
150         attribute = new BasicAttribute( "objectClass" );
151         attribute.add( "top" );
152         attribute.add( "organizationalUnit" );
153         attributes.put( attribute );
154         attributes.put( "ou", "subtest" );
155         ctx = ctx.createSubcontext( "ou=subtest", attributes );
156         assertNotNull( ctx );
157 
158         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
159         assertNotNull( ctx );
160 
161         attributes = ctx.getAttributes( "" );
162         assertNotNull( attributes );
163         assertEquals( "subtest", attributes.get( "ou" ).get() );
164         attribute = attributes.get( "objectClass" );
165         assertNotNull( attribute );
166         assertTrue( attribute.contains( "top" ) );
167         assertTrue( attribute.contains( "organizationalUnit" ) );
168     }
169 
170 
171     /**
172      * Add a new attribute without any value to a person entry: testcase for
173      * http://issues.apache.org/jira/browse/DIRSERVER-630.
174      * 
175      * @throws NamingException on error
176      */
177     @Test
178     public void testIllegalModifyAdd() throws Exception
179     {
180         createData();
181 
182         LdapContext sysRoot = getSystemContext( service );
183 
184         Attribute attr = new BasicAttribute( "description" );
185         Attributes attrs = new BasicAttributes( true );
186         attrs.put( attr );
187 
188         try
189         {
190             sysRoot.modifyAttributes( "uid=akarasulu,ou=users", DirContext.ADD_ATTRIBUTE, attrs );
191             fail( "error expected due to empty attribute value" );
192         }
193         catch ( LdapInvalidAttributeValueException e )
194         {
195             // expected
196         }
197 
198         // Check whether entry is unmodified, i.e. no description
199         Attributes entry = sysRoot.getAttributes( "uid=akarasulu,ou=users" );
200         assertNull( entry.get( "description" ) );
201     }
202 
203 
204 
205     @Test
206     public void testModifyOperation() throws Exception
207     {
208         createData();
209 
210         LdapContext sysRoot = getSystemContext( service );
211         Attributes attributes = new BasicAttributes( true );
212         attributes.put( "ou", "testCases" );
213         sysRoot.modifyAttributes( "ou=testing00", DirContext.ADD_ATTRIBUTE, attributes );
214 
215         DirContext ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
216         attributes = ctx.getAttributes( "" );
217         assertTrue( attributes.get( "ou" ).contains( "testCases" ) );
218 
219         Attribute attribute = attributes.get( "creatorsName" );
220         assertNull( attribute );
221 
222         attribute = attributes.get( "createTimestamp" );
223         assertNull( attribute );
224 
225         attribute = attributes.get( "modifiersName" );
226         assertNull( attribute );
227 
228         attributes.get( "modifyTimestamp" );
229         assertNull( attribute );
230     }
231 
232 
233     /**
234      * Test that if we try to remove a non existing attribute,
235      * we get a correct LdapNoSuchAttributeException
236      * 
237      * The test is currently disabled
238      */
239     //@Test
240     public void testRemoveNonExistingValueException() throws Exception
241     {
242         createData();
243 
244         LdapContext sysRoot = getSystemContext( service );
245         Attributes attributes = new BasicAttributes( true );
246         attributes.put( "ou", "testCases" );
247         
248         try
249         {
250             sysRoot.modifyAttributes( "ou=testing00", DirContext.REMOVE_ATTRIBUTE, attributes );
251             fail();
252         }
253         catch ( LdapNoSuchAttributeException lnsae )
254         {
255             // Expected
256             assertTrue( true );
257         }
258     }
259 
260     
261     @Test
262     public void testRemoveNonExistingValue() throws Exception
263     {
264         createData();
265 
266         LdapContext sysRoot = getSystemContext( service );
267         Attributes attributes = new BasicAttributes( true );
268         attributes.put( "ou", "testCases" );
269         sysRoot.modifyAttributes( "ou=testing00", DirContext.REMOVE_ATTRIBUTE, attributes );
270 
271         DirContext ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
272         attributes = ctx.getAttributes( "" );
273         assertFalse( attributes.get( "ou" ).contains( "testCases" ) );
274 
275         Attribute attribute = attributes.get( "creatorsName" );
276         assertNull( attribute );
277 
278         attribute = attributes.get( "createTimestamp" );
279         assertNull( attribute );
280 
281         attribute = attributes.get( "modifiersName" );
282         assertNull( attribute );
283 
284         attributes.get( "modifyTimestamp" );
285         assertNull( attribute );
286     }
287 }