]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f36b281224c7c0812f0998e3e9d20e08013a1aca
[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     private ContactClient client = new ContactClient();
61     final String SERVICE_PATH_COMPONENT = "contacts";
62     private String knownResourceId = null;
63     private List<String> allResourceIdsCreated = new ArrayList();
64
65     // ---------------------------------------------------------------
66     // CRUD tests : CREATE tests
67     // ---------------------------------------------------------------
68     // Success outcomes
69     @Override
70     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
71     public void create(String testName) throws Exception {
72
73         // Perform setup, such as initializing the type of service request
74         // (e.g. CREATE, DELETE), its valid and expected status codes, and
75         // its associated HTTP method name (e.g. POST, DELETE).
76         setupCreate(testName);
77
78         // Submit the request to the service and store the response.
79         String identifier = createIdentifier();
80
81         MultipartOutput multipart = ContactClientUtils.createContactInstance(identifier);
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         ClientResponse<MultipartInput> res = client.read(knownResourceId);
231         int statusCode = res.getStatus();
232
233         // Check the status code of the response: does it match
234         // the expected response(s)?
235         if(logger.isDebugEnabled()){
236             logger.debug(testName + ": status = " + statusCode);
237         }
238         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
239                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
240         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241
242         MultipartInput input = (MultipartInput) res.getEntity();
243         ContactsCommon contact = (ContactsCommon) extractPart(input,
244                 client.getCommonPartName(), ContactsCommon.class);
245         Assert.assertNotNull(contact);
246     }
247
248     // Failure outcomes
249     @Override
250     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
251         dependsOnMethods = {"read"})
252     public void readNonExistent(String testName) throws Exception {
253
254         // Perform setup.
255         setupReadNonExistent(testName);
256
257         // Submit the request to the service and store the response.
258         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
259         int statusCode = res.getStatus();
260
261         // Check the status code of the response: does it match
262         // the expected response(s)?
263         if(logger.isDebugEnabled()){
264             logger.debug(testName + ": status = " + statusCode);
265         }
266         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
267                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
268         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
269     }
270
271     // ---------------------------------------------------------------
272     // CRUD tests : READ_LIST tests
273     // ---------------------------------------------------------------
274     // Success outcomes
275     @Override
276     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
277         dependsOnMethods = {"read"})
278     public void readList(String testName) throws Exception {
279
280         // Perform setup.
281         setupReadList(testName);
282
283         // Submit the request to the service and store the response.
284         ClientResponse<ContactsCommonList> res = client.readList();
285         ContactsCommonList list = res.getEntity();
286         int statusCode = res.getStatus();
287
288         // Check the status code of the response: does it match
289         // the expected response(s)?
290         if(logger.isDebugEnabled()){
291             logger.debug(testName + ": status = " + statusCode);
292         }
293         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
294                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
295         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296
297         // Optionally output additional data about list members for debugging.
298         boolean iterateThroughList = false;
299         if(iterateThroughList && logger.isDebugEnabled()){
300             List<ContactsCommonList.ContactListItem> items =
301                     list.getContactListItem();
302             int i = 0;
303             for(ContactsCommonList.ContactListItem item : items){
304                 logger.debug(testName + ": list-item[" + i + "] csid=" +
305                         item.getCsid());
306                 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
307                         item.getAddressText1());
308                 logger.debug(testName + ": list-item[" + i + "] URI=" +
309                         item.getUri());
310                 i++;
311             }
312         }
313
314     }
315
316     // Failure outcomes
317     // None at present.
318     // ---------------------------------------------------------------
319     // CRUD tests : UPDATE tests
320     // ---------------------------------------------------------------
321     // Success outcomes
322     @Override
323     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
324         dependsOnMethods = {"read"})
325     public void update(String testName) throws Exception {
326
327         // Perform setup.
328         setupUpdate(testName);
329
330         ClientResponse<MultipartInput> res =
331                 client.read(knownResourceId);
332         if(logger.isDebugEnabled()){
333             logger.debug(testName + ": read status = " + res.getStatus());
334         }
335         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
336
337         if(logger.isDebugEnabled()){
338             logger.debug("got object to update with ID: " + knownResourceId);
339         }
340         MultipartInput input = (MultipartInput) res.getEntity();
341         ContactsCommon contact = (ContactsCommon) extractPart(input,
342                 client.getCommonPartName(), ContactsCommon.class);
343         Assert.assertNotNull(contact);
344
345         // Update the content of this resource.
346         contact.setAddressText1("updated-" + contact.getAddressText1());
347         contact.setPostcode1("updated-" + contact.getPostcode1());
348         if(logger.isDebugEnabled()){
349             logger.debug("to be updated object");
350             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
351         }
352         // Submit the request to the service and store the response.
353         MultipartOutput output = new MultipartOutput();
354         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
355         commonPart.getHeaders().add("label", client.getCommonPartName());
356
357         res = client.update(knownResourceId, output);
358         int statusCode = res.getStatus();
359         // Check the status code of the response: does it match the expected response(s)?
360         if(logger.isDebugEnabled()){
361             logger.debug(testName + ": status = " + statusCode);
362         }
363         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
364                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
365         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
366
367
368         input = (MultipartInput) res.getEntity();
369         ContactsCommon updatedContact =
370                 (ContactsCommon) extractPart(input,
371                         client.getCommonPartName(), ContactsCommon.class);
372         Assert.assertNotNull(updatedContact);
373
374         Assert.assertEquals(updatedContact.getPostcode1(),
375                 contact.getPostcode1(),
376                 "Data in updated object did not match submitted data.");
377
378     }
379
380     // Failure outcomes
381     // Placeholders until the three tests below can be uncommented.
382     // See Issue CSPACE-401.
383     @Override
384     public void updateWithEmptyEntityBody(String testName) throws Exception{
385     }
386     @Override
387     public void updateWithMalformedXml(String testName) throws Exception {
388     }
389     @Override
390     public void updateWithWrongXmlSchema(String testName) throws Exception {
391     }
392
393     /*
394     @Override
395     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
396         dependsOnMethods = {"create", "update", "testSubmitRequest"})
397     public void updateWithEmptyEntityBody(String testName) throws Exception {
398
399     // Perform setup.
400     setupUpdateWithEmptyEntityBody(testName);
401
402     // Submit the request to the service and store the response.
403     String method = REQUEST_TYPE.httpMethodName();
404     String url = getResourceURL(knownResourceId);
405     String mediaType = MediaType.APPLICATION_XML;
406     final String entity = "";
407     int statusCode = submitRequest(method, url, mediaType, entity);
408
409     // Check the status code of the response: does it match
410     // the expected response(s)?
411     if(logger.isDebugEnabled()){
412         logger.debug(testName + ": url=" + url +
413             " status=" + statusCode);
414      }
415     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
418     }
419
420     @Override
421     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
422         dependsOnMethods = {"create", "update", "testSubmitRequest"})
423     public void updateWithMalformedXml(String testName) throws Exception {
424
425     // Perform setup.
426     setupUpdateWithMalformedXml(testName);
427
428     // Submit the request to the service and store the response.
429     String method = REQUEST_TYPE.httpMethodName();
430     String url = getResourceURL(knownResourceId);
431     String mediaType = MediaType.APPLICATION_XML;
432     final String entity = MALFORMED_XML_DATA;
433     int statusCode = submitRequest(method, url, mediaType, entity);
434
435     // Check the status code of the response: does it match
436     // the expected response(s)?
437     if(logger.isDebugEnabled()){
438         logger.debug(testName + ": url=" + url +
439          " status=" + statusCode);
440      }
441     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
442     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
443     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
444     }
445
446     @Override
447     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
448         dependsOnMethods = {"create", "update", "testSubmitRequest"})
449     public void updateWithWrongXmlSchema(String testName) throws Exception {
450
451     // Perform setup.
452     setupUpdateWithWrongXmlSchema(testName);
453
454     // Submit the request to the service and store the response.
455     String method = REQUEST_TYPE.httpMethodName();
456     String url = getResourceURL(knownResourceId);
457     String mediaType = MediaType.APPLICATION_XML;
458     final String entity = WRONG_XML_SCHEMA_DATA;
459     int statusCode = submitRequest(method, url, mediaType, entity);
460
461     // Check the status code of the response: does it match
462     // the expected response(s)?
463     if(logger.isDebugEnabled()){
464         logger.debug(testName + ": url=" + url +
465         " status=" + statusCode);
466      }
467     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
468     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
469     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470     }
471      */
472
473     @Override
474     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
475         dependsOnMethods = {"update", "testSubmitRequest"})
476     public void updateNonExistent(String testName) throws Exception {
477
478         // Perform setup.
479         setupUpdateNonExistent(testName);
480
481         // Submit the request to the service and store the response.
482         // Note: The ID used in this 'create' call may be arbitrary.
483         // The only relevant ID may be the one used in update(), below.
484
485         // The only relevant ID may be the one used in update(), below.
486         MultipartOutput multipart = ContactClientUtils.createContactInstance(NON_EXISTENT_ID);
487         ClientResponse<MultipartInput> res =
488                 client.update(NON_EXISTENT_ID, multipart);
489         int statusCode = res.getStatus();
490
491         // Check the status code of the response: does it match
492         // the expected response(s)?
493         if(logger.isDebugEnabled()){
494             logger.debug(testName + ": status = " + statusCode);
495         }
496         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
497                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
498         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
499     }
500
501     // ---------------------------------------------------------------
502     // CRUD tests : DELETE tests
503     // ---------------------------------------------------------------
504     // Success outcomes
505     @Override
506     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
507         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
508     public void delete(String testName) throws Exception {
509
510         // Perform setup.
511         setupDelete(testName);
512
513         // Submit the request to the service and store the response.
514         ClientResponse<Response> res = client.delete(knownResourceId);
515         int statusCode = res.getStatus();
516
517         // Check the status code of the response: does it match
518         // the expected response(s)?
519         if(logger.isDebugEnabled()){
520             logger.debug(testName + ": status = " + statusCode);
521         }
522         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
523                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
524         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
525     }
526
527     // Failure outcomes
528     @Override
529     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
530         dependsOnMethods = {"delete"})
531     public void deleteNonExistent(String testName) throws Exception {
532
533         // Perform setup.
534         setupDeleteNonExistent(testName);
535
536         // Submit the request to the service and store the response.
537         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
538         int statusCode = res.getStatus();
539
540         // Check the status code of the response: does it match
541         // the expected response(s)?
542         if(logger.isDebugEnabled()){
543             logger.debug(testName + ": status = " + statusCode);
544         }
545         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
546                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
547         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
548     }
549
550     // ---------------------------------------------------------------
551     // Utility tests : tests of code used in tests above
552     // ---------------------------------------------------------------
553     /**
554      * Tests the code for manually submitting data that is used by several
555      * of the methods above.
556      */
557     @Test(dependsOnMethods = {"create", "read"})
558     public void testSubmitRequest() {
559
560         // Expected status code: 200 OK
561         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
562
563         // Submit the request to the service and store the response.
564         String method = ServiceRequestType.READ.httpMethodName();
565         String url = getResourceURL(knownResourceId);
566         int statusCode = submitRequest(method, url);
567
568         // Check the status code of the response: does it match
569         // the expected response(s)?
570         if(logger.isDebugEnabled()){
571             logger.debug("testSubmitRequest: url=" + url +
572                 " status=" + statusCode);
573         }
574         Assert.assertEquals(statusCode, EXPECTED_STATUS);
575
576     }
577
578     // ---------------------------------------------------------------
579     // Cleanup of resources created during testing
580     // ---------------------------------------------------------------
581
582     /**
583      * Deletes all resources created by tests, after all tests have been run.
584      *
585      * This cleanup method will always be run, even if one or more tests fail.
586      * For this reason, it attempts to remove all resources created
587      * at any point during testing, even if some of those resources
588      * may be expected to be deleted by certain tests.
589      */
590     @AfterClass(alwaysRun=true)
591     public void cleanUp() {
592         if (logger.isDebugEnabled()) {
593             logger.debug("Cleaning up temporary resources created for testing ...");
594         }
595         for (String resourceId : allResourceIdsCreated) {
596             // Note: Any non-success responses are ignored and not reported.
597             ClientResponse<Response> res = client.delete(resourceId);
598         }
599     }
600
601     // ---------------------------------------------------------------
602     // Utility methods used by tests above
603     // ---------------------------------------------------------------
604     @Override
605     public String getServicePathComponent() {
606         return SERVICE_PATH_COMPONENT;
607     }
608
609 }