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