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