]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
79c43c77999043b82f11823e09e16c3b8d95495c
[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 (c)) 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.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.client.PersonAuthorityClient;
33 import org.collectionspace.services.person.PersonauthoritiesCommon;
34 import org.collectionspace.services.person.PersonauthoritiesCommonList;
35 import org.collectionspace.services.person.PersonsCommon;
36 import org.collectionspace.services.person.PersonsCommonList;
37
38 import org.jboss.resteasy.client.ClientResponse;
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.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.Test;
47
48 /**
49  * PersonAuthorityServiceTest, carries out tests against a
50  * deployed and running PersonAuthority Service.
51  *
52  * $LastChangedRevision: 753 $
53  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
54  */
55 public class PersonAuthorityServiceTest extends AbstractServiceTest {
56
57     private final Logger logger =
58         LoggerFactory.getLogger(PersonAuthorityServiceTest.class);
59
60     // Instance variables specific to this test.
61     private PersonAuthorityClient client = new PersonAuthorityClient();
62     final String SERVICE_PATH_COMPONENT = "personauthorities";
63     final String ITEM_SERVICE_PATH_COMPONENT = "items";
64     private String knownResourceId = null;
65     private String lastPersonAuthId = null;
66     private String knownResourceRefName = null;
67     private String knownItemResourceId = null;
68     private int nItemsToCreateInList = 3;
69     private List<String> allResourceIdsCreated = new ArrayList<String>();
70     private Map<String, String> allResourceItemIdsCreated =
71         new HashMap<String, String>();
72     
73     protected String createPersonAuthRefName(String personAuthorityName) {
74         return "urn:cspace:org.collectionspace.demo:personauthority:name("
75                         +personAuthorityName+")";
76     }
77
78     protected String createPersonRefName(
79                                                 String personAuthRefName, String personName) {
80         return personAuthRefName+":person:name("+personName+")";
81     }
82
83
84     // ---------------------------------------------------------------
85     // CRUD tests : CREATE tests
86     // ---------------------------------------------------------------
87     // Success outcomes
88     @Override
89     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
90     public void create(String testName) throws Exception {
91
92         // Perform setup, such as initializing the type of service request
93         // (e.g. CREATE, DELETE), its valid and expected status codes, and
94         // its associated HTTP method name (e.g. POST, DELETE).
95         setupCreate(testName);
96
97         // Submit the request to the service and store the response.
98         String identifier = createIdentifier();
99         String displayName = "displayName-" + identifier;
100         String refName = createPersonAuthRefName(displayName);
101         String typeName = "vocabType-" + identifier;
102         MultipartOutput multipart = 
103                 createPersonAuthorityInstance(displayName, refName, typeName);
104         ClientResponse<Response> res = client.create(multipart);
105         int statusCode = res.getStatus();
106
107         // Check the status code of the response: does it match
108         // the expected response(s)?
109         //
110         // Specifically:
111         // Does it fall within the set of valid status codes?
112         // Does it exactly match the expected status code?
113         if(logger.isDebugEnabled()){
114             logger.debug(testName + ": status = " + statusCode);
115         }
116         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
117                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
118         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
119
120         // Store the refname from the first resource created
121         // for additional tests below.
122         knownResourceRefName = refName;
123
124         lastPersonAuthId = extractId(res);
125         // Store the ID returned from the first resource created
126         // for additional tests below.
127         if (knownResourceId == null){
128             knownResourceId = lastPersonAuthId;
129             if (logger.isDebugEnabled()) {
130                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
131             }
132         }
133         // Store the IDs from every resource created by tests,
134         // so they can be deleted after tests have been run.
135         allResourceIdsCreated.add(extractId(res));
136
137     }
138
139     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
140         dependsOnMethods = {"create"})
141     public void createItem(String testName) {
142         setupCreate(testName);
143
144         knownItemResourceId = createItemInAuthority(lastPersonAuthId, knownResourceRefName);
145         if(logger.isDebugEnabled()){
146             logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
147         }
148     }
149
150     private String createItemInAuthority(String vcsid, String authRefName) {
151
152         final String testName = "createItemInAuthority";
153         if(logger.isDebugEnabled()){
154             logger.debug(testName + ":...");
155         }
156
157         // Submit the request to the service and store the response.
158         String identifier = createIdentifier();
159         String refName = createPersonRefName(authRefName, "Patrick Schmitz");
160         MultipartOutput multipart = createPersonInstance(vcsid, refName,
161                         "Patrick","Lee","Schmitz","pls", null, null, null,
162                                 "1960", null, "Detroit, MI", null, "Coders", "American", "male",
163                                 null, null, null, null );
164         ClientResponse<Response> res = client.createItem(vcsid, multipart);
165         int statusCode = res.getStatus();
166
167         // Check the status code of the response: does it match
168         // the expected response(s)?
169         if(logger.isDebugEnabled()){
170             logger.debug(testName + ": status = " + statusCode);
171         }
172         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175
176         // Store the ID returned from the first item resource created
177         // for additional tests below.
178         if (knownItemResourceId == null){
179             knownItemResourceId = extractId(res);
180             if (logger.isDebugEnabled()) {
181                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
182             }
183         }
184
185         // Store the IDs from any item resources created
186         // by tests, along with the IDs of their parents, so these items
187         // can be deleted after all tests have been run.
188         //
189         // Item resource IDs are unique, so these are used as keys;
190         // the non-unique IDs of their parents are stored as associated values.
191         allResourceItemIdsCreated.put(extractId(res), vcsid);
192
193         return extractId(res);
194     }
195
196     @Override
197     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
198             dependsOnMethods = {"create", "createItem"})
199     public void createList(String testName) throws Exception {
200         for (int i = 0; i < 3; i++) {
201             create(testName);
202             knownResourceId = lastPersonAuthId;
203             if (logger.isDebugEnabled()) {
204                 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
205             }
206             // Add nItemsToCreateInList items to each personauthority
207             for (int j = 0; j < nItemsToCreateInList; j++) {
208                 createItem(testName);
209             }
210         }
211     }
212
213     // Failure outcomes
214     // Placeholders until the three tests below can be uncommented.
215     // See Issue CSPACE-401.
216     @Override
217     public void createWithEmptyEntityBody(String testName) throws Exception {
218     }
219
220     @Override
221     public void createWithMalformedXml(String testName) throws Exception {
222     }
223
224     @Override
225     public void createWithWrongXmlSchema(String testName) throws Exception {
226     }
227
228     /*
229     @Override
230     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
231         dependsOnMethods = {"create", "testSubmitRequest"})
232     public void createWithEmptyEntityBody(String testName) throws Exception {
233
234     // Perform setup.
235     setupCreateWithEmptyEntityBody(testName);
236
237     // Submit the request to the service and store the response.
238     String method = REQUEST_TYPE.httpMethodName();
239     String url = getServiceRootURL();
240     String mediaType = MediaType.APPLICATION_XML;
241     final String entity = "";
242     int statusCode = submitRequest(method, url, mediaType, entity);
243
244     // Check the status code of the response: does it match
245     // the expected response(s)?
246     if(logger.isDebugEnabled()) {
247         logger.debug(testName + ": url=" + url +
248             " status=" + statusCode);
249      }
250     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253     }
254
255     @Override
256     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
257         dependsOnMethods = {"create", "testSubmitRequest"})
258     public void createWithMalformedXml(String testName) throws Exception {
259
260     // Perform setup.
261     setupCreateWithMalformedXml(testName);
262
263     // Submit the request to the service and store the response.
264     String method = REQUEST_TYPE.httpMethodName();
265     String url = getServiceRootURL();
266     String mediaType = MediaType.APPLICATION_XML;
267     final String entity = MALFORMED_XML_DATA; // Constant from base class.
268     int statusCode = submitRequest(method, url, mediaType, entity);
269
270     // Check the status code of the response: does it match
271     // the expected response(s)?
272     if(logger.isDebugEnabled()){
273         logger.debug(testName + ": url=" + url +
274             " status=" + statusCode);
275      }
276     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
277     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
278     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
279     }
280
281     @Override
282     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
283         dependsOnMethods = {"create", "testSubmitRequest"})
284     public void createWithWrongXmlSchema(String testName) throws Exception {
285
286     // Perform setup.
287     setupCreateWithWrongXmlSchema(testName);
288
289     // Submit the request to the service and store the response.
290     String method = REQUEST_TYPE.httpMethodName();
291     String url = getServiceRootURL();
292     String mediaType = MediaType.APPLICATION_XML;
293     final String entity = WRONG_XML_SCHEMA_DATA;
294     int statusCode = submitRequest(method, url, mediaType, entity);
295
296     // Check the status code of the response: does it match
297     // the expected response(s)?
298     if(logger.isDebugEnabled()){
299         logger.debug(testName + ": url=" + url +
300             " status=" + statusCode);
301      }
302     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
305     }
306      */
307
308     // ---------------------------------------------------------------
309     // CRUD tests : READ tests
310     // ---------------------------------------------------------------
311     // Success outcomes
312     @Override
313     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
314         dependsOnMethods = {"create"})
315     public void read(String testName) throws Exception {
316
317         // Perform setup.
318         setupRead();
319
320         // Submit the request to the service and store the response.
321         ClientResponse<MultipartInput> res = client.read(knownResourceId);
322         int statusCode = res.getStatus();
323
324         // Check the status code of the response: does it match
325         // the expected response(s)?
326         if(logger.isDebugEnabled()){
327             logger.debug(testName + ": status = " + statusCode);
328         }
329         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
330                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
331         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
332         //FIXME: remove the following try catch once Aron fixes signatures
333         try {
334             MultipartInput input = (MultipartInput) res.getEntity();
335             PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
336                     client.getCommonPartName(), PersonauthoritiesCommon.class);
337             Assert.assertNotNull(personAuthority);
338         } catch (Exception e) {
339             throw new RuntimeException(e);
340         }
341     }
342
343     /*
344     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
345             dependsOnMethods = {"read"})
346         public void readByName(String testName) throws Exception {
347
348             // Perform setup.
349             setupRead();
350
351             // Submit the request to the service and store the response.
352             ClientResponse<MultipartInput> res = client.read(knownResourceId);
353             int statusCode = res.getStatus();
354
355             // Check the status code of the response: does it match
356             // the expected response(s)?
357             if(logger.isDebugEnabled()){
358                 logger.debug(testName + ": status = " + statusCode);
359             }
360             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
361                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
362             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
363             //FIXME: remove the following try catch once Aron fixes signatures
364             try {
365                 MultipartInput input = (MultipartInput) res.getEntity();
366                 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
367                         client.getCommonPartName(), PersonauthoritiesCommon.class);
368                 Assert.assertNotNull(personAuthority);
369             } catch (Exception e) {
370                 throw new RuntimeException(e);
371             }
372         }
373     */
374
375     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
376         dependsOnMethods = {"createItem", "read"})
377     public void readItem(String testName) throws Exception {
378
379         // Perform setup.
380         setupRead(testName);
381
382         // Submit the request to the service and store the response.
383         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
384         int statusCode = res.getStatus();
385
386         // Check the status code of the response: does it match
387         // the expected response(s)?
388         if(logger.isDebugEnabled()){
389             logger.debug(testName + ": status = " + statusCode);
390         }
391         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
392                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
393         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
394
395         // Check whether we've received a person.
396         MultipartInput input = (MultipartInput) res.getEntity();
397         PersonsCommon person = (PersonsCommon) extractPart(input,
398                 client.getItemCommonPartName(), PersonsCommon.class);
399         Assert.assertNotNull(person);
400
401     }
402
403     // Failure outcomes
404     @Override
405     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
406         dependsOnMethods = {"read"})
407     public void readNonExistent(String testName) {
408
409         // Perform setup.
410         setupReadNonExistent(testName);
411
412         // Submit the request to the service and store the response.
413         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
414         int statusCode = res.getStatus();
415
416         // Check the status code of the response: does it match
417         // the expected response(s)?
418         if(logger.isDebugEnabled()){
419             logger.debug(testName + ": 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     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
427         dependsOnMethods = {"readItem", "readNonExistent"})
428     public void readItemNonExistent(String testName) {
429
430         // Perform setup.
431         setupReadNonExistent(testName);
432
433         // Submit the request to the service and store the response.
434         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
435         int statusCode = res.getStatus();
436
437         // Check the status code of the response: does it match
438         // the expected response(s)?
439         if(logger.isDebugEnabled()){
440             logger.debug(testName + ": status = " + statusCode);
441         }
442         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
443                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
444         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
445     }
446     // ---------------------------------------------------------------
447     // CRUD tests : READ_LIST tests
448     // ---------------------------------------------------------------
449     // Success outcomes
450
451     @Override
452     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
453         dependsOnMethods = {"createList", "read"})
454     public void readList(String testName) throws Exception {
455
456         // Perform setup.
457         setupReadList(testName);
458
459         // Submit the request to the service and store the response.
460         ClientResponse<PersonauthoritiesCommonList> res = client.readList();
461         PersonauthoritiesCommonList list = res.getEntity();
462         int statusCode = res.getStatus();
463
464         // Check the status code of the response: does it match
465         // the expected response(s)?
466         if(logger.isDebugEnabled()){
467             logger.debug(testName + ": status = " + statusCode);
468         }
469         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
472
473         // Optionally output additional data about list members for debugging.
474         boolean iterateThroughList = false;
475         if (iterateThroughList && logger.isDebugEnabled()) {
476             List<PersonauthoritiesCommonList.PersonauthorityListItem> items =
477                     list.getPersonauthorityListItem();
478             int i = 0;
479             for (PersonauthoritiesCommonList.PersonauthorityListItem item : items) {
480                 String csid = item.getCsid();
481                 logger.debug(testName + ": list-item[" + i + "] csid=" +
482                         csid);
483                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
484                         item.getDisplayName());
485                 logger.debug(testName + ": list-item[" + i + "] URI=" +
486                         item.getUri());
487                 readItemList(csid);
488                 i++;
489             }
490         }
491     }
492
493     @Test(dependsOnMethods = {"createList", "readItem"})
494     public void readItemList() {
495         readItemList(knownResourceId);
496     }
497
498     private void readItemList(String vcsid) {
499
500         final String testName = "readItemList";
501
502         // Perform setup.
503         setupReadList(testName);
504
505         // Submit the request to the service and store the response.
506         ClientResponse<PersonsCommonList> res =
507                 client.readItemList(vcsid);
508         PersonsCommonList list = res.getEntity();
509         int statusCode = res.getStatus();
510
511         // Check the status code of the response: does it match
512         // the expected response(s)?
513         if(logger.isDebugEnabled()){
514             logger.debug("  " + testName + ": status = " + statusCode);
515         }
516         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
519
520         List<PersonsCommonList.PersonListItem> items =
521             list.getPersonListItem();
522         int nItemsReturned = items.size();
523         if(logger.isDebugEnabled()){
524             logger.debug("  " + testName + ": Expected "
525                         + nItemsToCreateInList+" items; got: "+nItemsReturned);
526         }
527         Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
528
529         // Optionally output additional data about list members for debugging.
530         boolean iterateThroughList = false;
531         if (iterateThroughList && logger.isDebugEnabled()) {
532             int i = 0;
533             for (PersonsCommonList.PersonListItem item : items) {
534                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
535                         item.getCsid());
536                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
537                         item.getDisplayName());
538                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
539                         item.getUri());
540                 i++;
541             }
542         }
543     }
544
545     // Failure outcomes
546     // None at present.
547     // ---------------------------------------------------------------
548     // CRUD tests : UPDATE tests
549     // ---------------------------------------------------------------
550     // Success outcomes
551     @Override
552     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
553         dependsOnMethods = {"read"})
554     public void update(String testName) throws Exception {
555
556         // Perform setup.
557         setupUpdate(testName);
558
559         // Retrieve the contents of a resource to update.
560         ClientResponse<MultipartInput> res =
561                 client.read(knownResourceId);
562         if(logger.isDebugEnabled()){
563             logger.debug(testName + ": read status = " + res.getStatus());
564         }
565         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
566
567         if(logger.isDebugEnabled()){
568             logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
569         }
570         MultipartInput input = (MultipartInput) res.getEntity();
571         PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
572                 client.getCommonPartName(), PersonauthoritiesCommon.class);
573         Assert.assertNotNull(personAuthority);
574
575         // Update the contents of this resource.
576         personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
577         personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
578         if(logger.isDebugEnabled()){
579             logger.debug("to be updated PersonAuthority");
580             logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
581         }
582
583         // Submit the updated resource to the service and store the response.
584         MultipartOutput output = new MultipartOutput();
585         OutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
586         commonPart.getHeaders().add("label", client.getCommonPartName());
587         res = client.update(knownResourceId, output);
588         int statusCode = res.getStatus();
589
590         // Check the status code of the response: does it match the expected response(s)?
591         if(logger.isDebugEnabled()){
592             logger.debug("update: status = " + statusCode);
593         }
594         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
595                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
596         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
597
598         // Retrieve the updated resource and verify that its contents exist.
599         input = (MultipartInput) res.getEntity();
600         PersonauthoritiesCommon updatedPersonAuthority =
601                 (PersonauthoritiesCommon) extractPart(input,
602                         client.getCommonPartName(), PersonauthoritiesCommon.class);
603         Assert.assertNotNull(updatedPersonAuthority);
604
605         // Verify that the updated resource received the correct data.
606         Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
607                 personAuthority.getDisplayName(),
608                 "Data in updated object did not match submitted data.");
609     }
610
611     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
612         dependsOnMethods = {"readItem", "update"})
613     public void updateItem(String testName) throws Exception {
614
615         // Perform setup.
616         setupUpdate(testName);
617
618         ClientResponse<MultipartInput> res =
619                 client.readItem(knownResourceId, knownItemResourceId);
620         if(logger.isDebugEnabled()){
621             logger.debug(testName + ": read status = " + res.getStatus());
622         }
623         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
624
625         if(logger.isDebugEnabled()){
626             logger.debug("got Person to update with ID: " +
627                 knownItemResourceId +
628                 " in PersonAuthority: " + knownResourceId );
629         }
630         MultipartInput input = (MultipartInput) res.getEntity();
631         PersonsCommon person = (PersonsCommon) extractPart(input,
632                 client.getItemCommonPartName(), PersonsCommon.class);
633         Assert.assertNotNull(person);
634
635         // Update the contents of this resource.
636         person.setForeName("updated-" + person.getForeName());
637         if(logger.isDebugEnabled()){
638             logger.debug("to be updated Person");
639             logger.debug(objectAsXmlString(person,
640                 PersonsCommon.class));
641         }
642
643         // Submit the updated resource to the service and store the response.
644         MultipartOutput output = new MultipartOutput();
645         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
646         commonPart.getHeaders().add("label", client.getItemCommonPartName());
647         res = client.updateItem(knownResourceId, knownItemResourceId, output);
648         int statusCode = res.getStatus();
649
650         // Check the status code of the response: does it match the expected response(s)?
651         if(logger.isDebugEnabled()){
652             logger.debug("updateItem: status = " + statusCode);
653         }
654         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
655                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
656         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
657
658         // Retrieve the updated resource and verify that its contents exist.
659         input = (MultipartInput) res.getEntity();
660         PersonsCommon updatedPerson =
661                 (PersonsCommon) extractPart(input,
662                         client.getItemCommonPartName(), PersonsCommon.class);
663         Assert.assertNotNull(updatedPerson);
664
665         // Verify that the updated resource received the correct data.
666         Assert.assertEquals(updatedPerson.getForeName(),
667                 person.getForeName(),
668                 "Data in updated Person did not match submitted data.");
669     }
670
671     // Failure outcomes
672     // Placeholders until the three tests below can be uncommented.
673     // See Issue CSPACE-401.
674     @Override
675     public void updateWithEmptyEntityBody(String testName) throws Exception {
676     }
677
678     @Override
679     public void updateWithMalformedXml(String testName) throws Exception {
680     }
681
682     @Override
683     public void updateWithWrongXmlSchema(String testName) throws Exception {
684     }
685
686     /*
687     @Override
688     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
689         dependsOnMethods = {"create", "update", "testSubmitRequest"})
690     public void updateWithEmptyEntityBody(String testName) throws Exception {
691
692     // Perform setup.
693     setupUpdateWithEmptyEntityBody(testName);
694
695     // Submit the request to the service and store the response.
696     String method = REQUEST_TYPE.httpMethodName();
697     String url = getResourceURL(knownResourceId);
698     String mediaType = MediaType.APPLICATION_XML;
699     final String entity = "";
700     int statusCode = submitRequest(method, url, mediaType, entity);
701
702     // Check the status code of the response: does it match
703     // the expected response(s)?
704     if(logger.isDebugEnabled()){
705         logger.debug(testName + ": url=" + url +
706             " status=" + statusCode);
707      }
708     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
709     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
710     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
711     }
712
713     @Override
714     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
715         dependsOnMethods = {"create", "update", "testSubmitRequest"})
716     public void updateWithMalformedXml(String testName) throws Exception {
717
718     // Perform setup.
719     setupUpdateWithMalformedXml(testName);
720
721     // Submit the request to the service and store the response.
722     String method = REQUEST_TYPE.httpMethodName();
723     String url = getResourceURL(knownResourceId);
724     String mediaType = MediaType.APPLICATION_XML;
725     final String entity = MALFORMED_XML_DATA;
726     int statusCode = submitRequest(method, url, mediaType, entity);
727
728     // Check the status code of the response: does it match
729     // the expected response(s)?
730     if(logger.isDebugEnabled()){
731         logger.debug(testName + ": url=" + url +
732            " status=" + statusCode);
733      }
734     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
735     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
736     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
737     }
738
739     @Override
740     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
741         dependsOnMethods = {"create", "update", "testSubmitRequest"})
742     public void updateWithWrongXmlSchema(String testName) throws Exception {
743
744     // Perform setup.
745     setupUpdateWithWrongXmlSchema(testName);
746
747     // Submit the request to the service and store the response.
748     String method = REQUEST_TYPE.httpMethodName();
749     String url = getResourceURL(knownResourceId);
750     String mediaType = MediaType.APPLICATION_XML;
751     final String entity = WRONG_XML_SCHEMA_DATA;
752     int statusCode = submitRequest(method, url, mediaType, entity);
753
754     // Check the status code of the response: does it match
755     // the expected response(s)?
756     if(logger.isDebugEnabled()){
757         logger.debug("updateWithWrongXmlSchema: url=" + url +
758             " status=" + statusCode);
759      }
760     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
761     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
762     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
763     }
764      */
765
766
767     @Override
768     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
769         dependsOnMethods = {"update", "testSubmitRequest"})
770     public void updateNonExistent(String testName) throws Exception {
771
772         // Perform setup.
773         setupUpdateNonExistent(testName);
774
775         // Submit the request to the service and store the response.
776         // Note: The ID used in this 'create' call may be arbitrary.
777         // The only relevant ID may be the one used in update(), below.
778
779         // The only relevant ID may be the one used in update(), below.
780         MultipartOutput multipart = createPersonAuthorityInstance(NON_EXISTENT_ID);
781         ClientResponse<MultipartInput> res =
782                 client.update(NON_EXISTENT_ID, multipart);
783         int statusCode = res.getStatus();
784
785         // Check the status code of the response: does it match
786         // the expected response(s)?
787         if(logger.isDebugEnabled()){
788             logger.debug(testName + ": status = " + statusCode);
789         }
790         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
791                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
792         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
793     }
794
795     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
796         dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
797     public void updateNonExistentItem(String testName) throws Exception {
798
799         // Perform setup.
800         setupUpdateNonExistent(testName);
801
802         // Submit the request to the service and store the response.
803         // Note: The ID used in this 'create' call may be arbitrary.
804         // The only relevant ID may be the one used in update(), below.
805
806         // The only relevant ID may be the one used in update(), below.
807         MultipartOutput multipart = createPersonInstance(NON_EXISTENT_ID, 
808                         createPersonRefName(NON_EXISTENT_ID, NON_EXISTENT_ID),  
809                         "Patrick","Lee","Schmitz","pls", null, null, null,
810                                 "1960", null, "Detroit, MI", null, "Coders", "American", "male",
811                                 null, null, null, null );
812         ClientResponse<MultipartInput> res =
813                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
814         int statusCode = res.getStatus();
815
816         // Check the status code of the response: does it match
817         // the expected response(s)?
818         if(logger.isDebugEnabled()){
819             logger.debug(testName + ": status = " + statusCode);
820         }
821         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
822                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
823         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
824     }
825
826     // ---------------------------------------------------------------
827     // CRUD tests : DELETE tests
828     // ---------------------------------------------------------------
829     // Success outcomes
830     @Override
831     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
832         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
833     public void delete(String testName) throws Exception {
834
835         // Perform setup.
836         setupDelete(testName);
837
838         // Submit the request to the service and store the response.
839         ClientResponse<Response> res = client.delete(knownResourceId);
840         int statusCode = res.getStatus();
841
842         // Check the status code of the response: does it match
843         // the expected response(s)?
844         if(logger.isDebugEnabled()){
845             logger.debug(testName + ": status = " + statusCode);
846         }
847         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
848                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
849         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
850     }
851
852    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
853         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
854             "updateItem"})
855     public void deleteItem(String testName) throws Exception {
856
857         // Perform setup.
858         setupDelete(testName);
859
860         // Submit the request to the service and store the response.
861         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
862         int statusCode = res.getStatus();
863
864         // Check the status code of the response: does it match
865         // the expected response(s)?
866         if(logger.isDebugEnabled()){
867             logger.debug("delete: status = " + statusCode);
868         }
869         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
870                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
871         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
872     }
873
874     // Failure outcomes
875     @Override
876     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
877         dependsOnMethods = {"delete"})
878     public void deleteNonExistent(String testName) throws Exception {
879
880         // Perform setup.
881         setupDeleteNonExistent(testName);
882
883         // Submit the request to the service and store the response.
884         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
885         int statusCode = res.getStatus();
886
887         // Check the status code of the response: does it match
888         // the expected response(s)?
889         if(logger.isDebugEnabled()){
890             logger.debug(testName + ": status = " + statusCode);
891         }
892         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
893                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
894         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
895     }
896
897     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
898         dependsOnMethods = {"deleteItem"})
899     public void deleteNonExistentItem(String testName) {
900
901         // Perform setup.
902         setupDeleteNonExistent(testName);
903
904         // Submit the request to the service and store the response.
905         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
906         int statusCode = res.getStatus();
907
908         // Check the status code of the response: does it match
909         // the expected response(s)?
910         if(logger.isDebugEnabled()){
911             logger.debug(testName + ": status = " + statusCode);
912         }
913         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
914                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
915         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
916     }
917
918     // ---------------------------------------------------------------
919     // Utility tests : tests of code used in tests above
920     // ---------------------------------------------------------------
921     /**
922      * Tests the code for manually submitting data that is used by several
923      * of the methods above.
924      */
925     @Test(dependsOnMethods = {"create", "read"})
926     public void testSubmitRequest() {
927
928         // Expected status code: 200 OK
929         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
930
931         // Submit the request to the service and store the response.
932         String method = ServiceRequestType.READ.httpMethodName();
933         String url = getResourceURL(knownResourceId);
934         int statusCode = submitRequest(method, url);
935
936         // Check the status code of the response: does it match
937         // the expected response(s)?
938         if(logger.isDebugEnabled()){
939             logger.debug("testSubmitRequest: url=" + url +
940                 " status=" + statusCode);
941         }
942         Assert.assertEquals(statusCode, EXPECTED_STATUS);
943
944     }
945
946     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
947     public void testItemSubmitRequest() {
948
949         // Expected status code: 200 OK
950         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
951
952         // Submit the request to the service and store the response.
953         String method = ServiceRequestType.READ.httpMethodName();
954         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
955         int statusCode = submitRequest(method, url);
956
957         // Check the status code of the response: does it match
958         // the expected response(s)?
959         if(logger.isDebugEnabled()){
960             logger.debug("testItemSubmitRequest: url=" + url +
961                 " status=" + statusCode);
962         }
963         Assert.assertEquals(statusCode, EXPECTED_STATUS);
964
965     }
966
967     // ---------------------------------------------------------------
968     // Cleanup of resources created during testing
969     // ---------------------------------------------------------------
970     
971     /**
972      * Deletes all resources created by tests, after all tests have been run.
973      *
974      * This cleanup method will always be run, even if one or more tests fail.
975      * For this reason, it attempts to remove all resources created
976      * at any point during testing, even if some of those resources
977      * may be expected to be deleted by certain tests.
978      */
979     @AfterClass(alwaysRun=true)
980     public void cleanUp() {
981         if (logger.isDebugEnabled()) {
982             logger.debug("Cleaning up temporary resources created for testing ...");
983         }
984         // Clean up person resources.
985         String personAuthorityResourceId;
986         String personResourceId;
987         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
988             personResourceId = entry.getKey();
989             personAuthorityResourceId = entry.getValue();
990             // Note: Any non-success responses are ignored and not reported.
991             ClientResponse<Response> res =
992                 client.deleteItem(personAuthorityResourceId, personResourceId);
993         }
994         // Clean up personAuthority resources.
995         for (String resourceId : allResourceIdsCreated) {
996             // Note: Any non-success responses are ignored and not reported.
997             ClientResponse<Response> res = client.delete(resourceId);
998         }
999     }
1000
1001     // ---------------------------------------------------------------
1002     // Utility methods used by tests above
1003     // ---------------------------------------------------------------
1004     @Override
1005     public String getServicePathComponent() {
1006         return SERVICE_PATH_COMPONENT;
1007     }
1008
1009     public String getItemServicePathComponent() {
1010         return ITEM_SERVICE_PATH_COMPONENT;
1011     }
1012
1013     /**
1014      * Returns the root URL for a service.
1015      *
1016      * This URL consists of a base URL for all services, followed by
1017      * a path component for the owning personAuthority, followed by the 
1018      * path component for the items.
1019      *
1020      * @return The root URL for a service.
1021      */
1022     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1023         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1024     }
1025
1026     /**
1027      * Returns the URL of a specific resource managed by a service, and
1028      * designated by an identifier (such as a universally unique ID, or UUID).
1029      *
1030      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
1031      *
1032      * @return The URL of a specific resource managed by a service.
1033      */
1034     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1035         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1036     }
1037
1038     private MultipartOutput createPersonAuthorityInstance(String identifier) {
1039         String displayName = "displayName-" + identifier;
1040         String refName = createPersonAuthRefName(displayName);
1041         String typeName = "vocabType-" + identifier;
1042         return createPersonAuthorityInstance(
1043                 displayName, refName,typeName );
1044     }
1045
1046     private MultipartOutput createPersonAuthorityInstance(
1047                 String displayName, String refName, String vocabType) {
1048         PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();
1049         personAuthority.setDisplayName(displayName);
1050         if(refName!=null)
1051             personAuthority.setRefName(refName);
1052         personAuthority.setVocabType(vocabType);
1053         MultipartOutput multipart = new MultipartOutput();
1054         OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
1055         commonPart.getHeaders().add("label", client.getCommonPartName());
1056
1057         if(logger.isDebugEnabled()) {
1058             logger.debug("to be created, personAuthority common");
1059             logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
1060         }
1061         return multipart;
1062     }
1063
1064     private MultipartOutput createPersonInstance(String inAuthority, String refName,
1065         String foreName, String middleName, String surName, 
1066         String initials, String salutation, String title, String nameAdditions,  
1067         String birthDate, String deathDate, String birthPlace, String deathPlace, 
1068         String group, String nationality, String gender, String occupation, 
1069         String schoolOrStyle, String bioNote, String nameNote ) {
1070         PersonsCommon person = new PersonsCommon();
1071         person.setInAuthority(inAuthority);
1072         person.setRefName(refName);
1073         if(foreName!=null)
1074                 person.setForeName(foreName);
1075         if(middleName!=null)
1076                 person.setMiddleName(middleName);
1077         if(surName!=null)
1078                 person.setSurName(surName);
1079         if(initials!=null)
1080                 person.setInitials(initials);
1081         if(salutation!=null)
1082                 person.setSalutation(salutation);
1083         if(title!=null)
1084                 person.setTitle(title);
1085         if(nameAdditions!=null)
1086                 person.setNameAdditions(nameAdditions);
1087         if(birthDate!=null)
1088                 person.setBirthDate(birthDate);
1089         if(deathDate!=null)
1090                 person.setDeathDate(deathDate);
1091         if(birthPlace!=null)
1092                 person.setBirthPlace(birthPlace);
1093         if(deathPlace!=null)
1094                 person.setDeathPlace(deathPlace);
1095         if(group!=null)
1096                 person.setGroup(group);
1097         if(nationality!=null)
1098                 person.setNationality(nationality);
1099         if(gender!=null)
1100                 person.setGender(gender);
1101         if(occupation!=null)
1102                 person.setOccupation(occupation);
1103         if(schoolOrStyle!=null)
1104                 person.setSchoolOrStyle(schoolOrStyle);
1105         if(bioNote!=null)
1106                 person.setBioNote(bioNote);
1107         if(nameNote!=null)
1108                 person.setNameNote(nameNote);
1109         MultipartOutput multipart = new MultipartOutput();
1110         OutputPart commonPart = multipart.addPart(person,
1111             MediaType.APPLICATION_XML_TYPE);
1112         commonPart.getHeaders().add("label", client.getItemCommonPartName());
1113
1114         if(logger.isDebugEnabled()){
1115             logger.debug("to be created, person common");
1116             logger.debug(objectAsXmlString(person,
1117                 PersonsCommon.class));
1118         }
1119
1120         return multipart;
1121     }
1122 }