1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.operations.delete;
21
22
23 import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredConnection;
24 import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContextThrowOnRefferal;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import javax.naming.NameNotFoundException;
30 import javax.naming.ReferralException;
31 import javax.naming.ldap.LdapContext;
32
33 import netscape.ldap.LDAPConnection;
34 import netscape.ldap.LDAPConstraints;
35 import netscape.ldap.LDAPControl;
36 import netscape.ldap.LDAPException;
37 import netscape.ldap.LDAPResponse;
38 import netscape.ldap.LDAPResponseListener;
39 import netscape.ldap.LDAPSearchConstraints;
40
41 import org.apache.directory.server.core.integ.Level;
42 import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
43 import org.apache.directory.server.core.integ.annotations.CleanupLevel;
44 import org.apache.directory.server.integ.SiRunner;
45 import org.apache.directory.server.ldap.LdapService;
46 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58
59 @RunWith ( SiRunner.class )
60 @CleanupLevel ( Level.SUITE )
61 @ApplyLdifs( {
62
63 "dn: uid=akarasulu,ou=users,ou=system\n" +
64 "objectClass: uidObject\n" +
65 "objectClass: person\n" +
66 "objectClass: top\n" +
67 "uid: akarasulu\n" +
68 "cn: Alex Karasulu\n" +
69 "sn: karasulu\n\n" +
70
71 "dn: ou=Computers,uid=akarasulu,ou=users,ou=system\n" +
72 "objectClass: organizationalUnit\n" +
73 "objectClass: top\n" +
74 "ou: computers\n" +
75 "description: Computers for Alex\n" +
76 "seeAlso: ou=Machines,uid=akarasulu,ou=users,ou=system\n\n" +
77
78 "dn: uid=akarasuluref,ou=users,ou=system\n" +
79 "objectClass: uidObject\n" +
80 "objectClass: referral\n" +
81 "objectClass: top\n" +
82 "uid: akarasuluref\n" +
83 "ref: ldap://localhost:10389/uid=akarasulu,ou=users,ou=system\n" +
84 "ref: ldap://foo:10389/uid=akarasulu,ou=users,ou=system\n" +
85 "ref: ldap://bar:10389/uid=akarasulu,ou=users,ou=system\n\n"
86 }
87 )
88 public class DeleteIT
89 {
90 private static final Logger LOG = LoggerFactory.getLogger( DeleteIT.class );
91
92 public static LdapService ldapService;
93
94
95
96
97
98
99 @Test
100 public void testNormalDeleteFailContextNotEmpty() throws Exception
101 {
102 LDAPConnection conn = getWiredConnection( ldapService );
103
104
105 try
106 {
107 conn.delete( "uid=akarasulu,ou=users,ou=system" );
108 fail( "Should never get here." );
109 }
110 catch ( LDAPException e )
111 {
112 assertEquals( ResultCodeEnum.NOT_ALLOWED_ON_NON_LEAF.getValue(), e.getLDAPResultCode() );
113 }
114
115 conn.disconnect();
116 }
117
118
119
120
121
122
123 @Test
124 public void testNormalDelete() throws Exception
125 {
126 LDAPConnection conn = getWiredConnection( ldapService );
127
128
129 conn.delete( "ou=computers,uid=akarasulu,ou=users,ou=system" );
130
131
132 try
133 {
134 conn.delete( "uid=elecharny,ou=users,ou=system" );
135 fail( "Should never get here." );
136 }
137 catch ( LDAPException e )
138 {
139 assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
140 }
141
142 conn.disconnect();
143 }
144
145
146
147
148
149
150 @Test
151 public void testDeleteNonExistent() throws Exception
152 {
153 LDAPConnection conn = getWiredConnection( ldapService );
154
155
156 try
157 {
158 conn.delete( "uid=elecharny,ou=users,ou=system" );
159 fail( "Should never get here." );
160 }
161 catch ( LDAPException e )
162 {
163 assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
164 }
165
166 conn.disconnect();
167 }
168
169
170
171
172
173 @Test
174 public void testOnReferralWithManageDsaITControl() throws Exception
175 {
176 LDAPConnection conn = getWiredConnection( ldapService );
177 LDAPConstraints constraints = new LDAPSearchConstraints();
178 constraints.setClientControls( new LDAPControl( LDAPControl.MANAGEDSAIT, true, new byte[0] ) );
179 constraints.setServerControls( new LDAPControl( LDAPControl.MANAGEDSAIT, true, new byte[0] ) );
180 conn.setConstraints( constraints );
181
182
183 conn.delete( "uid=akarasuluref,ou=users,ou=system", constraints );
184
185 try
186 {
187 conn.read( "uid=akarasuluref,ou=users,ou=system", ( LDAPSearchConstraints ) constraints );
188 fail( "Should never get here." );
189 }
190 catch ( LDAPException e )
191 {
192 assertEquals( ResultCodeEnum.NO_SUCH_OBJECT.getValue(), e.getLDAPResultCode() );
193 }
194
195 conn.disconnect();
196 }
197
198
199
200
201
202
203
204 @Test
205 public void testOnReferral() throws Exception
206 {
207 LDAPConnection conn = getWiredConnection( ldapService );
208 LDAPConstraints constraints = new LDAPConstraints();
209 constraints.setReferrals( false );
210 conn.setConstraints( constraints );
211
212
213 LDAPResponseListener listener = null;
214 LDAPResponse response = null;
215
216 listener = conn.delete( "uid=akarasuluref,ou=users,ou=system", null, constraints );
217 response = listener.getResponse();
218 assertEquals( ResultCodeEnum.REFERRAL.getValue(), response.getResultCode() );
219
220 assertEquals( "ldap://localhost:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[0] );
221 assertEquals( "ldap://foo:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[1] );
222 assertEquals( "ldap://bar:10389/uid=akarasulu,ou=users,ou=system", response.getReferrals()[2] );
223
224 conn.disconnect();
225 }
226
227
228
229
230
231
232
233 @Test
234 public void testThrowOnReferralWithJndi() throws Exception
235 {
236 LdapContext ctx = getWiredContextThrowOnRefferal( ldapService );
237
238
239 ctx.destroySubcontext( "ou=computers,uid=akarasulu,ou=users,ou=system" );
240
241 try
242 {
243 ctx.lookup( "ou=computers,uid=akarasulu,ou=users,ou=system" );
244 fail( "Should never get here." );
245 }
246 catch ( NameNotFoundException e )
247 {
248 }
249
250
251 try
252 {
253 ctx.destroySubcontext( "uid=akarasuluref,ou=users,ou=system" );
254 fail( "Should never get here" );
255 }
256 catch ( ReferralException e )
257 {
258
259 assertEquals( "ldap://localhost:10389/uid=akarasulu,ou=users,ou=system", e.getReferralInfo() );
260 }
261
262 ctx.close();
263 }
264
265
266
267
268
269 @Test
270 public void testAncestorReferral() throws Exception
271 {
272 LOG.debug( "" );
273
274 LDAPConnection conn = getWiredConnection( ldapService );
275 LDAPConstraints constraints = new LDAPConstraints();
276 conn.setConstraints( constraints );
277
278
279 LDAPResponseListener listener = null;
280 LDAPResponse response = null;
281
282 listener = conn.delete( "ou=Computers,uid=akarasuluref,ou=users,ou=system", null, constraints );
283 response = listener.getResponse();
284 assertEquals( ResultCodeEnum.REFERRAL.getValue(), response.getResultCode() );
285
286 assertEquals( "ldap://localhost:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[0] );
287 assertEquals( "ldap://foo:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[1] );
288 assertEquals( "ldap://bar:10389/ou=Computers,uid=akarasulu,ou=users,ou=system", response.getReferrals()[2] );
289
290 conn.disconnect();
291 }
292
293
294
295
296
297
298
299 public void testDeleteWithIllegalName() throws Exception
300 {
301 LDAPConnection conn = getWiredConnection( ldapService );
302
303 try
304 {
305 conn.delete("This is an illegal name,dc=example,dc=com" );
306 fail( "deletion should fail" );
307 }
308 catch ( LDAPException e )
309 {
310 assertTrue( e.getLDAPResultCode() == LDAPException.INVALID_DN_SYNTAX ||
311 e.getLDAPResultCode() == LDAPException.NO_SUCH_OBJECT );
312 }
313 }
314 }