]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c6316ad9d41933a8d70506fae1e0036a662509ba
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29
30 import org.collectionspace.services.client.ContactClient;
31 import org.collectionspace.services.client.ContactClientUtils;
32 import org.collectionspace.services.contact.ContactsCommon;
33 import org.collectionspace.services.contact.ContactsCommonList;
34
35 import org.jboss.resteasy.client.ClientResponse;
36
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.Test;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * ContactServiceTest, carries out tests against a
49  * deployed and running Contact Service.
50  *
51  * $LastChangedRevision: 917 $
52  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
53  */
54 public class ContactServiceTest extends AbstractServiceTestImpl {
55
56    private final Logger logger =
57        LoggerFactory.getLogger(ContactServiceTest.class);
58
59     // Instance variables specific to this test.
60     final String SERVICE_PATH_COMPONENT = "contacts";
61     private String knownResourceId = null;
62     private List<String> allResourceIdsCreated = new ArrayList();
63
64     // ---------------------------------------------------------------
65     // CRUD tests : CREATE tests
66     // ---------------------------------------------------------------
67     // Success outcomes
68     @Override
69     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
70     public void create(String testName) throws Exception {
71
72         // Perform setup, such as initializing the type of service request
73         // (e.g. CREATE, DELETE), its valid and expected status codes, and
74         // its associated HTTP method name (e.g. POST, DELETE).
75         setupCreate(testName);
76
77         // Submit the request to the service and store the response.
78         ContactClient client = new ContactClient();
79         String identifier = createIdentifier();
80         MultipartOutput multipart =
81             ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
82         ClientResponse<Response> res = client.create(multipart);
83
84         int statusCode = res.getStatus();
85
86         // Check the status code of the response: does it match
87         // the expected response(s)?
88         //
89         // Specifically:
90         // Does it fall within the set of valid status codes?
91         // Does it exactly match the expected status code?
92         if(logger.isDebugEnabled()){
93             logger.debug(testName + ": status = " + statusCode);
94         }
95         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
96                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
97         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
98
99         // Store the ID returned from the first resource created
100         // for additional tests below.
101         if (knownResourceId == null){
102             knownResourceId = extractId(res);
103             if (logger.isDebugEnabled()) {
104                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
105             }
106         }
107
108         // Store the IDs from every resource created by tests,
109         // so they can be deleted after tests have been run.
110         allResourceIdsCreated.add(extractId(res));
111     }
112
113     @Override
114     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
115         dependsOnMethods = {"create"})
116     public void createList(String testName) throws Exception {
117         for(int i = 0; i < 3; i++){
118             create(testName);
119         }
120     }
121
122     // Failure outcomes
123     // Placeholders until the three tests below can be uncommented.
124     // See Issue CSPACE-401.
125     @Override
126     public void createWithEmptyEntityBody(String testName) throws Exception {
127     }
128
129     @Override
130     public void createWithMalformedXml(String testName) throws Exception {
131     }
132
133     @Override
134     public void createWithWrongXmlSchema(String testName) throws Exception {
135     }
136
137     /*
138     @Override
139     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
140         dependsOnMethods = {"create", "testSubmitRequest"})
141     public void createWithEmptyEntityBody(String testName) throws Exception {
142
143     // Perform setup.
144     setupCreateWithEmptyEntityBody(testName);
145
146     // Submit the request to the service and store the response.
147     String method = REQUEST_TYPE.httpMethodName();
148     String url = getServiceRootURL();
149     String mediaType = MediaType.APPLICATION_XML;
150     final String entity = "";
151     int statusCode = submitRequest(method, url, mediaType, entity);
152
153     // Check the status code of the response: does it match
154     // the expected response(s)?
155     if(logger.isDebugEnabled()){
156         logger.debug("createWithEmptyEntityBody url=" + url +
157             " status=" + statusCode);
158      }
159     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
160     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
161     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
162     }
163
164     @Override
165     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
166         dependsOnMethods = {"create", "testSubmitRequest"})
167     public void createWithMalformedXml(String testName) throws Exception {
168
169     // Perform setup.
170     setupCreateWithMalformedXml(testName);
171
172     // Submit the request to the service and store the response.
173     String method = REQUEST_TYPE.httpMethodName();
174     String url = getServiceRootURL();
175     String mediaType = MediaType.APPLICATION_XML;
176     final String entity = MALFORMED_XML_DATA; // Constant from base class.
177     int statusCode = submitRequest(method, url, mediaType, entity);
178
179     // Check the status code of the response: does it match
180     // the expected response(s)?
181     if(logger.isDebugEnabled()){
182         logger.debug(testName + ": url=" + url +
183             " status=" + statusCode);
184      }
185     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
188     }
189
190     @Override
191     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
192         dependsOnMethods = {"create", "testSubmitRequest"})
193     public void createWithWrongXmlSchema(String testName) throws Exception {
194
195     // Perform setup.
196     setupCreateWithWrongXmlSchema(testName);
197
198     // Submit the request to the service and store the response.
199     String method = REQUEST_TYPE.httpMethodName();
200     String url = getServiceRootURL();
201     String mediaType = MediaType.APPLICATION_XML;
202     final String entity = WRONG_XML_SCHEMA_DATA;
203     int statusCode = submitRequest(method, url, mediaType, entity);
204
205     // Check the status code of the response: does it match
206     // the expected response(s)?
207     if(logger.isDebugEnabled()){
208         logger.debug(testName + ": url=" + url +
209             " status=" + statusCode);
210      }
211     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
212     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
213     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
214     }
215      */
216
217     // ---------------------------------------------------------------
218     // CRUD tests : READ tests
219     // ---------------------------------------------------------------
220     // Success outcomes
221     @Override
222     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
223         dependsOnMethods = {"create"})
224     public void read(String testName) throws Exception {
225
226         // Perform setup.
227         setupRead(testName);
228
229         // Submit the request to the service and store the response.
230         ContactClient client = new ContactClient();
231         ClientResponse<MultipartInput> res = client.read(knownResourceId);
232         int statusCode = res.getStatus();
233
234         // Check the status code of the response: does it match
235         // the expected response(s)?
236         if(logger.isDebugEnabled()){
237             logger.debug(testName + ": status = " + statusCode);
238         }
239         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
242
243         MultipartInput input = (MultipartInput) res.getEntity();
244         ContactsCommon contact = (ContactsCommon) extractPart(input,
245                 client.getCommonPartName(), ContactsCommon.class);
246         Assert.assertNotNull(contact);
247     }
248
249     // Failure outcomes
250     @Override
251     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252         dependsOnMethods = {"read"})
253     public void readNonExistent(String testName) throws Exception {
254
255         // Perform setup.
256         setupReadNonExistent(testName);
257
258         // Submit the request to the service and store the response.
259         ContactClient client = new ContactClient();
260         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
261         int statusCode = res.getStatus();
262
263         // Check the status code of the response: does it match
264         // the expected response(s)?
265         if(logger.isDebugEnabled()){
266             logger.debug(testName + ": status = " + statusCode);
267         }
268         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271     }
272
273     // ---------------------------------------------------------------
274     // CRUD tests : READ_LIST tests
275     // ---------------------------------------------------------------
276     // Success outcomes
277     @Override
278     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
279         dependsOnMethods = {"read"})
280     public void readList(String testName) throws Exception {
281
282         // Perform setup.
283         setupReadList(testName);
284
285         // Submit the request to the service and store the response.
286         ContactClient client = new ContactClient();
287         ClientResponse<ContactsCommonList> res = client.readList();
288         ContactsCommonList list = res.getEntity();
289         int statusCode = res.getStatus();
290
291         // Check the status code of the response: does it match
292         // the expected response(s)?
293         if(logger.isDebugEnabled()){
294             logger.debug(testName + ": status = " + statusCode);
295         }
296         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
297                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
298         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
299
300         // Optionally output additional data about list members for debugging.
301         boolean iterateThroughList = false;
302         if(iterateThroughList && logger.isDebugEnabled()){
303             List<ContactsCommonList.ContactListItem> items =
304                     list.getContactListItem();
305             int i = 0;
306             for(ContactsCommonList.ContactListItem item : items){
307                 logger.debug(testName + ": list-item[" + i + "] csid=" +
308                         item.getCsid());
309                 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
310                         item.getAddressPlace());
311                 logger.debug(testName + ": list-item[" + i + "] URI=" +
312                         item.getUri());
313                 i++;
314             }
315         }
316
317     }
318
319     // Failure outcomes
320     // None at present.
321     // ---------------------------------------------------------------
322     // CRUD tests : UPDATE tests
323     // ---------------------------------------------------------------
324     // Success outcomes
325     @Override
326     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
327         dependsOnMethods = {"read"})
328     public void update(String testName) throws Exception {
329
330         // Perform setup.
331         setupUpdate(testName);
332
333         // Submit the request to the service and store the response.
334         ContactClient client = new ContactClient();
335         ClientResponse<MultipartInput> res =
336                 client.read(knownResourceId);
337         if(logger.isDebugEnabled()){
338             logger.debug(testName + ": read status = " + res.getStatus());
339         }
340         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
341
342         if(logger.isDebugEnabled()){
343             logger.debug("got object to update with ID: " + knownResourceId);
344         }
345         MultipartInput input = (MultipartInput) res.getEntity();
346         ContactsCommon contact = (ContactsCommon) extractPart(input,
347                 client.getCommonPartName(), ContactsCommon.class);
348         Assert.assertNotNull(contact);
349
350         // Update the content of this resource.
351         contact.setAddressType("updated-" + contact.getAddressType());
352         contact.setAddressPlace("updated-" + contact.getAddressPlace());
353         contact.setEmail("updated-" + contact.getEmail());
354         if(logger.isDebugEnabled()){
355             logger.debug("to be updated object");
356             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
357         }
358         // Submit the request to the service and store the response.
359         MultipartOutput output = new MultipartOutput();
360         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
361         commonPart.getHeaders().add("label", client.getCommonPartName());
362
363         res = client.update(knownResourceId, output);
364         int statusCode = res.getStatus();
365         // Check the status code of the response: does it match the expected response(s)?
366         if(logger.isDebugEnabled()){
367             logger.debug(testName + ": status = " + statusCode);
368         }
369         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
370                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
371         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372
373
374         input = (MultipartInput) res.getEntity();
375         ContactsCommon updatedContact =
376                 (ContactsCommon) extractPart(input,
377                         client.getCommonPartName(), ContactsCommon.class);
378         Assert.assertNotNull(updatedContact);
379
380         Assert.assertEquals(updatedContact.getEmail(),
381                 contact.getEmail(),
382                 "Data in updated object did not match submitted data.");
383
384     }
385
386     // Failure outcomes
387     // Placeholders until the three tests below can be uncommented.
388     // See Issue CSPACE-401.
389     @Override
390     public void updateWithEmptyEntityBody(String testName) throws Exception{
391     }
392     @Override
393     public void updateWithMalformedXml(String testName) throws Exception {
394     }
395     @Override
396     public void updateWithWrongXmlSchema(String testName) throws Exception {
397     }
398
399     /*
400     @Override
401     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
402         dependsOnMethods = {"create", "update", "testSubmitRequest"})
403     public void updateWithEmptyEntityBody(String testName) throws Exception {
404
405     // Perform setup.
406     setupUpdateWithEmptyEntityBody(testName);
407
408     // Submit the request to the service and store the response.
409     String method = REQUEST_TYPE.httpMethodName();
410     String url = getResourceURL(knownResourceId);
411     String mediaType = MediaType.APPLICATION_XML;
412     final String entity = "";
413     int statusCode = submitRequest(method, url, mediaType, entity);
414
415     // Check the status code of the response: does it match
416     // the expected response(s)?
417     if(logger.isDebugEnabled()){
418         logger.debug(testName + ": url=" + url +
419             " status=" + statusCode);
420      }
421     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
424     }
425
426     @Override
427     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
428         dependsOnMethods = {"create", "update", "testSubmitRequest"})
429     public void updateWithMalformedXml(String testName) throws Exception {
430
431     // Perform setup.
432     setupUpdateWithMalformedXml(testName);
433
434     // Submit the request to the service and store the response.
435     String method = REQUEST_TYPE.httpMethodName();
436     String url = getResourceURL(knownResourceId);
437     String mediaType = MediaType.APPLICATION_XML;
438     final String entity = MALFORMED_XML_DATA;
439     int statusCode = submitRequest(method, url, mediaType, entity);
440
441     // Check the status code of the response: does it match
442     // the expected response(s)?
443     if(logger.isDebugEnabled()){
444         logger.debug(testName + ": url=" + url +
445          " status=" + statusCode);
446      }
447     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
448     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
449     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
450     }
451
452     @Override
453     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
454         dependsOnMethods = {"create", "update", "testSubmitRequest"})
455     public void updateWithWrongXmlSchema(String testName) throws Exception {
456
457     // Perform setup.
458     setupUpdateWithWrongXmlSchema(testName);
459
460     // Submit the request to the service and store the response.
461     String method = REQUEST_TYPE.httpMethodName();
462     String url = getResourceURL(knownResourceId);
463     String mediaType = MediaType.APPLICATION_XML;
464     final String entity = WRONG_XML_SCHEMA_DATA;
465     int statusCode = submitRequest(method, url, mediaType, entity);
466
467     // Check the status code of the response: does it match
468     // the expected response(s)?
469     if(logger.isDebugEnabled()){
470         logger.debug(testName + ": url=" + url +
471         " status=" + statusCode);
472      }
473     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
474     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
475     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476     }
477      */
478
479     @Override
480     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
481         dependsOnMethods = {"update", "testSubmitRequest"})
482     public void updateNonExistent(String testName) throws Exception {
483
484         // Perform setup.
485         setupUpdateNonExistent(testName);
486
487         // Submit the request to the service and store the response.
488         // Note: The ID used in this 'create' call may be arbitrary.
489         // The only relevant ID may be the one used in update(), below.
490         ContactClient client = new ContactClient();
491         MultipartOutput multipart =
492                 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
493         ClientResponse<MultipartInput> res =
494                 client.update(NON_EXISTENT_ID, multipart);
495         int statusCode = res.getStatus();
496
497         // Check the status code of the response: does it match
498         // the expected response(s)?
499         if(logger.isDebugEnabled()){
500             logger.debug(testName + ": status = " + statusCode);
501         }
502         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
503                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
504         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
505     }
506
507     // ---------------------------------------------------------------
508     // CRUD tests : DELETE tests
509     // ---------------------------------------------------------------
510     // Success outcomes
511     @Override
512     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
513         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
514     public void delete(String testName) throws Exception {
515
516         // Perform setup.
517         setupDelete(testName);
518
519         // Submit the request to the service and store the response.
520         ContactClient client = new ContactClient();
521         ClientResponse<Response> res = client.delete(knownResourceId);
522         int statusCode = res.getStatus();
523
524         // Check the status code of the response: does it match
525         // the expected response(s)?
526         if(logger.isDebugEnabled()){
527             logger.debug(testName + ": status = " + statusCode);
528         }
529         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
530                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
531         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
532     }
533
534     // Failure outcomes
535     @Override
536     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
537         dependsOnMethods = {"delete"})
538     public void deleteNonExistent(String testName) throws Exception {
539
540         // Perform setup.
541         setupDeleteNonExistent(testName);
542
543         // Submit the request to the service and store the response.
544         ContactClient client = new ContactClient();
545         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
546         int statusCode = res.getStatus();
547
548         // Check the status code of the response: does it match
549         // the expected response(s)?
550         if(logger.isDebugEnabled()){
551             logger.debug(testName + ": status = " + statusCode);
552         }
553         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
554                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
555         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
556     }
557
558     // ---------------------------------------------------------------
559     // Utility tests : tests of code used in tests above
560     // ---------------------------------------------------------------
561     /**
562      * Tests the code for manually submitting data that is used by several
563      * of the methods above.
564      */
565     @Test(dependsOnMethods = {"create", "read"})
566     public void testSubmitRequest() {
567
568         // Expected status code: 200 OK
569         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
570
571         // Submit the request to the service and store the response.
572         String method = ServiceRequestType.READ.httpMethodName();
573         String url = getResourceURL(knownResourceId);
574         int statusCode = submitRequest(method, url);
575
576         // Check the status code of the response: does it match
577         // the expected response(s)?
578         if(logger.isDebugEnabled()){
579             logger.debug("testSubmitRequest: url=" + url +
580                 " status=" + statusCode);
581         }
582         Assert.assertEquals(statusCode, EXPECTED_STATUS);
583
584     }
585
586     // ---------------------------------------------------------------
587     // Cleanup of resources created during testing
588     // ---------------------------------------------------------------
589
590     /**
591      * Deletes all resources created by tests, after all tests have been run.
592      *
593      * This cleanup method will always be run, even if one or more tests fail.
594      * For this reason, it attempts to remove all resources created
595      * at any point during testing, even if some of those resources
596      * may be expected to be deleted by certain tests.
597      */
598     @AfterClass(alwaysRun=true)
599     public void cleanUp() {
600         if (logger.isDebugEnabled()) {
601             logger.debug("Cleaning up temporary resources created for testing ...");
602         }
603         ContactClient client = new ContactClient();
604         for (String resourceId : allResourceIdsCreated) {
605             // Note: Any non-success responses are ignored and not reported.
606             ClientResponse<Response> res = client.delete(resourceId);
607         }
608     }
609
610     // ---------------------------------------------------------------
611     // Utility methods used by tests above
612     // ---------------------------------------------------------------
613     @Override
614     public String getServicePathComponent() {
615         return SERVICE_PATH_COMPONENT;
616     }
617
618 }