KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > modifyRequest > ModifyRequestTest


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
21 package org.apache.directory.ldapstudio.dsmlv2.modifyRequest;
22
23
24 import java.util.List JavaDoc;
25
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.directory.Attribute JavaDoc;
28 import javax.naming.directory.ModificationItem JavaDoc;
29
30 import org.apache.directory.ldapstudio.dsmlv2.AbstractTest;
31 import org.apache.directory.ldapstudio.dsmlv2.Dsmlv2Parser;
32 import org.apache.directory.shared.ldap.codec.Control;
33 import org.apache.directory.shared.ldap.codec.LdapConstants;
34 import org.apache.directory.shared.ldap.codec.modify.ModifyRequest;
35 import org.apache.directory.shared.ldap.message.ModificationItemImpl;
36 import org.apache.directory.shared.ldap.util.StringTools;
37
38
39 /**
40  * Tests for the Modify Request parsing
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class ModifyRequestTest extends AbstractTest
46 {
47     /**
48      * Test parsing of a request with the (optional) requestID attribute
49      */

50     public void testRequestWithRequestId()
51     {
52         Dsmlv2Parser parser = null;
53         try
54         {
55             parser = new Dsmlv2Parser();
56
57             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_requestID_attribute.xml" )
58                 .getFile() );
59
60             parser.parse();
61         }
62         catch ( Exception JavaDoc e )
63         {
64             fail( e.getMessage() );
65         }
66
67         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
68
69         assertEquals( 456, modifyRequest.getMessageId() );
70     }
71
72
73     /**
74      * Test parsing of a request with the (optional) requestID attribute equals to 0
75      */

76     public void testRequestWithRequestIdEquals0()
77     {
78         testParsingFail( ModifyRequestTest.class, "request_with_requestID_equals_0.xml" );
79     }
80
81
82     /**
83      * Test parsing of a request with a (optional) Control element
84      */

85     public void testRequestWith1Control()
86     {
87         Dsmlv2Parser parser = null;
88         try
89         {
90             parser = new Dsmlv2Parser();
91
92             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_1_control.xml" ).getFile() );
93
94             parser.parse();
95         }
96         catch ( Exception JavaDoc e )
97         {
98             fail( e.getMessage() );
99         }
100
101         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
102         Control control = modifyRequest.getCurrentControl();
103
104         assertEquals( 1, modifyRequest.getControls().size() );
105         assertTrue( control.getCriticality() );
106         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
107         assertEquals( "Some text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
108     }
109
110
111     /**
112      * Test parsing of a request with a (optional) Control element with Base64 Value
113      */

114     public void testRequestWith1ControlBase64Value()
115     {
116         Dsmlv2Parser parser = null;
117         try
118         {
119             parser = new Dsmlv2Parser();
120
121             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_1_control_base64_value.xml" )
122                 .getFile() );
123
124             parser.parse();
125         }
126         catch ( Exception JavaDoc e )
127         {
128             fail( e.getMessage() );
129         }
130
131         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
132         Control control = modifyRequest.getCurrentControl();
133
134         assertEquals( 1, modifyRequest.getControls().size() );
135         assertTrue( control.getCriticality() );
136         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
137         assertEquals( "DSMLv2.0 rocks!!", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
138     }
139
140
141     /**
142      * Test parsing of a request with a (optional) Control element with empty value
143      */

144     public void testRequestWith1ControlEmptyValue()
145     {
146         Dsmlv2Parser parser = null;
147         try
148         {
149             parser = new Dsmlv2Parser();
150
151             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_1_control_empty_value.xml" )
152                 .getFile() );
153
154             parser.parse();
155         }
156         catch ( Exception JavaDoc e )
157         {
158             fail( e.getMessage() );
159         }
160
161         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
162         Control control = modifyRequest.getCurrentControl();
163
164         assertEquals( 1, modifyRequest.getControls().size() );
165         assertTrue( control.getCriticality() );
166         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
167         assertEquals( StringTools.EMPTY_BYTES, control.getControlValue() );
168     }
169
170
171     /**
172      * Test parsing of a request with 2 (optional) Control elements
173      */

174     public void testRequestWith2Controls()
175     {
176         Dsmlv2Parser parser = null;
177         try
178         {
179             parser = new Dsmlv2Parser();
180
181             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_2_controls.xml" ).getFile() );
182
183             parser.parse();
184         }
185         catch ( Exception JavaDoc e )
186         {
187             fail( e.getMessage() );
188         }
189
190         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
191         Control control = modifyRequest.getCurrentControl();
192
193         assertEquals( 2, modifyRequest.getControls().size() );
194         assertFalse( control.getCriticality() );
195         assertEquals( "1.2.840.113556.1.4.789", control.getControlType() );
196         assertEquals( "Some other text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
197     }
198
199
200     /**
201      * Test parsing of a request with 3 (optional) Control elements without value
202      */

203     public void testRequestWith3ControlsWithoutValue()
204     {
205         Dsmlv2Parser parser = null;
206         try
207         {
208             parser = new Dsmlv2Parser();
209
210             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_3_controls_without_value.xml" )
211                 .getFile() );
212
213             parser.parse();
214         }
215         catch ( Exception JavaDoc e )
216         {
217             fail( e.getMessage() );
218         }
219
220         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
221         Control control = modifyRequest.getCurrentControl();
222
223         assertEquals( 3, modifyRequest.getControls().size() );
224         assertTrue( control.getCriticality() );
225         assertEquals( "1.2.840.113556.1.4.456", control.getControlType() );
226         assertEquals( StringTools.EMPTY_BYTES, control.getControlValue() );
227     }
228
229
230     /**
231      * Test parsing of a request without dn attribute
232      */

233     public void testRequestWithoutDnAttribute()
234     {
235         testParsingFail( ModifyRequestTest.class, "request_without_dn_attribute.xml" );
236     }
237
238
239     /**
240      * Test parsing of a request with a Modification element
241      * @throws NamingException
242      */

243     public void testRequestWith1Modification() throws NamingException JavaDoc
244     {
245         Dsmlv2Parser parser = null;
246         try
247         {
248             parser = new Dsmlv2Parser();
249
250             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_1_modification.xml" ).getFile() );
251
252             parser.parse();
253         }
254         catch ( Exception JavaDoc e )
255         {
256             fail( e.getMessage() );
257         }
258
259         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
260
261         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
262
263         assertEquals( "directreport", modifyRequest.getCurrentAttributeType() );
264
265         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
266
267         assertEquals( 1, modifications.size() );
268
269         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 0 );
270
271         Attribute JavaDoc attribute = modification.getAttribute();
272
273         assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get( 0 ) );
274     }
275
276
277     /**
278      * Test parsing of a request with a Modification element with Base64 Value
279      * @throws NamingException
280      */

281     public void testRequestWith1ModificationBase64Value() throws NamingException JavaDoc
282     {
283         Dsmlv2Parser parser = null;
284         try
285         {
286             parser = new Dsmlv2Parser();
287
288             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_1_modification_base64_value.xml" )
289                 .getFile() );
290
291             parser.parse();
292         }
293         catch ( Exception JavaDoc e )
294         {
295             fail( e.getMessage() );
296         }
297
298         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
299
300         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
301
302         assertEquals( "directreport", modifyRequest.getCurrentAttributeType() );
303
304         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
305
306         assertEquals( 1, modifications.size() );
307
308         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 0 );
309
310         Attribute JavaDoc attribute = modification.getAttribute();
311
312         assertEquals( "cn=Emmanuel L├ęcharny, ou=people, dc=example, dc=com", new String JavaDoc( ( byte[] ) attribute.get( 0 ) ) );
313     }
314
315
316     /**
317      * Test parsing of a request with 2 Modification elements
318      * @throws NamingException
319      */

320     public void testRequestWith2Modifications() throws NamingException JavaDoc
321     {
322         Dsmlv2Parser parser = null;
323         try
324         {
325             parser = new Dsmlv2Parser();
326
327             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_2_modifications.xml" ).getFile() );
328
329             parser.parse();
330         }
331         catch ( Exception JavaDoc e )
332         {
333             fail( e.getMessage() );
334         }
335
336         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
337
338         assertEquals( LdapConstants.OPERATION_REPLACE, modifyRequest.getCurrentOperation() );
339
340         assertEquals( "sn", modifyRequest.getCurrentAttributeType() );
341
342         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
343
344         assertEquals( 2, modifications.size() );
345
346         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 1 );
347
348         Attribute JavaDoc attribute = modification.getAttribute();
349
350         assertEquals( "CN=Steve Jobs, DC=apple, DC=com", attribute.get( 0 ) );
351     }
352
353
354     /**
355      * Test parsing of a request without name attribute to the Modification element
356      */

357     public void testRequestWithoutNameAttribute()
358     {
359         testParsingFail( ModifyRequestTest.class, "request_without_name_attribute.xml" );
360     }
361
362
363     /**
364      * Test parsing of a request without operation attribute to the Modification element
365      */

366     public void testRequestWithoutOperationAttribute()
367     {
368         testParsingFail( ModifyRequestTest.class, "request_without_operation_attribute.xml" );
369     }
370
371
372     /**
373      * Test parsing of a request with operation attribute to Add value
374      * @throws NamingException
375      */

376     public void testRequestWithOperationAdd() throws NamingException JavaDoc
377     {
378         Dsmlv2Parser parser = null;
379         try
380         {
381             parser = new Dsmlv2Parser();
382
383             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_operation_add.xml" ).getFile() );
384
385             parser.parse();
386         }
387         catch ( Exception JavaDoc e )
388         {
389             fail( e.getMessage() );
390         }
391
392         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
393
394         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
395     }
396
397
398     /**
399      * Test parsing of a request with operation attribute to Delete value
400      * @throws NamingException
401      */

402     public void testRequestWithOperationDelete() throws NamingException JavaDoc
403     {
404         Dsmlv2Parser parser = null;
405         try
406         {
407             parser = new Dsmlv2Parser();
408
409             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_operation_delete.xml" ).getFile() );
410
411             parser.parse();
412         }
413         catch ( Exception JavaDoc e )
414         {
415             fail( e.getMessage() );
416         }
417
418         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
419
420         assertEquals( LdapConstants.OPERATION_DELETE, modifyRequest.getCurrentOperation() );
421     }
422
423
424     /**
425      * Test parsing of a request with operation attribute to Replace value
426      * @throws NamingException
427      */

428     public void testRequestWithOperationReplace() throws NamingException JavaDoc
429     {
430         Dsmlv2Parser parser = null;
431         try
432         {
433             parser = new Dsmlv2Parser();
434
435             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_operation_replace.xml" ).getFile() );
436
437             parser.parse();
438         }
439         catch ( Exception JavaDoc e )
440         {
441             fail( e.getMessage() );
442         }
443
444         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
445
446         assertEquals( LdapConstants.OPERATION_REPLACE, modifyRequest.getCurrentOperation() );
447     }
448
449
450     /**
451      * Test parsing of a request without operation attribute to the Modification element
452      */

453     public void testRequestWithOperationError()
454     {
455         testParsingFail( ModifyRequestTest.class, "request_with_operation_error.xml" );
456     }
457
458
459     /**
460      * Test parsing of a request with a Modification element without Value element
461      * @throws NamingException
462      */

463     public void testRequestWithModificationWithoutValue() throws NamingException JavaDoc
464     {
465         Dsmlv2Parser parser = null;
466         try
467         {
468             parser = new Dsmlv2Parser();
469
470             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_modification_without_value.xml" )
471                 .getFile() );
472
473             parser.parse();
474         }
475         catch ( Exception JavaDoc e )
476         {
477             fail( e.getMessage() );
478         }
479
480         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
481
482         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
483
484         assertEquals( "directreport", modifyRequest.getCurrentAttributeType() );
485
486         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
487
488         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 0 );
489
490         Attribute JavaDoc attribute = modification.getAttribute();
491
492         assertEquals( 0, attribute.size() );
493     }
494
495
496     /**
497      * Test parsing of a request with a Modification element
498      * @throws NamingException
499      */

500     public void testRequestWithModificationWith2Values() throws NamingException JavaDoc
501     {
502         Dsmlv2Parser parser = null;
503         try
504         {
505             parser = new Dsmlv2Parser();
506
507             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_modification_with_2_values.xml" )
508                 .getFile() );
509
510             parser.parse();
511         }
512         catch ( Exception JavaDoc e )
513         {
514             fail( e.getMessage() );
515         }
516
517         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
518
519         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
520
521         assertEquals( "directreport", modifyRequest.getCurrentAttributeType() );
522
523         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
524
525         assertEquals( 1, modifications.size() );
526
527         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 0 );
528
529         Attribute JavaDoc attribute = modification.getAttribute();
530
531         assertEquals( 2, attribute.size() );
532         assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get( 0 ) );
533         assertEquals( "CN=Steve Jobs, DC=apple, DC=com", attribute.get( 1 ) );
534     }
535
536
537     /**
538      * Test parsing of a request with a Modification element with an empty value
539      * @throws NamingException
540      */

541     public void testRequestWithModificationWithEmptyValue() throws NamingException JavaDoc
542     {
543         Dsmlv2Parser parser = null;
544         try
545         {
546             parser = new Dsmlv2Parser();
547
548             parser.setInputFile( ModifyRequestTest.class.getResource( "request_with_modification_with_empty_value.xml" )
549                 .getFile() );
550
551             parser.parse();
552         }
553         catch ( Exception JavaDoc e )
554         {
555             fail( e.getMessage() );
556         }
557
558         ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
559
560         assertEquals( LdapConstants.OPERATION_ADD, modifyRequest.getCurrentOperation() );
561
562         assertEquals( "directreport", modifyRequest.getCurrentAttributeType() );
563
564         List JavaDoc<ModificationItemImpl> modifications = modifyRequest.getModifications();
565
566         assertEquals( 1, modifications.size() );
567
568         ModificationItem JavaDoc modification = ( ModificationItem JavaDoc ) modifications.get( 0 );
569
570         Attribute JavaDoc attribute = modification.getAttribute();
571
572         assertEquals( 1, attribute.size() );
573         assertEquals( "", attribute.get( 0 ) );
574     }
575
576
577     /**
578      * Test parsing of a request with a needed requestID attribute
579      *
580      * DIRSTUDIO-1
581      */

582     public void testRequestWithNeededRequestId()
583     {
584         testParsingFail( ModifyRequestTest.class, "request_with_needed_requestID.xml" );
585     }
586 }
587
Popular Tags