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