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.apache.directory.shared.ldap.exception.LdapInvalidAttributeValueException;
27  import org.apache.directory.shared.ldap.exception.LdapSchemaViolationException;
28  import static org.junit.Assert.assertTrue;
29  import static org.junit.Assert.fail;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  
33  import javax.naming.directory.Attribute;
34  import javax.naming.directory.Attributes;
35  import javax.naming.directory.BasicAttribute;
36  import javax.naming.directory.BasicAttributes;
37  import javax.naming.ldap.LdapContext;
38  
39  
40  /**
41   * Contributed by Luke Taylor to fix DIRSERVER-169.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$
45   */
46  @RunWith ( CiRunner.class )
47  public class AddIT
48  {
49      public static DirectoryService service;
50      
51  
52  //    /**
53  //     * Test that attribute name case is preserved after adding an entry
54  //     * in the case the user added them.  This is to test DIRSERVER-832.
55  //     */
56  //    public void testAddCasePreservedOnAttributeNames() throws Exception
57  //    {
58  //        Attributes attrs = new AttributesImpl( true );
59  //        Attribute oc = new AttributeImpl( "ObjectClass", "top" );
60  //        oc.add( "PERSON" );
61  //        oc.add( "organizationalPerson" );
62  //        oc.add( "inetORGperson" );
63  //        Attribute cn = new AttributeImpl( "Cn", "Kevin Spacey" );
64  //        Attribute dc = new AttributeImpl( "sN", "Spacey" );
65  //        attrs.put( oc );
66  //        attrs.put( cn );
67  //        attrs.put( dc);
68  //        sysRoot.createSubcontext( "uID=kevin", attrs );
69  //        Attributes returned = sysRoot.getObject( "UID=kevin" );
70  //        
71  //        NamingEnumeration attrList = returned.getAll();
72  //        while( attrList.hasMore() )
73  //        {
74  //            Attribute attr = ( Attribute ) attrList.next();
75  //            
76  //            if ( attr.getID().equalsIgnoreCase( "uid" ) )
77  //            {
78  //                assertEquals( "uID", attr.getID() );
79  //            }
80  //            
81  //            if ( attr.getID().equalsIgnoreCase( "objectClass" ) )
82  //            {
83  //                assertEquals( "ObjectClass", attr.getID() );
84  //            }
85  //            
86  //            if ( attr.getID().equalsIgnoreCase( "sn" ) )
87  //            {
88  //                assertEquals( "sN", attr.getID() );
89  //            }
90  //            
91  //            if ( attr.getID().equalsIgnoreCase( "cn" ) )
92  //            {
93  //                assertEquals( "Cn", attr.getID() );
94  //            }
95  //        }
96  //    }
97      
98      
99      /**
100      * Test that we can't add an entry with an attribute type not within
101      * any of the MUST or MAY of any of its objectClasses
102      * 
103      * @throws Exception on error
104      */
105     @Test
106     public void testAddAttributesNotInObjectClasses() throws Exception
107     {
108         LdapContext sysRoot = getSystemContext( service );
109 
110         Attributes attrs = new BasicAttributes( true );
111         Attribute oc = new BasicAttribute( "ObjectClass", "top" );
112         Attribute cn = new BasicAttribute( "cn", "kevin Spacey" );
113         Attribute dc = new BasicAttribute( "dc", "ke" );
114         attrs.put( oc );
115         attrs.put( cn );
116         attrs.put( dc);
117 
118         String base = "uid=kevin";
119 
120         //create subcontext
121         try
122         {
123             sysRoot.createSubcontext( base, attrs );
124             fail( "Should not reach this state" );
125         }
126         catch ( LdapSchemaViolationException e )
127         {
128             assertTrue( true );
129         }
130     }
131 
132 
133     /**
134      * Test that we can't add an entry with an attribute with a bad syntax
135      *
136      * @throws Exception on error
137      */
138     @Test
139     public void testAddAttributesBadSyntax() throws Exception
140     {
141         LdapContext sysRoot = getSystemContext( service );
142 
143         Attributes attrs = new BasicAttributes( true );
144         Attribute oc = new BasicAttribute( "ObjectClass", "top" );
145         oc.add( "person" );
146         Attribute cn = new BasicAttribute( "cn", "kevin Spacey" );
147         Attribute sn = new BasicAttribute( "sn", "ke" );
148         Attribute telephone = new BasicAttribute( "telephoneNumber", "0123456abc" );
149         attrs.put( oc );
150         attrs.put( cn );
151         attrs.put( sn );
152         attrs.put( telephone );
153 
154         String base = "sn=kevin";
155 
156         //create subcontext
157         try
158         {
159             sysRoot.createSubcontext( base, attrs );
160             fail( "Should not reach this state" );
161         }
162         catch ( LdapInvalidAttributeValueException e )
163         {
164             assertTrue( true );
165         }
166     }
167 }