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