View Javadoc

1   package org.apache.archiva.redback.components.apacheds;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.directory.shared.ldap.util.AttributeUtils;
24  import org.apache.archiva.redback.components.apacheds.ApacheDs;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.test.context.ContextConfiguration;
30  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31  
32  import javax.inject.Inject;
33  import javax.naming.NamingEnumeration;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.BasicAttribute;
38  import javax.naming.directory.BasicAttributes;
39  import javax.naming.directory.DirContext;
40  import javax.naming.directory.InitialDirContext;
41  import javax.naming.directory.SearchControls;
42  import javax.naming.directory.SearchResult;
43  import java.io.File;
44  
45  /**
46   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
47   * @author Olivier Lamy
48   *
49   */
50  
51  @RunWith( SpringJUnit4ClassRunner.class )
52  @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53  public class ApacheDsTest
54      extends TestCase
55  {
56      private String suffix = "dc=plexus,dc=codehaus,dc=org";
57  
58      @Inject
59      private ApacheDs apacheDs;
60  
61      private Logger logger = LoggerFactory.getLogger( getClass() );
62  
63      protected void setUp()
64          throws Exception
65      {
66          super.setUp();
67  
68  
69                  
70      }
71  
72      @Test
73      public void testBasic()
74          throws Exception
75      {
76  
77          apacheDs.setBasedir( new File( "./target/plexus-home" ) );
78          
79          apacheDs.addSimplePartition( "test", new String[]{"plexus", "codehaus", "org"} ).getSuffix();
80          apacheDs.startServer();
81  
82          InitialDirContext context = apacheDs.getAdminContext();
83  
84          String cn = "trygvis";
85          createUser( context, cn, createDn( cn ) );
86          assertExist( context, createDn( cn ), "cn", cn );
87  
88          cn = "bolla";
89          createUser( context, cn, createDn( cn ) );
90          assertExist( context, createDn( cn ), "cn", cn );
91  
92          SearchControls ctls = new SearchControls();
93  
94          ctls.setDerefLinkFlag( true );
95          ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
96          ctls.setReturningAttributes( new String[] { "*" } );
97  
98          String filter = "(&(objectClass=inetOrgPerson)(cn=trygvis))";        
99          
100         NamingEnumeration<SearchResult> results = context.search( suffix, filter, ctls );
101        
102         assertTrue( "a result should have been returned", results.hasMoreElements() );
103         
104         SearchResult result = results.nextElement();
105         
106         Attributes attrs = result.getAttributes();
107         
108         logger.info("Attributes {}", AttributeUtils.toString( attrs ) );
109         
110         assertFalse( "should only have one result returned", results.hasMoreElements() );
111         
112         apacheDs.stopServer();
113 
114 
115 
116         // ----------------------------------------------------------------------
117         // Start it again
118         // ----------------------------------------------------------------------
119 
120         apacheDs.startServer();
121 
122         context = apacheDs.getAdminContext();
123         
124         assertExist( context, createDn( "trygvis" ), "cn", "trygvis" );
125         context.unbind( createDn( "trygvis" ) );
126         assertExist( context, createDn( "bolla" ), "cn", "bolla" );
127         context.unbind( createDn( "bolla" ) );
128 
129         apacheDs.stopServer();
130     }
131 
132     private void createUser( DirContext context, String cn, String dn )
133         throws NamingException
134     {
135         Attributes attributes = new BasicAttributes();
136         BasicAttribute objectClass = new BasicAttribute( "objectClass" );
137         objectClass.add( "top" );
138         objectClass.add( "inetOrgPerson" );
139         attributes.put( objectClass );
140         attributes.put( "cn", cn );
141         attributes.put( "sn", cn );
142         context.createSubcontext( dn, attributes );
143     }
144 
145     private String createDn( String cn )
146     {
147         return "cn=" + cn + "," + suffix;
148     }
149 
150     private void assertExist( DirContext context, String dn, String attribute, String value )
151         throws NamingException
152     {
153     	SearchControls ctls = new SearchControls();
154 
155         ctls.setDerefLinkFlag( true );
156         ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
157         ctls.setReturningAttributes( new String[] { "*" } );
158     	   	
159     	BasicAttributes matchingAttributes = new BasicAttributes();
160     	matchingAttributes.put( attribute, value );
161     	
162     	NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
163     	//NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls  );
164 
165         assertTrue( results.hasMoreElements() );    	
166     	SearchResult result = results.nextElement();    	
167     	Attributes attrs = result.getAttributes();    	
168     	Attribute testAttr = attrs.get( attribute );    	
169     	assertEquals( value, testAttr.get() );
170       
171     }
172 }