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