]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
263cb3863f7ebb0077bceff647c324552fc7d884
[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
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32
33 import org.collectionspace.services.OrganizationJAXBSchema;
34 import org.collectionspace.services.client.AbstractCommonListUtils;
35 import org.collectionspace.services.client.AuthorityClient;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.ContactClient;
38 import org.collectionspace.services.client.ContactClientUtils;
39 import org.collectionspace.services.client.PayloadOutputPart;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.contact.AddressGroup;
43 import org.collectionspace.services.contact.AddressGroupList;
44 import org.collectionspace.services.contact.ContactsCommon;
45 import org.collectionspace.services.client.OrganizationClient;
46 import org.collectionspace.services.client.OrgAuthorityClientUtils;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.collectionspace.services.organization.OrgauthoritiesCommon;
49 import org.collectionspace.services.organization.OrganizationsCommon;
50 import org.collectionspace.services.organization.OrgTermGroup;
51 import org.collectionspace.services.organization.OrgTermGroupList;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.testng.Assert;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.Test;
57
58 /**
59  * OrgAuthorityServiceTest, carries out tests against a
60  * deployed and running OrgAuthority Service.
61  *
62  * $LastChangedRevision$
63  * $LastChangedDate$
64  */
65 public class OrgAuthorityServiceTest extends AbstractAuthorityServiceTest<OrgauthoritiesCommon, OrganizationsCommon> {
66
67     /** The logger. */
68     private final String CLASS_NAME = OrgAuthorityServiceTest.class.getName();
69     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70
71     /**
72      * Default constructor.  Used to set the short ID for all tests authority items
73      */
74     public OrgAuthorityServiceTest() {
75         super();
76         TEST_SHORTID = "TestOrg";
77     }
78     
79     @Override
80     public String getServicePathComponent() {
81         return OrganizationClient.SERVICE_PATH_COMPONENT;
82     }
83
84     @Override
85     protected String getServiceName() {
86         return OrganizationClient.SERVICE_NAME;
87     }
88     
89     private final String TEST_ORG_NAME = "Test Org";
90     private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
91     private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
92     private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
93     
94     /** The known item resource short ID. */
95     private String knownItemResourceShortIdentifer = null;
96     
97     /** The known contact resource id. */
98     private String knownContactResourceId = null;
99     
100     /** The all contact resource ids created. */
101     private Map<String, String> allContactResourceIdsCreated = new HashMap<String, String>();
102
103     /**
104      * 
105      */
106     protected void setKnownItemResource(String id, String shortIdentifer) {
107         knownItemResourceId = id;
108         knownItemResourceShortIdentifer = shortIdentifer;
109     }
110
111     /* (non-Javadoc)
112      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
113      */
114     @Override
115     protected CollectionSpaceClient getClientInstance() throws Exception {
116         return new OrganizationClient();
117     }
118
119         @Override
120         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
121         return new OrganizationClient(clientPropertiesFilename);
122         }
123
124     @Override
125     protected PoxPayloadOut createInstance(String identifier) throws Exception {
126         OrganizationClient client = new OrganizationClient();
127         String displayName = "displayName-" + identifier;
128         PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
129                 displayName, identifier, client.getCommonPartName());
130         return multipart;
131     }
132
133     @Override
134     protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) throws Exception {
135         String headerLabel = new OrganizationClient().getItemCommonPartName();
136         
137         String shortId = TEST_SHORTID + identifier;
138         Map<String, String> testOrgMap = new HashMap<String, String>();
139         testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
140         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
141         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
142         
143         List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
144         OrgTermGroup term = new OrgTermGroup();
145         term.setTermDisplayName(TEST_ORG_NAME);
146         term.setTermName(TEST_ORG_NAME);
147         term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
148         terms.add(term);
149
150         return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
151     }
152
153         @Override
154         protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
155                 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
156         }
157     
158     /**
159      * Creates the item in authority.
160      *
161      * @param vcsid the vcsid
162      * @param authRefName the auth ref name
163      * @return the string
164      */
165     private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
166
167         final String testName = "createItemInAuthority";
168         if (logger.isDebugEnabled()) {
169             logger.debug(testName + ":...");
170         }
171         
172         Map<String, String> testOrgMap = new HashMap<String, String>();
173         testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
174         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
175         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
176         
177         List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
178         OrgTermGroup term = new OrgTermGroup();
179         term.setTermDisplayName(TEST_ORG_NAME);
180         term.setTermName(TEST_ORG_NAME);
181         term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
182         terms.add(term);
183         
184         Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
185         List<String> testOrgContactNames = new ArrayList<String>();
186         testOrgContactNames.add("joe@example.org");
187         testOrgContactNames.add("sally@example.org");
188         testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
189
190         String newID = OrgAuthorityClientUtils.createItemInAuthority(
191                 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, (OrganizationClient) client);
192
193         // Store the ID returned from the first item resource created
194         // for additional tests below.
195         if (knownItemResourceId == null) {
196             setKnownItemResource(newID, shortId);
197             if (logger.isDebugEnabled()) {
198                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
199             }
200         }
201
202         // Store the IDs from any item resources created
203         // by tests, along with the IDs of their parents, so these items
204         // can be deleted after all tests have been run.
205         allResourceItemIdsCreated.put(newID, vcsid);
206
207         return newID;
208     }
209
210     /**
211      * Creates the contact.
212      *
213      * @param testName the test name
214      * @throws Exception 
215      */
216     @Test(dataProvider = "testName", groups = {"create"},
217                 dependsOnMethods = {"createItem"})
218     public void createContact(String testName) throws Exception {
219         setupCreate();
220         String newID = createContactInItem(knownResourceId, knownItemResourceId);
221     }
222
223     /**
224      * Creates the contact in item.
225      *
226      * @param parentcsid the parentcsid
227      * @param itemcsid the itemcsid
228      * @return the string
229      * @throws Exception 
230      */
231     private String createContactInItem(String parentcsid, String itemcsid) throws Exception {
232
233         final String testName = "createContactInItem";
234         if (logger.isDebugEnabled()) {
235             logger.debug(getTestBanner(testName, CLASS_NAME));
236         }
237         setupCreate();
238
239         // Submit the request to the service and store the response.
240         OrganizationClient client = new OrganizationClient();
241         String identifier = createIdentifier();
242         PoxPayloadOut multipart =
243                 ContactClientUtils.createContactInstance(parentcsid,
244                 itemcsid, identifier, new ContactClient().getCommonPartName());
245
246         String newID = null;
247         Response res = client.createContact(parentcsid, itemcsid, multipart);
248         try {
249             assertStatusCode(res, testName);
250             newID = OrgAuthorityClientUtils.extractId(res);
251         } finally {
252                 if (res != null) {
253                 res.close();
254             }
255         }
256
257         // Store the ID returned from the first contact resource created
258         // for additional tests below.
259         if (knownContactResourceId == null) {
260             knownContactResourceId = newID;
261             if (logger.isDebugEnabled()) {
262                 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
263             }
264         }
265
266         // Store the IDs from any contact resources created
267         // by tests, along with the IDs of their parent items,
268         // so these items can be deleted after all tests have been run.
269         allContactResourceIdsCreated.put(newID, itemcsid);
270
271         return newID;
272     }
273
274     /**
275      * Creates the contact list.
276      *
277      * @param testName the test name
278      * @throws Exception the exception
279      */
280     @Test(dataProvider = "testName", groups = {"createList"},
281                 dependsOnMethods = {"createItemList"})
282     public void createContactList(String testName) throws Exception {
283         // Add contacts to the initially-created, known item record.
284         for (int j = 0; j < nItemsToCreateInList; j++) {
285             createContact(testName);
286         }
287     }
288
289     // ---------------------------------------------------------------
290     // CRUD tests : READ tests
291     // ---------------------------------------------------------------
292     // Success outcomes
293     /* (non-Javadoc)
294      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
295      */
296     @Override
297 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
298 //    groups = {"read"}, dependsOnGroups = {"create"})
299     public void read(String testName) throws Exception {
300         readInternal(testName, knownResourceId, null);
301     }
302
303     /**
304      * Read by name.
305      *
306      * @param testName the test name
307      * @throws Exception the exception
308      */
309 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310 //    groups = {"read"}, dependsOnGroups = {"create"})
311     public void readByName(String testName) throws Exception {
312         readInternal(testName, null, knownResourceShortIdentifer);
313     }
314
315     protected void readInternal(String testName, String CSID, String shortId) throws Exception {
316         // Perform setup.
317         setupRead();
318
319         // Submit the request to the service and store the response.
320         OrganizationClient client = new OrganizationClient();
321         Response res = null;
322         if (CSID != null) {
323             res = client.read(CSID);
324         } else if (shortId != null) {
325             res = client.readByName(shortId);
326         } else {
327             Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
328         }
329         try {
330             assertStatusCode(res, testName);            
331             //FIXME: remove the following try catch once Aron fixes signatures
332             try {
333                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
334                 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
335                         new OrganizationClient().getCommonPartName(), OrgauthoritiesCommon.class);
336                 if (logger.isDebugEnabled()) {
337                     logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
338                 }
339                 Assert.assertNotNull(orgAuthority);
340             } catch (Exception e) {
341                 throw new RuntimeException(e);
342             }
343         } finally {
344                 if (res != null) {
345                 res.close();
346             }
347         }
348     }
349
350     /**
351      * Read item in Named Auth.
352      * 
353      * TODO Enable this if we really need this - it is a funky case, where we would have
354      * the shortId of the item, but the CSID of the parent authority!? Unlikely.
355      *
356      * @param testName the test name
357      * @throws Exception the exception
358     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
359     groups = {"readItem"}, dependsOnGroups = {"read"})
360     public void readItemInNamedAuth(String testName) throws Exception {
361     readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
362     }
363      */
364     
365     /**
366      * Read named item.
367      *
368      * @param testName the test name
369      * @throws Exception the exception
370      */
371     @Test(dataProvider = "testName", groups = {"readItem"},
372                 dependsOnMethods = {"readItemInNamedAuth"})
373     public void readNamedItem(String testName) throws Exception {
374         readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
375     }
376     
377     /**
378      * Read item in Named Auth.
379      *
380      * @param testName the test name
381      * @throws Exception the exception
382      */
383     @Test(dataProvider = "testName", groups = {"readItem"},
384                 dependsOnMethods = {"readItem"})
385     public void readItemInNamedAuth(String testName) throws Exception {
386         readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
387     }
388
389     /**
390      * Read Named item in Named Auth.
391      *
392      * @param testName the test name
393      * @throws Exception the exception
394      */
395     @Test(dataProvider = "testName", groups = {"readItem"},
396                 dependsOnMethods = {"readItem"})
397     public void readNamedItemInNamedAuth(String testName) throws Exception {
398         readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
399     }
400
401     protected void readItemInternal(String testName,
402             String authCSID, String authShortId, String itemCSID, String itemShortId)
403             throws Exception {
404         // Perform setup.
405         setupRead();
406
407         // Submit the request to the service and store the response.
408         OrganizationClient client = new OrganizationClient();
409         Response res = null;
410         if (authCSID != null) {
411             if (itemCSID != null) {
412                 res = client.readItem(authCSID, itemCSID);
413             } else if (itemShortId != null) {
414                 res = client.readNamedItem(authCSID, itemShortId);
415             } else {
416                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
417             }
418         } else if (authShortId != null) {
419             if (itemCSID != null) {
420                 res = client.readItemInNamedAuthority(authShortId, itemCSID);
421             } else if (itemShortId != null) {
422                 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
423             } else {
424                 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
425             }
426         } else {
427             Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
428         }
429         try {
430             assertStatusCode(res, testName);
431             // Check whether we've received a organization.
432             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
433             OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
434                     client.getItemCommonPartName(), OrganizationsCommon.class);
435             Assert.assertNotNull(organization);
436             boolean showFull = true;
437             if (showFull && logger.isDebugEnabled()) {
438                 logger.debug(testName + ": returned payload:");
439                 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
440             }
441
442             // Check that the organization item is within the expected OrgAuthority.
443             Assert.assertEquals(organization.getInAuthority(), knownResourceId);
444
445             // Verify the number and contents of values in a repeatable field,
446             // as created in the instance record used for testing.
447             List<String> contactNames = organization.getContactNames().getContactName();
448             Assert.assertTrue(contactNames.size() > 0);
449             Assert.assertNotNull(contactNames.get(0));
450
451         } finally {
452                 if (res != null) {
453                 res.close();
454             }
455         }
456     }
457     
458     @Override
459     protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
460         List<String> contactNames = item.getContactNames().getContactName();
461         Assert.assertTrue(contactNames.size() > 0);
462         Assert.assertNotNull(contactNames.get(0));
463     }
464     
465     /**
466      * Verify illegal item display name.
467      *
468      * @param testName the test name
469      * @throws Exception the exception
470      */
471     @Test(dataProvider = "testName")
472     public void verifyIllegalItemDisplayName(String testName) throws Exception {
473         // Perform setup for read.
474         setupRead();
475         //
476         // First read our known resource.
477         //
478         OrganizationClient client = new OrganizationClient();
479         Response res = client.readItem(knownResourceId, knownItemResourceId);
480         OrganizationsCommon organization = null;
481         try {
482             assertStatusCode(res, testName);            
483             // Check whether organization has expected displayName.
484             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
485             organization = (OrganizationsCommon) extractPart(input,
486                     client.getItemCommonPartName(), OrganizationsCommon.class);
487             Assert.assertNotNull(organization);
488         } finally {
489                 if (res != null) {
490                 res.close();
491             }
492         }
493         
494         //
495         // Make an invalid UPDATE request, without a display name
496         //
497         OrgTermGroupList termList = organization.getOrgTermGroupList();
498         Assert.assertNotNull(termList);
499         List<OrgTermGroup> terms = termList.getOrgTermGroup();
500         Assert.assertNotNull(terms);
501         Assert.assertTrue(terms.size() > 0);
502         terms.get(0).setTermDisplayName(null);
503         terms.get(0).setTermName(null);
504         
505         setupUpdateWithInvalidBody(); // we expect a failure
506         // Submit the updated resource to the service and store the response.
507         PoxPayloadOut output = new PoxPayloadOut(OrganizationClient.SERVICE_ITEM_PAYLOAD_NAME);
508         PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
509                         organization);
510         res = client.updateItem(knownResourceId, knownItemResourceId, output);
511         try {
512                 assertStatusCode(res, testName);
513         } finally {
514                 if (res != null) {
515                 res.close();
516             }
517         }
518     }
519
520     /**
521      * Read contact.
522      *
523      * @param testName the test name
524      * @throws Exception the exception
525      */
526     @Test(dataProvider = "testName", groups = {"readItem"},
527                 dependsOnMethods = {"readItem"})
528     public void readContact(String testName) throws Exception {
529         // Perform setup.
530         setupRead();
531
532         // Submit the request to the service and store the response.
533         OrganizationClient client = new OrganizationClient();
534         Response res =client.readContact(knownResourceId, knownItemResourceId,
535                 knownContactResourceId);
536         try {
537             assertStatusCode(res, testName);            
538             // Check whether we've received a contact.
539             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
540             ContactsCommon contact = (ContactsCommon) extractPart(input,
541                     new ContactClient().getCommonPartName(), ContactsCommon.class);
542             Assert.assertNotNull(contact);
543             boolean showFull = true;
544             if (showFull && logger.isDebugEnabled()) {
545                 logger.debug(testName + ": returned payload:");
546                 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
547             }
548             Assert.assertEquals(contact.getInAuthority(), knownResourceId);
549             Assert.assertEquals(contact.getInItem(), knownItemResourceId);
550         } finally {
551                 if (res != null) {
552                 res.close();
553             }
554         }
555     }
556
557     /**
558      * Read contact non existent.
559      *
560      * @param testName the test name
561      * @throws Exception 
562      */
563     @Test(dataProvider = "testName", groups = {"readItem"},
564                 dependsOnMethods = {"readContact"})
565     public void readContactNonExistent(String testName) throws Exception {
566         // Perform setup.
567         setupReadNonExistent();
568
569         // Submit the request to the service and store the response.
570         OrganizationClient client = new OrganizationClient();
571         Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
572         try {
573             int statusCode = res.getStatus();
574
575             // Check the status code of the response: does it match
576             // the expected response(s)?
577             if (logger.isDebugEnabled()) {
578                 logger.debug(testName + ": status = " + statusCode);
579             }
580             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
581                     invalidStatusCodeMessage(testRequestType, statusCode));
582             Assert.assertEquals(statusCode, testExpectedStatusCode);
583         } finally {
584                 if (res != null) {
585                 res.close();
586             }
587         }
588     }
589
590     /**
591      * Read item list.
592      * @throws Exception 
593      */
594     @Override
595 //      @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
596     public void readItemList(String testName) throws Exception {
597         readItemList(knownAuthorityWithItems, null);
598     }
599
600     /**
601      * Read item list by authority name.
602      * @throws Exception 
603      */
604     @Override
605 //    @Test(dataProvider = "testName",
606 //              dependsOnMethods = {"readItem"})
607     public void readItemListByName(String testName) throws Exception {
608         readItemList(null, READITEMS_SHORT_IDENTIFIER);
609     }
610
611     /**
612      * Read item list.
613      *
614      * @param vcsid the vcsid
615      * @param name the name
616      * @throws Exception 
617      */
618     private void readItemList(String vcsid, String name) throws Exception {
619
620         final String testName = "readItemList";
621         // Perform setup.
622         setupReadList();
623
624         // Submit the request to the service and store the response.
625         OrganizationClient client = new OrganizationClient();
626         Response res = null;
627         if (vcsid != null) {
628             res = client.readItemList(vcsid, null, null);
629         } else if (name != null) {
630             res = client.readItemListForNamedAuthority(name, null, null);
631         } else {
632             Assert.fail("readItemList passed null csid and name!");
633         }
634         
635         AbstractCommonList list = null;
636         try {
637             assertStatusCode(res, testName);            
638             list = res.readEntity(AbstractCommonList.class);
639         } finally {
640                 if (res != null) {
641                 res.close();
642             }
643         }
644         
645         List<AbstractCommonList.ListItem> items = list.getListItem();
646         int nItemsReturned = items.size();
647         // There will be 'nItemsToCreateInList'
648         // items created by the createItemList test,
649         // all associated with the same parent resource.
650         int nExpectedItems = nItemsToCreateInList;
651         if (logger.isDebugEnabled()) {
652             logger.debug(testName + ": Expected "
653                     + nExpectedItems + " items; got: " + nItemsReturned);
654         }
655         Assert.assertEquals(nItemsReturned, nExpectedItems);
656
657         for (AbstractCommonList.ListItem item : items) {
658             String value =
659                     AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
660             Assert.assertTrue((null != value), "Item refName is null!");
661             value =
662                     AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
663             Assert.assertTrue((null != value), "Item termDisplayName is null!");
664         }
665         if (logger.isTraceEnabled()) {
666             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
667         }
668     }
669
670     /**
671      * Read contact list.
672      * @throws Exception 
673      */
674     @Test(groups = {"readList"},
675                 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
676     public void readContactList() throws Exception {
677         readContactList(knownResourceId, knownItemResourceId);
678     }
679
680     /**
681      * Read contact list.
682      *
683      * @param parentcsid the parentcsid
684      * @param itemcsid the itemcsid
685      * @throws Exception 
686      */
687     private void readContactList(String parentcsid, String itemcsid) throws Exception {
688         final String testName = "readContactList";
689         // Perform setup.
690         setupReadList();
691
692         // Submit the request to the service and store the response.
693         OrganizationClient client = new OrganizationClient();
694         Response res = client.readContactList(parentcsid, itemcsid);
695         AbstractCommonList list = null;
696         try {
697             assertStatusCode(res, testName);
698             list = res.readEntity(AbstractCommonList.class);
699         } finally {
700                 if (res != null) {
701                 res.close();
702             }
703         }
704
705         List<AbstractCommonList.ListItem> listitems =
706             list.getListItem();
707         int nItemsReturned = listitems.size();
708         // There will be one item created, associated with a
709         // known parent resource, by the createItem test.
710         //
711         // In addition, there will be 'nItemsToCreateInList'
712         // additional items created by the createItemList test,
713         // all associated with the same parent resource.
714         int nExpectedItems = nItemsToCreateInList + 1;
715         if (logger.isDebugEnabled()) {
716             logger.debug(testName + ": Expected "
717                     + nExpectedItems + " items; got: " + nItemsReturned);
718         }
719         Assert.assertEquals(nItemsReturned, nExpectedItems);
720
721         // Optionally output additional data about list members for debugging.
722         boolean iterateThroughList = false;
723         if (iterateThroughList && logger.isDebugEnabled()) {
724             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
725         }
726     }
727
728     /**
729      * Update contact.
730      *
731      * @param testName the test name
732      * @throws Exception the exception
733      */
734     @Test(dataProvider = "testName", groups = {"update"},
735                 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
736     public void updateContact(String testName) throws Exception {
737         // Perform setup.
738         setupUpdate();
739
740         // Retrieve the contents of a resource to update.
741         OrganizationClient client = new OrganizationClient();
742         Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
743         ContactsCommon contact = null;
744         try {
745             assertStatusCode(res, testName);            
746             if (logger.isDebugEnabled()) {
747                 logger.debug("got Contact to update with ID: "
748                         + knownContactResourceId
749                         + " in item: " + knownItemResourceId
750                         + " in parent: " + knownResourceId);
751             }
752             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
753             contact = (ContactsCommon) extractPart(input,
754                     new ContactClient().getCommonPartName(), ContactsCommon.class);
755             Assert.assertNotNull(contact);
756         } finally {
757                 if (res != null) {
758                 res.close();
759             }
760         }
761
762         // Verify the contents of this resource
763         AddressGroupList addressGroupList = contact.getAddressGroupList();
764         Assert.assertNotNull(addressGroupList);
765         List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
766         Assert.assertNotNull(addressGroups);
767         Assert.assertTrue(addressGroups.size() > 0);
768         String addressPlace1 = addressGroups.get(0).getAddressPlace1();
769         Assert.assertNotNull(addressPlace1);
770
771         // Update the contents of this resource.
772         addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
773         contact.setAddressGroupList(addressGroupList);
774         if (logger.isDebugEnabled()) {
775             logger.debug("to be updated Contact");
776             logger.debug(objectAsXmlString(contact,
777                     ContactsCommon.class));
778         }
779
780         // Submit the updated resource to the service and store the response.
781         PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
782         PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
783         commonPart.setLabel(new ContactClient().getCommonPartName());
784
785         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
786         try {
787                 assertStatusCode(res, testName);
788                 // Retrieve the updated resource and verify that its contents exist.
789                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
790                 ContactsCommon updatedContact =
791                         (ContactsCommon) extractPart(input,
792                         new ContactClient().getCommonPartName(), ContactsCommon.class);
793                 Assert.assertNotNull(updatedContact);
794         
795                 // Verify that the updated resource received the correct data.
796                 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
797                         contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
798                         "Data in updated object did not match submitted data.");
799         } finally {
800                 if (res != null) {
801                 res.close();
802             }
803         }
804     }
805
806     /**
807      * Update non existent contact.
808      *
809      * @param testName the test name
810      * @throws Exception the exception
811      */
812     @Test(dataProvider = "testName", groups = {"update"},
813                 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
814     public void updateNonExistentContact(String testName) throws Exception {
815         // Currently a no-op test
816     }
817
818     // ---------------------------------------------------------------
819     // CRUD tests : DELETE tests
820     // ---------------------------------------------------------------
821     // Success outcomes
822     // Note: delete sub-resources in ascending hierarchical order,
823     // before deleting their parents.
824     /**
825      * Delete contact.
826      *
827      * @param testName the test name
828      * @throws Exception the exception
829      */
830     @Test(dataProvider = "testName", groups = {"delete"},
831                 dependsOnMethods = {"updateContact"})
832     public void deleteContact(String testName) throws Exception {
833         // Perform setup.
834         setupDelete();
835
836         if (logger.isDebugEnabled()) {
837             logger.debug("parentcsid =" + knownResourceId
838                     + " itemcsid = " + knownItemResourceId
839                     + " csid = " + knownContactResourceId);
840         }
841
842         // Submit the request to the service and store the response.
843         OrganizationClient client = new OrganizationClient();
844         Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
845         try {
846             assertStatusCode(res, testName);
847         } finally {
848                 if (res != null) {
849                 res.close();
850             }
851         }
852     }
853
854     @Override
855     public void delete(String testName) throws Exception {
856         // Do nothing.  See localDelete().  This ensure proper test order.
857     }
858     
859     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})    
860     public void localDelete(String testName) throws Exception {
861         super.delete(testName);
862     }
863
864     @Override
865     public void deleteItem(String testName) throws Exception {
866         // Do nothing.  We need to wait until after the test "localDelete" gets run.  When it does,
867         // its dependencies will get run first and then we can call the base class' delete method.
868     }
869     
870     @Test(dataProvider = "testName", groups = {"delete"},
871         dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
872     public void localDeleteItem(String testName) throws Exception {
873         super.deleteItem(testName);
874     }    
875     
876     /**
877      * Delete non existent contact.
878      *
879      * @param testName the test name
880      * @throws Exception 
881      */
882     @Test(dataProvider = "testName", groups = {"delete"},
883                 dependsOnMethods = {"deleteContact"})
884     public void deleteNonExistentContact(String testName) throws Exception {
885         // Perform setup.
886         setupDeleteNonExistent();
887
888         // Submit the request to the service and store the response.
889         OrganizationClient client = new OrganizationClient();
890         Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
891         try {
892             assertStatusCode(res, testName);
893         } finally {
894                 if (res != null) {
895                 res.close();
896             }
897         }
898     }
899
900     /**
901      * Test contact submit request.
902      */
903     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
904     public void testContactSubmitRequest() {
905
906         // Expected status code: 200 OK
907         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
908
909         // Submit the request to the service and store the response.
910         String method = ServiceRequestType.READ.httpMethodName();
911         String url = getContactResourceURL(knownResourceId,
912                 knownItemResourceId, knownContactResourceId);
913         int statusCode = submitRequest(method, url);
914
915         // Check the status code of the response: does it match
916         // the expected response(s)?
917         if (logger.isDebugEnabled()) {
918             logger.debug("testContactSubmitRequest: url=" + url
919                     + " status=" + statusCode);
920         }
921         Assert.assertEquals(statusCode, EXPECTED_STATUS);
922
923     }
924
925     // ---------------------------------------------------------------
926     // Cleanup of resources created during testing
927     // ---------------------------------------------------------------
928     /**
929      * Deletes all resources created by tests, after all tests have been run.
930      *
931      * This cleanup method will always be run, even if one or more tests fail.
932      * For this reason, it attempts to remove all resources created
933      * at any point during testing, even if some of those resources
934      * may be expected to be deleted by certain tests.
935      * @throws Exception 
936      */
937     @AfterClass(alwaysRun = true)
938     @Override
939     public void cleanUp() throws Exception {
940         String noTest = System.getProperty("noTestCleanup");
941         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
942             if (logger.isDebugEnabled()) {
943                 logger.debug("Skipping Cleanup phase ...");
944             }
945             return;
946         }
947         if (logger.isDebugEnabled()) {
948             logger.debug("Cleaning up temporary resources created for testing ...");
949         }
950
951         String parentResourceId;
952         String itemResourceId;
953         String contactResourceId;
954         // Clean up contact resources.
955         parentResourceId = knownResourceId;
956         OrganizationClient client = new OrganizationClient();
957         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
958             contactResourceId = entry.getKey();
959             itemResourceId = entry.getValue();
960             // Note: Any non-success responses from the delete operation
961             // below are ignored and not reported.
962             client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
963         }
964
965         // Clean up parent resources.
966         super.cleanUp();
967
968     }
969
970     // ---------------------------------------------------------------
971     // Utility methods used by tests above
972     // ---------------------------------------------------------------
973     /* (non-Javadoc)
974      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
975      */
976     /**
977      * Gets the item service path component.
978      *
979      * @return the item service path component
980      */
981     public String getItemServicePathComponent() {
982         return AuthorityClient.ITEMS;
983     }
984
985     /**
986      * Gets the contact service path component.
987      *
988      * @return the contact service path component
989      */
990     public String getContactServicePathComponent() {
991         return ContactClient.SERVICE_PATH_COMPONENT;
992     }
993
994     /**
995      * Returns the root URL for the item service.
996      *
997      * This URL consists of a base URL for all services, followed by
998      * a path component for the owning parent, followed by the
999      * path component for the items.
1000      *
1001      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1002      * parent authority resource of the relevant item resource.
1003      *
1004      * @return The root URL for the item service.
1005      */
1006     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1007         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1008     }
1009
1010     /**
1011      * Returns the URL of a specific item resource managed by a service, and
1012      * designated by an identifier (such as a universally unique ID, or UUID).
1013      *
1014      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1015      * parent authority resource of the relevant item resource.
1016      *
1017      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1018      * item resource.
1019      *
1020      * @return The URL of a specific item resource managed by a service.
1021      */
1022     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1023         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1024     }
1025
1026     /**
1027      * Returns the root URL for the contact service.
1028      *
1029      * This URL consists of a base URL for all services, followed by
1030      * a path component for the owning authority, followed by the
1031      * path component for the owning item, followed by the path component
1032      * for the contact service.
1033      *
1034      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1035      * parent authority resource of the relevant item resource.
1036      *
1037      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1038      * item resource.
1039      *
1040      * @return The root URL for the contact service.
1041      */
1042     protected String getContactServiceRootURL(String parentResourceIdentifier,
1043             String itemResourceIdentifier) {
1044         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1045                 + getContactServicePathComponent();
1046     }
1047
1048     /**
1049      * Returns the URL of a specific contact resource managed by a service, and
1050      * designated by an identifier (such as a universally unique ID, or UUID).
1051      *
1052      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1053      * parent resource of the relevant item resource.
1054      *
1055      * @param  resourceIdentifier  An identifier (such as a UUID) for an
1056      * item resource.
1057      *
1058      * @return The URL of a specific resource managed by a service.
1059      */
1060     protected String getContactResourceURL(String parentResourceIdentifier,
1061             String itemResourceIdentifier, String contactResourceIdentifier) {
1062         return getContactServiceRootURL(parentResourceIdentifier,
1063                 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1064     }
1065
1066         @Override
1067         public void authorityTests(String testName) {
1068                 // TODO Auto-generated method stub
1069                 
1070         }
1071
1072         @Override
1073         protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1074                             
1075             OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1076             Assert.assertNotNull(termList);
1077             List<OrgTermGroup> terms = termList.getOrgTermGroup();
1078             Assert.assertNotNull(terms);
1079             Assert.assertTrue(terms.size() > 0);
1080             terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1081             terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1082             organizationsCommon.setOrgTermGroupList(termList);
1083
1084             return organizationsCommon;
1085         }
1086
1087         @Override
1088         protected void compareUpdatedItemInstances(OrganizationsCommon original,
1089                         OrganizationsCommon updated,
1090                         boolean compareRevNumbers) throws Exception {
1091             
1092             OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1093             Assert.assertNotNull(originalTermList);
1094             List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1095             Assert.assertNotNull(originalTerms);
1096             Assert.assertTrue(originalTerms.size() > 0);
1097             
1098             OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1099             Assert.assertNotNull(updatedTermList);
1100             List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1101             Assert.assertNotNull(updatedTerms);
1102             Assert.assertTrue(updatedTerms.size() > 0);
1103             
1104             Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1105                 originalTerms.get(0).getTermDisplayName(),
1106                 "Value in updated record did not match submitted data.");
1107             
1108             if (compareRevNumbers == true) {
1109                 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
1110             }
1111         }
1112
1113         @Override
1114         protected PoxPayloadOut createInstance(String commonPartName,
1115                         String identifier) {
1116         String shortId = identifier;
1117         String displayName = "displayName-" + shortId;
1118         //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1119         PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1120                 displayName, shortId, commonPartName);
1121         return result;
1122         }
1123
1124         @Override
1125         protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1126                         String identifier) {
1127         String shortId = identifier;
1128         String displayName = "displayName-" + shortId;
1129         //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1130         PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1131                 displayName, shortId, commonPartName);
1132         return result;
1133         }
1134         
1135     protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1136                 String identifier) {
1137         Map<String, String> nonexOrgMap = new HashMap<String, String>();
1138         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1139         PoxPayloadOut result =
1140                 OrgAuthorityClientUtils.createOrganizationInstance(
1141                 knownResourceRefName,
1142                 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1143         return result;
1144     }
1145
1146         @Override
1147         protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1148                 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1149                 
1150         result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1151         result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1152         
1153                 return result;
1154         }
1155
1156         @Override
1157         protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1158                         OrgauthoritiesCommon updated) throws Exception {
1159         // Verify that the updated resource received the correct data.
1160         Assert.assertEquals(updated.getDisplayName(),
1161                         original.getDisplayName(),
1162                 "Display name in updated object did not match submitted data.");
1163         }
1164 }