View Javadoc

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  package org.apache.directory.server.core.unit;
20  
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.directory.server.core.CoreSession;
24  import org.apache.directory.server.core.entry.DefaultServerEntry;
25  import org.apache.directory.shared.ldap.entry.EntryAttribute;
26  import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
27  import org.apache.directory.shared.ldap.ldif.ChangeType;
28  import org.apache.directory.shared.ldap.ldif.LdifEntry;
29  import org.apache.directory.shared.ldap.name.LdapDN;
30  import org.apache.directory.shared.ldap.name.Rdn;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.naming.InvalidNameException;
35  import javax.naming.NamingException;
36  import java.io.File;
37  import java.io.IOException;
38  
39  
40  /**
41   * Integration test utility methods.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$, $Date$
45   */
46  public class IntegrationUtils
47  {
48      private static final Logger LOG = LoggerFactory.getLogger( IntegrationUtils.class );
49  
50  
51      /**
52       * Deletes the working directory.
53       *
54       * @param wkdir the working directory to delete
55       * @throws IOException if the working directory cannot be deleted
56       */
57      public static void doDelete( File wkdir ) throws IOException
58      {
59          if ( wkdir.exists() )
60          {
61              try
62              {
63                  FileUtils.deleteDirectory( wkdir );
64              }
65              catch ( IOException e )
66              {
67                  LOG.error( "Failed to delete the working directory.", e );
68              }
69          }
70          if ( wkdir.exists() )
71          {
72              throw new IOException( "Failed to delete: " + wkdir );
73          }
74      }
75  
76  
77      public static LdifEntry getUserAddLdif() throws InvalidNameException, NamingException
78      {
79          return getUserAddLdif( "uid=akarasulu,ou=users,ou=system", "test".getBytes(), "Alex Karasulu", "Karasulu" );
80      }
81  
82  
83  
84      public static void apply( CoreSession root, LdifEntry entry ) throws Exception
85      {
86          LdapDN dn = new LdapDN( entry.getDn() );
87  
88          switch( entry.getChangeType().getChangeType() )
89          {
90              case( ChangeType.ADD_ORDINAL ):
91                  root.add( 
92                      new DefaultServerEntry( 
93                          root.getDirectoryService().getRegistries(), entry.getEntry() ) ); 
94                  break;
95                  
96              case( ChangeType.DELETE_ORDINAL ):
97                  root.delete( entry.getDn() );
98                  break;
99                  
100             case( ChangeType.MODDN_ORDINAL ):
101             case( ChangeType.MODRDN_ORDINAL ):
102                 Rdn newRdn = new Rdn( entry.getNewRdn() );
103                 
104                 if ( entry.getNewSuperior() != null )
105                 {
106                     // It's a move. The superior have changed
107                     // Let's see if it's a rename too
108                     Rdn oldRdn = dn.getRdn();
109                     LdapDN newSuperior = new LdapDN( entry.getNewSuperior() );
110                     
111                     if ( dn.size() == 0 )
112                     {
113                         throw new IllegalStateException( "can't move the root DSE" );
114                     }
115                     else if ( oldRdn.equals( newRdn ) )
116                     {
117                         // Same rdn : it's a move
118                         root.move( dn, newSuperior );
119                     }
120                     else
121                     {
122                         // it's a move and rename 
123                         root.moveAndRename( dn, newSuperior, newRdn, entry.isDeleteOldRdn() );
124                     }
125                 }
126                 else
127                 {
128                     // it's a rename
129                     root.rename( dn, newRdn, entry.isDeleteOldRdn() );
130                 }
131                 
132                 break;
133                 
134             case( ChangeType.MODIFY_ORDINAL ):
135                 root.modify( dn, entry.getModificationItems() );
136                 break;
137 
138             default:
139                 throw new IllegalStateException( "Unidentified change type value: " + entry.getChangeType() );
140         }
141     }
142 
143 
144     public static LdifEntry getUserAddLdif( String dnstr, byte[] password, String cn, String sn )
145             throws InvalidNameException, NamingException
146     {
147         LdapDN dn = new LdapDN( dnstr );
148         LdifEntry ldif = new LdifEntry();
149         ldif.setDn( dnstr );
150         ldif.setChangeType( ChangeType.Add );
151 
152         EntryAttribute attr = new DefaultClientAttribute( "objectClass", 
153             "top", "person", "organizationalPerson", "inetOrgPerson" );
154         
155         ldif.addAttribute( attr );
156 
157         attr = new DefaultClientAttribute( "ou", "Engineering", "People" );
158         ldif.addAttribute( attr );
159 
160         String uid = ( String ) dn.getRdn().getValue();
161         ldif.putAttribute( "uid", uid );
162 
163         ldif.putAttribute( "l", "Bogusville" );
164         ldif.putAttribute( "cn", cn );
165         ldif.putAttribute( "sn", sn );
166         ldif.putAttribute( "mail", uid + "@apache.org" );
167         ldif.putAttribute( "telephoneNumber", "+1 408 555 4798" );
168         ldif.putAttribute( "facsimileTelephoneNumber", "+1 408 555 9751" );
169         ldif.putAttribute( "roomnumber", "4612" );
170         ldif.putAttribute( "userPassword", password );
171 
172         String givenName = cn.split( " " )[0];
173         ldif.putAttribute( "givenName", givenName );
174         return ldif;
175     }
176 }