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