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.partition;
21  
22  
23  import java.util.HashMap;
24  
25  import org.apache.directory.server.core.DefaultDirectoryService;
26  import org.apache.directory.server.core.DirectoryService;
27  import org.apache.directory.server.core.integ.CiRunner;
28  import org.apache.directory.server.core.integ.DirectoryServiceFactory;
29  import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
30  import org.apache.directory.server.core.integ.annotations.Factory;
31  import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
32  
33  import static org.apache.directory.server.core.integ.IntegrationUtils.getRootContext;
34  
35  import static org.junit.Assert.assertTrue;
36  import static org.junit.Assert.assertNotNull;
37  import static org.junit.Assert.fail;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import javax.naming.directory.Attribute;
44  import javax.naming.directory.Attributes;
45  import javax.naming.directory.BasicAttributes;
46  import javax.naming.ldap.LdapContext;
47  
48  
49  
50  /**
51   * Test cases for partition handling.
52   *
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   * @version $Rev$
55   */
56  @RunWith ( CiRunner.class )
57  @Factory ( PartitionIT.Factory.class )
58  @ApplyLdifs (
59      {
60          "dn: dc=foo,dc=com\n" +
61          "objectClass: top\n" +
62          "objectClass: domain\n" +
63          "dc: foo\n\n" +
64  
65          "dn: dc=bar,dc=com\n" +
66          "objectClass: top\n" +
67          "objectClass: domain\n" +
68          "dc: bar\n\n"
69      }
70  )
71  public final class PartitionIT
72  {
73      private static final Logger LOG = LoggerFactory.getLogger( PartitionIT.class );
74      public static DirectoryService service;
75  
76      
77      /**
78       * Creates a DirectoryService configured with two separate dc=com based 
79       * domains to test multiple partitions.
80       */
81      public static class Factory implements DirectoryServiceFactory
82      {
83  		public DirectoryService newInstance() throws Exception 
84  		{
85              DirectoryService service = new DefaultDirectoryService();
86              service.getChangeLog().setEnabled( true );
87              
88              Partition foo = new JdbmPartition();
89              foo.setId( "foo" );
90              foo.setSuffix( "dc=foo,dc=com" );
91              service.addPartition( foo );
92              
93              Partition bar = new JdbmPartition();
94              bar.setId( "bar" );
95              bar.setSuffix( "dc=bar,dc=com" );
96              service.addPartition( bar );
97              
98              return service;
99  		}
100     }
101     
102 
103     /**
104      * Test case to weed out issue in DIRSERVER-1118.
105      *
106      * @see https://issues.apache.org/jira/browse/DIRSERVER-1118
107      */
108     @Test
109     public void testDIRSERVER_1118() throws Exception
110     {
111         /*
112          * Confirm the presence of the partitions foo and bar through DS API
113          */
114         HashMap<String, Partition> partitionMap = new HashMap<String, Partition>();
115         
116         for ( Partition partition : service.getPartitions() )
117         {
118             LOG.debug( "partition id = {}", partition.getId() );
119             partitionMap.put( partition.getId(), partition );
120         }
121         
122         assertNotNull( partitionMap.containsKey( "foo" ) );
123         assertNotNull( partitionMap.containsKey( "bar" ) );
124 
125         /*
126          * Confirm presence and publishing of foo and bar partitions as 
127          * namingContexts as values innamingContexts attribute of the rootDSE
128          */
129         LdapContext rootDSE = getRootContext( service );
130         Attribute namingContexts = rootDSE.getAttributes( "", 
131             new String[] { "namingContexts" } ).get( "namingContexts" );
132         assertTrue( namingContexts.contains( "dc=foo,dc=com" ) );
133         assertTrue( namingContexts.contains( "dc=bar,dc=com" ) );
134         LOG.debug( "Found both dc=foo,dc=com and dc=bar,dc=com in namingContexts" );
135         
136         /*
137          * Add, lookup, then delete entry in both foo and bar partitions
138          */
139         addLookupDelete( "dc=foo,dc=com" );
140         addLookupDelete( "dc=bar,dc=com" );
141     }
142     
143 
144     /**
145      * Given the suffix DN of a partition this method will add an entry, look 
146      * it up, then delete it making sure all checks out.
147      *
148      * @param partitionSuffix the DN of the partition suffix
149      */
150     public void addLookupDelete( String partitionSuffix ) throws Exception
151     {
152         LdapContext rootDSE = getRootContext( service );
153         Attributes attrs = new BasicAttributes( "objectClass", "organizationalUnit", true );
154         attrs.put( "ou", "people" );
155         String entryDn = "ou=people," + partitionSuffix;
156         rootDSE.createSubcontext( entryDn, attrs );
157         LOG.debug( "added entry {} to partition {}", entryDn, partitionSuffix );
158         
159         Attributes reloaded = rootDSE.getAttributes( entryDn );
160         assertNotNull( reloaded );
161         assertTrue( reloaded.get( "ou" ).contains( "people" ) );
162         LOG.debug( "looked up entry {} from partition {}", entryDn, partitionSuffix );
163         
164         rootDSE.destroySubcontext( entryDn );
165         try
166         {
167             rootDSE.getAttributes( entryDn );
168             fail( "should never get here" );
169         }
170         catch ( Exception e )
171         {
172             LOG.debug( "Successfully deleted entry {} from partition {}", entryDn, partitionSuffix );
173         }
174     }
175 }