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