]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
11101e8306e301f0968b9a8f96d2904eff346d58
[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.AuthorityClient;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37
38 import org.collectionspace.services.client.ContactClient;
39 import org.collectionspace.services.client.ContactClientUtils;
40 import org.collectionspace.services.common.AbstractCommonListUtils;
41 import org.collectionspace.services.contact.AddressGroup;
42 import org.collectionspace.services.contact.AddressGroupList;
43 import org.collectionspace.services.contact.ContactsCommon;
44
45 import org.collectionspace.services.client.PersonAuthorityClient;
46 import org.collectionspace.services.client.PersonAuthorityClientUtils;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.collectionspace.services.PersonJAXBSchema;
49 import org.collectionspace.services.person.PersonauthoritiesCommon;
50 import org.collectionspace.services.person.PersonsCommon;
51
52 import org.jboss.resteasy.client.ClientResponse;
53 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.Assert;
57 import org.testng.annotations.AfterClass;
58 import org.testng.annotations.Test;
59
60 /**
61  * PersonAuthorityServiceTest, carries out tests against a
62  * deployed and running PersonAuthority Service.
63  *
64  * $LastChangedRevision: 753 $
65  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
66  */
67 public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
68
69     /** The logger. */
70     private final String CLASS_NAME = PersonAuthorityServiceTest.class.getName();
71     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
72     private final String REFNAME = "refName";
73     private final String DISPLAYNAME = "displayName";
74
75     @Override
76     public String getServicePathComponent() {
77         return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
78     }
79
80     @Override
81     protected String getServiceName() {
82         return PersonAuthorityClient.SERVICE_NAME;
83     }
84
85     public String getItemServicePathComponent() {
86         return AuthorityClient.ITEMS;
87     }
88     /** The test forename. */
89     final String TEST_FORE_NAME = "John";
90     /** The test middle name. */
91     final String TEST_MIDDLE_NAME = null;
92     /** The test surname. */
93     final String TEST_SUR_NAME = "Wayne";
94     /** The test birthdate. */
95     final String TEST_BIRTH_DATE = "May 26, 1907";
96     /** The test death date. */
97     final String TEST_DEATH_DATE = "June 11, 1979";
98     // Hold some values for a recently created item to verify upon read.
99     private String knownResourceId = null;
100     private String knownResourceShortIdentifer = null;
101     //private String knownResourceRefName = null;
102     private String knownItemResourceId = null;
103     private String knownItemResourceShortIdentifer = null;
104     // The resource ID of an item resource used for partial term matching tests.
105     private String knownItemPartialTermResourceId = null;
106     /** The known contact resource id. */
107     private String knownContactResourceId = null;
108     /** The n items to create in list. */
109     private int nItemsToCreateInList = 3;
110     /** The all item resource ids created. */
111     private Map<String, String> allItemResourceIdsCreated =
112             new HashMap<String, String>();
113     /** The all contact resource ids created. */
114     private Map<String, String> allContactResourceIdsCreated =
115             new HashMap<String, String>();
116
117     protected void setKnownResource(String id, String shortIdentifer,
118             String refName) {
119         knownResourceId = id;
120         knownResourceShortIdentifer = shortIdentifer;
121         //knownResourceRefName = refName;
122     }
123
124     protected void setKnownItemResource(String id, String shortIdentifer) {
125         knownItemResourceId = id;
126         knownItemResourceShortIdentifer = shortIdentifer;
127     }
128
129     /* (non-Javadoc)
130      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
131      */
132     @Override
133     protected CollectionSpaceClient getClientInstance() {
134         return new PersonAuthorityClient();
135     }
136
137     // ---------------------------------------------------------------
138     // CRUD tests : CREATE tests
139     // ---------------------------------------------------------------
140     // Success outcomes
141     /* (non-Javadoc)
142      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
143      */
144     @Override
145     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
146     groups = {"create"})
147     public void create(String testName) throws Exception {
148
149         if (logger.isDebugEnabled()) {
150             logger.debug(testBanner(testName, CLASS_NAME));
151         }
152         // Perform setup, such as initializing the type of service request
153         // (e.g. CREATE, DELETE), its valid and expected status codes, and
154         // its associated HTTP method name (e.g. POST, DELETE).
155         setupCreate();
156
157         // Submit the request to the service and store the response.
158         PersonAuthorityClient client = new PersonAuthorityClient();
159         String shortId = createIdentifier();
160         String displayName = "displayName-" + shortId;
161         //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
162         PoxPayloadOut multipart =
163                 PersonAuthorityClientUtils.createPersonAuthorityInstance(
164                 displayName, shortId, client.getCommonPartName());
165
166         String newID = null;
167         ClientResponse<Response> res = client.create(multipart);
168         try {
169             int statusCode = res.getStatus();
170
171             // Check the status code of the response: does it match
172             // the expected response(s)?
173             //
174             // Specifically:
175             // Does it fall within the set of valid status codes?
176             // Does it exactly match the expected status code?
177             if (logger.isDebugEnabled()) {
178                 logger.debug(testName + ": status = " + statusCode);
179             }
180             Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
181                     invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
182             Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
183
184             newID = PersonAuthorityClientUtils.extractId(res);
185         } finally {
186             res.releaseConnection();
187         }
188         // Save values for additional tests
189         if (knownResourceId == null) {
190             setKnownResource(newID, shortId, null ); //baseRefName);
191             if (logger.isDebugEnabled()) {
192                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
193             }
194         }
195         // Store the IDs from every resource created by tests,
196         // so they can be deleted after tests have been run.
197         allResourceIdsCreated.add(newID);
198     }
199
200     @Override
201     protected PoxPayloadOut createInstance(String identifier) {
202         PersonAuthorityClient client = new PersonAuthorityClient();
203         String displayName = "displayName-" + identifier;
204         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
205                 displayName, identifier, client.getCommonPartName());
206         return multipart;
207     }
208
209     @Override
210     protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
211         String headerLabel = new PersonAuthorityClient().getItemCommonPartName();
212         HashMap<String, String> personInfo = new HashMap<String, String>();
213         String shortId = "johnWayneTempActor";
214         personInfo.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
215         personInfo.put(PersonJAXBSchema.DISPLAY_NAME, "John Wayne Temp");
216         personInfo.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false");
217         personInfo.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, "JohnWayneTemp");
218         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
219
220         return PersonAuthorityClientUtils.createPersonInstance(parentCsid, identifier, personInfo, headerLabel);
221     }
222
223     /**
224      * Creates the item.
225      *
226      * @param testName the test name
227      */
228     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
229     groups = {"create"}, dependsOnMethods = {"create"})
230     public void createItem(String testName) {
231         if (logger.isDebugEnabled()) {
232             logger.debug(testBanner(testName, CLASS_NAME));
233         }
234         setupCreate();
235         String newID = createItemInAuthority(knownResourceId, null ); //knownResourceRefName);
236     }
237
238     /**
239      * Creates the item with an empty short identifier.
240      *
241      * @param testName the test name
242      */
243     /*
244     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
245     groups = {"create"}, dependsOnMethods = {"create"})
246     public void createItemWithEmptyShortId(String testName) {
247     if (logger.isDebugEnabled()) {
248     logger.debug(testBanner(testName, CLASS_NAME));
249     }
250     setupCreate();
251     
252     // Fill the property map
253     String shortId = "";
254     Map<String, String> fieldProperties = new HashMap<String,String>();
255     fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
256     fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME, "Rod Beck");
257     fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false");
258     fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, "RodBeck");
259     
260     final Map NULL_REPEATABLE_FIELD_PROPERTIES = null;
261     String newID = createItemInAuthority(knownResourceId, knownResourceRefName,
262     shortId, fieldProperties, NULL_REPEATABLE_FIELD_PROPERTIES);
263     allResourceIdsCreated.add(newID);
264     }
265      */
266     /**
267      * Creates an item in an authority, using test data.
268      *
269      * @param vcsid the vcsid
270      * @param authRefName the auth ref name
271      * @return the string
272      */
273     private String createItemInAuthority(String vcsid, String authRefName) {
274
275         final String testName = "createItemInAuthority";
276         if (logger.isDebugEnabled()) {
277             logger.debug(testName + ":" + vcsid + "...");
278         }
279
280         Map<String, String> johnWayneMap = new HashMap<String, String>();
281         //
282         // Fill the property map
283         //
284         String shortId = "johnWayneActor";
285         johnWayneMap.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
286         johnWayneMap.put(PersonJAXBSchema.DISPLAY_NAME, "John Wayne");
287         johnWayneMap.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false");
288         johnWayneMap.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, "JohnWayne");
289         johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
290
291         johnWayneMap.put(PersonJAXBSchema.FORE_NAME, TEST_FORE_NAME);
292         johnWayneMap.put(PersonJAXBSchema.SUR_NAME, TEST_SUR_NAME);
293         johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
294         johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
295         johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
296         johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
297         johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better"
298                 + "known by his stage name John Wayne, was an American film actor, director "
299                 + "and producer. He epitomized rugged masculinity and has become an enduring "
300                 + "American icon. He is famous for his distinctive voice, walk and height. "
301                 + "He was also known for his conservative political views and his support in "
302                 + "the 1950s for anti-communist positions.");
303
304         Map<String, List<String>> johnWayneRepeatablesMap = new HashMap<String, List<String>>();
305         List<String> johnWayneGroups = new ArrayList<String>();
306         johnWayneGroups.add("Irish");
307         johnWayneGroups.add("Scottish");
308         johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups);
309
310         return createItemInAuthority(vcsid, null /*authRefName*/, shortId, johnWayneMap, johnWayneRepeatablesMap);
311
312     }
313
314     /**
315      * Creates an item in an authority.
316      *
317      * @param vcsid the vcsid
318      * @param authRefName the auth ref name
319      * @param itemFieldProperties a set of properties specifying the values of fields.
320      * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields.
321      * @return the string
322      */
323     private String createItemInAuthority(String vcsid, String authRefName, String shortId,
324             Map itemFieldProperties, Map itemRepeatableFieldProperties) {
325
326         final String testName = "createItemInAuthority";
327         if (logger.isDebugEnabled()) {
328             logger.debug(testName + ":" + vcsid + "...");
329         }
330
331         // Submit the request to the service and store the response.
332         PersonAuthorityClient client = new PersonAuthorityClient();
333
334         PoxPayloadOut multipart =
335                 PersonAuthorityClientUtils.createPersonInstance(vcsid, null /*authRefName*/, itemFieldProperties,
336                 itemRepeatableFieldProperties, client.getItemCommonPartName());
337
338         String newID = null;
339         ClientResponse<Response> res = client.createItem(vcsid, multipart);
340         try {
341             int statusCode = res.getStatus();
342             // Check the status code of the response: does it match
343             // the expected response(s)?
344             if (logger.isDebugEnabled()) {
345                 logger.debug(testName + ": status = " + statusCode);
346             }
347             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350
351             newID = PersonAuthorityClientUtils.extractId(res);
352         } finally {
353             res.releaseConnection();
354         }
355
356         // Store the ID returned from the first item resource created
357         // for additional tests below.
358         if (knownItemResourceId == null) {
359             setKnownItemResource(newID, shortId);
360             if (logger.isDebugEnabled()) {
361                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
362             }
363         }
364         if (logger.isDebugEnabled()) {
365             logger.debug(testName + " (created):" + vcsid + "/(" + newID + "," + shortId + ")");
366         }
367
368         // Store the IDs from any item resources created
369         // by tests, along with the IDs of their parents, so these items
370         // can be deleted after all tests have been run.
371         allItemResourceIdsCreated.put(newID, vcsid);
372
373         return newID;
374     }
375
376     /**
377      * Creates the contact.
378      *
379      * @param testName the test name
380      */
381     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
382     groups = {"create"}, dependsOnMethods = {"createItem"})
383     public void createContact(String testName) {
384         if (logger.isDebugEnabled()) {
385             logger.debug(testBanner(testName, CLASS_NAME));
386         }
387         setupCreate();
388         String newID = createContactInItem(knownResourceId, knownItemResourceId);
389     }
390
391     /**
392      * Creates the contact in item.
393      *
394      * @param parentcsid the parentcsid
395      * @param itemcsid the itemcsid
396      * @return the string
397      */
398     private String createContactInItem(String parentcsid, String itemcsid) {
399
400         final String testName = "createContactInItem";
401         if (logger.isDebugEnabled()) {
402             logger.debug(testName + ":...");
403         }
404
405         setupCreate();
406
407         // Submit the request to the service and store the response.
408         PersonAuthorityClient client = new PersonAuthorityClient();
409         String identifier = createIdentifier();
410         PoxPayloadOut multipart = ContactClientUtils.createContactInstance(parentcsid,
411                 itemcsid, identifier, new ContactClient().getCommonPartName());
412
413         String newID = null;
414         ClientResponse<Response> res =
415                 client.createContact(parentcsid, itemcsid, multipart);
416         try {
417             int statusCode = res.getStatus();
418             // Check the status code of the response: does it match
419             // the expected response(s)?
420             if (logger.isDebugEnabled()) {
421                 logger.debug(testName + ": status = " + statusCode);
422             }
423             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426
427             newID = PersonAuthorityClientUtils.extractId(res);
428         } finally {
429             res.releaseConnection();
430         }
431
432         // Store the ID returned from the first contact resource created
433         // for additional tests below.
434         if (knownContactResourceId == null) {
435             knownContactResourceId = newID;
436             if (logger.isDebugEnabled()) {
437                 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
438             }
439         }
440
441         // Store the IDs from any contact resources created
442         // by tests, along with the IDs of their parent items,
443         // so these items can be deleted after all tests have been run.
444         allContactResourceIdsCreated.put(newID, itemcsid);
445
446         return newID;
447     }
448
449     // Failure outcomes
450     // Placeholders until the three tests below can be uncommented.
451     // See Issue CSPACE-401.
452     /* (non-Javadoc)
453      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
454      */
455     @Override
456     public void createWithEmptyEntityBody(String testName) throws Exception {
457         //Should this really be empty?
458     }
459
460     /* (non-Javadoc)
461      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
462      */
463     @Override
464     public void createWithMalformedXml(String testName) throws Exception {
465         //Should this really be empty?
466     }
467
468     /* (non-Javadoc)
469      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
470      */
471     @Override
472     public void createWithWrongXmlSchema(String testName) throws Exception {
473         //Should this really be empty?
474     }
475
476     /*
477     @Override
478     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
479     groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
480     public void createWithEmptyEntityBody(String testName) throws Exception {
481     
482     if (logger.isDebugEnabled()) {
483     logger.debug(testBanner(testName, CLASS_NAME));
484     }
485     // Perform setup.
486     setupCreateWithEmptyEntityBody();
487     
488     // Submit the request to the service and store the response.
489     String method = REQUEST_TYPE.httpMethodName();
490     String url = getServiceRootURL();
491     String mediaType = MediaType.APPLICATION_XML;
492     final String entity = "";
493     int statusCode = submitRequest(method, url, mediaType, entity);
494     
495     // Check the status code of the response: does it match
496     // the expected response(s)?
497     if(logger.isDebugEnabled()) {
498     logger.debug(testName + ": url=" + url +
499     " status=" + statusCode);
500     }
501     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504     }
505     
506     @Override
507     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
508     groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
509     public void createWithMalformedXml(String testName) throws Exception {
510     
511     if (logger.isDebugEnabled()) {
512     logger.debug(testBanner(testName, CLASS_NAME));
513     }
514     // Perform setup.
515     setupCreateWithMalformedXml();
516     
517     // Submit the request to the service and store the response.
518     String method = REQUEST_TYPE.httpMethodName();
519     String url = getServiceRootURL();
520     String mediaType = MediaType.APPLICATION_XML;
521     final String entity = MALFORMED_XML_DATA; // Constant from base class.
522     int statusCode = submitRequest(method, url, mediaType, entity);
523     
524     // Check the status code of the response: does it match
525     // the expected response(s)?
526     if(logger.isDebugEnabled()){
527     logger.debug(testName + ": url=" + url +
528     " status=" + statusCode);
529     }
530     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
531     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
532     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
533     }
534     
535     @Override
536     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
537     groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
538     public void createWithWrongXmlSchema(String testName) throws Exception {
539     
540     if (logger.isDebugEnabled()) {
541     logger.debug(testBanner(testName, CLASS_NAME));
542     }
543     // Perform setup.
544     setupCreateWithWrongXmlSchema();
545     
546     // Submit the request to the service and store the response.
547     String method = REQUEST_TYPE.httpMethodName();
548     String url = getServiceRootURL();
549     String mediaType = MediaType.APPLICATION_XML;
550     final String entity = WRONG_XML_SCHEMA_DATA;
551     int statusCode = submitRequest(method, url, mediaType, entity);
552     
553     // Check the status code of the response: does it match
554     // the expected response(s)?
555     if(logger.isDebugEnabled()){
556     logger.debug(testName + ": url=" + url +
557     " status=" + statusCode);
558     }
559     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
560     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
561     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
562     }
563      */
564     /**
565      * Attempts to create an authority with an short identifier that contains
566      * non-word characters.
567      *
568      * @param testName the test name
569      */
570     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
571     groups = {"create", "nonWordCharsInShortId"})
572     public void createWithShortIdNonWordChars(String testName) throws Exception {
573         if (logger.isDebugEnabled()) {
574             logger.debug(testBanner(testName, CLASS_NAME));
575         }
576         EXPECTED_STATUS_CODE = STATUS_BAD_REQUEST;
577         REQUEST_TYPE = ServiceRequestType.CREATE;
578         testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
579
580         // Create the payload to be included in the body of the request
581         PersonAuthorityClient client = new PersonAuthorityClient();
582         String shortId = createIdentifier() + "*" + createIdentifier();
583         String displayName = "displayName-" + shortId;
584         PoxPayloadOut multipart =
585                 PersonAuthorityClientUtils.createPersonAuthorityInstance(
586                 displayName, shortId, client.getCommonPartName());
587
588         // Submit the request to the service and store the response.
589         ClientResponse<Response> res = client.create(multipart);
590
591         // Check the status code of the response: does it match
592         // the expected response(s)?
593         try {
594             int statusCode = res.getStatus();
595             if (logger.isDebugEnabled()) {
596                 logger.debug(testName + ": status = " + statusCode);
597             }
598             Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
599                     invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
600             Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
601         } finally {
602             res.releaseConnection();
603         }
604
605     }
606
607     /**
608      * Attempts to create an item with an short identifier that contains
609      * non-word characters.
610      *
611      * @param testName the test name
612      */
613     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
614     groups = {"create", "nonWordCharsInShortId"}, dependsOnMethods = {"create"})
615     public void createItemWithShortIdNonWordChars(String testName) {
616         if (logger.isDebugEnabled()) {
617             logger.debug(testBanner(testName, CLASS_NAME));
618         }
619         EXPECTED_STATUS_CODE = STATUS_BAD_REQUEST;
620         REQUEST_TYPE = ServiceRequestType.CREATE;
621         testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
622
623         PersonAuthorityClient client = new PersonAuthorityClient();
624
625         // Create the payload to be included in the body of the request
626         String shortId = "7-Eleven";
627         Map<String, String> fieldProperties = new HashMap<String, String>();
628         fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
629         fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME, shortId);
630         fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false");
631         fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, shortId);
632         fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
633         final Map NULL_REPEATABLE_FIELD_PROPERTIES = null;
634         PoxPayloadOut multipart =
635                 PersonAuthorityClientUtils.createPersonInstance(knownResourceId,
636                 null /*knownResourceRefName*/, fieldProperties,
637                 NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName());
638
639         // Send the request and receive a response
640         ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
641
642         // Check the status code of the response: does it match
643         // the expected response(s)?
644         try {
645             int statusCode = res.getStatus();
646             // Check the status code of the response: does it match
647             // the expected response(s)?
648             if (logger.isDebugEnabled()) {
649                 logger.debug(testName + ": status = " + statusCode);
650             }
651             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
652                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
653             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
654         } finally {
655             res.releaseConnection();
656
657         }
658     }
659
660     // ---------------------------------------------------------------
661     // CRUD tests : CREATE LIST tests
662     // ---------------------------------------------------------------
663     // Success outcomes
664     /* (non-Javadoc)
665      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
666      */
667     @Override
668     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
669     groups = {"createList"}, dependsOnGroups = {"create"})
670     public void createList(String testName) throws Exception {
671         if (logger.isDebugEnabled()) {
672             logger.debug(testBanner(testName, CLASS_NAME));
673         }
674         for (int i = 0; i < nItemsToCreateInList; i++) {
675             create(testName);
676         }
677     }
678
679     /**
680      * Creates the item list.
681      *
682      * @param testName the test name
683      * @throws Exception the exception
684      */
685     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
686     groups = {"createList"}, dependsOnMethods = {"createList"})
687     public void createItemList(String testName) throws Exception {
688         if (logger.isDebugEnabled()) {
689             logger.debug(testBanner(testName, CLASS_NAME));
690         }
691         // Add items to the initially-created, known parent record.
692         for (int j = 0; j < nItemsToCreateInList; j++) {
693             createItem(testName);
694         }
695     }
696
697     /**
698      * Creates the contact list.
699      *
700      * @param testName the test name
701      * @throws Exception the exception
702      */
703     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
704     groups = {"createList"}, dependsOnMethods = {"createItemList"})
705     public void createContactList(String testName) throws Exception {
706         // Add contacts to the initially-created, known item record.
707         for (int j = 0; j < nItemsToCreateInList; j++) {
708             createContact(testName);
709         }
710     }
711
712     // ---------------------------------------------------------------
713     // CRUD tests : READ tests
714     // ---------------------------------------------------------------
715     // Success outcomes
716     /* (non-Javadoc)
717      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
718      */
719     @Override
720     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
721     groups = {"read"}, dependsOnGroups = {"create"})
722     public void read(String testName) throws Exception {
723         readInternal(testName, knownResourceId, null);
724     }
725
726     /**
727      * Read by name.
728      *
729      * @param testName the test name
730      * @throws Exception the exception
731      */
732     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
733     groups = {"read"}, dependsOnMethods = {"read"})
734     public void readByName(String testName) throws Exception {
735         readInternal(testName, null, knownResourceShortIdentifer);
736     }
737
738     protected void readInternal(String testName, String CSID, String shortId) {
739         if (logger.isDebugEnabled()) {
740             logger.debug(testBanner(testName, CLASS_NAME));
741         }
742         // Perform setup.
743         setupRead();
744
745         // Submit the request to the service and store the response.
746         PersonAuthorityClient client = new PersonAuthorityClient();
747         ClientResponse<String> res = null;
748         if (CSID != null) {
749             res = client.read(CSID);
750         } else if (shortId != null) {
751             res = client.readByName(shortId);
752         } else {
753             Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
754         }
755         assertStatusCode(res, testName);
756         try {
757             //FIXME: remove the following try catch once Aron fixes signatures
758             try {
759                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
760                 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
761                         client.getCommonPartName(), PersonauthoritiesCommon.class);
762                 Assert.assertNotNull(personAuthority);
763             } catch (Exception e) {
764                 throw new RuntimeException(e);
765             }
766         } finally {
767             res.releaseConnection();
768         }
769     }
770
771     /**
772      * Read item.
773      *
774      * @param testName the test name
775      * @throws Exception the exception
776      */
777     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
778     groups = {"readItem"}, dependsOnGroups = {"read"})
779     public void readItem(String testName) throws Exception {
780         readItemInternal(testName, knownResourceId, null, knownItemResourceId, null);
781     }
782
783     /**
784      * Read item in Named Auth.
785      *
786      * @param testName the test name
787      * @throws Exception the exception
788      */
789     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
790     groups = {"readItem"}, dependsOnMethods = {"readItem"})
791     public void readItemInNamedAuth(String testName) throws Exception {
792         readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
793     }
794
795     /**
796      * Read named item.
797      *
798      * @param testName the test name
799      * @throws Exception the exception
800      */
801     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
802     groups = {"readItem"}, dependsOnMethods = {"readItem"})
803     public void readNamedItem(String testName) throws Exception {
804         readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
805     }
806
807     /**
808      * Read Named item in Named Auth.
809      *
810      * @param testName the test name
811      * @throws Exception the exception
812      */
813     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
814     groups = {"readItem"}, dependsOnMethods = {"readItem"})
815     public void readNamedItemInNamedAuth(String testName) throws Exception {
816         readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
817     }
818
819     protected void readItemInternal(String testName,
820             String authCSID, String authShortId, String itemCSID, String itemShortId)
821             throws Exception {
822
823         if (logger.isDebugEnabled()) {
824             logger.debug(testBanner(testName, CLASS_NAME));
825             logger.debug("Reading:" + ((authCSID != null) ? authCSID : authShortId) + "/"
826                     + ((itemCSID != null) ? authCSID : itemShortId));
827         }
828         // Perform setup.
829         setupRead();
830
831         // Submit the request to the service and store the response.
832         PersonAuthorityClient client = new PersonAuthorityClient();
833         ClientResponse<String> res = null;
834         if (authCSID != null) {
835             if (itemCSID != null) {
836                 res = client.readItem(authCSID, itemCSID);
837             } else if (itemShortId != null) {
838                 res = client.readNamedItem(authCSID, itemShortId);
839             } else {
840                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
841             }
842         } else if (authShortId != null) {
843             if (itemCSID != null) {
844                 res = client.readItemInNamedAuthority(authShortId, itemCSID);
845             } else if (itemShortId != null) {
846                 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
847             } else {
848                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
849             }
850         } else {
851             Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
852         }
853         assertStatusCode(res, testName);
854         try {
855             // Check whether we've received a person.
856             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
857             PersonsCommon person = (PersonsCommon) extractPart(input,
858                     client.getItemCommonPartName(), PersonsCommon.class);
859             Assert.assertNotNull(person);
860             boolean showFull = true;
861             if (showFull && logger.isDebugEnabled()) {
862                 logger.debug(testName + ": returned payload:");
863                 logger.debug(objectAsXmlString(person, PersonsCommon.class));
864             }
865
866             // Check that the person item is within the expected Person Authority.
867             Assert.assertEquals(person.getInAuthority(), knownResourceId);
868
869             // Verify the number and contents of values in a repeatable field,
870             // as created in the instance record used for testing.
871             List<String> groups = person.getGroups().getGroup();
872             Assert.assertTrue(groups.size() > 0);
873             Assert.assertNotNull(groups.get(0));
874
875         } finally {
876             res.releaseConnection();
877         }
878     }
879
880     /**
881      * Verify item display name.
882      *
883      * @param testName the test name
884      * @throws Exception the exception
885      */
886     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
887     groups = {"update"}, dependsOnMethods = {"updateItem"})
888     public void verifyItemDisplayNames(String testName) throws Exception {
889
890         if (logger.isDebugEnabled()) {
891             logger.debug(testBanner(testName, CLASS_NAME));
892         }
893         // Perform setup.
894         setupUpdate();
895
896         // Submit the request to the service and store the response.
897         PersonAuthorityClient client = new PersonAuthorityClient();
898         PoxPayloadIn input = null;
899         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
900         assertStatusCode(res, testName);
901         try {
902             input = new PoxPayloadIn(res.getEntity());
903         } finally {
904             res.releaseConnection();
905         }
906
907         PersonsCommon person = (PersonsCommon) extractPart(input,
908                 client.getItemCommonPartName(), PersonsCommon.class);
909         Assert.assertNotNull(person);
910         // Check whether person has expected displayName.
911         // Make sure displayName matches computed form
912         String displayName = person.getDisplayName();
913         String expectedDisplayName =
914                 PersonAuthorityClientUtils.prepareDefaultDisplayName(
915                 TEST_FORE_NAME, null, TEST_SUR_NAME,
916                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
917         Assert.assertFalse(displayName.equals(expectedDisplayName));
918
919         // Make sure short displayName matches computed form
920         String shortDisplayName = person.getShortDisplayName();
921         String expectedShortDisplayName =
922                 PersonAuthorityClientUtils.prepareDefaultDisplayName(
923                 TEST_FORE_NAME, null, TEST_SUR_NAME, null, null);
924         Assert.assertFalse(expectedShortDisplayName.equals(shortDisplayName));
925
926         // Update the forename and verify the computed name is updated.
927         person.setCsid(null);
928         person.setDisplayNameComputed(true);
929         person.setShortDisplayNameComputed(true);
930         person.setForeName("updated-" + TEST_FORE_NAME);
931         expectedDisplayName =
932                 PersonAuthorityClientUtils.prepareDefaultDisplayName(
933                 "updated-" + TEST_FORE_NAME, null, TEST_SUR_NAME,
934                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
935         expectedShortDisplayName =
936                 PersonAuthorityClientUtils.prepareDefaultDisplayName(
937                 "updated-" + TEST_FORE_NAME, null, TEST_SUR_NAME, null, null);
938
939         // Submit the updated resource to the service and store the response.
940         PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
941         PayloadOutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
942         commonPart.setLabel(client.getItemCommonPartName());
943         res = client.updateItem(knownResourceId, knownItemResourceId, output);
944         assertStatusCode(res, testName);
945         try {
946             // Retrieve the updated resource and verify that its contents exist.
947             input = new PoxPayloadIn(res.getEntity());
948         } finally {
949             res.releaseConnection();
950         }
951
952         PersonsCommon updatedPerson =
953                 (PersonsCommon) extractPart(input,
954                 client.getItemCommonPartName(), PersonsCommon.class);
955         Assert.assertNotNull(updatedPerson);
956
957         // Verify that the updated resource received the correct data.
958         Assert.assertEquals(updatedPerson.getForeName(), person.getForeName(),
959                 "Updated ForeName in Person did not match submitted data.");
960         // Verify that the updated resource computes the right displayName.
961         Assert.assertEquals(updatedPerson.getDisplayName(), expectedDisplayName,
962                 "Updated ForeName in Person not reflected in computed DisplayName.");
963         // Verify that the updated resource computes the right displayName.
964         Assert.assertEquals(updatedPerson.getShortDisplayName(), expectedShortDisplayName,
965                 "Updated ForeName in Person not reflected in computed ShortDisplayName.");
966
967         // Now Update the displayName, not computed and verify the computed name is overriden.
968         person.setDisplayNameComputed(false);
969         expectedDisplayName = "TestName";
970         person.setDisplayName(expectedDisplayName);
971         person.setShortDisplayNameComputed(false);
972         person.setShortDisplayName(expectedDisplayName);
973
974         // Submit the updated resource to the service and store the response.
975         output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
976         commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
977         commonPart.setLabel(client.getItemCommonPartName());
978         res = client.updateItem(knownResourceId, knownItemResourceId, output);
979         assertStatusCode(res, testName);
980         try {
981             // Retrieve the updated resource and verify that its contents exist.
982             input = new PoxPayloadIn(res.getEntity());
983         } finally {
984             res.releaseConnection();
985         }
986
987         updatedPerson =
988                 (PersonsCommon) extractPart(input,
989                 client.getItemCommonPartName(), PersonsCommon.class);
990         Assert.assertNotNull(updatedPerson);
991
992         // Verify that the updated resource received the correct data.
993         Assert.assertEquals(updatedPerson.isDisplayNameComputed(), false,
994                 "Updated displayNameComputed in Person did not match submitted data.");
995         // Verify that the updated resource computes the right displayName.
996         Assert.assertEquals(updatedPerson.getDisplayName(),
997                 expectedDisplayName,
998                 "Updated DisplayName (not computed) in Person not stored.");
999         // Verify that the updated resource received the correct data.
1000         Assert.assertEquals(updatedPerson.isShortDisplayNameComputed(), false,
1001                 "Updated shortDisplayNameComputed in Person did not match submitted data.");
1002         // Verify that the updated resource computes the right displayName.
1003         Assert.assertEquals(updatedPerson.getShortDisplayName(),
1004                 expectedDisplayName,
1005                 "Updated ShortDisplayName (not computed) in Person not stored.");
1006     }
1007
1008     /**
1009      * Verify illegal item display name.
1010      *
1011      * @param testName the test name
1012      * @throws Exception the exception
1013      */
1014     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1015     groups = {"update"}, dependsOnMethods = {"verifyItemDisplayNames"})
1016     public void verifyIllegalItemDisplayName(String testName) throws Exception {
1017
1018         if (logger.isDebugEnabled()) {
1019             logger.debug(testBanner(testName, CLASS_NAME));
1020         }
1021
1022         // Perform setup for read.
1023         setupRead();
1024
1025         // Submit the request to the service and store the response.
1026         PersonAuthorityClient client = new PersonAuthorityClient();
1027         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
1028         assertStatusCode(res, testName);
1029         
1030         // Perform setup for update.
1031         testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
1032
1033         PoxPayloadIn input = null;
1034         try {
1035             input = new PoxPayloadIn(res.getEntity());
1036         } finally {
1037             res.releaseConnection();
1038         }
1039
1040         PersonsCommon person = (PersonsCommon) extractPart(input,
1041                 client.getItemCommonPartName(), PersonsCommon.class);
1042         Assert.assertNotNull(person);
1043         // Try to Update with computed false and no displayName
1044         person.setDisplayNameComputed(false);
1045         person.setDisplayName(null);
1046
1047         // Submit the updated resource to the service and store the response.
1048         PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1049         PayloadOutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
1050         commonPart.setLabel(client.getItemCommonPartName());
1051         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1052         assertStatusCode(res, testName);
1053         try {
1054             // intentionally empty try block
1055         } finally {
1056             res.releaseConnection();
1057         }
1058     }
1059
1060     /**
1061      * Read contact.
1062      *
1063      * @param testName the test name
1064      * @throws Exception the exception
1065      */
1066     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1067     groups = {"readItem"}, dependsOnMethods = {"readItem"})
1068     public void readContact(String testName) throws Exception {
1069
1070         if (logger.isDebugEnabled()) {
1071             logger.debug(testBanner(testName, CLASS_NAME));
1072         }
1073         // Perform setup.
1074         setupRead();
1075
1076         // Submit the request to the service and store the response.
1077         PersonAuthorityClient client = new PersonAuthorityClient();
1078         PoxPayloadIn input = null;
1079         ClientResponse<String> res =
1080                 client.readContact(knownResourceId, knownItemResourceId,
1081                 knownContactResourceId);
1082         assertStatusCode(res, testName);
1083         try {
1084             // Check whether we've received a contact.
1085             input = new PoxPayloadIn(res.getEntity());
1086         } finally {
1087             res.releaseConnection();
1088         }
1089
1090         ContactsCommon contact = (ContactsCommon) extractPart(input,
1091                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1092         Assert.assertNotNull(contact);
1093         boolean showFull = true;
1094         if (showFull && logger.isDebugEnabled()) {
1095             logger.debug(testName + ": returned payload:");
1096             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
1097         }
1098         Assert.assertEquals(contact.getInAuthority(), knownResourceId);
1099         Assert.assertEquals(contact.getInItem(), knownItemResourceId);
1100
1101     }
1102
1103     // Failure outcomes
1104     /* (non-Javadoc)
1105      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
1106      */
1107     @Override
1108     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1109     groups = {"read"}, dependsOnMethods = {"read"})
1110     public void readNonExistent(String testName) {
1111
1112         if (logger.isDebugEnabled()) {
1113             logger.debug(testBanner(testName, CLASS_NAME));
1114         }
1115         // Perform setup.
1116         setupReadNonExistent();
1117
1118         // Submit the request to the service and store the response.
1119         PersonAuthorityClient client = new PersonAuthorityClient();
1120         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
1121         try {
1122             int statusCode = res.getStatus();
1123             // Check the status code of the response: does it match
1124             // the expected response(s)?
1125             if (logger.isDebugEnabled()) {
1126                 logger.debug(testName + ": status = " + statusCode);
1127             }
1128             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1129                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1130             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1131         } finally {
1132             res.releaseConnection();
1133         }
1134     }
1135
1136     /**
1137      * Read item non existent.
1138      *
1139      * @param testName the test name
1140      */
1141     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1142     groups = {"readItem"}, dependsOnMethods = {"readItem"})
1143     public void readItemNonExistent(String testName) {
1144
1145         if (logger.isDebugEnabled()) {
1146             logger.debug(testBanner(testName, CLASS_NAME));
1147         }
1148         // Perform setup.
1149         setupReadNonExistent();
1150
1151         // Submit the request to the service and store the response.
1152         PersonAuthorityClient client = new PersonAuthorityClient();
1153         ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
1154         try {
1155             int statusCode = res.getStatus();
1156
1157             // Check the status code of the response: does it match
1158             // the expected response(s)?
1159             if (logger.isDebugEnabled()) {
1160                 logger.debug(testName + ": status = " + statusCode);
1161             }
1162             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1163                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1164             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1165         } finally {
1166             res.releaseConnection();
1167         }
1168     }
1169
1170     /**
1171      * Read contact non existent.
1172      *
1173      * @param testName the test name
1174      */
1175     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1176     groups = {"readItem"}, dependsOnMethods = {"readContact"})
1177     public void readContactNonExistent(String testName) {
1178
1179         if (logger.isDebugEnabled()) {
1180             logger.debug(testBanner(testName, CLASS_NAME));
1181         }
1182         // Perform setup.
1183         setupReadNonExistent();
1184
1185         // Submit the request to the service and store the response.
1186         PersonAuthorityClient client = new PersonAuthorityClient();
1187         ClientResponse<String> res =
1188                 client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1189         try {
1190             int statusCode = res.getStatus();
1191
1192             // Check the status code of the response: does it match
1193             // the expected response(s)?
1194             if (logger.isDebugEnabled()) {
1195                 logger.debug(testName + ": status = " + statusCode);
1196             }
1197             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1198                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1199             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1200         } finally {
1201             res.releaseConnection();
1202         }
1203     }
1204
1205     // ---------------------------------------------------------------
1206     // CRUD tests : READ_LIST tests
1207     // ---------------------------------------------------------------
1208     // Success outcomes
1209
1210     /* (non-Javadoc)
1211      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
1212      */
1213     @Override
1214     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1215     groups = {"readList"}, dependsOnGroups = {"createList", "read"})
1216     public void readList(String testName) throws Exception {
1217
1218         if (logger.isDebugEnabled()) {
1219             logger.debug(testBanner(testName, CLASS_NAME));
1220         }
1221         // Perform setup.
1222         setupReadList();
1223
1224         // Submit the request to the service and store the response.
1225         PersonAuthorityClient client = new PersonAuthorityClient();
1226         AbstractCommonList list = null;
1227         ClientResponse<AbstractCommonList> res = client.readList();
1228         assertStatusCode(res, testName);
1229         try {
1230             list = res.getEntity();
1231         } finally {
1232             res.releaseConnection();
1233         }
1234
1235         // Optionally output additional data about list members for debugging.
1236         if (logger.isTraceEnabled()) {
1237             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1238         }
1239     }
1240
1241     /**
1242      * Read item list.
1243      */
1244     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1245     groups = {"readList"}, dependsOnMethods = {"readList"})
1246     public void readItemList(String testName) {
1247         readItemList(knownResourceId, null, testName);
1248     }
1249
1250     /**
1251      * Read item list by authority name.
1252      */
1253     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1254     groups = {"readList"}, dependsOnMethods = {"readItemList"})
1255     public void readItemListByAuthorityName(String testName) {
1256         readItemList(null, knownResourceShortIdentifer, testName);
1257     }
1258
1259     /**
1260      * Read item list.
1261      *
1262      * @param vcsid the vcsid
1263      * @param name the name
1264      */
1265     private void readItemList(String vcsid, String name, String testName) {
1266
1267         // Perform setup.
1268         setupReadList();
1269
1270         // Submit the request to the service and store the response.
1271         PersonAuthorityClient client = new PersonAuthorityClient();
1272         ClientResponse<AbstractCommonList> res = null;
1273         if (vcsid != null) {
1274             res = client.readItemList(vcsid, null, null);
1275         } else if (name != null) {
1276             res = client.readItemListForNamedAuthority(name, null, null);
1277         } else {
1278             Assert.fail("readItemList passed null csid and name!");
1279         }
1280         assertStatusCode(res, testName);
1281         AbstractCommonList list = null;
1282         try {
1283             list = res.getEntity();
1284         } finally {
1285             res.releaseConnection();
1286         }
1287
1288         List<AbstractCommonList.ListItem> items =
1289                 list.getListItem();
1290         int nItemsReturned = items.size();
1291         // There will be one item created, associated with a
1292         // known parent resource, by the createItem test.
1293         //
1294         // In addition, there will be 'nItemsToCreateInList'
1295         // additional items created by the createItemList test,
1296         // all associated with the same parent resource.
1297         int nExpectedItems = nItemsToCreateInList + 1;
1298         if (logger.isDebugEnabled()) {
1299             logger.debug(testName + ": Expected "
1300                     + nExpectedItems + " items; got: " + nItemsReturned);
1301         }
1302         Assert.assertEquals(nItemsReturned, nExpectedItems);
1303
1304         for (AbstractCommonList.ListItem item : items) {
1305             String value =
1306                     AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
1307             Assert.assertTrue((null != value), "Item refName is null!");
1308             value =
1309                     AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
1310             Assert.assertTrue((null != value), "Item displayName is null!");
1311         }
1312         if (logger.isTraceEnabled()) {
1313             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1314         }
1315     }
1316
1317     /**
1318      * Read contact list.
1319      */
1320     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
1321     public void readContactList() {
1322         readContactList(knownResourceId, knownItemResourceId);
1323     }
1324
1325     /**
1326      * Read contact list.
1327      *
1328      * @param parentcsid the parentcsid
1329      * @param itemcsid the itemcsid
1330      */
1331     private void readContactList(String parentcsid, String itemcsid) {
1332         final String testName = "readContactList";
1333
1334         // Perform setup.
1335         setupReadList();
1336
1337         // Submit the request to the service and store the response.
1338         PersonAuthorityClient client = new PersonAuthorityClient();
1339         AbstractCommonList list = null;
1340         ClientResponse<AbstractCommonList> res =
1341                 client.readContactList(parentcsid, itemcsid);
1342         assertStatusCode(res, testName);
1343         try {
1344             list = res.getEntity();
1345         } finally {
1346             res.releaseConnection();
1347         }
1348
1349         List<AbstractCommonList.ListItem> listitems =
1350                 list.getListItem();
1351         int nItemsReturned = listitems.size();
1352         // There will be one item created, associated with a
1353         // known parent resource, by the createItem test.
1354         //
1355         // In addition, there will be 'nItemsToCreateInList'
1356         // additional items created by the createItemList test,
1357         // all associated with the same parent resource.
1358         int nExpectedItems = nItemsToCreateInList + 1;
1359         if (logger.isDebugEnabled()) {
1360             logger.debug(testName + ": Expected "
1361                     + nExpectedItems + " items; got: " + nItemsReturned);
1362         }
1363         Assert.assertEquals(nItemsReturned, nExpectedItems);
1364         
1365         // Optionally output additional data about list members for debugging.
1366         boolean iterateThroughList = false;
1367         if (iterateThroughList && logger.isDebugEnabled()) {
1368             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1369         }
1370     }
1371
1372     // Failure outcomes
1373     // There are no failure outcome tests at present.
1374     // ---------------------------------------------------------------
1375     // CRUD tests : UPDATE tests
1376     // ---------------------------------------------------------------
1377     // Success outcomes
1378     /* (non-Javadoc)
1379      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
1380      */
1381     @Override
1382     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1383     groups = {"update"}, dependsOnGroups = {"readItem", "readList"})
1384     public void update(String testName) throws Exception {
1385
1386         if (logger.isDebugEnabled()) {
1387             logger.debug(testBanner(testName, CLASS_NAME));
1388         }
1389         // Perform setup.
1390         setupUpdate();
1391
1392         // Retrieve the contents of a resource to update.
1393         PersonAuthorityClient client = new PersonAuthorityClient();
1394         PoxPayloadIn input = null;
1395         ClientResponse<String> res = client.read(knownResourceId);
1396         assertStatusCode(res, testName);
1397         try {
1398             input = new PoxPayloadIn(res.getEntity());
1399         } finally {
1400             res.releaseConnection();
1401         }
1402
1403         PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
1404                 client.getCommonPartName(), PersonauthoritiesCommon.class);
1405         Assert.assertNotNull(personAuthority);
1406
1407         // Update the contents of this resource.
1408         personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
1409         personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
1410         if (logger.isDebugEnabled()) {
1411             logger.debug("to be updated PersonAuthority");
1412             logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
1413         }
1414
1415         // Submit the updated resource to the service and store the response.
1416         PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_PAYLOAD_NAME);
1417         PayloadOutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
1418         commonPart.setLabel(client.getCommonPartName());
1419         res = client.update(knownResourceId, output);
1420         assertStatusCode(res, testName);
1421         try {
1422             // Retrieve the updated resource and verify that its contents exist.
1423             input = new PoxPayloadIn(res.getEntity());
1424         } finally {
1425             res.releaseConnection();
1426         }
1427
1428         PersonauthoritiesCommon updatedPersonAuthority =
1429                 (PersonauthoritiesCommon) extractPart(input,
1430                 client.getCommonPartName(), PersonauthoritiesCommon.class);
1431         Assert.assertNotNull(updatedPersonAuthority);
1432
1433         // Verify that the updated resource received the correct data.
1434         Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
1435                 personAuthority.getDisplayName(),
1436                 "Data in updated object did not match submitted data.");
1437     }
1438
1439     /**
1440      * Update item.
1441      *
1442      * @param testName the test name
1443      * @throws Exception the exception
1444      */
1445     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1446     groups = {"update"}, dependsOnMethods = {"update"})
1447     public void updateItem(String testName) throws Exception {
1448
1449         if (logger.isDebugEnabled()) {
1450             logger.debug(testBanner(testName, CLASS_NAME));
1451         }
1452         // Perform setup.
1453         setupUpdate();
1454
1455         // Retrieve the contents of a resource to update.
1456         PersonAuthorityClient client = new PersonAuthorityClient();
1457         PoxPayloadIn input = null;
1458         ClientResponse<String> res =
1459                 client.readItem(knownResourceId, knownItemResourceId);
1460         assertStatusCode(res, testName);
1461         try {
1462             input = new PoxPayloadIn(res.getEntity());
1463         } finally {
1464             res.releaseConnection();
1465         }
1466
1467         PersonsCommon person = (PersonsCommon) extractPart(input,
1468                 client.getItemCommonPartName(), PersonsCommon.class);
1469         Assert.assertNotNull(person);
1470
1471         if (logger.isDebugEnabled() == true) {
1472             logger.debug("About to update the following person...");
1473             logger.debug(objectAsXmlString(person, PersonsCommon.class));
1474         }
1475
1476         // Update the contents of this resource.
1477         person.setCsid(null);
1478         person.setForeName("updated-" + person.getForeName());
1479         if (logger.isDebugEnabled()) {
1480             logger.debug("to be updated Person");
1481             logger.debug(objectAsXmlString(person,
1482                     PersonsCommon.class));
1483         }
1484
1485         // Submit the updated resource to the service and store the response.
1486         PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1487         PayloadOutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
1488         commonPart.setLabel(client.getItemCommonPartName());
1489         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1490         assertStatusCode(res, testName);
1491         try {
1492             // Retrieve the updated resource and verify that its contents exist.
1493             input = new PoxPayloadIn(res.getEntity());
1494         } finally {
1495             res.releaseConnection();
1496         }
1497
1498         PersonsCommon updatedPerson =
1499                 (PersonsCommon) extractPart(input,
1500                 client.getItemCommonPartName(), PersonsCommon.class);
1501         Assert.assertNotNull(updatedPerson);
1502
1503         if (logger.isDebugEnabled() == true) {
1504             logger.debug("Updated to following person to:");
1505             logger.debug(objectAsXmlString(updatedPerson, PersonsCommon.class));
1506         }
1507
1508         // Verify that the updated resource received the correct data.
1509         Assert.assertEquals(updatedPerson.getForeName(),
1510                 person.getForeName(),
1511                 "Data in updated Person did not match submitted data.");
1512     }
1513
1514     /**
1515      * Update contact.
1516      *
1517      * @param testName the test name
1518      * @throws Exception the exception
1519      */
1520     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1521     groups = {"update"}, dependsOnMethods = {"updateItem"})
1522     public void updateContact(String testName) throws Exception {
1523
1524         if (logger.isDebugEnabled()) {
1525             logger.debug(testBanner(testName, CLASS_NAME));
1526         }
1527         // Perform setup.
1528         setupUpdate();
1529
1530         // Retrieve the contents of a resource to update.
1531         PersonAuthorityClient client = new PersonAuthorityClient();
1532         PoxPayloadIn input = null;
1533         ClientResponse<String> res =
1534                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1535         assertStatusCode(res, testName);
1536         try {
1537             input = new PoxPayloadIn(res.getEntity());
1538         } finally {
1539             res.releaseConnection();
1540         }
1541
1542         ContactsCommon contact = (ContactsCommon) extractPart(input,
1543                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1544         Assert.assertNotNull(contact);
1545
1546         // Verify the contents of this resource
1547         AddressGroupList addressGroupList = contact.getAddressGroupList();
1548         Assert.assertNotNull(addressGroupList);
1549         List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1550         Assert.assertNotNull(addressGroups);
1551         Assert.assertTrue(addressGroups.size() > 0);
1552         String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1553         Assert.assertNotNull(addressPlace1);
1554
1555         // Update the contents of this resource.
1556         addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1557         contact.setAddressGroupList(addressGroupList);
1558         if (logger.isDebugEnabled()) {
1559             logger.debug("to be updated Contact");
1560             logger.debug(objectAsXmlString(contact,
1561                     ContactsCommon.class));
1562         }
1563
1564         // Submit the updated resource to the service and store the response.
1565         PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1566         PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE); //FIXME: REM - Replace with output.addPart(contact, client.getCommonPartName())
1567         commonPart.setLabel(client.getCommonPartName());
1568         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1569         assertStatusCode(res, testName);
1570         try {
1571             // Retrieve the updated resource and verify that its contents exist.
1572             input = new PoxPayloadIn(res.getEntity());;
1573         } finally {
1574             res.releaseConnection();
1575         }
1576         ContactsCommon updatedContact =
1577                 (ContactsCommon) extractPart(input,
1578                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1579         Assert.assertNotNull(updatedContact);
1580
1581         // Verify that the updated resource received the correct data.
1582         Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1583                 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1584                 "Data in updated object did not match submitted data.");
1585     }
1586
1587     // Failure outcomes
1588     // Placeholders until the three tests below can be uncommented.
1589     // See Issue CSPACE-401.
1590     /* (non-Javadoc)
1591      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
1592      */
1593     @Override
1594     public void updateWithEmptyEntityBody(String testName) throws Exception {
1595         //Should this really be empty?
1596     }
1597
1598     /* (non-Javadoc)
1599      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
1600      */
1601     @Override
1602     public void updateWithMalformedXml(String testName) throws Exception {
1603         //Should this really be empty?
1604     }
1605
1606     /* (non-Javadoc)
1607      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
1608      */
1609     @Override
1610     public void updateWithWrongXmlSchema(String testName) throws Exception {
1611         //Should this really be empty?
1612     }
1613
1614     /*  //FIXME: REM - Can we kill all this dead code please?
1615     @Override
1616     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1617     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1618     public void updateWithEmptyEntityBody(String testName) throws Exception {
1619     
1620     if (logger.isDebugEnabled()) {
1621     logger.debug(testBanner(testName, CLASS_NAME));
1622     }
1623     // Perform setup.
1624     setupUpdateWithEmptyEntityBody(testName, logger);
1625     
1626     // Submit the request to the service and store the response.
1627     String method = REQUEST_TYPE.httpMethodName();
1628     String url = getResourceURL(knownResourceId);
1629     String mediaType = MediaType.APPLICATION_XML;
1630     final String entity = "";
1631     int statusCode = submitRequest(method, url, mediaType, entity);
1632     
1633     // Check the status code of the response: does it match
1634     // the expected response(s)?
1635     if(logger.isDebugEnabled()){
1636     logger.debug(testName + ": url=" + url +
1637     " status=" + statusCode);
1638     }
1639     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1640     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1641     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1642     }
1643     
1644     @Override
1645     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1646     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1647     public void updateWithMalformedXml(String testName) throws Exception {
1648     
1649     if (logger.isDebugEnabled()) {
1650     logger.debug(testBanner(testName, CLASS_NAME));
1651     }
1652     // Perform setup.
1653     setupUpdateWithMalformedXml();
1654     
1655     // Submit the request to the service and store the response.
1656     String method = REQUEST_TYPE.httpMethodName();
1657     String url = getResourceURL(knownResourceId);
1658     String mediaType = MediaType.APPLICATION_XML;
1659     final String entity = MALFORMED_XML_DATA;
1660     int statusCode = submitRequest(method, url, mediaType, entity);
1661     
1662     // Check the status code of the response: does it match
1663     // the expected response(s)?
1664     if(logger.isDebugEnabled()){
1665     logger.debug(testName + ": url=" + url +
1666     " status=" + statusCode);
1667     }
1668     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1669     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1670     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1671     }
1672     
1673     @Override
1674     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1675     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1676     public void updateWithWrongXmlSchema(String testName) throws Exception {
1677     
1678     if (logger.isDebugEnabled()) {
1679     logger.debug(testBanner(testName, CLASS_NAME));
1680     }
1681     // Perform setup.
1682     setupUpdateWithWrongXmlSchema();
1683     
1684     // Submit the request to the service and store the response.
1685     String method = REQUEST_TYPE.httpMethodName();
1686     String url = getResourceURL(knownResourceId);
1687     String mediaType = MediaType.APPLICATION_XML;
1688     final String entity = WRONG_XML_SCHEMA_DATA;
1689     int statusCode = submitRequest(method, url, mediaType, entity);
1690     
1691     // Check the status code of the response: does it match
1692     // the expected response(s)?
1693     if(logger.isDebugEnabled()){
1694     logger.debug("updateWithWrongXmlSchema: url=" + url +
1695     " status=" + statusCode);
1696     }
1697     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1698     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1699     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1700     }
1701      */
1702
1703     /* (non-Javadoc)
1704      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
1705      */
1706     @Override
1707     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1708     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1709     public void updateNonExistent(String testName) throws Exception {
1710
1711         if (logger.isDebugEnabled()) {
1712             logger.debug(testBanner(testName, CLASS_NAME));
1713         }
1714         // Perform setup.
1715         setupUpdateNonExistent();
1716
1717         // Submit the request to the service and store the response.
1718         // Note: The ID(s) used when creating the request payload may be arbitrary.
1719         // The only relevant ID may be the one used in update(), below.
1720         PersonAuthorityClient client = new PersonAuthorityClient();
1721         String displayName = "displayName-NON_EXISTENT_ID";
1722         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1723                 displayName, "NON_EXISTENT_SHORT_ID", client.getCommonPartName());
1724         ClientResponse<String> res =
1725                 client.update(NON_EXISTENT_ID, multipart);
1726         try {
1727             int statusCode = res.getStatus();
1728
1729             // Check the status code of the response: does it match
1730             // the expected response(s)?
1731             if (logger.isDebugEnabled()) {
1732                 logger.debug(testName + ": status = " + statusCode);
1733             }
1734             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1735                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1736             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1737         } finally {
1738             res.releaseConnection();
1739         }
1740     }
1741
1742     /**
1743      * Update non existent item.
1744      *
1745      * @param testName the test name
1746      * @throws Exception the exception
1747      */
1748     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1749     groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1750     public void updateNonExistentItem(String testName) throws Exception {
1751
1752         if (logger.isDebugEnabled()) {
1753             logger.debug(testBanner(testName, CLASS_NAME));
1754         }
1755         // Perform setup.
1756         setupUpdateNonExistent();
1757
1758         // Submit the request to the service and store the response.
1759         // Note: The ID used in this 'create' call may be arbitrary.
1760         // The only relevant ID may be the one used in update(), below.
1761         PersonAuthorityClient client = new PersonAuthorityClient();
1762         Map<String, String> nonexMap = new HashMap<String, String>();
1763         nonexMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, "nonEX");
1764         nonexMap.put(PersonJAXBSchema.FORE_NAME, "John");
1765         nonexMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
1766         nonexMap.put(PersonJAXBSchema.GENDER, "male");
1767         Map<String, List<String>> nonexRepeatablesMap = new HashMap<String, List<String>>();
1768         PoxPayloadOut multipart =
1769                 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
1770                 null, //PersonAuthorityClientUtils.createPersonAuthRefName(NON_EXISTENT_ID, null),
1771                 nonexMap, nonexRepeatablesMap, client.getItemCommonPartName());
1772         ClientResponse<String> res =
1773                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1774         try {
1775             int statusCode = res.getStatus();
1776
1777             // Check the status code of the response: does it match
1778             // the expected response(s)?
1779             if (logger.isDebugEnabled()) {
1780                 logger.debug(testName + ": status = " + statusCode);
1781             }
1782             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1783                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1784             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1785         } finally {
1786             res.releaseConnection();
1787         }
1788     }
1789
1790     /**
1791      * Update non existent contact.
1792      *
1793      * @param testName the test name
1794      * @throws Exception the exception
1795      */
1796     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1797     groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1798     public void updateNonExistentContact(String testName) throws Exception {
1799         // Currently a no-op test
1800     }
1801
1802     // ---------------------------------------------------------------
1803     // CRUD tests : DELETE tests
1804     // ---------------------------------------------------------------
1805     // Success outcomes
1806     // Note: delete sub-resources in ascending hierarchical order,
1807     // before deleting their parents.
1808     /**
1809      * Delete contact.
1810      *
1811      * @param testName the test name
1812      * @throws Exception the exception
1813      */
1814     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1815     groups = {"delete"}, dependsOnGroups = {"update"})
1816     public void deleteContact(String testName) throws Exception {
1817
1818         if (logger.isDebugEnabled()) {
1819             logger.debug(testBanner(testName, CLASS_NAME));
1820         }
1821         // Perform setup.
1822         setupDelete();
1823
1824         if (logger.isDebugEnabled()) {
1825             logger.debug("parentcsid =" + knownResourceId
1826                     + " itemcsid = " + knownItemResourceId
1827                     + " csid = " + knownContactResourceId);
1828         }
1829
1830         // Submit the request to the service and store the response.
1831         PersonAuthorityClient client = new PersonAuthorityClient();
1832         ClientResponse<Response> res =
1833                 client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1834         try {
1835             int statusCode = res.getStatus();
1836
1837             // Check the status code of the response: does it match
1838             // the expected response(s)?
1839             if (logger.isDebugEnabled()) {
1840                 logger.debug(testName + ": status = " + statusCode);
1841             }
1842             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1843                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1844             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1845         } finally {
1846             res.releaseConnection();
1847         }
1848     }
1849
1850     /**
1851      * Delete item.
1852      *
1853      * @param testName the test name
1854      * @throws Exception the exception
1855      */
1856     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1857     groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1858     public void deleteItem(String testName) throws Exception {
1859
1860         if (logger.isDebugEnabled()) {
1861             logger.debug(testBanner(testName, CLASS_NAME));
1862         }
1863         // Perform setup.
1864         setupDelete();
1865
1866         if (logger.isDebugEnabled()) {
1867             logger.debug("parentcsid =" + knownResourceId
1868                     + " itemcsid = " + knownItemResourceId);
1869         }
1870
1871         // Submit the request to the service and store the response.
1872         PersonAuthorityClient client = new PersonAuthorityClient();
1873         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1874         try {
1875             int statusCode = res.getStatus();
1876
1877             // Check the status code of the response: does it match
1878             // the expected response(s)?
1879             if (logger.isDebugEnabled()) {
1880                 logger.debug(testName + ": status = " + statusCode);
1881             }
1882             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1883                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1884             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1885         } finally {
1886             res.releaseConnection();
1887         }
1888     }
1889
1890     /* (non-Javadoc)
1891      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1892      */
1893     @Override
1894     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1895     groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1896     public void delete(String testName) throws Exception {
1897
1898         if (logger.isDebugEnabled()) {
1899             logger.debug(testBanner(testName, CLASS_NAME));
1900         }
1901         // Perform setup.
1902         setupDelete();
1903
1904         if (logger.isDebugEnabled()) {
1905             logger.debug("parentcsid =" + knownResourceId);
1906         }
1907
1908         // Submit the request to the service and store the response.
1909         PersonAuthorityClient client = new PersonAuthorityClient();
1910         ClientResponse<Response> res = client.delete(knownResourceId);
1911         try {
1912             int statusCode = res.getStatus();
1913
1914             // Check the status code of the response: does it match
1915             // the expected response(s)?
1916             if (logger.isDebugEnabled()) {
1917                 logger.debug(testName + ": status = " + statusCode);
1918             }
1919             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1920                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1921             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1922         } finally {
1923             res.releaseConnection();
1924         }
1925     }
1926
1927     // Failure outcomes
1928     /* (non-Javadoc)
1929      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1930      */
1931     @Override
1932     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1933     groups = {"delete"}, dependsOnMethods = {"delete"})
1934     public void deleteNonExistent(String testName) throws Exception {
1935
1936         if (logger.isDebugEnabled()) {
1937             logger.debug(testBanner(testName, CLASS_NAME));
1938         }
1939         // Perform setup.
1940         setupDeleteNonExistent();
1941
1942         // Submit the request to the service and store the response.
1943         PersonAuthorityClient client = new PersonAuthorityClient();
1944         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1945         try {
1946             int statusCode = res.getStatus();
1947
1948             // Check the status code of the response: does it match
1949             // the expected response(s)?
1950             if (logger.isDebugEnabled()) {
1951                 logger.debug(testName + ": status = " + statusCode);
1952             }
1953             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1954                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1955             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1956         } finally {
1957             res.releaseConnection();
1958         }
1959     }
1960
1961     /**
1962      * Delete non existent item.
1963      *
1964      * @param testName the test name
1965      */
1966     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1967     groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1968     public void deleteNonExistentItem(String testName) {
1969
1970         if (logger.isDebugEnabled()) {
1971             logger.debug(testBanner(testName, CLASS_NAME));
1972         }
1973         // Perform setup.
1974         setupDeleteNonExistent();
1975
1976         // Submit the request to the service and store the response.
1977         PersonAuthorityClient client = new PersonAuthorityClient();
1978         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1979         try {
1980             int statusCode = res.getStatus();
1981
1982             // Check the status code of the response: does it match
1983             // the expected response(s)?
1984             if (logger.isDebugEnabled()) {
1985                 logger.debug(testName + ": status = " + statusCode);
1986             }
1987             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1988                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1989             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1990         } finally {
1991             res.releaseConnection();
1992         }
1993     }
1994
1995     /**
1996      * Delete non existent contact.
1997      *
1998      * @param testName the test name
1999      */
2000     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
2001     groups = {"delete"}, dependsOnMethods = {"deleteContact"})
2002     public void deleteNonExistentContact(String testName) {
2003
2004         if (logger.isDebugEnabled()) {
2005             logger.debug(testBanner(testName, CLASS_NAME));
2006         }
2007         // Perform setup.
2008         setupDeleteNonExistent();
2009
2010         // Submit the request to the service and store the response.
2011         PersonAuthorityClient client = new PersonAuthorityClient();
2012         ClientResponse<Response> res =
2013                 client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
2014         try {
2015             int statusCode = res.getStatus();
2016
2017             // Check the status code of the response: does it match
2018             // the expected response(s)?
2019             if (logger.isDebugEnabled()) {
2020                 logger.debug(testName + ": status = " + statusCode);
2021             }
2022             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
2023                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
2024             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
2025         } finally {
2026             res.releaseConnection();
2027         }
2028     }
2029
2030     // ---------------------------------------------------------------
2031     // Utility tests : tests of code used in tests above
2032     // ---------------------------------------------------------------
2033     /**
2034      * Tests the code for manually submitting data that is used by several
2035      * of the methods above.
2036      */
2037     @Test(dependsOnMethods = {"create", "read"})
2038     public void testSubmitRequest() {
2039
2040         // Expected status code: 200 OK
2041         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2042
2043         // Submit the request to the service and store the response.
2044         String method = ServiceRequestType.READ.httpMethodName();
2045         String url = getResourceURL(knownResourceId);
2046         int statusCode = submitRequest(method, url);
2047
2048         // Check the status code of the response: does it match
2049         // the expected response(s)?
2050         if (logger.isDebugEnabled()) {
2051             logger.debug("testSubmitRequest: url=" + url
2052                     + " status=" + statusCode);
2053         }
2054         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2055
2056     }
2057
2058     /**
2059      * Test item submit request.
2060      */
2061     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
2062     public void testItemSubmitRequest() {
2063
2064         // Expected status code: 200 OK
2065         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2066
2067         // Submit the request to the service and store the response.
2068         String method = ServiceRequestType.READ.httpMethodName();
2069         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
2070         int statusCode = submitRequest(method, url);
2071
2072         // Check the status code of the response: does it match
2073         // the expected response(s)?
2074         if (logger.isDebugEnabled()) {
2075             logger.debug("testItemSubmitRequest: url=" + url
2076                     + " status=" + statusCode);
2077         }
2078         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2079
2080     }
2081
2082     /**
2083      * Test contact submit request.
2084      */
2085     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
2086     public void testContactSubmitRequest() {
2087
2088         // Expected status code: 200 OK
2089         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2090
2091         // Submit the request to the service and store the response.
2092         String method = ServiceRequestType.READ.httpMethodName();
2093         String url = getContactResourceURL(knownResourceId,
2094                 knownItemResourceId, knownContactResourceId);
2095         int statusCode = submitRequest(method, url);
2096
2097         // Check the status code of the response: does it match
2098         // the expected response(s)?
2099         if (logger.isDebugEnabled()) {
2100             logger.debug("testItemSubmitRequest: url=" + url
2101                     + " status=" + statusCode);
2102         }
2103         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2104
2105     }
2106
2107     // ---------------------------------------------------------------
2108     // Cleanup of resources created during testing
2109     // ---------------------------------------------------------------
2110     /**
2111      * Deletes all resources created by tests, after all tests have been run.
2112      *
2113      * This cleanup method will always be run, even if one or more tests fail.
2114      * For this reason, it attempts to remove all resources created
2115      * at any point during testing, even if some of those resources
2116      * may be expected to be deleted by certain tests.
2117      */
2118     @AfterClass(alwaysRun = true)
2119     @Override
2120     public void cleanUp() {
2121         String noTest = System.getProperty("noTestCleanup");
2122         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
2123             if (logger.isDebugEnabled()) {
2124                 logger.debug("Skipping Cleanup phase ...");
2125             }
2126             return;
2127         }
2128         if (logger.isDebugEnabled()) {
2129             logger.debug("Cleaning up temporary resources created for testing ...");
2130         }
2131         String parentResourceId;
2132         String itemResourceId;
2133         String contactResourceId;
2134         // Clean up contact resources.
2135         PersonAuthorityClient client = new PersonAuthorityClient();
2136         parentResourceId = knownResourceId;
2137         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
2138             contactResourceId = entry.getKey();
2139             itemResourceId = entry.getValue();
2140             // Note: Any non-success responses from the delete operation
2141             // below are ignored and not reported.
2142             ClientResponse<Response> res =
2143                     client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
2144             res.releaseConnection();
2145         }
2146         // Clean up item resources.
2147         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
2148             itemResourceId = entry.getKey();
2149             parentResourceId = entry.getValue();
2150             // Note: Any non-success responses from the delete operation
2151             // below are ignored and not reported.
2152             ClientResponse<Response> res =
2153                     client.deleteItem(parentResourceId, itemResourceId);
2154             res.releaseConnection();
2155         }
2156         // Clean up parent resources.
2157         super.cleanUp();
2158     }
2159
2160     // ---------------------------------------------------------------
2161     // Utility methods used by tests above
2162     // ---------------------------------------------------------------
2163     /**
2164      * Gets the contact service path component.
2165      *
2166      * @return the contact service path component
2167      */
2168     public String getContactServicePathComponent() {
2169         return ContactClient.SERVICE_PATH_COMPONENT;
2170     }
2171
2172     /**
2173      * Returns the root URL for the item service.
2174      *
2175      * This URL consists of a base URL for all services, followed by
2176      * a path component for the owning parent, followed by the
2177      * path component for the items.
2178      *
2179      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2180      * parent authority resource of the relevant item resource.
2181      *
2182      * @return The root URL for the item service.
2183      */
2184     protected String getItemServiceRootURL(String parentResourceIdentifier) {
2185         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
2186     }
2187
2188     /**
2189      * Returns the URL of a specific item resource managed by a service, and
2190      * designated by an identifier (such as a universally unique ID, or UUID).
2191      *
2192      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2193      * parent authority resource of the relevant item resource.
2194      *
2195      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2196      * item resource.
2197      *
2198      * @return The URL of a specific item resource managed by a service.
2199      */
2200     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
2201         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
2202     }
2203
2204     /**
2205      * Returns the root URL for the contact service.
2206      *
2207      * This URL consists of a base URL for all services, followed by
2208      * a path component for the owning authority, followed by the
2209      * path component for the owning item, followed by the path component
2210      * for the contact service.
2211      *
2212      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2213      * parent authority resource of the relevant item resource.
2214      *
2215      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2216      * item resource.
2217      *
2218      * @return The root URL for the contact service.
2219      */
2220     protected String getContactServiceRootURL(String parentResourceIdentifier,
2221             String itemResourceIdentifier) {
2222         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
2223                 + getContactServicePathComponent();
2224     }
2225
2226     /**
2227      * Returns the URL of a specific contact resource managed by a service, and
2228      * designated by an identifier (such as a universally unique ID, or UUID).
2229      *
2230      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2231      * parent resource of the relevant item resource.
2232      *
2233      * @param  resourceIdentifier  An identifier (such as a UUID) for an
2234      * item resource.
2235      *
2236      * @return The URL of a specific resource managed by a service.
2237      */
2238     protected String getContactResourceURL(String parentResourceIdentifier,
2239             String itemResourceIdentifier, String contactResourceIdentifier) {
2240         return getContactServiceRootURL(parentResourceIdentifier,
2241                 itemResourceIdentifier) + "/" + contactResourceIdentifier;
2242     }
2243 }