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.schema;
21  
22  
23  import org.apache.directory.server.constants.MetaSchemaConstants;
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.*;
27  import org.apache.directory.shared.ldap.name.LdapDN;
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.NamingException;
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  
39  
40  @RunWith ( CiRunner.class )
41  public class ObjectClassCreateIT
42  {
43      private String testOID = "1.3.6.1.4.1.18060.0.4.0.3.1.555555.5555.5555555";
44  
45  
46      public static DirectoryService service;
47  
48  
49      /**
50       * Gets relative DN to ou=schema.
51       *
52       * @param schemaName the name of the schema
53       * @return the dn of the objectClass container
54       * @throws NamingException on error
55       */
56      private LdapDN getObjectClassContainer( String schemaName ) throws NamingException
57      {
58          return new LdapDN( "ou=objectClasses,cn=" + schemaName );
59      }
60  
61  
62      /*
63       * Test that I can create an ObjectClass entry with an invalid
64       */
65      @Test
66      public void testCreateObjectClassWithInvalidNameAttribute() throws Exception
67      {
68          Attributes attributes = new BasicAttributes();
69          Attribute  objectClassAttribute = new BasicAttribute( "objectClass" );
70          
71          objectClassAttribute.add( "top" );
72          objectClassAttribute.add( "metaTop" );
73          objectClassAttribute.add( "metaObjectClass" );
74          
75          attributes.put( objectClassAttribute );
76          
77          attributes.put( "m-oid", "testOID" );
78          
79          // This name is invalid
80          attributes.put( "m-name", "http://example.com/users/accounts/L0" );
81          
82          LdapDN dn = getObjectClassContainer( "apachemeta" );
83          dn.add( MetaSchemaConstants.M_OID_AT + "=" + testOID );
84          
85          try
86          {
87              getSchemaContext( service ).createSubcontext( dn, attributes );
88              fail(); // Should not reach this point
89          }
90          catch ( NamingException ne )
91          {
92              assertTrue( true );
93          }
94      }
95  
96      /*
97       * Test that I can create an ObjectClass entry with an invalid name
98       */
99      @Test
100     public void testCreateObjectClassWithNoObjectClass() throws Exception
101     {
102         Attributes attributes = new BasicAttributes();
103         Attribute  objectClassAttribute = new BasicAttribute( "objectClass" );
104         
105         objectClassAttribute.add( "top" );
106         objectClassAttribute.add( "metaTop" );
107         objectClassAttribute.add( "metaObjectClass" );
108         
109         // Don't put the objectclasses in the entry : this is in purpose !
110         // attributes.put( objectClassAttribute );
111         
112         attributes.put( "m-oid", "testOID" );
113         
114         // This name is invalid
115         attributes.put( "m-name", "no-objectClasses" );
116         
117         LdapDN dn = getObjectClassContainer( "apachemeta" );
118         dn.add( MetaSchemaConstants.M_OID_AT + "=" + testOID );
119         
120         try
121         {
122             getSchemaContext( service ).createSubcontext( dn, attributes );
123             fail(); // Should not reach this point
124         }
125         catch ( NamingException ne )
126         {
127             assertTrue( true );
128         }
129     }
130 }