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