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