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