]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
db4d0ac4d7526469d5a150a48af64f132ae6e4e8
[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.CollectionSpaceClient;
34 import org.collectionspace.services.client.ContactClient;
35 import org.collectionspace.services.client.ContactClientUtils;
36 import org.collectionspace.services.contact.ContactsCommon;
37 import org.collectionspace.services.contact.ContactsCommonList;
38 import org.collectionspace.services.client.OrgAuthorityClient;
39 import org.collectionspace.services.client.OrgAuthorityClientUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.organization.OrgauthoritiesCommon;
42 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
43 import org.collectionspace.services.organization.OrganizationsCommon;
44 import org.collectionspace.services.organization.OrganizationsCommonList;
45
46 import org.jboss.resteasy.client.ClientResponse;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
49 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.Test;
55
56 /**
57  * OrgAuthorityServiceTest, carries out tests against a
58  * deployed and running OrgAuthority Service.
59  *
60  * $LastChangedRevision: 753 $
61  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
62  */
63 public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
64
65     private final Logger logger =
66         LoggerFactory.getLogger(OrgAuthorityServiceTest.class);
67
68     // Instance variables specific to this test.
69     final String SERVICE_PATH_COMPONENT = "orgauthorities";
70     final String ITEM_SERVICE_PATH_COMPONENT = "items";
71     final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
72     private final String TEST_ORG_SHORTNAME = "Test Org";
73     private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
74     private String knownResourceId = null;
75     private String knownResourceDisplayName = null;
76     private String knownResourceRefName = null;
77     private String knownItemResourceId = null;
78     private String knownContactResourceId = null;
79     private int nItemsToCreateInList = 3;
80     private List<String> allResourceIdsCreated = new ArrayList<String>();
81     private Map<String, String> allItemResourceIdsCreated =
82         new HashMap<String, String>();
83     private Map<String, String> allContactResourceIdsCreated =
84         new HashMap<String, String>();
85     
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88      */
89     @Override
90     protected CollectionSpaceClient getClientInstance() {
91         return new OrgAuthorityClient();
92     }
93     
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
96      */
97     @Override
98         protected AbstractCommonList getAbstractCommonList(
99                         ClientResponse<AbstractCommonList> response) {
100         return response.getEntity(OrganizationsCommonList.class);
101     }
102  
103     // ---------------------------------------------------------------
104     // CRUD tests : CREATE tests
105     // ---------------------------------------------------------------
106     // Success outcomes
107     @Override
108     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
109         groups = {"create"})
110     public void create(String testName) throws Exception {
111
112         // Perform setup, such as initializing the type of service request
113         // (e.g. CREATE, DELETE), its valid and expected status codes, and
114         // its associated HTTP method name (e.g. POST, DELETE).
115         setupCreate(testName);
116
117         // Submit the request to the service and store the response.
118         OrgAuthorityClient client = new OrgAuthorityClient();
119         String identifier = createIdentifier();
120         String displayName = "displayName-" + identifier;
121         String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
122         MultipartOutput multipart = 
123             OrgAuthorityClientUtils.createOrgAuthorityInstance(
124                 displayName, refName, client.getCommonPartName());
125         ClientResponse<Response> res = client.create(multipart);
126         int statusCode = res.getStatus();
127
128         // Check the status code of the response: does it match
129         // the expected response(s)?
130         //
131         // Specifically:
132         // Does it fall within the set of valid status codes?
133         // Does it exactly match the expected status code?
134         if(logger.isDebugEnabled()){
135             logger.debug(testName + ": status = " + statusCode);
136         }
137         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
138                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
139         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
140
141         // Store the refname from the first resource created
142         // for additional tests below.
143         knownResourceRefName = refName;
144
145         String newID = OrgAuthorityClientUtils.extractId(res);
146         // Store the ID returned from the first resource created
147         // for additional tests below.
148         if (knownResourceId == null){
149             knownResourceId = newID;
150             knownResourceDisplayName = displayName;
151             if (logger.isDebugEnabled()) {
152                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
153             }
154         }
155         // Store the IDs from every resource created by tests,
156         // so they can be deleted after tests have been run.
157         allResourceIdsCreated.add(newID);
158     }
159
160     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
161         groups = {"create"}, dependsOnMethods = {"create"})
162     public void createItem(String testName) {
163         setupCreate(testName);
164         String newID = createItemInAuthority(knownResourceId, knownResourceRefName);
165     }
166
167     private String createItemInAuthority(String vcsid, String authRefName) {
168
169         final String testName = "createItemInAuthority";
170         if(logger.isDebugEnabled()){
171             logger.debug(testName + ":...");
172         }
173
174         // Submit the request to the service and store the response.
175         OrgAuthorityClient client = new OrgAuthorityClient();
176         String identifier = createIdentifier();
177         String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, identifier, true);
178         Map<String, String> testOrgMap = new HashMap<String,String>();
179         testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, TEST_ORG_SHORTNAME);
180         testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
181         testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
182         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
183         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
184         testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
185         String newID = OrgAuthorityClientUtils.createItemInAuthority(
186                         vcsid, authRefName, testOrgMap, client);
187         // Store the ID returned from the first item resource created
188         // for additional tests below.
189         if (knownItemResourceId == null){
190             knownItemResourceId = newID;
191             if (logger.isDebugEnabled()) {
192                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
193             }
194         }
195
196         // Store the IDs from any item resources created
197         // by tests, along with the IDs of their parents, so these items
198         // can be deleted after all tests have been run.
199         allItemResourceIdsCreated.put(newID, vcsid);
200
201         return newID;
202     }
203
204     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
205         groups = {"create"}, dependsOnMethods = {"createItem"})
206     public void createContact(String testName) {
207         setupCreate(testName);
208         String newID = createContactInItem(knownResourceId, knownItemResourceId);
209     }
210
211    private String createContactInItem(String parentcsid, String itemcsid) {
212
213         final String testName = "createContactInItem";
214         setupCreate(testName);
215         if(logger.isDebugEnabled()){
216             logger.debug(testName + ":...");
217         }
218
219         // Submit the request to the service and store the response.
220         OrgAuthorityClient client = new OrgAuthorityClient();
221         String identifier = createIdentifier();
222         MultipartOutput multipart =
223             ContactClientUtils.createContactInstance(parentcsid,
224             itemcsid, identifier, new ContactClient().getCommonPartName());
225         ClientResponse<Response> res =
226              client.createContact(parentcsid, itemcsid, multipart);
227         int statusCode = res.getStatus();
228         String newID = OrgAuthorityClientUtils.extractId(res);
229
230         // Check the status code of the response: does it match
231         // the expected response(s)?
232         if(logger.isDebugEnabled()){
233             logger.debug(testName + ": status = " + statusCode);
234         }
235         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238
239         // Store the ID returned from the first contact resource created
240         // for additional tests below.
241         if (knownContactResourceId == null){
242             knownContactResourceId = newID;
243             if (logger.isDebugEnabled()) {
244                 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
245             }
246         }
247
248         // Store the IDs from any contact resources created
249         // by tests, along with the IDs of their parent items,
250         // so these items can be deleted after all tests have been run.
251         allContactResourceIdsCreated.put(newID, itemcsid);
252
253         return newID;
254     }
255
256     // Failure outcomes
257
258     // Placeholders until the three tests below can be uncommented.
259     // See Issue CSPACE-401.
260     @Override
261     public void createWithEmptyEntityBody(String testName) throws Exception {
262     }
263
264     @Override
265     public void createWithMalformedXml(String testName) throws Exception {
266     }
267
268     @Override
269     public void createWithWrongXmlSchema(String testName) throws Exception {
270     }
271
272 /*
273     @Override
274     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
275         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
276     public void createWithEmptyEntityBody(String testName) throws Exception {
277
278     // Perform setup.
279     setupCreateWithEmptyEntityBody(testName);
280
281     // Submit the request to the service and store the response.
282     String method = REQUEST_TYPE.httpMethodName();
283     String url = getServiceRootURL();
284     String mediaType = MediaType.APPLICATION_XML;
285     final String entity = "";
286     int statusCode = submitRequest(method, url, mediaType, entity);
287
288     // Check the status code of the response: does it match
289     // the expected response(s)?
290     if(logger.isDebugEnabled()) {
291         logger.debug(testName + ": url=" + url +
292             " status=" + statusCode);
293      }
294     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
295     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
296     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
297     }
298
299     @Override
300     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
301         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
302     public void createWithMalformedXml(String testName) throws Exception {
303
304     // Perform setup.
305     setupCreateWithMalformedXml(testName);
306
307     // Submit the request to the service and store the response.
308     String method = REQUEST_TYPE.httpMethodName();
309     String url = getServiceRootURL();
310     String mediaType = MediaType.APPLICATION_XML;
311     final String entity = MALFORMED_XML_DATA; // Constant from base class.
312     int statusCode = submitRequest(method, url, mediaType, entity);
313
314     // Check the status code of the response: does it match
315     // the expected response(s)?
316     if(logger.isDebugEnabled()){
317         logger.debug(testName + ": url=" + url +
318             " status=" + statusCode);
319      }
320     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
321     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
322     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323     }
324
325     @Override
326     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
327         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
328     public void createWithWrongXmlSchema(String testName) throws Exception {
329
330     // Perform setup.
331     setupCreateWithWrongXmlSchema(testName);
332
333     // Submit the request to the service and store the response.
334     String method = REQUEST_TYPE.httpMethodName();
335     String url = getServiceRootURL();
336     String mediaType = MediaType.APPLICATION_XML;
337     final String entity = WRONG_XML_SCHEMA_DATA;
338     int statusCode = submitRequest(method, url, mediaType, entity);
339
340     // Check the status code of the response: does it match
341     // the expected response(s)?
342     if(logger.isDebugEnabled()){
343         logger.debug(testName + ": url=" + url +
344             " status=" + statusCode);
345      }
346     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349     }
350 */
351
352     // ---------------------------------------------------------------
353     // CRUD tests : CREATE LIST tests
354     // ---------------------------------------------------------------
355     // Success outcomes
356     @Override
357     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
358         groups = {"createList"}, dependsOnGroups = {"create"})
359     public void createList(String testName) throws Exception {
360         for (int i = 0; i < nItemsToCreateInList; i++) {
361             create(testName);
362         }
363     }
364
365     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
366         groups = {"createList"}, dependsOnMethods = {"createList"})
367     public void createItemList(String testName) throws Exception {
368         // Add items to the initially-created, known parent record.
369         for (int j = 0; j < nItemsToCreateInList; j++) {
370             createItem(testName);
371         }
372     }
373
374     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
375         groups = {"createList"}, dependsOnMethods = {"createItemList"})
376     public void createContactList(String testName) throws Exception {
377         // Add contacts to the initially-created, known item record.
378         for (int j = 0; j < nItemsToCreateInList; j++) {
379             createContact(testName);
380         }
381     }
382
383     // ---------------------------------------------------------------
384     // CRUD tests : READ tests
385     // ---------------------------------------------------------------
386     // Success outcomes
387     @Override
388     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
389         groups = {"read"}, dependsOnGroups = {"create"})
390     public void read(String testName) throws Exception {
391
392         // Perform setup.
393         setupRead();
394
395         // Submit the request to the service and store the response.
396         OrgAuthorityClient client = new OrgAuthorityClient();
397         ClientResponse<MultipartInput> res = client.read(knownResourceId);
398         int statusCode = res.getStatus();
399
400         // Check the status code of the response: does it match
401         // the expected response(s)?
402         if(logger.isDebugEnabled()){
403             logger.debug(testName + ": status = " + statusCode);
404         }
405         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408         //FIXME: remove the following try catch once Aron fixes signatures
409         try {
410             MultipartInput input = (MultipartInput) res.getEntity();
411             OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
412                     client.getCommonPartName(), OrgauthoritiesCommon.class);
413             Assert.assertNotNull(orgAuthority);
414         } catch (Exception e) {
415             throw new RuntimeException(e);
416         }
417     }
418
419     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
420             groups = {"read"}, dependsOnGroups = {"create"})
421         public void readByName(String testName) throws Exception {
422
423         // Perform setup.
424         setupRead();
425
426         // Submit the request to the service and store the response.
427         OrgAuthorityClient client = new OrgAuthorityClient();
428         ClientResponse<MultipartInput> res = client.readByName(knownResourceDisplayName);
429         int statusCode = res.getStatus();
430
431         // Check the status code of the response: does it match
432         // the expected response(s)?
433         if(logger.isDebugEnabled()){
434             logger.debug(testName + ": status = " + statusCode);
435         }
436         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
437                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
438         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
439         //FIXME: remove the following try catch once Aron fixes signatures
440         try {
441             MultipartInput input = (MultipartInput) res.getEntity();
442             OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
443                     new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
444             Assert.assertNotNull(orgAuthority);
445         } catch (Exception e) {
446             throw new RuntimeException(e);
447         }
448     }
449
450 /*
451     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
452         groups = {"read"}, dependsOnMethods = {"read"})
453     public void readByName(String testName) throws Exception {
454
455         // Perform setup.
456         setupRead();
457
458         // Submit the request to the service and store the response.
459         ClientResponse<MultipartInput> res = client.read(knownResourceId);
460         int statusCode = res.getStatus();
461
462         // Check the status code of the response: does it match
463         // the expected response(s)?
464         if(logger.isDebugEnabled()){
465             logger.debug(testName + ": status = " + statusCode);
466         }
467         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
468                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
469         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470         //FIXME: remove the following try catch once Aron fixes signatures
471         try {
472             MultipartInput input = (MultipartInput) res.getEntity();
473             OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
474                     client.getCommonPartName(), OrgauthoritiesCommon.class);
475             Assert.assertNotNull(orgAuthority);
476         } catch (Exception e) {
477             throw new RuntimeException(e);
478         }
479     }
480 */
481
482     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
483         groups = {"read"}, dependsOnMethods = {"read"})
484     public void readItem(String testName) throws Exception {
485
486         // Perform setup.
487         setupRead(testName);
488
489         // Submit the request to the service and store the response.
490         OrgAuthorityClient client = new OrgAuthorityClient();
491         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
492         int statusCode = res.getStatus();
493
494         // Check the status code of the response: does it match
495         // the expected response(s)?
496         if(logger.isDebugEnabled()){
497             logger.debug(testName + ": status = " + statusCode);
498         }
499         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
502
503         // Check whether we've received a organization.
504         MultipartInput input = (MultipartInput) res.getEntity();
505         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
506                 client.getItemCommonPartName(), OrganizationsCommon.class);
507         Assert.assertNotNull(organization);
508         boolean showFull = true;
509         if(showFull && logger.isDebugEnabled()){
510             logger.debug(testName + ": returned payload:");
511             logger.debug(objectAsXmlString(organization,
512                     OrganizationsCommon.class));
513         }
514         Assert.assertEquals(organization.getInAuthority(), knownResourceId);
515     }
516
517     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
518             dependsOnMethods = {"readItem", "updateItem"})
519     public void verifyItemDisplayName(String testName) throws Exception {
520
521         // Perform setup.
522         setupUpdate(testName);
523
524         // Submit the request to the service and store the response.
525         OrgAuthorityClient client = new OrgAuthorityClient();
526         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
527         int statusCode = res.getStatus();
528
529         // Check the status code of the response: does it match
530         // the expected response(s)?
531         if(logger.isDebugEnabled()){
532             logger.debug(testName + ": status = " + statusCode);
533         }
534         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
535                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
536         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
537
538         // Check whether organization has expected displayName.
539         MultipartInput input = (MultipartInput) res.getEntity();
540         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
541                 client.getItemCommonPartName(), OrganizationsCommon.class);
542         Assert.assertNotNull(organization);
543         String displayName = organization.getDisplayName();
544         // Make sure displayName matches computed form
545         String expectedDisplayName = 
546             OrgAuthorityClientUtils.prepareDefaultDisplayName(
547                 TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
548         Assert.assertNotNull(displayName, expectedDisplayName);
549         
550         // Update the shortName and verify the computed name is updated.
551         organization.setCsid(null);
552         organization.setDisplayNameComputed(true);
553         organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
554         expectedDisplayName = 
555             OrgAuthorityClientUtils.prepareDefaultDisplayName(
556                 "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
557
558         // Submit the updated resource to the service and store the response.
559         MultipartOutput output = new MultipartOutput();
560         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
561         commonPart.getHeaders().add("label", client.getItemCommonPartName());
562         res = client.updateItem(knownResourceId, knownItemResourceId, output);
563         statusCode = res.getStatus();
564
565         // Check the status code of the response: does it match the expected response(s)?
566         if(logger.isDebugEnabled()){
567             logger.debug("updateItem: status = " + statusCode);
568         }
569         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
570                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
571         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
572
573         // Retrieve the updated resource and verify that its contents exist.
574         input = (MultipartInput) res.getEntity();
575         OrganizationsCommon updatedOrganization =
576                 (OrganizationsCommon) extractPart(input,
577                         client.getItemCommonPartName(), OrganizationsCommon.class);
578         Assert.assertNotNull(updatedOrganization);
579
580         // Verify that the updated resource received the correct data.
581         Assert.assertEquals(updatedOrganization.getShortName(), organization.getShortName(),
582             "Updated ShortName in Organization did not match submitted data.");
583         // Verify that the updated resource computes the right displayName.
584         Assert.assertEquals(updatedOrganization.getDisplayName(), expectedDisplayName,
585             "Updated ShortName in Organization not reflected in computed DisplayName.");
586
587         // Now Update the displayName, not computed and verify the computed name is overriden.
588         organization.setDisplayNameComputed(false);
589         expectedDisplayName = "TestName";
590         organization.setDisplayName(expectedDisplayName);
591
592         // Submit the updated resource to the service and store the response.
593         output = new MultipartOutput();
594         commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
595         commonPart.getHeaders().add("label", client.getItemCommonPartName());
596         res = client.updateItem(knownResourceId, knownItemResourceId, output);
597         statusCode = res.getStatus();
598
599         // Check the status code of the response: does it match the expected response(s)?
600         if(logger.isDebugEnabled()){
601             logger.debug("updateItem: status = " + statusCode);
602         }
603         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
604                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
605         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
606
607         // Retrieve the updated resource and verify that its contents exist.
608         input = (MultipartInput) res.getEntity();
609         updatedOrganization =
610                 (OrganizationsCommon) extractPart(input,
611                         client.getItemCommonPartName(), OrganizationsCommon.class);
612         Assert.assertNotNull(updatedOrganization);
613
614         // Verify that the updated resource received the correct data.
615         Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
616                 "Updated displayNameComputed in Organization did not match submitted data.");
617         // Verify that the updated resource computes the right displayName.
618         Assert.assertEquals(updatedOrganization.getDisplayName(),
619                         expectedDisplayName,
620                 "Updated DisplayName (not computed) in Organization not stored.");
621     }
622
623     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
624             dependsOnMethods = {"verifyItemDisplayName"})
625     public void verifyIllegalItemDisplayName(String testName) throws Exception {
626
627         // Perform setup.
628         setupUpdateWithWrongXmlSchema(testName);
629
630         // Submit the request to the service and store the response.
631         OrgAuthorityClient client = new OrgAuthorityClient();
632         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
633         int statusCode = res.getStatus();
634
635         // Check the status code of the response: does it match
636         // the expected response(s)?
637         if(logger.isDebugEnabled()){
638             logger.debug(testName + ": status = " + statusCode);
639         }
640         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
641                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
642         Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
643
644         // Check whether organization has expected displayName.
645         MultipartInput input = (MultipartInput) res.getEntity();
646         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
647                 client.getItemCommonPartName(), OrganizationsCommon.class);
648         Assert.assertNotNull(organization);
649         // Try to Update with computed false and no displayName
650         organization.setDisplayNameComputed(false);
651         organization.setDisplayName(null);
652
653         // Submit the updated resource to the service and store the response.
654         MultipartOutput output = new MultipartOutput();
655         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
656         commonPart.getHeaders().add("label", client.getItemCommonPartName());
657         res = client.updateItem(knownResourceId, knownItemResourceId, output);
658         statusCode = res.getStatus();
659
660         // Check the status code of the response: does it match the expected response(s)?
661         if(logger.isDebugEnabled()){
662             logger.debug("updateItem: status = " + statusCode);
663         }
664         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
665                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
666         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
667     }
668
669     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
670         groups = {"read"}, dependsOnMethods = {"readItem"})
671     public void readContact(String testName) throws Exception {
672
673         // Perform setup.
674         setupRead(testName);
675
676         // Submit the request to the service and store the response.
677         OrgAuthorityClient client = new OrgAuthorityClient();
678         ClientResponse<MultipartInput> res =
679             client.readContact(knownResourceId, knownItemResourceId,
680             knownContactResourceId);
681         int statusCode = res.getStatus();
682
683         // Check the status code of the response: does it match
684         // the expected response(s)?
685         if(logger.isDebugEnabled()){
686             logger.debug(testName + ": status = " + statusCode);
687         }
688         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
689                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
690         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
691
692         // Check whether we've received a contact.
693         MultipartInput input = (MultipartInput) res.getEntity();
694         ContactsCommon contact = (ContactsCommon) extractPart(input,
695                 new ContactClient().getCommonPartName(), ContactsCommon.class);
696         Assert.assertNotNull(contact);
697         boolean showFull = true;
698         if(showFull && logger.isDebugEnabled()){
699             logger.debug(testName + ": returned payload:");
700             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
701         }
702         Assert.assertEquals(contact.getInAuthority(), knownResourceId);
703         Assert.assertEquals(contact.getInItem(), knownItemResourceId);
704
705     }
706
707     // Failure outcomes
708     @Override
709     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
710         groups = {"read"}, dependsOnMethods = {"read"})
711     public void readNonExistent(String testName) {
712
713         // Perform setup.
714         setupReadNonExistent(testName);
715
716         // Submit the request to the service and store the response.
717         OrgAuthorityClient client = new OrgAuthorityClient();
718         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
719         int statusCode = res.getStatus();
720
721         // Check the status code of the response: does it match
722         // the expected response(s)?
723         if(logger.isDebugEnabled()){
724             logger.debug(testName + ": status = " + statusCode);
725         }
726         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
727                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
728         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
729     }
730
731     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
732         groups = {"read"}, dependsOnMethods = {"readItem"})
733     public void readItemNonExistent(String testName) {
734
735         // Perform setup.
736         setupReadNonExistent(testName);
737
738         // Submit the request to the service and store the response.
739         OrgAuthorityClient client = new OrgAuthorityClient();
740         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
741         int statusCode = res.getStatus();
742
743         // Check the status code of the response: does it match
744         // the expected response(s)?
745         if(logger.isDebugEnabled()){
746             logger.debug(testName + ": status = " + statusCode);
747         }
748         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
749                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
750         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
751     }
752
753     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
754         groups = {"read"}, dependsOnMethods = {"readContact"})
755     public void readContactNonExistent(String testName) {
756
757         // Perform setup.
758         setupReadNonExistent(testName);
759
760         // Submit the request to the service and store the response.
761         OrgAuthorityClient client = new OrgAuthorityClient();
762         ClientResponse<MultipartInput> res =
763             client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
764         int statusCode = res.getStatus();
765
766         // Check the status code of the response: does it match
767         // the expected response(s)?
768         if(logger.isDebugEnabled()){
769             logger.debug(testName + ": status = " + statusCode);
770         }
771         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
772                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
773         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
774     }
775
776     // ---------------------------------------------------------------
777     // CRUD tests : READ_LIST tests
778     // ---------------------------------------------------------------
779     // Success outcomes
780
781     @Override
782     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
783         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
784     public void readList(String testName) throws Exception {
785
786         // Perform setup.
787         setupReadList(testName);
788
789         // Submit the request to the service and store the response.
790         OrgAuthorityClient client = new OrgAuthorityClient();
791         ClientResponse<OrgauthoritiesCommonList> res = client.readList();
792         OrgauthoritiesCommonList list = res.getEntity();
793         int statusCode = res.getStatus();
794
795         // Check the status code of the response: does it match
796         // the expected response(s)?
797         if(logger.isDebugEnabled()){
798             logger.debug(testName + ": status = " + statusCode);
799         }
800         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
801                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
802         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803
804         // Optionally output additional data about list members for debugging.
805         boolean iterateThroughList = false;
806         if (iterateThroughList && logger.isDebugEnabled()) {
807             List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
808                     list.getOrgauthorityListItem();
809             int i = 0;
810             for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
811                 String csid = item.getCsid();
812                 logger.debug(testName + ": list-item[" + i + "] csid=" +
813                         csid);
814                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
815                         item.getDisplayName());
816                 logger.debug(testName + ": list-item[" + i + "] URI=" +
817                         item.getUri());
818                 readItemList(csid, null);
819                 i++;
820             }
821         }
822     }
823
824     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
825     public void readItemList() {
826         readItemList(knownResourceId, null);
827     }
828
829     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
830     public void readItemListByAuthorityName() {
831         readItemList(null, knownResourceDisplayName);
832     }
833
834     private void readItemList(String vcsid, String name) {
835
836         final String testName = "readItemList";
837
838         // Perform setup.
839         setupReadList(testName);
840
841         // Submit the request to the service and store the response.
842         OrgAuthorityClient client = new OrgAuthorityClient();
843         ClientResponse<OrganizationsCommonList> res = null;
844         if(vcsid!= null) {
845                 res = client.readItemList(vcsid);
846         } else if(name!= null) {
847                 res = client.readItemListForNamedAuthority(name);
848         } else {
849                 Assert.fail("readItemList passed null csid and name!");
850         }
851         OrganizationsCommonList list = res.getEntity();
852         int statusCode = res.getStatus();
853
854         // Check the status code of the response: does it match
855         // the expected response(s)?
856         if(logger.isDebugEnabled()){
857             logger.debug(testName + ": status = " + statusCode);
858         }
859         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
860                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
861         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
862
863         List<OrganizationsCommonList.OrganizationListItem> items =
864             list.getOrganizationListItem();
865         int nItemsReturned = items.size();
866         // There will be one item created, associated with a
867         // known parent resource, by the createItem test.
868         //
869         // In addition, there will be 'nItemsToCreateInList'
870         // additional items created by the createItemList test,
871         // all associated with the same parent resource.
872         int nExpectedItems = nItemsToCreateInList + 1;
873         if(logger.isDebugEnabled()){
874             logger.debug(testName + ": Expected "
875                         + nExpectedItems +" items; got: "+nItemsReturned);
876         }
877         Assert.assertEquals(nItemsReturned, nExpectedItems);
878
879         int i = 0;
880         for (OrganizationsCommonList.OrganizationListItem item : items) {
881                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
882                 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
883                 // Optionally output additional data about list members for debugging.
884                 boolean showDetails = true;
885                 if (showDetails && logger.isDebugEnabled()) {
886                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
887                         item.getCsid());
888                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
889                         item.getRefName());
890                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
891                         item.getDisplayName());
892                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
893                         item.getUri());
894             }
895             i++;
896         }
897     }
898
899     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
900     public void readContactList() {
901         readContactList(knownResourceId, knownItemResourceId);
902     }
903
904     private void readContactList(String parentcsid, String itemcsid) {
905         final String testName = "readContactList";
906
907         // Perform setup.
908         setupReadList(testName);
909
910         // Submit the request to the service and store the response.
911         OrgAuthorityClient client = new OrgAuthorityClient();
912         ClientResponse<ContactsCommonList> res =
913                 client.readContactList(parentcsid, itemcsid);
914         ContactsCommonList list = res.getEntity();
915         int statusCode = res.getStatus();
916
917         // Check the status code of the response: does it match
918         // the expected response(s)?
919         if(logger.isDebugEnabled()){
920             logger.debug(testName + ": status = " + statusCode);
921         }
922         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
923                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
924         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
925
926         List<ContactsCommonList.ContactListItem> listitems =
927             list.getContactListItem();
928         int nItemsReturned = listitems.size();
929         // There will be one item created, associated with a
930         // known parent resource, by the createItem test.
931         //
932         // In addition, there will be 'nItemsToCreateInList'
933         // additional items created by the createItemList test,
934         // all associated with the same parent resource.
935         int nExpectedItems = nItemsToCreateInList + 1;
936         if(logger.isDebugEnabled()){
937             logger.debug(testName + ": Expected "
938                         + nExpectedItems +" items; got: "+nItemsReturned);
939         }
940         Assert.assertEquals(nItemsReturned, nExpectedItems);
941
942         int i = 0;
943         for (ContactsCommonList.ContactListItem listitem : listitems) {
944                 // Optionally output additional data about list members for debugging.
945                 boolean showDetails = false;
946                 if (showDetails && logger.isDebugEnabled()) {
947                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
948                         listitem.getCsid());
949                 logger.debug("  " + testName + ": list-item[" + i + "] addressPlace=" +
950                         listitem.getAddressPlace());
951                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
952                         listitem.getUri());
953             }
954             i++;
955         }
956     }
957
958     // Failure outcomes
959     // None at present.
960     
961     // ---------------------------------------------------------------
962     // CRUD tests : UPDATE tests
963     // ---------------------------------------------------------------
964     // Success outcomes
965     @Override
966     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
967         groups = {"update"}, dependsOnGroups = {"read", "readList"})
968     public void update(String testName) throws Exception {
969
970         // Perform setup.
971         setupUpdate(testName);
972
973         // Retrieve the contents of a resource to update.
974         OrgAuthorityClient client = new OrgAuthorityClient();
975         ClientResponse<MultipartInput> res =
976                 client.read(knownResourceId);
977         if(logger.isDebugEnabled()){
978             logger.debug(testName + ": read status = " + res.getStatus());
979         }
980         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
981
982         if(logger.isDebugEnabled()){
983             logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
984         }
985         MultipartInput input = (MultipartInput) res.getEntity();
986         OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
987                 client.getCommonPartName(), OrgauthoritiesCommon.class);
988         Assert.assertNotNull(orgAuthority);
989
990         // Update the contents of this resource.
991         orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
992         orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
993         if(logger.isDebugEnabled()){
994             logger.debug("to be updated OrgAuthority");
995             logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
996         }
997
998         // Submit the updated resource to the service and store the response.
999         MultipartOutput output = new MultipartOutput();
1000         OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
1001         commonPart.getHeaders().add("label", client.getCommonPartName());
1002         res = client.update(knownResourceId, output);
1003         int statusCode = res.getStatus();
1004
1005         // Check the status code of the response: does it match the expected response(s)?
1006         if(logger.isDebugEnabled()){
1007             logger.debug(testName + ": status = " + statusCode);
1008         }
1009         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1010                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1011         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1012
1013         // Retrieve the updated resource and verify that its contents exist.
1014         input = (MultipartInput) res.getEntity();
1015         OrgauthoritiesCommon updatedOrgAuthority =
1016                 (OrgauthoritiesCommon) extractPart(input,
1017                         client.getCommonPartName(), OrgauthoritiesCommon.class);
1018         Assert.assertNotNull(updatedOrgAuthority);
1019
1020         // Verify that the updated resource received the correct data.
1021         Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
1022                 orgAuthority.getDisplayName(),
1023                 "Data in updated object did not match submitted data.");
1024     }
1025
1026     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1027         groups = {"update"}, dependsOnMethods = {"update"})
1028     public void updateItem(String testName) throws Exception {
1029
1030         // Perform setup.
1031         setupUpdate(testName);
1032
1033         // Retrieve the contents of a resource to update.
1034         OrgAuthorityClient client = new OrgAuthorityClient();
1035         ClientResponse<MultipartInput> res =
1036                 client.readItem(knownResourceId, knownItemResourceId);
1037         if(logger.isDebugEnabled()){
1038             logger.debug(testName + ": read status = " + res.getStatus());
1039         }
1040         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1041
1042         if(logger.isDebugEnabled()){
1043             logger.debug("got Organization to update with ID: " +
1044                 knownItemResourceId +
1045                 " in OrgAuthority: " + knownResourceId );
1046         }
1047         MultipartInput input = (MultipartInput) res.getEntity();
1048         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
1049                 client.getItemCommonPartName(), OrganizationsCommon.class);
1050         Assert.assertNotNull(organization);
1051
1052         // Update the contents of this resource.
1053         organization.setCsid(null);
1054         organization.setShortName("updated-" + organization.getShortName());
1055         if(logger.isDebugEnabled()){
1056             logger.debug("to be updated Organization");
1057             logger.debug(objectAsXmlString(organization,
1058                 OrganizationsCommon.class));
1059         }
1060
1061         // Submit the updated resource to the service and store the response.
1062         MultipartOutput output = new MultipartOutput();
1063         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
1064         commonPart.getHeaders().add("label", client.getItemCommonPartName());
1065         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1066         int statusCode = res.getStatus();
1067
1068         // Check the status code of the response: does it match the expected response(s)?
1069         if(logger.isDebugEnabled()){
1070             logger.debug(testName + ": status = " + statusCode);
1071         }
1072         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1073                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1074         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1075
1076         // Retrieve the updated resource and verify that its contents exist.
1077         input = (MultipartInput) res.getEntity();
1078         OrganizationsCommon updatedOrganization =
1079                 (OrganizationsCommon) extractPart(input,
1080                         client.getItemCommonPartName(), OrganizationsCommon.class);
1081         Assert.assertNotNull(updatedOrganization);
1082
1083         // Verify that the updated resource received the correct data.
1084         Assert.assertEquals(updatedOrganization.getShortName(),
1085                 organization.getShortName(),
1086                 "Data in updated Organization did not match submitted data.");
1087     }
1088
1089     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1090         groups = {"update"}, dependsOnMethods = {"updateItem"})
1091     public void updateContact(String testName) throws Exception {
1092
1093         // Perform setup.
1094         setupUpdate(testName);
1095
1096         // Retrieve the contents of a resource to update.
1097         OrgAuthorityClient client = new OrgAuthorityClient();
1098         ClientResponse<MultipartInput> res =
1099                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1100         if(logger.isDebugEnabled()){
1101             logger.debug(testName + ": read status = " + res.getStatus());
1102         }
1103         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1104
1105         if(logger.isDebugEnabled()){
1106             logger.debug("got Contact to update with ID: " +
1107                 knownContactResourceId +
1108                 " in item: " + knownItemResourceId +
1109                 " in parent: " + knownResourceId );
1110         }
1111         MultipartInput input = (MultipartInput) res.getEntity();
1112         ContactsCommon contact = (ContactsCommon) extractPart(input,
1113                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1114         Assert.assertNotNull(contact);
1115
1116         // Update the contents of this resource.
1117         contact.setAddressPlace("updated-" + contact.getAddressPlace());
1118         if(logger.isDebugEnabled()){
1119             logger.debug("to be updated Contact");
1120             logger.debug(objectAsXmlString(contact,
1121                 ContactsCommon.class));
1122         }
1123
1124         // Submit the updated resource to the service and store the response.
1125         MultipartOutput output = new MultipartOutput();
1126         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
1127         commonPart.getHeaders().add("label", new ContactClient().getCommonPartName());
1128         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1129         int statusCode = res.getStatus();
1130
1131         // Check the status code of the response: does it match the expected response(s)?
1132         if(logger.isDebugEnabled()){
1133             logger.debug(testName + ": status = " + statusCode);
1134         }
1135         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1136                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1137         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1138
1139         // Retrieve the updated resource and verify that its contents exist.
1140         input = (MultipartInput) res.getEntity();
1141         ContactsCommon updatedContact =
1142                 (ContactsCommon) extractPart(input,
1143                         new ContactClient().getCommonPartName(), ContactsCommon.class);
1144         Assert.assertNotNull(updatedContact);
1145
1146         // Verify that the updated resource received the correct data.
1147         Assert.assertEquals(updatedContact.getAddressPlace(),
1148                 contact.getAddressPlace(),
1149                 "Data in updated Contact did not match submitted data.");
1150     }
1151
1152     // Failure outcomes
1153     // Placeholders until the three tests below can be uncommented.
1154     // See Issue CSPACE-401.
1155     @Override
1156     public void updateWithEmptyEntityBody(String testName) throws Exception {
1157     }
1158
1159     @Override
1160     public void updateWithMalformedXml(String testName) throws Exception {
1161     }
1162
1163     @Override
1164     public void updateWithWrongXmlSchema(String testName) throws Exception {
1165     }
1166
1167 /*
1168     @Override
1169     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1170         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1171     public void updateWithEmptyEntityBody(String testName) throws Exception {
1172
1173     // Perform setup.
1174     setupUpdateWithEmptyEntityBody(testName);
1175
1176     // Submit the request to the service and store the response.
1177     String method = REQUEST_TYPE.httpMethodName();
1178     String url = getResourceURL(knownResourceId);
1179     String mediaType = MediaType.APPLICATION_XML;
1180     final String entity = "";
1181     int statusCode = submitRequest(method, url, mediaType, entity);
1182
1183     // Check the status code of the response: does it match
1184     // the expected response(s)?
1185     if(logger.isDebugEnabled()){
1186         logger.debug(testName + ": url=" + url +
1187             " status=" + statusCode);
1188      }
1189     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1190     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1191     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1192     }
1193
1194     @Override
1195     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1196         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1197     public void updateWithMalformedXml(String testName) throws Exception {
1198
1199     // Perform setup.
1200     setupUpdateWithMalformedXml(testName);
1201
1202     // Submit the request to the service and store the response.
1203     String method = REQUEST_TYPE.httpMethodName();
1204     String url = getResourceURL(knownResourceId);
1205     String mediaType = MediaType.APPLICATION_XML;
1206     final String entity = MALFORMED_XML_DATA;
1207     int statusCode = submitRequest(method, url, mediaType, entity);
1208
1209     // Check the status code of the response: does it match
1210     // the expected response(s)?
1211     if(logger.isDebugEnabled()){
1212         logger.debug(testName + ": url=" + url +
1213            " status=" + statusCode);
1214      }
1215     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1216     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1217     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1218     }
1219
1220     @Override
1221     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1222         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1223     public void updateWithWrongXmlSchema(String testName) throws Exception {
1224
1225     // Perform setup.
1226     setupUpdateWithWrongXmlSchema(testName);
1227
1228     // Submit the request to the service and store the response.
1229     String method = REQUEST_TYPE.httpMethodName();
1230     String url = getResourceURL(knownResourceId);
1231     String mediaType = MediaType.APPLICATION_XML;
1232     final String entity = WRONG_XML_SCHEMA_DATA;
1233     int statusCode = submitRequest(method, url, mediaType, entity);
1234
1235     // Check the status code of the response: does it match
1236     // the expected response(s)?
1237     if(logger.isDebugEnabled()){
1238         logger.debug("updateWithWrongXmlSchema: url=" + url +
1239             " status=" + statusCode);
1240      }
1241     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1242     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1243     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1244     }
1245 */
1246
1247     @Override
1248     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1249         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1250     public void updateNonExistent(String testName) throws Exception {
1251
1252         // Perform setup.
1253         setupUpdateNonExistent(testName);
1254
1255         // Submit the request to the service and store the response.
1256         // Note: The ID used in this 'create' call may be arbitrary.
1257         // The only relevant ID may be the one used in update(), below.
1258         OrgAuthorityClient client = new OrgAuthorityClient();
1259         MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
1260         ClientResponse<MultipartInput> res =
1261                 client.update(NON_EXISTENT_ID, multipart);
1262         int statusCode = res.getStatus();
1263
1264         // Check the status code of the response: does it match
1265         // the expected response(s)?
1266         if(logger.isDebugEnabled()){
1267             logger.debug(testName + ": status = " + statusCode);
1268         }
1269         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1270                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1271         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1272     }
1273
1274     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1275         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1276     public void updateNonExistentItem(String testName) throws Exception {
1277
1278         // Perform setup.
1279         setupUpdateNonExistent(testName);
1280
1281         // Submit the request to the service and store the response.
1282         // Note: The ID(s) used when creating the request payload may be arbitrary.
1283         // The only relevant ID may be the one used in update(), below.
1284         OrgAuthorityClient client = new OrgAuthorityClient();
1285         Map<String, String> nonexOrgMap = new HashMap<String,String>();
1286         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
1287         String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
1288         MultipartOutput multipart = 
1289                 OrgAuthorityClientUtils.createOrganizationInstance(
1290                         NON_EXISTENT_ID, refName,
1291                         nonexOrgMap, client.getItemCommonPartName() );
1292         ClientResponse<MultipartInput> res =
1293                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1294         int statusCode = res.getStatus();
1295
1296         // Check the status code of the response: does it match
1297         // the expected response(s)?
1298         if(logger.isDebugEnabled()){
1299             logger.debug(testName + ": status = " + statusCode);
1300         }
1301         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1302                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1303         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1304     }
1305
1306     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1307         groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1308     public void updateNonExistentContact(String testName) throws Exception {
1309         // Currently a no-op test
1310     }
1311
1312     // ---------------------------------------------------------------
1313     // CRUD tests : DELETE tests
1314     // ---------------------------------------------------------------
1315     // Success outcomes
1316
1317     // Note: delete sub-resources in ascending hierarchical order,
1318     // before deleting their parents.
1319
1320     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1321         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1322     public void deleteContact(String testName) throws Exception {
1323
1324         // Perform setup.
1325         setupDelete(testName);
1326
1327          if(logger.isDebugEnabled()){
1328             logger.debug("parentcsid =" + knownResourceId +
1329                 " itemcsid = " + knownItemResourceId +
1330                 " csid = " + knownContactResourceId);
1331         }
1332
1333         // Submit the request to the service and store the response.
1334         OrgAuthorityClient client = new OrgAuthorityClient();
1335         ClientResponse<Response> res =
1336             client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1337         int statusCode = res.getStatus();
1338
1339         // Check the status code of the response: does it match
1340         // the expected response(s)?
1341         if(logger.isDebugEnabled()){
1342             logger.debug(testName + ": status = " + statusCode);
1343         }
1344         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1345                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1346         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1347     }
1348
1349    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1350         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1351     public void deleteItem(String testName) throws Exception {
1352
1353         // Perform setup.
1354         setupDelete(testName);
1355
1356         if(logger.isDebugEnabled()){
1357             logger.debug("parentcsid =" + knownResourceId +
1358                 " itemcsid = " + knownItemResourceId);
1359         }
1360
1361         // Submit the request to the service and store the response.
1362         OrgAuthorityClient client = new OrgAuthorityClient();
1363         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1364         int statusCode = res.getStatus();
1365
1366         // Check the status code of the response: does it match
1367         // the expected response(s)?
1368         if(logger.isDebugEnabled()){
1369             logger.debug(testName + ": status = " + statusCode);
1370         }
1371         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1372                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1373         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1374     }
1375
1376     @Override
1377     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1378         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1379     public void delete(String testName) throws Exception {
1380
1381         // Perform setup.
1382         setupDelete(testName);
1383
1384         if(logger.isDebugEnabled()){
1385             logger.debug("parentcsid =" + knownResourceId);
1386         }
1387
1388         // Submit the request to the service and store the response.
1389         OrgAuthorityClient client = new OrgAuthorityClient();
1390         ClientResponse<Response> res = client.delete(knownResourceId);
1391         int statusCode = res.getStatus();
1392
1393         // Check the status code of the response: does it match
1394         // the expected response(s)?
1395         if(logger.isDebugEnabled()){
1396             logger.debug(testName + ": status = " + statusCode);
1397         }
1398         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1399                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1400         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1401     }
1402
1403     // Failure outcomes
1404     @Override
1405     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1406         groups = {"delete"}, dependsOnMethods = {"delete"})
1407     public void deleteNonExistent(String testName) throws Exception {
1408
1409         // Perform setup.
1410         setupDeleteNonExistent(testName);
1411
1412         // Submit the request to the service and store the response.
1413         OrgAuthorityClient client = new OrgAuthorityClient();
1414         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1415         int statusCode = res.getStatus();
1416
1417         // Check the status code of the response: does it match
1418         // the expected response(s)?
1419         if(logger.isDebugEnabled()){
1420             logger.debug(testName + ": status = " + statusCode);
1421         }
1422         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1423                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1424         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1425     }
1426
1427     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1428         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1429     public void deleteNonExistentItem(String testName) {
1430
1431         // Perform setup.
1432         setupDeleteNonExistent(testName);
1433
1434         // Submit the request to the service and store the response.
1435         OrgAuthorityClient client = new OrgAuthorityClient();
1436         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1437         int statusCode = res.getStatus();
1438
1439         // Check the status code of the response: does it match
1440         // the expected response(s)?
1441         if(logger.isDebugEnabled()){
1442             logger.debug(testName + ": status = " + statusCode);
1443         }
1444         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1445                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1446         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1447     }
1448
1449     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1450         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1451     public void deleteNonExistentContact(String testName) {
1452
1453         // Perform setup.
1454         setupDeleteNonExistent(testName);
1455
1456         // Submit the request to the service and store the response.
1457         OrgAuthorityClient client = new OrgAuthorityClient();
1458         ClientResponse<Response> res =
1459             client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1460         int statusCode = res.getStatus();
1461
1462         // Check the status code of the response: does it match
1463         // the expected response(s)?
1464         if(logger.isDebugEnabled()){
1465             logger.debug(testName + ": status = " + statusCode);
1466         }
1467         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1468                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1469         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1470     }
1471
1472     // ---------------------------------------------------------------
1473     // Utility tests : tests of code used in tests above
1474     // ---------------------------------------------------------------
1475     /**
1476      * Tests the code for manually submitting data that is used by several
1477      * of the methods above.
1478      */
1479     @Test(dependsOnMethods = {"create", "read"})
1480     public void testSubmitRequest() {
1481
1482         // Expected status code: 200 OK
1483         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1484
1485         // Submit the request to the service and store the response.
1486         String method = ServiceRequestType.READ.httpMethodName();
1487         String url = getResourceURL(knownResourceId);
1488         int statusCode = submitRequest(method, url);
1489
1490         // Check the status code of the response: does it match
1491         // the expected response(s)?
1492         if(logger.isDebugEnabled()){
1493             logger.debug("testSubmitRequest: url=" + url +
1494                 " status=" + statusCode);
1495         }
1496         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1497
1498     }
1499
1500     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1501     public void testItemSubmitRequest() {
1502
1503         // Expected status code: 200 OK
1504         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1505
1506         // Submit the request to the service and store the response.
1507         String method = ServiceRequestType.READ.httpMethodName();
1508         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1509         int statusCode = submitRequest(method, url);
1510
1511         // Check the status code of the response: does it match
1512         // the expected response(s)?
1513         if(logger.isDebugEnabled()){
1514             logger.debug("testItemSubmitRequest: url=" + url +
1515                 " status=" + statusCode);
1516         }
1517         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1518
1519     }
1520
1521     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1522     public void testContactSubmitRequest() {
1523
1524         // Expected status code: 200 OK
1525         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1526
1527         // Submit the request to the service and store the response.
1528         String method = ServiceRequestType.READ.httpMethodName();
1529         String url = getContactResourceURL(knownResourceId,
1530             knownItemResourceId, knownContactResourceId);
1531         int statusCode = submitRequest(method, url);
1532
1533         // Check the status code of the response: does it match
1534         // the expected response(s)?
1535         if(logger.isDebugEnabled()){
1536             logger.debug("testItemSubmitRequest: url=" + url +
1537                 " status=" + statusCode);
1538         }
1539         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1540
1541     }
1542
1543
1544     // ---------------------------------------------------------------
1545     // Cleanup of resources created during testing
1546     // ---------------------------------------------------------------
1547     
1548     /**
1549      * Deletes all resources created by tests, after all tests have been run.
1550      *
1551      * This cleanup method will always be run, even if one or more tests fail.
1552      * For this reason, it attempts to remove all resources created
1553      * at any point during testing, even if some of those resources
1554      * may be expected to be deleted by certain tests.
1555      */
1556
1557     @AfterClass(alwaysRun=true)
1558     public void cleanUp() {
1559         String noTest = System.getProperty("noTestCleanup");
1560         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1561             if (logger.isDebugEnabled()) {
1562                 logger.debug("Skipping Cleanup phase ...");
1563             }
1564             return;
1565         }
1566         if (logger.isDebugEnabled()) {
1567             logger.debug("Cleaning up temporary resources created for testing ...");
1568         }
1569         String parentResourceId;
1570         String itemResourceId;
1571         String contactResourceId;
1572         // Clean up contact resources.
1573         parentResourceId = knownResourceId;
1574         OrgAuthorityClient client = new OrgAuthorityClient();
1575         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1576             contactResourceId = entry.getKey();
1577             itemResourceId = entry.getValue();
1578             // Note: Any non-success responses from the delete operation
1579             // below are ignored and not reported.
1580             ClientResponse<Response> res =
1581                 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
1582         }
1583         // Clean up item resources.
1584         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1585             itemResourceId = entry.getKey();
1586             parentResourceId = entry.getValue();
1587             // Note: Any non-success responses from the delete operation
1588             // below are ignored and not reported.
1589             ClientResponse<Response> res =
1590                 client.deleteItem(parentResourceId, itemResourceId);
1591         }
1592         // Clean up parent resources.
1593         for (String resourceId : allResourceIdsCreated) {
1594             // Note: Any non-success responses from the delete operation
1595             // below are ignored and not reported.
1596             ClientResponse<Response> res = client.delete(resourceId);
1597         }
1598     }
1599
1600     // ---------------------------------------------------------------
1601     // Utility methods used by tests above
1602     // ---------------------------------------------------------------
1603     @Override
1604     public String getServicePathComponent() {
1605         return SERVICE_PATH_COMPONENT;
1606     }
1607
1608     public String getItemServicePathComponent() {
1609         return ITEM_SERVICE_PATH_COMPONENT;
1610     }
1611
1612     public String getContactServicePathComponent() {
1613         return CONTACT_SERVICE_PATH_COMPONENT;
1614     }
1615
1616     /**
1617      * Returns the root URL for the item service.
1618      *
1619      * This URL consists of a base URL for all services, followed by
1620      * a path component for the owning parent, followed by the
1621      * path component for the items.
1622      *
1623      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1624      * parent authority resource of the relevant item resource.
1625      *
1626      * @return The root URL for the item service.
1627      */
1628     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1629         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1630     }
1631
1632     /**
1633      * Returns the URL of a specific item resource managed by a service, and
1634      * designated by an identifier (such as a universally unique ID, or UUID).
1635      *
1636      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1637      * parent authority resource of the relevant item resource.
1638      *
1639      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1640      * item resource.
1641      *
1642      * @return The URL of a specific item resource managed by a service.
1643      */
1644     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1645         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1646     }
1647
1648
1649     /**
1650      * Returns the root URL for the contact service.
1651      *
1652      * This URL consists of a base URL for all services, followed by
1653      * a path component for the owning authority, followed by the
1654      * path component for the owning item, followed by the path component
1655      * for the contact service.
1656      *
1657      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1658      * parent authority resource of the relevant item resource.
1659      *
1660      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1661      * item resource.
1662      *
1663      * @return The root URL for the contact service.
1664      */
1665     protected String getContactServiceRootURL(String parentResourceIdentifier,
1666         String itemResourceIdentifier) {
1667         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/" +
1668                 getContactServicePathComponent();
1669     }
1670
1671     /**
1672      * Returns the URL of a specific contact resource managed by a service, and
1673      * designated by an identifier (such as a universally unique ID, or UUID).
1674      *
1675      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1676      * parent resource of the relevant item resource.
1677      *
1678      * @param  resourceIdentifier  An identifier (such as a UUID) for an
1679      * item resource.
1680      *
1681      * @return The URL of a specific resource managed by a service.
1682      */
1683     protected String getContactResourceURL(String parentResourceIdentifier,
1684         String itemResourceIdentifier, String contactResourceIdentifier) {
1685         return getContactServiceRootURL(parentResourceIdentifier,
1686             itemResourceIdentifier) + "/" + contactResourceIdentifier;
1687     }
1688
1689     private MultipartOutput createOrgAuthorityInstance(String identifier) {
1690         String displayName = "displayName-" + identifier;
1691         String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1692         return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1693                                 displayName, refName, 
1694                                 new OrgAuthorityClient().getCommonPartName());
1695     }
1696 }