]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8529b975f55fb4916889c9402d43d4283b71d36b
[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, 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         try {
557             int statusCode = res.getStatus();
558
559             // Check the status code of the response: does it match
560             // the expected response(s)?
561             if (logger.isDebugEnabled()) {
562                 logger.debug(testName + ": status = " + statusCode);
563             }
564             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
565                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
566             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
567             //FIXME: remove the following try catch once Aron fixes signatures
568             try {
569                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
570                 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
571                         new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
572                 if (logger.isDebugEnabled()) {
573                     logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
574                 }
575                 Assert.assertNotNull(orgAuthority);
576             } catch (Exception e) {
577                 throw new RuntimeException(e);
578             }
579         } finally {
580             res.releaseConnection();
581         }
582     }
583
584     /**
585      * Read item.
586      *
587      * @param testName the test name
588      * @throws Exception the exception
589      */
590     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
591     groups = {"readItem"}, dependsOnGroups = {"read"})
592     public void readItem(String testName) throws Exception {
593         readItemInternal(testName, knownResourceId, null, knownItemResourceId, null);
594     }
595
596     /**
597      * Read item in Named Auth.
598      * 
599      * TODO Enable this if we really need this - it is a funky case, where we would have
600      * the shortId of the item, but the CSID of the parent authority!? Unlikely.
601      *
602      * @param testName the test name
603      * @throws Exception the exception
604     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
605     groups = {"readItem"}, dependsOnGroups = {"read"})
606     public void readItemInNamedAuth(String testName) throws Exception {
607     readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
608     }
609      */
610     /**
611      * Read named item.
612      *
613      * @param testName the test name
614      * @throws Exception the exception
615      */
616     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
617     groups = {"readItem"}, dependsOnGroups = {"read"})
618     public void readNamedItem(String testName) throws Exception {
619         readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
620     }
621
622     /**
623      * Read Named item in Named Auth.
624      *
625      * @param testName the test name
626      * @throws Exception the exception
627      */
628     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
629     groups = {"readItem"}, dependsOnGroups = {"read"})
630     public void readNamedItemInNamedAuth(String testName) throws Exception {
631         readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
632     }
633
634     protected void readItemInternal(String testName,
635             String authCSID, String authShortId, String itemCSID, String itemShortId)
636             throws Exception {
637
638         if (logger.isDebugEnabled()) {
639             logger.debug(testBanner(testName, CLASS_NAME));
640         }
641         // Perform setup.
642         setupRead();
643
644         // Submit the request to the service and store the response.
645         OrgAuthorityClient client = new OrgAuthorityClient();
646         ClientResponse<String> res = null;
647         if (authCSID != null) {
648             if (itemCSID != null) {
649                 res = client.readItem(authCSID, itemCSID);
650             } else if (itemShortId != null) {
651                 res = client.readNamedItem(authCSID, itemShortId);
652             } else {
653                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
654             }
655         } else if (authShortId != null) {
656             if (itemCSID != null) {
657                 res = client.readItemInNamedAuthority(authShortId, itemCSID);
658             } else if (itemShortId != null) {
659                 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
660             } else {
661                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
662             }
663         } else {
664             Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
665         }
666         try {
667             int statusCode = res.getStatus();
668
669             // Check the status code of the response: does it match
670             // the expected response(s)?
671             if (logger.isDebugEnabled()) {
672                 logger.debug(testName + ": status = " + statusCode);
673             }
674             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
675                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
676             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
677
678             // Check whether we've received a organization.
679             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
680             OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
681                     client.getItemCommonPartName(), OrganizationsCommon.class);
682             Assert.assertNotNull(organization);
683             boolean showFull = true;
684             if (showFull && logger.isDebugEnabled()) {
685                 logger.debug(testName + ": returned payload:");
686                 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
687             }
688
689             // Check that the organization item is within the expected OrgAuthority.
690             Assert.assertEquals(organization.getInAuthority(), knownResourceId);
691
692             // Verify the number and contents of values in a repeatable field,
693             // as created in the instance record used for testing.
694             List<String> contactNames = organization.getContactNames().getContactName();
695             Assert.assertTrue(contactNames.size() > 0);
696             Assert.assertNotNull(contactNames.get(0));
697
698         } finally {
699             res.releaseConnection();
700         }
701     }
702
703     /**
704      * Verify item display name.
705      *
706      * @param testName the test name
707      * @throws Exception the exception
708      */
709     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
710     dependsOnMethods = {"readItem", "updateItem"})
711     public void verifyItemDisplayName(String testName) throws Exception {
712
713         if (logger.isDebugEnabled()) {
714             logger.debug(testBanner(testName, CLASS_NAME));
715         }
716         // Perform setup.
717         setupUpdate();
718
719         // Submit the request to the service and store the response.
720         OrgAuthorityClient client = new OrgAuthorityClient();
721         PoxPayloadIn input = null;
722         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
723         try {
724             int statusCode = res.getStatus();
725
726             // Check the status code of the response: does it match
727             // the expected response(s)?
728             if (logger.isDebugEnabled()) {
729                 logger.debug(testName + ": status = " + statusCode);
730             }
731             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
732                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
733             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
734
735             // Check whether organization has expected displayName.
736             input = new PoxPayloadIn(res.getEntity());
737         } finally {
738             res.releaseConnection();
739         }
740
741         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
742                 client.getItemCommonPartName(), OrganizationsCommon.class);
743         Assert.assertNotNull(organization);
744         String displayName = organization.getDisplayName();
745         // Make sure displayName matches computed form
746         String expectedDisplayName =
747                 OrgAuthorityClientUtils.prepareDefaultDisplayName(
748                 TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
749         Assert.assertNotNull(displayName, expectedDisplayName);
750
751         // Update the shortName and verify the computed name is updated.
752         organization.setCsid(null);
753         organization.setDisplayNameComputed(true);
754
755         MainBodyGroupList mainBodyList = organization.getMainBodyGroupList();
756         List<MainBodyGroup> mainBodyGroups = mainBodyList.getMainBodyGroup();
757         MainBodyGroup mainBodyGroup = new MainBodyGroup();
758         String updatedShortName = "updated-" + TEST_ORG_SHORTNAME;
759         mainBodyGroup.setShortName(updatedShortName);
760         mainBodyGroups.add(mainBodyGroup);
761         organization.setMainBodyGroupList(mainBodyList);
762
763         expectedDisplayName =
764                 OrgAuthorityClientUtils.prepareDefaultDisplayName(
765                 updatedShortName, TEST_ORG_FOUNDING_PLACE);
766
767         // Submit the updated resource to the service and store the response.
768         PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
769         PayloadOutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
770         commonPart.setLabel(client.getItemCommonPartName());
771         res = client.updateItem(knownResourceId, knownItemResourceId, output);
772         try {
773             int statusCode = res.getStatus();
774
775             // Check the status code of the response: does it match the expected response(s)?
776             if (logger.isDebugEnabled()) {
777                 logger.debug("updateItem: status = " + statusCode);
778             }
779             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
780                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
781             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
782
783             // Retrieve the updated resource and verify that its contents exist.
784             input = new PoxPayloadIn(res.getEntity());
785         } finally {
786             res.releaseConnection();
787         }
788
789         OrganizationsCommon updatedOrganization =
790                 (OrganizationsCommon) extractPart(input,
791                 client.getItemCommonPartName(), OrganizationsCommon.class);
792         Assert.assertNotNull(updatedOrganization);
793
794         // Verify that the updated resource received the correct data.
795         mainBodyList = organization.getMainBodyGroupList();
796         Assert.assertNotNull(mainBodyList);
797         Assert.assertTrue(mainBodyList.getMainBodyGroup().size() > 0);
798         Assert.assertEquals(updatedOrganization.getMainBodyGroupList().getMainBodyGroup().get(0).getShortName(),
799                 updatedShortName, "Updated ShortName in Organization did not match submitted data.");
800
801         // Verify that the updated resource computes the right displayName.
802         Assert.assertEquals(updatedOrganization.getDisplayName(), expectedDisplayName,
803                 "Updated ShortName in Organization not reflected in computed DisplayName.");
804
805         // Now Update the displayName, not computed and verify the computed name is overriden.
806         organization.setDisplayNameComputed(false);
807         expectedDisplayName = "TestName";
808         organization.setDisplayName(expectedDisplayName);
809
810         // Submit the updated resource to the service and store the response.
811         output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
812         commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
813         commonPart.setLabel(client.getItemCommonPartName());
814         res = client.updateItem(knownResourceId, knownItemResourceId, output);
815         try {
816             int statusCode = res.getStatus();
817
818             // Check the status code of the response: does it match the expected response(s)?
819             if (logger.isDebugEnabled()) {
820                 logger.debug("updateItem: status = " + statusCode);
821             }
822             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
823                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
824             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
825
826             // Retrieve the updated resource and verify that its contents exist.
827             input = new PoxPayloadIn(res.getEntity());
828         } finally {
829             res.releaseConnection();
830         }
831
832         updatedOrganization =
833                 (OrganizationsCommon) extractPart(input,
834                 client.getItemCommonPartName(), OrganizationsCommon.class);
835         Assert.assertNotNull(updatedOrganization);
836
837         // Verify that the updated resource received the correct data.
838         Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
839                 "Updated displayNameComputed in Organization did not match submitted data.");
840         // Verify that the updated resource computes the right displayName.
841         Assert.assertEquals(updatedOrganization.getDisplayName(),
842                 expectedDisplayName,
843                 "Updated DisplayName (not computed) in Organization not stored.");
844     }
845
846     /**
847      * Verify illegal item display name.
848      *
849      * @param testName the test name
850      * @throws Exception the exception
851      */
852     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
853     dependsOnMethods = {"verifyItemDisplayName"})
854     public void verifyIllegalItemDisplayName(String testName) throws Exception {
855
856         if (logger.isDebugEnabled()) {
857             logger.debug(testBanner(testName, CLASS_NAME));
858         }
859         // Perform setup.
860         testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
861         // setupUpdateWithWrongXmlSchema(testName, logger);
862
863         // Submit the request to the service and store the response.
864         OrgAuthorityClient client = new OrgAuthorityClient();
865         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
866         try {
867             int statusCode = res.getStatus();
868
869             // Check the status code of the response: does it match
870             // the expected response(s)?
871             if (logger.isDebugEnabled()) {
872                 logger.debug(testName + ": status = " + statusCode);
873             }
874             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
875                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
876             Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
877
878             // Check whether organization has expected displayName.
879             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
880             OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
881                     client.getItemCommonPartName(), OrganizationsCommon.class);
882             Assert.assertNotNull(organization);
883             // Try to Update with computed false and no displayName
884             organization.setDisplayNameComputed(false);
885             organization.setDisplayName(null);
886
887             // Submit the updated resource to the service and store the response.
888             PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
889             PayloadOutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
890             commonPart.setLabel(client.getItemCommonPartName());
891             res.releaseConnection();
892             res = client.updateItem(knownResourceId, knownItemResourceId, output);
893             statusCode = res.getStatus();
894
895             // Check the status code of the response: does it match the expected response(s)?
896             if (logger.isDebugEnabled()) {
897                 logger.debug("updateItem: status = " + statusCode);
898             }
899             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
900                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
901             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
902         } finally {
903             res.releaseConnection();
904         }
905     }
906
907     /**
908      * Read contact.
909      *
910      * @param testName the test name
911      * @throws Exception the exception
912      */
913     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
914     groups = {"readItem"}, dependsOnMethods = {"readItem"})
915     public void readContact(String testName) throws Exception {
916
917         if (logger.isDebugEnabled()) {
918             logger.debug(testBanner(testName, CLASS_NAME));
919         }
920         // Perform setup.
921         setupRead();
922
923         // Submit the request to the service and store the response.
924         OrgAuthorityClient client = new OrgAuthorityClient();
925         ClientResponse<String> res =
926                 client.readContact(knownResourceId, knownItemResourceId,
927                 knownContactResourceId);
928         try {
929             int statusCode = res.getStatus();
930
931             // Check the status code of the response: does it match
932             // the expected response(s)?
933             if (logger.isDebugEnabled()) {
934                 logger.debug(testName + ": status = " + statusCode);
935             }
936             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
937                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
938             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
939
940             // Check whether we've received a contact.
941             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
942             ContactsCommon contact = (ContactsCommon) extractPart(input,
943                     new ContactClient().getCommonPartName(), ContactsCommon.class);
944             Assert.assertNotNull(contact);
945             boolean showFull = true;
946             if (showFull && logger.isDebugEnabled()) {
947                 logger.debug(testName + ": returned payload:");
948                 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
949             }
950             Assert.assertEquals(contact.getInAuthority(), knownResourceId);
951             Assert.assertEquals(contact.getInItem(), knownItemResourceId);
952         } finally {
953             res.releaseConnection();
954         }
955     }
956
957     // Failure outcomes
958     /* (non-Javadoc)
959      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
960      */
961     @Override
962     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
963     groups = {"read"}, dependsOnMethods = {"read"})
964     public void readNonExistent(String testName) {
965
966         if (logger.isDebugEnabled()) {
967             logger.debug(testBanner(testName, CLASS_NAME));
968         }
969         // Perform setup.
970         setupReadNonExistent();
971
972         // Submit the request to the service and store the response.
973         OrgAuthorityClient client = new OrgAuthorityClient();
974         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
975         try {
976             int statusCode = res.getStatus();
977
978             // Check the status code of the response: does it match
979             // the expected response(s)?
980             if (logger.isDebugEnabled()) {
981                 logger.debug(testName + ": status = " + statusCode);
982             }
983             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
984                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
985             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
986         } finally {
987             res.releaseConnection();
988         }
989     }
990
991     /**
992      * Read item non existent.
993      *
994      * @param testName the test name
995      */
996     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
997     groups = {"readItem"}, dependsOnMethods = {"readItem"})
998     public void readItemNonExistent(String testName) {
999
1000         if (logger.isDebugEnabled()) {
1001             logger.debug(testBanner(testName, CLASS_NAME));
1002         }
1003         // Perform setup.
1004         setupReadNonExistent();
1005
1006         // Submit the request to the service and store the response.
1007         OrgAuthorityClient client = new OrgAuthorityClient();
1008         ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
1009         try {
1010             int statusCode = res.getStatus();
1011
1012             // Check the status code of the response: does it match
1013             // the expected response(s)?
1014             if (logger.isDebugEnabled()) {
1015                 logger.debug(testName + ": status = " + statusCode);
1016             }
1017             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1018                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1019             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1020         } finally {
1021             res.releaseConnection();
1022         }
1023     }
1024
1025     /**
1026      * Read contact non existent.
1027      *
1028      * @param testName the test name
1029      */
1030     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1031     groups = {"readItem"}, dependsOnMethods = {"readContact"})
1032     public void readContactNonExistent(String testName) {
1033
1034         if (logger.isDebugEnabled()) {
1035             logger.debug(testBanner(testName, CLASS_NAME));
1036         }
1037         // Perform setup.
1038         setupReadNonExistent();
1039
1040         // Submit the request to the service and store the response.
1041         OrgAuthorityClient client = new OrgAuthorityClient();
1042         ClientResponse<String> res =
1043                 client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1044         try {
1045             int statusCode = res.getStatus();
1046
1047             // Check the status code of the response: does it match
1048             // the expected response(s)?
1049             if (logger.isDebugEnabled()) {
1050                 logger.debug(testName + ": status = " + statusCode);
1051             }
1052             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1053                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1054             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1055         } finally {
1056             res.releaseConnection();
1057         }
1058     }
1059
1060     // ---------------------------------------------------------------
1061     // CRUD tests : READ_LIST tests
1062     // ---------------------------------------------------------------
1063     // Success outcomes
1064
1065     /* (non-Javadoc)
1066      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
1067      */
1068     @Override
1069     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1070     groups = {"readList"}, dependsOnGroups = {"createList", "read"})
1071     public void readList(String testName) throws Exception {
1072
1073         if (logger.isDebugEnabled()) {
1074             logger.debug(testBanner(testName, CLASS_NAME));
1075         }
1076         // Perform setup.
1077         setupReadList();
1078
1079         // Submit the request to the service and store the response.
1080         OrgAuthorityClient client = new OrgAuthorityClient();
1081         ClientResponse<AbstractCommonList> res = client.readList();
1082         try {
1083             AbstractCommonList list = res.getEntity();
1084             int statusCode = res.getStatus();
1085
1086             // Check the status code of the response: does it match
1087             // the expected response(s)?
1088             if (logger.isDebugEnabled()) {
1089                 logger.debug(testName + ": status = " + statusCode);
1090             }
1091             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1092                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1093             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1094
1095             // Optionally output additional data about list members for debugging.
1096             if (logger.isTraceEnabled()) {
1097                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1098             }
1099         } finally {
1100             res.releaseConnection();
1101         }
1102     }
1103
1104     /**
1105      * Read item list.
1106      */
1107     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
1108     public void readItemList() {
1109         readItemList(knownResourceId, null);
1110     }
1111
1112     /**
1113      * Read item list by authority name.
1114      */
1115     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
1116     public void readItemListByAuthorityName() {
1117         readItemList(null, knownResourceShortIdentifer);
1118     }
1119
1120     /**
1121      * Read item list.
1122      *
1123      * @param vcsid the vcsid
1124      * @param name the name
1125      */
1126     private void readItemList(String vcsid, String name) {
1127
1128         final String testName = "readItemList";
1129
1130         if (logger.isDebugEnabled()) {
1131             logger.debug(testBanner(testName, CLASS_NAME));
1132         }
1133         // Perform setup.
1134         setupReadList();
1135
1136         // Submit the request to the service and store the response.
1137         OrgAuthorityClient client = new OrgAuthorityClient();
1138         ClientResponse<AbstractCommonList> res = null;
1139         if (vcsid != null) {
1140             res = client.readItemList(vcsid, null, null);
1141         } else if (name != null) {
1142             res = client.readItemListForNamedAuthority(name, null, null);
1143         } else {
1144             Assert.fail("readItemList passed null csid and name!");
1145         }
1146         try {
1147             AbstractCommonList list = res.getEntity();
1148             int statusCode = res.getStatus();
1149
1150             // Check the status code of the response: does it match
1151             // the expected response(s)?
1152             if (logger.isDebugEnabled()) {
1153                 logger.debug(testName + ": status = " + statusCode);
1154             }
1155             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1156                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1157             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1158
1159             List<AbstractCommonList.ListItem> items =
1160                     list.getListItem();
1161             int nItemsReturned = items.size();
1162             // There will be one item created, associated with a
1163             // known parent resource, by the createItem test.
1164             //
1165             // In addition, there will be 'nItemsToCreateInList'
1166             // additional items created by the createItemList test,
1167             // all associated with the same parent resource.
1168             int nExpectedItems = nItemsToCreateInList + 1;
1169             if (logger.isDebugEnabled()) {
1170                 logger.debug(testName + ": Expected "
1171                         + nExpectedItems + " items; got: " + nItemsReturned);
1172             }
1173             Assert.assertEquals(nItemsReturned, nExpectedItems);
1174
1175             for (AbstractCommonList.ListItem item : items) {
1176                 String value =
1177                         AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
1178                 Assert.assertTrue((null != value), "Item refName is null!");
1179                 value =
1180                         AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
1181                 Assert.assertTrue((null != value), "Item displayName is null!");
1182             }
1183             if (logger.isTraceEnabled()) {
1184                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1185             }
1186         } finally {
1187             res.releaseConnection();
1188         }
1189     }
1190
1191     /**
1192      * Read contact list.
1193      */
1194     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
1195     public void readContactList() {
1196         readContactList(knownResourceId, knownItemResourceId);
1197     }
1198
1199     /**
1200      * Read contact list.
1201      *
1202      * @param parentcsid the parentcsid
1203      * @param itemcsid the itemcsid
1204      */
1205     private void readContactList(String parentcsid, String itemcsid) {
1206         final String testName = "readContactList";
1207
1208         if (logger.isDebugEnabled()) {
1209             logger.debug(testBanner(testName, CLASS_NAME));
1210         }
1211         // Perform setup.
1212         setupReadList();
1213
1214         // Submit the request to the service and store the response.
1215         OrgAuthorityClient client = new OrgAuthorityClient();
1216         AbstractCommonList list = null;
1217         ClientResponse<AbstractCommonList> res =
1218                 client.readContactList(parentcsid, itemcsid);
1219         try {
1220             list = res.getEntity();
1221             int statusCode = res.getStatus();
1222
1223             // Check the status code of the response: does it match
1224             // the expected response(s)?
1225             if (logger.isDebugEnabled()) {
1226                 logger.debug(testName + ": status = " + statusCode);
1227             }
1228             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1229                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1230             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1231
1232             List<AbstractCommonList.ListItem> listitems =
1233                 list.getListItem();
1234             int nItemsReturned = listitems.size();
1235             // There will be one item created, associated with a
1236             // known parent resource, by the createItem test.
1237             //
1238             // In addition, there will be 'nItemsToCreateInList'
1239             // additional items created by the createItemList test,
1240             // all associated with the same parent resource.
1241             int nExpectedItems = nItemsToCreateInList + 1;
1242             if (logger.isDebugEnabled()) {
1243                 logger.debug(testName + ": Expected "
1244                         + nExpectedItems + " items; got: " + nItemsReturned);
1245             }
1246             Assert.assertEquals(nItemsReturned, nExpectedItems);
1247
1248             // Optionally output additional data about list members for debugging.
1249             boolean iterateThroughList = false;
1250             if (iterateThroughList && logger.isDebugEnabled()) {
1251                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
1252             }
1253         } finally {
1254             res.releaseConnection();
1255         }
1256     }
1257
1258     // Failure outcomes
1259     // None at present.
1260     // ---------------------------------------------------------------
1261     // CRUD tests : UPDATE tests
1262     // ---------------------------------------------------------------
1263     // Success outcomes
1264     /* (non-Javadoc)
1265      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
1266      */
1267     @Override
1268     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1269     groups = {"update"}, dependsOnGroups = {"read", "readList"})
1270     public void update(String testName) throws Exception {
1271
1272         if (logger.isDebugEnabled()) {
1273             logger.debug(testBanner(testName, CLASS_NAME));
1274         }
1275         // Perform setup.
1276         setupUpdate();
1277
1278         // Retrieve the contents of a resource to update.
1279         OrgAuthorityClient client = new OrgAuthorityClient();
1280         ClientResponse<String> res =
1281                 client.read(knownResourceId);
1282         try {
1283             if (logger.isDebugEnabled()) {
1284                 logger.debug(testName + ": read status = " + res.getStatus());
1285             }
1286             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1287
1288             if (logger.isDebugEnabled()) {
1289                 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
1290             }
1291             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
1292             OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
1293                     client.getCommonPartName(), OrgauthoritiesCommon.class);
1294             Assert.assertNotNull(orgAuthority);
1295
1296             // Update the contents of this resource.
1297             orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
1298             orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
1299             if (logger.isDebugEnabled()) {
1300                 logger.debug("to be updated OrgAuthority");
1301                 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
1302             }
1303
1304             // Submit the updated resource to the service and store the response.
1305             PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_PAYLOAD_NAME);
1306             PayloadOutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
1307             commonPart.setLabel(client.getCommonPartName());
1308             res.releaseConnection();
1309             res = client.update(knownResourceId, output);
1310             int statusCode = res.getStatus();
1311
1312             // Check the status code of the response: does it match the expected response(s)?
1313             if (logger.isDebugEnabled()) {
1314                 logger.debug(testName + ": status = " + statusCode);
1315             }
1316             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1317                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1318             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1319
1320             // Retrieve the updated resource and verify that its contents exist.
1321             input = new PoxPayloadIn(res.getEntity());
1322             OrgauthoritiesCommon updatedOrgAuthority =
1323                     (OrgauthoritiesCommon) extractPart(input,
1324                     client.getCommonPartName(), OrgauthoritiesCommon.class);
1325             Assert.assertNotNull(updatedOrgAuthority);
1326
1327             // Verify that the updated resource received the correct data.
1328             Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
1329                     orgAuthority.getDisplayName(),
1330                     "Data in updated object did not match submitted data.");
1331         } finally {
1332             res.releaseConnection();
1333         }
1334     }
1335
1336     /**
1337      * Update item.
1338      *
1339      * @param testName the test name
1340      * @throws Exception the exception
1341      */
1342     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1343     groups = {"update"}, dependsOnMethods = {"update"})
1344     public void updateItem(String testName) throws Exception {
1345
1346         if (logger.isDebugEnabled()) {
1347             logger.debug(testBanner(testName, CLASS_NAME));
1348         }
1349         // Perform setup.
1350         setupUpdate();
1351
1352         // Retrieve the contents of a resource to update.
1353         OrgAuthorityClient client = new OrgAuthorityClient();
1354         ClientResponse<String> res =
1355                 client.readItem(knownResourceId, knownItemResourceId);
1356         try {
1357             if (logger.isDebugEnabled()) {
1358                 logger.debug(testName + ": read status = " + res.getStatus());
1359             }
1360             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1361
1362             if (logger.isDebugEnabled()) {
1363                 logger.debug("got Organization to update with ID: "
1364                         + knownItemResourceId
1365                         + " in OrgAuthority: " + knownResourceId);
1366             }
1367             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
1368             OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
1369                     client.getItemCommonPartName(), OrganizationsCommon.class);
1370             Assert.assertNotNull(organization);
1371
1372             // Update the contents of this resource.
1373             organization.setCsid(null);
1374
1375             MainBodyGroupList mainBodyList = organization.getMainBodyGroupList();
1376             Assert.assertNotNull(mainBodyList);
1377             List<MainBodyGroup> mainBodyGroups = mainBodyList.getMainBodyGroup();
1378             Assert.assertTrue(mainBodyList.getMainBodyGroup().size() > 0);
1379             String updatedShortName = "updated-" + mainBodyGroups.get(0).getShortName();
1380             mainBodyGroups.get(0).setShortName(updatedShortName);
1381
1382             if (logger.isDebugEnabled()) {
1383                 logger.debug("to be updated Organization");
1384                 logger.debug(objectAsXmlString(organization,
1385                         OrganizationsCommon.class));
1386             }
1387
1388             // Submit the updated resource to the service and store the response.
1389             PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1390             PayloadOutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
1391             commonPart.setLabel(client.getItemCommonPartName());
1392             res.releaseConnection();
1393             res = client.updateItem(knownResourceId, knownItemResourceId, output);
1394             int statusCode = res.getStatus();
1395
1396             // Check the status code of the response: does it match the expected response(s)?
1397             if (logger.isDebugEnabled()) {
1398                 logger.debug(testName + ": status = " + statusCode);
1399             }
1400             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1401                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1402             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1403
1404             // Retrieve the updated resource and verify that its contents exist.
1405             input = new PoxPayloadIn(res.getEntity());
1406             OrganizationsCommon updatedOrganization =
1407                     (OrganizationsCommon) extractPart(input,
1408                     client.getItemCommonPartName(), OrganizationsCommon.class);
1409             Assert.assertNotNull(updatedOrganization);
1410
1411             // Verify that the updated resource received the correct data.
1412             mainBodyList = organization.getMainBodyGroupList();
1413             Assert.assertNotNull(mainBodyList);
1414             Assert.assertTrue(mainBodyList.getMainBodyGroup().size() > 0);
1415             Assert.assertEquals(updatedOrganization.getMainBodyGroupList().getMainBodyGroup().get(0).getShortName(),
1416                     updatedShortName, "Data in updated Organization did not match submitted data.");
1417         } finally {
1418             res.releaseConnection();
1419         }
1420     }
1421
1422     /**
1423      * Update contact.
1424      *
1425      * @param testName the test name
1426      * @throws Exception the exception
1427      */
1428     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1429     groups = {"update"}, dependsOnMethods = {"updateItem"})
1430     public void updateContact(String testName) throws Exception {
1431
1432         if (logger.isDebugEnabled()) {
1433             logger.debug(testBanner(testName, CLASS_NAME));
1434         }
1435         // Perform setup.
1436         setupUpdate();
1437
1438         // Retrieve the contents of a resource to update.
1439         OrgAuthorityClient client = new OrgAuthorityClient();
1440         ClientResponse<String> res =
1441                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1442         try {
1443             if (logger.isDebugEnabled()) {
1444                 logger.debug(testName + ": read status = " + res.getStatus());
1445             }
1446             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1447
1448             if (logger.isDebugEnabled()) {
1449                 logger.debug("got Contact to update with ID: "
1450                         + knownContactResourceId
1451                         + " in item: " + knownItemResourceId
1452                         + " in parent: " + knownResourceId);
1453             }
1454             PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
1455             ContactsCommon contact = (ContactsCommon) extractPart(input,
1456                     new ContactClient().getCommonPartName(), ContactsCommon.class);
1457             Assert.assertNotNull(contact);
1458
1459             // Verify the contents of this resource
1460             AddressGroupList addressGroupList = contact.getAddressGroupList();
1461             Assert.assertNotNull(addressGroupList);
1462             List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1463             Assert.assertNotNull(addressGroups);
1464             Assert.assertTrue(addressGroups.size() > 0);
1465             String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1466             Assert.assertNotNull(addressPlace1);
1467
1468             // Update the contents of this resource.
1469             addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1470             contact.setAddressGroupList(addressGroupList);
1471             if (logger.isDebugEnabled()) {
1472                 logger.debug("to be updated Contact");
1473                 logger.debug(objectAsXmlString(contact,
1474                         ContactsCommon.class));
1475             }
1476
1477             // Submit the updated resource to the service and store the response.
1478             PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1479             PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
1480             commonPart.setLabel(new ContactClient().getCommonPartName());
1481             res.releaseConnection();
1482             res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1483             int statusCode = res.getStatus();
1484
1485             // Check the status code of the response: does it match the expected response(s)?
1486             if (logger.isDebugEnabled()) {
1487                 logger.debug(testName + ": status = " + statusCode);
1488             }
1489             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1490                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1491             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1492
1493             // Retrieve the updated resource and verify that its contents exist.
1494             input = new PoxPayloadIn(res.getEntity());
1495             ContactsCommon updatedContact =
1496                     (ContactsCommon) extractPart(input,
1497                     new ContactClient().getCommonPartName(), ContactsCommon.class);
1498             Assert.assertNotNull(updatedContact);
1499
1500             // Verify that the updated resource received the correct data.
1501             Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1502                     contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1503                     "Data in updated object did not match submitted data.");
1504
1505         } finally {
1506             res.releaseConnection();
1507         }
1508     }
1509
1510     // Failure outcomes
1511     // Placeholders until the three tests below can be uncommented.
1512     // See Issue CSPACE-401.
1513     /* (non-Javadoc)
1514      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
1515      */
1516     @Override
1517     public void updateWithEmptyEntityBody(String testName) throws Exception {
1518         //Should this really be empty?
1519     }
1520
1521     /* (non-Javadoc)
1522      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
1523      */
1524     @Override
1525     public void updateWithMalformedXml(String testName) throws Exception {
1526         //Should this really be empty?
1527     }
1528
1529     /* (non-Javadoc)
1530      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
1531      */
1532     @Override
1533     public void updateWithWrongXmlSchema(String testName) throws Exception {
1534         //Should this really be empty?
1535     }
1536
1537     /*
1538     @Override
1539     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1540     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1541     public void updateWithEmptyEntityBody(String testName) throws Exception {
1542     
1543     if (logger.isDebugEnabled()) {
1544     logger.debug(testBanner(testName, CLASS_NAME));
1545     }
1546     // Perform setup.
1547     setupUpdateWithEmptyEntityBody();
1548     
1549     // Submit the request to the service and store the response.
1550     String method = REQUEST_TYPE.httpMethodName();
1551     String url = getResourceURL(knownResourceId);
1552     String mediaType = MediaType.APPLICATION_XML;
1553     final String entity = "";
1554     int statusCode = submitRequest(method, url, mediaType, entity);
1555     
1556     // Check the status code of the response: does it match
1557     // the expected response(s)?
1558     if(logger.isDebugEnabled()){
1559     logger.debug(testName + ": url=" + url +
1560     " status=" + statusCode);
1561     }
1562     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1563     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1564     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1565     }
1566     
1567     @Override
1568     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1569     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1570     public void updateWithMalformedXml(String testName) throws Exception {
1571     
1572     if (logger.isDebugEnabled()) {
1573     logger.debug(testBanner(testName, CLASS_NAME));
1574     }
1575     // Perform setup.
1576     setupUpdateWithMalformedXml();
1577     
1578     // Submit the request to the service and store the response.
1579     String method = REQUEST_TYPE.httpMethodName();
1580     String url = getResourceURL(knownResourceId);
1581     String mediaType = MediaType.APPLICATION_XML;
1582     final String entity = MALFORMED_XML_DATA;
1583     int statusCode = submitRequest(method, url, mediaType, entity);
1584     
1585     // Check the status code of the response: does it match
1586     // the expected response(s)?
1587     if(logger.isDebugEnabled()){
1588     logger.debug(testName + ": url=" + url +
1589     " status=" + statusCode);
1590     }
1591     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1592     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1593     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1594     }
1595     
1596     @Override
1597     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1598     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1599     public void updateWithWrongXmlSchema(String testName) throws Exception {
1600     
1601     // Perform setup.
1602     setupUpdateWithWrongXmlSchema(testName, logger);
1603     
1604     // Submit the request to the service and store the response.
1605     String method = REQUEST_TYPE.httpMethodName();
1606     String url = getResourceURL(knownResourceId);
1607     String mediaType = MediaType.APPLICATION_XML;
1608     final String entity = WRONG_XML_SCHEMA_DATA;
1609     int statusCode = submitRequest(method, url, mediaType, entity);
1610     
1611     // Check the status code of the response: does it match
1612     // the expected response(s)?
1613     if(logger.isDebugEnabled()){
1614     logger.debug("updateWithWrongXmlSchema: url=" + url +
1615     " status=" + statusCode);
1616     }
1617     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1618     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1619     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1620     }
1621      */
1622
1623     /* (non-Javadoc)
1624      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
1625      */
1626     @Override
1627     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1628     groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1629     public void updateNonExistent(String testName) throws Exception {
1630
1631         if (logger.isDebugEnabled()) {
1632             logger.debug(testBanner(testName, CLASS_NAME));
1633         }
1634         // Perform setup.
1635         setupUpdateNonExistent();
1636
1637         // Submit the request to the service and store the response.
1638         // Note: The ID used in this 'create' call may be arbitrary.
1639         // The only relevant ID may be the one used in update(), below.
1640         OrgAuthorityClient client = new OrgAuthorityClient();
1641         PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1642                 NON_EXISTENT_ID, NON_EXISTENT_ID,
1643                 new OrgAuthorityClient().getCommonPartName());
1644         ClientResponse<String> res =
1645                 client.update(NON_EXISTENT_ID, multipart);
1646         try {
1647             int statusCode = res.getStatus();
1648
1649             // Check the status code of the response: does it match
1650             // the expected response(s)?
1651             if (logger.isDebugEnabled()) {
1652                 logger.debug(testName + ": status = " + statusCode);
1653             }
1654             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1655                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1656             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1657         } finally {
1658             res.releaseConnection();
1659         }
1660     }
1661
1662     /**
1663      * Update non existent item.
1664      *
1665      * @param testName the test name
1666      * @throws Exception the exception
1667      */
1668     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1669     groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1670     public void updateNonExistentItem(String testName) throws Exception {
1671
1672         if (logger.isDebugEnabled()) {
1673             logger.debug(testBanner(testName, CLASS_NAME));
1674         }
1675         // Perform setup.
1676         setupUpdateNonExistent();
1677
1678         // Submit the request to the service and store the response.
1679         // Note: The ID(s) used when creating the request payload may be arbitrary.
1680         // The only relevant ID may be the one used in update(), below.
1681         OrgAuthorityClient client = new OrgAuthorityClient();
1682         Map<String, String> nonexOrgMap = new HashMap<String, String>();
1683         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1684         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
1685         PoxPayloadOut multipart =
1686                 OrgAuthorityClientUtils.createOrganizationInstance(
1687                 knownResourceRefName,
1688                 nonexOrgMap, client.getItemCommonPartName());
1689         ClientResponse<String> res =
1690                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
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     /**
1708      * Update non existent contact.
1709      *
1710      * @param testName the test name
1711      * @throws Exception the exception
1712      */
1713     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1714     groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1715     public void updateNonExistentContact(String testName) throws Exception {
1716         // Currently a no-op test
1717     }
1718
1719     // ---------------------------------------------------------------
1720     // CRUD tests : DELETE tests
1721     // ---------------------------------------------------------------
1722     // Success outcomes
1723     // Note: delete sub-resources in ascending hierarchical order,
1724     // before deleting their parents.
1725     /**
1726      * Delete contact.
1727      *
1728      * @param testName the test name
1729      * @throws Exception the exception
1730      */
1731     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1732     groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1733     public void deleteContact(String testName) throws Exception {
1734
1735         if (logger.isDebugEnabled()) {
1736             logger.debug(testBanner(testName, CLASS_NAME));
1737         }
1738         // Perform setup.
1739         setupDelete();
1740
1741         if (logger.isDebugEnabled()) {
1742             logger.debug("parentcsid =" + knownResourceId
1743                     + " itemcsid = " + knownItemResourceId
1744                     + " csid = " + knownContactResourceId);
1745         }
1746
1747         // Submit the request to the service and store the response.
1748         OrgAuthorityClient client = new OrgAuthorityClient();
1749         ClientResponse<Response> res =
1750                 client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1751         try {
1752             int statusCode = res.getStatus();
1753
1754             // Check the status code of the response: does it match
1755             // the expected response(s)?
1756             if (logger.isDebugEnabled()) {
1757                 logger.debug(testName + ": status = " + statusCode);
1758             }
1759             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1760                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1761             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1762         } finally {
1763             res.releaseConnection();
1764         }
1765     }
1766
1767     /**
1768      * Delete item.
1769      *
1770      * @param testName the test name
1771      * @throws Exception the exception
1772      */
1773     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1774     groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1775     public void deleteItem(String testName) throws Exception {
1776
1777         if (logger.isDebugEnabled()) {
1778             logger.debug(testBanner(testName, CLASS_NAME));
1779         }
1780         // Perform setup.
1781         setupDelete();
1782
1783         if (logger.isDebugEnabled()) {
1784             logger.debug("parentcsid =" + knownResourceId
1785                     + " itemcsid = " + knownItemResourceId);
1786         }
1787
1788         // Submit the request to the service and store the response.
1789         OrgAuthorityClient client = new OrgAuthorityClient();
1790         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1791         try {
1792             int statusCode = res.getStatus();
1793
1794             // Check the status code of the response: does it match
1795             // the expected response(s)?
1796             if (logger.isDebugEnabled()) {
1797                 logger.debug(testName + ": status = " + statusCode);
1798             }
1799             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1800                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1801             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1802         } finally {
1803             res.releaseConnection();
1804         }
1805     }
1806
1807     /* (non-Javadoc)
1808      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1809      */
1810     @Override
1811     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1812     groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1813     public void delete(String testName) throws Exception {
1814
1815         if (logger.isDebugEnabled()) {
1816             logger.debug(testBanner(testName, CLASS_NAME));
1817         }
1818         // Perform setup.
1819         setupDelete();
1820
1821         if (logger.isDebugEnabled()) {
1822             logger.debug("parentcsid =" + knownResourceId);
1823         }
1824
1825         // Submit the request to the service and store the response.
1826         OrgAuthorityClient client = new OrgAuthorityClient();
1827         ClientResponse<Response> res = client.delete(knownResourceId);
1828         try {
1829             int statusCode = res.getStatus();
1830
1831             // Check the status code of the response: does it match
1832             // the expected response(s)?
1833             if (logger.isDebugEnabled()) {
1834                 logger.debug(testName + ": status = " + statusCode);
1835             }
1836             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1837                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1838             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1839         } finally {
1840             res.releaseConnection();
1841         }
1842     }
1843
1844     // Failure outcomes
1845     /* (non-Javadoc)
1846      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1847      */
1848     @Override
1849     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1850     groups = {"delete"}, dependsOnMethods = {"delete"})
1851     public void deleteNonExistent(String testName) throws Exception {
1852
1853         if (logger.isDebugEnabled()) {
1854             logger.debug(testBanner(testName, CLASS_NAME));
1855         }
1856         // Perform setup.
1857         setupDeleteNonExistent();
1858
1859         // Submit the request to the service and store the response.
1860         OrgAuthorityClient client = new OrgAuthorityClient();
1861         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1862         try {
1863             int statusCode = res.getStatus();
1864
1865             // Check the status code of the response: does it match
1866             // the expected response(s)?
1867             if (logger.isDebugEnabled()) {
1868                 logger.debug(testName + ": status = " + statusCode);
1869             }
1870             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1871                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1872             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1873         } finally {
1874             res.releaseConnection();
1875         }
1876     }
1877
1878     /**
1879      * Delete non existent item.
1880      *
1881      * @param testName the test name
1882      */
1883     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1884     groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1885     public void deleteNonExistentItem(String testName) {
1886
1887         if (logger.isDebugEnabled()) {
1888             logger.debug(testBanner(testName, CLASS_NAME));
1889         }
1890         // Perform setup.
1891         setupDeleteNonExistent();
1892
1893         // Submit the request to the service and store the response.
1894         OrgAuthorityClient client = new OrgAuthorityClient();
1895         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1896         try {
1897             int statusCode = res.getStatus();
1898
1899             // Check the status code of the response: does it match
1900             // the expected response(s)?
1901             if (logger.isDebugEnabled()) {
1902                 logger.debug(testName + ": status = " + statusCode);
1903             }
1904             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1905                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1906             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1907         } finally {
1908             res.releaseConnection();
1909         }
1910     }
1911
1912     /**
1913      * Delete non existent contact.
1914      *
1915      * @param testName the test name
1916      */
1917     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1918     groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1919     public void deleteNonExistentContact(String testName) {
1920
1921         if (logger.isDebugEnabled()) {
1922             logger.debug(testBanner(testName, CLASS_NAME));
1923         }
1924         // Perform setup.
1925         setupDeleteNonExistent();
1926
1927         // Submit the request to the service and store the response.
1928         OrgAuthorityClient client = new OrgAuthorityClient();
1929         ClientResponse<Response> res =
1930                 client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1931         try {
1932             int statusCode = res.getStatus();
1933
1934             // Check the status code of the response: does it match
1935             // the expected response(s)?
1936             if (logger.isDebugEnabled()) {
1937                 logger.debug(testName + ": status = " + statusCode);
1938             }
1939             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1940                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1941             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1942         } finally {
1943             res.releaseConnection();
1944         }
1945     }
1946
1947     // ---------------------------------------------------------------
1948     // Utility tests : tests of code used in tests above
1949     // ---------------------------------------------------------------
1950     /**
1951      * Tests the code for manually submitting data that is used by several
1952      * of the methods above.
1953      */
1954     @Test(dependsOnMethods = {"create", "read"})
1955     public void testSubmitRequest() {
1956
1957         // Expected status code: 200 OK
1958         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1959
1960         // Submit the request to the service and store the response.
1961         String method = ServiceRequestType.READ.httpMethodName();
1962         String url = getResourceURL(knownResourceId);
1963         int statusCode = submitRequest(method, url);
1964
1965         // Check the status code of the response: does it match
1966         // the expected response(s)?
1967         if (logger.isDebugEnabled()) {
1968             logger.debug("testSubmitRequest: url=" + url
1969                     + " status=" + statusCode);
1970         }
1971         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1972
1973     }
1974
1975     /**
1976      * Test item submit request.
1977      */
1978     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1979     public void testItemSubmitRequest() {
1980
1981         // Expected status code: 200 OK
1982         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1983
1984         // Submit the request to the service and store the response.
1985         String method = ServiceRequestType.READ.httpMethodName();
1986         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1987         int statusCode = submitRequest(method, url);
1988
1989         // Check the status code of the response: does it match
1990         // the expected response(s)?
1991         if (logger.isDebugEnabled()) {
1992             logger.debug("testItemSubmitRequest: url=" + url
1993                     + " status=" + statusCode);
1994         }
1995         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1996
1997     }
1998
1999     /**
2000      * Test contact submit request.
2001      */
2002     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
2003     public void testContactSubmitRequest() {
2004
2005         // Expected status code: 200 OK
2006         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2007
2008         // Submit the request to the service and store the response.
2009         String method = ServiceRequestType.READ.httpMethodName();
2010         String url = getContactResourceURL(knownResourceId,
2011                 knownItemResourceId, knownContactResourceId);
2012         int statusCode = submitRequest(method, url);
2013
2014         // Check the status code of the response: does it match
2015         // the expected response(s)?
2016         if (logger.isDebugEnabled()) {
2017             logger.debug("testItemSubmitRequest: url=" + url
2018                     + " status=" + statusCode);
2019         }
2020         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2021
2022     }
2023
2024     // ---------------------------------------------------------------
2025     // Cleanup of resources created during testing
2026     // ---------------------------------------------------------------
2027     /**
2028      * Deletes all resources created by tests, after all tests have been run.
2029      *
2030      * This cleanup method will always be run, even if one or more tests fail.
2031      * For this reason, it attempts to remove all resources created
2032      * at any point during testing, even if some of those resources
2033      * may be expected to be deleted by certain tests.
2034      */
2035     @AfterClass(alwaysRun = true)
2036     @Override
2037     public void cleanUp() {
2038         String noTest = System.getProperty("noTestCleanup");
2039         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
2040             if (logger.isDebugEnabled()) {
2041                 logger.debug("Skipping Cleanup phase ...");
2042             }
2043             return;
2044         }
2045         if (logger.isDebugEnabled()) {
2046             logger.debug("Cleaning up temporary resources created for testing ...");
2047         }
2048
2049         String parentResourceId;
2050         String itemResourceId;
2051         String contactResourceId;
2052         // Clean up contact resources.
2053         parentResourceId = knownResourceId;
2054         OrgAuthorityClient client = new OrgAuthorityClient();
2055         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
2056             contactResourceId = entry.getKey();
2057             itemResourceId = entry.getValue();
2058             // Note: Any non-success responses from the delete operation
2059             // below are ignored and not reported.
2060             ClientResponse<Response> res =
2061                     client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
2062             res.releaseConnection();
2063         }
2064         // Clean up item resources.
2065         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
2066             itemResourceId = entry.getKey();
2067             parentResourceId = entry.getValue();
2068             // Note: Any non-success responses from the delete operation
2069             // below are ignored and not reported.
2070             ClientResponse<Response> res =
2071                     client.deleteItem(parentResourceId, itemResourceId);
2072             res.releaseConnection();
2073         }
2074         // Clean up parent resources.
2075         super.cleanUp();
2076
2077     }
2078
2079     // ---------------------------------------------------------------
2080     // Utility methods used by tests above
2081     // ---------------------------------------------------------------
2082     /* (non-Javadoc)
2083      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
2084      */
2085     /**
2086      * Gets the item service path component.
2087      *
2088      * @return the item service path component
2089      */
2090     public String getItemServicePathComponent() {
2091         return AuthorityClient.ITEMS;
2092     }
2093
2094     /**
2095      * Gets the contact service path component.
2096      *
2097      * @return the contact service path component
2098      */
2099     public String getContactServicePathComponent() {
2100         return ContactClient.SERVICE_PATH_COMPONENT;
2101     }
2102
2103     /**
2104      * Returns the root URL for the item service.
2105      *
2106      * This URL consists of a base URL for all services, followed by
2107      * a path component for the owning parent, followed by the
2108      * path component for the items.
2109      *
2110      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2111      * parent authority resource of the relevant item resource.
2112      *
2113      * @return The root URL for the item service.
2114      */
2115     protected String getItemServiceRootURL(String parentResourceIdentifier) {
2116         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
2117     }
2118
2119     /**
2120      * Returns the URL of a specific item resource managed by a service, and
2121      * designated by an identifier (such as a universally unique ID, or UUID).
2122      *
2123      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2124      * parent authority resource of the relevant item resource.
2125      *
2126      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2127      * item resource.
2128      *
2129      * @return The URL of a specific item resource managed by a service.
2130      */
2131     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
2132         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
2133     }
2134
2135     /**
2136      * Returns the root URL for the contact service.
2137      *
2138      * This URL consists of a base URL for all services, followed by
2139      * a path component for the owning authority, followed by the
2140      * path component for the owning item, followed by the path component
2141      * for the contact service.
2142      *
2143      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2144      * parent authority resource of the relevant item resource.
2145      *
2146      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2147      * item resource.
2148      *
2149      * @return The root URL for the contact service.
2150      */
2151     protected String getContactServiceRootURL(String parentResourceIdentifier,
2152             String itemResourceIdentifier) {
2153         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
2154                 + getContactServicePathComponent();
2155     }
2156
2157     /**
2158      * Returns the URL of a specific contact resource managed by a service, and
2159      * designated by an identifier (such as a universally unique ID, or UUID).
2160      *
2161      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2162      * parent resource of the relevant item resource.
2163      *
2164      * @param  resourceIdentifier  An identifier (such as a UUID) for an
2165      * item resource.
2166      *
2167      * @return The URL of a specific resource managed by a service.
2168      */
2169     protected String getContactResourceURL(String parentResourceIdentifier,
2170             String itemResourceIdentifier, String contactResourceIdentifier) {
2171         return getContactServiceRootURL(parentResourceIdentifier,
2172                 itemResourceIdentifier) + "/" + contactResourceIdentifier;
2173     }
2174 }