1 package org.apache.archiva.redback.components.apacheds;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
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
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
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 }