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