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