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