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