]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
32d6d6a381d48974946108fdd1a06decdc8f2155
[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.setCsid(null);
528         organization.setDisplayNameComputed(true);
529         organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
530         expectedDisplayName = 
531             OrgAuthorityClientUtils.prepareDefaultDisplayName(
532                 "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
533
534         // Submit the updated resource to the service and store the response.
535         MultipartOutput output = new MultipartOutput();
536         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
537         commonPart.getHeaders().add("label", client.getItemCommonPartName());
538         res = client.updateItem(knownResourceId, knownItemResourceId, output);
539         statusCode = res.getStatus();
540
541         // Check the status code of the response: does it match the expected response(s)?
542         if(logger.isDebugEnabled()){
543             logger.debug("updateItem: status = " + statusCode);
544         }
545         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
546                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
547         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
548
549         // Retrieve the updated resource and verify that its contents exist.
550         input = (MultipartInput) res.getEntity();
551         OrganizationsCommon updatedOrganization =
552                 (OrganizationsCommon) extractPart(input,
553                         client.getItemCommonPartName(), OrganizationsCommon.class);
554         Assert.assertNotNull(updatedOrganization);
555
556         // Verify that the updated resource received the correct data.
557         Assert.assertEquals(updatedOrganization.getShortName(), organization.getShortName(),
558             "Updated ShortName in Organization did not match submitted data.");
559         // Verify that the updated resource computes the right displayName.
560         Assert.assertEquals(updatedOrganization.getDisplayName(), expectedDisplayName,
561             "Updated ShortName in Organization not reflected in computed DisplayName.");
562
563         // Now Update the displayName, not computed and verify the computed name is overriden.
564         organization.setDisplayNameComputed(false);
565         expectedDisplayName = "TestName";
566         organization.setDisplayName(expectedDisplayName);
567
568         // Submit the updated resource to the service and store the response.
569         output = new MultipartOutput();
570         commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
571         commonPart.getHeaders().add("label", client.getItemCommonPartName());
572         res = client.updateItem(knownResourceId, knownItemResourceId, output);
573         statusCode = res.getStatus();
574
575         // Check the status code of the response: does it match the expected response(s)?
576         if(logger.isDebugEnabled()){
577             logger.debug("updateItem: status = " + statusCode);
578         }
579         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
580                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
581         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
582
583         // Retrieve the updated resource and verify that its contents exist.
584         input = (MultipartInput) res.getEntity();
585         updatedOrganization =
586                 (OrganizationsCommon) extractPart(input,
587                         client.getItemCommonPartName(), OrganizationsCommon.class);
588         Assert.assertNotNull(updatedOrganization);
589
590         // Verify that the updated resource received the correct data.
591         Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
592                 "Updated displayNameComputed in Organization did not match submitted data.");
593         // Verify that the updated resource computes the right displayName.
594         Assert.assertEquals(updatedOrganization.getDisplayName(),
595                         expectedDisplayName,
596                 "Updated DisplayName (not computed) in Organization not stored.");
597     }
598
599     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
600             dependsOnMethods = {"verifyItemDisplayName"})
601     public void verifyIllegalItemDisplayName(String testName) throws Exception {
602
603         // Perform setup.
604         setupUpdateWithWrongXmlSchema(testName);
605
606         // Submit the request to the service and store the response.
607         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
608         int statusCode = res.getStatus();
609
610         // Check the status code of the response: does it match
611         // the expected response(s)?
612         if(logger.isDebugEnabled()){
613             logger.debug(testName + ": status = " + statusCode);
614         }
615         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617         Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
618
619         // Check whether organization has expected displayName.
620         MultipartInput input = (MultipartInput) res.getEntity();
621         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
622                 client.getItemCommonPartName(), OrganizationsCommon.class);
623         Assert.assertNotNull(organization);
624         // Try to Update with computed false and no displayName
625         organization.setDisplayNameComputed(false);
626         organization.setDisplayName(null);
627
628         // Submit the updated resource to the service and store the response.
629         MultipartOutput output = new MultipartOutput();
630         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
631         commonPart.getHeaders().add("label", client.getItemCommonPartName());
632         res = client.updateItem(knownResourceId, knownItemResourceId, output);
633         statusCode = res.getStatus();
634
635         // Check the status code of the response: does it match the expected response(s)?
636         if(logger.isDebugEnabled()){
637             logger.debug("updateItem: status = " + statusCode);
638         }
639         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
640                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
641         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
642     }
643
644     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
645         groups = {"read"}, dependsOnMethods = {"readItem"})
646     public void readContact(String testName) throws Exception {
647
648         // Perform setup.
649         setupRead(testName);
650
651         // Submit the request to the service and store the response.
652         ClientResponse<MultipartInput> res =
653             client.readContact(knownResourceId, knownItemResourceId,
654             knownContactResourceId);
655         int statusCode = res.getStatus();
656
657         // Check the status code of the response: does it match
658         // the expected response(s)?
659         if(logger.isDebugEnabled()){
660             logger.debug(testName + ": status = " + statusCode);
661         }
662         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
663                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
664         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
665
666         // Check whether we've received a contact.
667         MultipartInput input = (MultipartInput) res.getEntity();
668         ContactsCommon contact = (ContactsCommon) extractPart(input,
669                 contactClient.getCommonPartName(), ContactsCommon.class);
670         Assert.assertNotNull(contact);
671         boolean showFull = true;
672         if(showFull && logger.isDebugEnabled()){
673             logger.debug(testName + ": returned payload:");
674             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
675         }
676         Assert.assertEquals(contact.getInAuthority(), knownResourceId);
677         Assert.assertEquals(contact.getInItem(), knownItemResourceId);
678
679     }
680
681     // Failure outcomes
682     @Override
683     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
684         groups = {"read"}, dependsOnMethods = {"read"})
685     public void readNonExistent(String testName) {
686
687         // Perform setup.
688         setupReadNonExistent(testName);
689
690         // Submit the request to the service and store the response.
691         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
692         int statusCode = res.getStatus();
693
694         // Check the status code of the response: does it match
695         // the expected response(s)?
696         if(logger.isDebugEnabled()){
697             logger.debug(testName + ": status = " + statusCode);
698         }
699         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
700                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
701         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
702     }
703
704     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
705         groups = {"read"}, dependsOnMethods = {"readItem"})
706     public void readItemNonExistent(String testName) {
707
708         // Perform setup.
709         setupReadNonExistent(testName);
710
711         // Submit the request to the service and store the response.
712         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
713         int statusCode = res.getStatus();
714
715         // Check the status code of the response: does it match
716         // the expected response(s)?
717         if(logger.isDebugEnabled()){
718             logger.debug(testName + ": status = " + statusCode);
719         }
720         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
721                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
722         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
723     }
724
725     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
726         groups = {"read"}, dependsOnMethods = {"readContact"})
727     public void readContactNonExistent(String testName) {
728
729         // Perform setup.
730         setupReadNonExistent(testName);
731
732         // Submit the request to the service and store the response.
733         ClientResponse<MultipartInput> res =
734             client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
735         int statusCode = res.getStatus();
736
737         // Check the status code of the response: does it match
738         // the expected response(s)?
739         if(logger.isDebugEnabled()){
740             logger.debug(testName + ": status = " + statusCode);
741         }
742         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
743                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
744         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
745     }
746
747     // ---------------------------------------------------------------
748     // CRUD tests : READ_LIST tests
749     // ---------------------------------------------------------------
750     // Success outcomes
751
752     @Override
753     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
754         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
755     public void readList(String testName) throws Exception {
756
757         // Perform setup.
758         setupReadList(testName);
759
760         // Submit the request to the service and store the response.
761         ClientResponse<OrgauthoritiesCommonList> res = client.readList();
762         OrgauthoritiesCommonList list = res.getEntity();
763         int statusCode = res.getStatus();
764
765         // Check the status code of the response: does it match
766         // the expected response(s)?
767         if(logger.isDebugEnabled()){
768             logger.debug(testName + ": status = " + statusCode);
769         }
770         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
771                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
772         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
773
774         // Optionally output additional data about list members for debugging.
775         boolean iterateThroughList = false;
776         if (iterateThroughList && logger.isDebugEnabled()) {
777             List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
778                     list.getOrgauthorityListItem();
779             int i = 0;
780             for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
781                 String csid = item.getCsid();
782                 logger.debug(testName + ": list-item[" + i + "] csid=" +
783                         csid);
784                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
785                         item.getDisplayName());
786                 logger.debug(testName + ": list-item[" + i + "] URI=" +
787                         item.getUri());
788                 readItemList(csid, null);
789                 i++;
790             }
791         }
792     }
793
794     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
795     public void readItemList() {
796         readItemList(knownResourceId, null);
797     }
798
799     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
800     public void readItemListByAuthorityName() {
801         readItemList(null, knownResourceDisplayName);
802     }
803
804     private void readItemList(String vcsid, String name) {
805
806         final String testName = "readItemList";
807
808         // Perform setup.
809         setupReadList(testName);
810
811         // Submit the request to the service and store the response.
812         ClientResponse<OrganizationsCommonList> res = null;
813         
814         if(vcsid!= null) {
815                 // Submit the request to the service and store the response.
816                 res = client.readItemList(vcsid);
817         } else if(name!= null) {
818                 // Submit the request to the service and store the response.
819                 res = client.readItemListForNamedAuthority(name);
820         } else {
821                 Assert.fail("readItemList passed null csid and name!");
822         }
823         OrganizationsCommonList list = res.getEntity();
824         int statusCode = res.getStatus();
825
826         // Check the status code of the response: does it match
827         // the expected response(s)?
828         if(logger.isDebugEnabled()){
829             logger.debug(testName + ": status = " + statusCode);
830         }
831         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
832                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
833         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
834
835         List<OrganizationsCommonList.OrganizationListItem> items =
836             list.getOrganizationListItem();
837         int nItemsReturned = items.size();
838         // There will be one item created, associated with a
839         // known parent resource, by the createItem test.
840         //
841         // In addition, there will be 'nItemsToCreateInList'
842         // additional items created by the createItemList test,
843         // all associated with the same parent resource.
844         int nExpectedItems = nItemsToCreateInList + 1;
845         if(logger.isDebugEnabled()){
846             logger.debug(testName + ": Expected "
847                         + nExpectedItems +" items; got: "+nItemsReturned);
848         }
849         Assert.assertEquals(nItemsReturned, nExpectedItems);
850
851         int i = 0;
852         for (OrganizationsCommonList.OrganizationListItem item : items) {
853                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
854                 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
855                 // Optionally output additional data about list members for debugging.
856                 boolean showDetails = true;
857                 if (showDetails && logger.isDebugEnabled()) {
858                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
859                         item.getCsid());
860                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
861                         item.getRefName());
862                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
863                         item.getDisplayName());
864                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
865                         item.getUri());
866             }
867             i++;
868         }
869     }
870
871     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
872     public void readContactList() {
873         readContactList(knownResourceId, knownItemResourceId);
874     }
875
876     private void readContactList(String parentcsid, String itemcsid) {
877         final String testName = "readContactList";
878
879         // Perform setup.
880         setupReadList(testName);
881
882         // Submit the request to the service and store the response.
883         ClientResponse<ContactsCommonList> res =
884                 client.readContactList(parentcsid, itemcsid);
885         ContactsCommonList list = res.getEntity();
886         int statusCode = res.getStatus();
887
888         // Check the status code of the response: does it match
889         // the expected response(s)?
890         if(logger.isDebugEnabled()){
891             logger.debug(testName + ": status = " + statusCode);
892         }
893         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
894                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
895         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
896
897         List<ContactsCommonList.ContactListItem> listitems =
898             list.getContactListItem();
899         int nItemsReturned = listitems.size();
900         // There will be one item created, associated with a
901         // known parent resource, by the createItem test.
902         //
903         // In addition, there will be 'nItemsToCreateInList'
904         // additional items created by the createItemList test,
905         // all associated with the same parent resource.
906         int nExpectedItems = nItemsToCreateInList + 1;
907         if(logger.isDebugEnabled()){
908             logger.debug(testName + ": Expected "
909                         + nExpectedItems +" items; got: "+nItemsReturned);
910         }
911         Assert.assertEquals(nItemsReturned, nExpectedItems);
912
913         int i = 0;
914         for (ContactsCommonList.ContactListItem listitem : listitems) {
915                 // Optionally output additional data about list members for debugging.
916                 boolean showDetails = false;
917                 if (showDetails && logger.isDebugEnabled()) {
918                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
919                         listitem.getCsid());
920                 logger.debug("  " + testName + ": list-item[" + i + "] addressPlace=" +
921                         listitem.getAddressPlace());
922                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
923                         listitem.getUri());
924             }
925             i++;
926         }
927     }
928
929     // Failure outcomes
930     // None at present.
931     
932     // ---------------------------------------------------------------
933     // CRUD tests : UPDATE tests
934     // ---------------------------------------------------------------
935     // Success outcomes
936     @Override
937     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
938         groups = {"update"}, dependsOnGroups = {"read", "readList"})
939     public void update(String testName) throws Exception {
940
941         // Perform setup.
942         setupUpdate(testName);
943
944         // Retrieve the contents of a resource to update.
945         ClientResponse<MultipartInput> res =
946                 client.read(knownResourceId);
947         if(logger.isDebugEnabled()){
948             logger.debug(testName + ": read status = " + res.getStatus());
949         }
950         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
951
952         if(logger.isDebugEnabled()){
953             logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
954         }
955         MultipartInput input = (MultipartInput) res.getEntity();
956         OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
957                 client.getCommonPartName(), OrgauthoritiesCommon.class);
958         Assert.assertNotNull(orgAuthority);
959
960         // Update the contents of this resource.
961         orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
962         orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
963         if(logger.isDebugEnabled()){
964             logger.debug("to be updated OrgAuthority");
965             logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
966         }
967
968         // Submit the updated resource to the service and store the response.
969         MultipartOutput output = new MultipartOutput();
970         OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
971         commonPart.getHeaders().add("label", client.getCommonPartName());
972         res = client.update(knownResourceId, output);
973         int statusCode = res.getStatus();
974
975         // Check the status code of the response: does it match the expected response(s)?
976         if(logger.isDebugEnabled()){
977             logger.debug(testName + ": status = " + statusCode);
978         }
979         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
980                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
981         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
982
983         // Retrieve the updated resource and verify that its contents exist.
984         input = (MultipartInput) res.getEntity();
985         OrgauthoritiesCommon updatedOrgAuthority =
986                 (OrgauthoritiesCommon) extractPart(input,
987                         client.getCommonPartName(), OrgauthoritiesCommon.class);
988         Assert.assertNotNull(updatedOrgAuthority);
989
990         // Verify that the updated resource received the correct data.
991         Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
992                 orgAuthority.getDisplayName(),
993                 "Data in updated object did not match submitted data.");
994     }
995
996     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
997         groups = {"update"}, dependsOnMethods = {"update"})
998     public void updateItem(String testName) throws Exception {
999
1000         // Perform setup.
1001         setupUpdate(testName);
1002
1003         ClientResponse<MultipartInput> res =
1004                 client.readItem(knownResourceId, knownItemResourceId);
1005         if(logger.isDebugEnabled()){
1006             logger.debug(testName + ": read status = " + res.getStatus());
1007         }
1008         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1009
1010         if(logger.isDebugEnabled()){
1011             logger.debug("got Organization to update with ID: " +
1012                 knownItemResourceId +
1013                 " in OrgAuthority: " + knownResourceId );
1014         }
1015         MultipartInput input = (MultipartInput) res.getEntity();
1016         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
1017                 client.getItemCommonPartName(), OrganizationsCommon.class);
1018         Assert.assertNotNull(organization);
1019
1020         // Update the contents of this resource.
1021         organization.setCsid(null);
1022         organization.setShortName("updated-" + organization.getShortName());
1023         if(logger.isDebugEnabled()){
1024             logger.debug("to be updated Organization");
1025             logger.debug(objectAsXmlString(organization,
1026                 OrganizationsCommon.class));
1027         }
1028
1029         // Submit the updated resource to the service and store the response.
1030         MultipartOutput output = new MultipartOutput();
1031         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
1032         commonPart.getHeaders().add("label", client.getItemCommonPartName());
1033         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1034         int statusCode = res.getStatus();
1035
1036         // Check the status code of the response: does it match the expected response(s)?
1037         if(logger.isDebugEnabled()){
1038             logger.debug(testName + ": status = " + statusCode);
1039         }
1040         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1041                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1042         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1043
1044         // Retrieve the updated resource and verify that its contents exist.
1045         input = (MultipartInput) res.getEntity();
1046         OrganizationsCommon updatedOrganization =
1047                 (OrganizationsCommon) extractPart(input,
1048                         client.getItemCommonPartName(), OrganizationsCommon.class);
1049         Assert.assertNotNull(updatedOrganization);
1050
1051         // Verify that the updated resource received the correct data.
1052         Assert.assertEquals(updatedOrganization.getShortName(),
1053                 organization.getShortName(),
1054                 "Data in updated Organization did not match submitted data.");
1055     }
1056
1057     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1058         groups = {"update"}, dependsOnMethods = {"updateItem"})
1059     public void updateContact(String testName) throws Exception {
1060
1061         // Perform setup.
1062         setupUpdate(testName);
1063
1064         ClientResponse<MultipartInput> res =
1065                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1066         if(logger.isDebugEnabled()){
1067             logger.debug(testName + ": read status = " + res.getStatus());
1068         }
1069         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1070
1071         if(logger.isDebugEnabled()){
1072             logger.debug("got Contact to update with ID: " +
1073                 knownContactResourceId +
1074                 " in item: " + knownItemResourceId +
1075                 " in parent: " + knownResourceId );
1076         }
1077         MultipartInput input = (MultipartInput) res.getEntity();
1078         ContactsCommon contact = (ContactsCommon) extractPart(input,
1079                 contactClient.getCommonPartName(), ContactsCommon.class);
1080         Assert.assertNotNull(contact);
1081
1082         // Update the contents of this resource.
1083         contact.setAddressPlace("updated-" + contact.getAddressPlace());
1084         if(logger.isDebugEnabled()){
1085             logger.debug("to be updated Contact");
1086             logger.debug(objectAsXmlString(contact,
1087                 ContactsCommon.class));
1088         }
1089
1090         // Submit the updated resource to the service and store the response.
1091         MultipartOutput output = new MultipartOutput();
1092         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
1093         commonPart.getHeaders().add("label", contactClient.getCommonPartName());
1094         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1095         int statusCode = res.getStatus();
1096
1097         // Check the status code of the response: does it match the expected response(s)?
1098         if(logger.isDebugEnabled()){
1099             logger.debug(testName + ": status = " + statusCode);
1100         }
1101         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1102                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1103         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1104
1105         // Retrieve the updated resource and verify that its contents exist.
1106         input = (MultipartInput) res.getEntity();
1107         ContactsCommon updatedContact =
1108                 (ContactsCommon) extractPart(input,
1109                         contactClient.getCommonPartName(), ContactsCommon.class);
1110         Assert.assertNotNull(updatedContact);
1111
1112         // Verify that the updated resource received the correct data.
1113         Assert.assertEquals(updatedContact.getAddressPlace(),
1114                 contact.getAddressPlace(),
1115                 "Data in updated Contact did not match submitted data.");
1116     }
1117
1118     // Failure outcomes
1119     // Placeholders until the three tests below can be uncommented.
1120     // See Issue CSPACE-401.
1121     @Override
1122     public void updateWithEmptyEntityBody(String testName) throws Exception {
1123     }
1124
1125     @Override
1126     public void updateWithMalformedXml(String testName) throws Exception {
1127     }
1128
1129     @Override
1130     public void updateWithWrongXmlSchema(String testName) throws Exception {
1131     }
1132
1133 /*
1134     @Override
1135     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1136         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1137     public void updateWithEmptyEntityBody(String testName) throws Exception {
1138
1139     // Perform setup.
1140     setupUpdateWithEmptyEntityBody(testName);
1141
1142     // Submit the request to the service and store the response.
1143     String method = REQUEST_TYPE.httpMethodName();
1144     String url = getResourceURL(knownResourceId);
1145     String mediaType = MediaType.APPLICATION_XML;
1146     final String entity = "";
1147     int statusCode = submitRequest(method, url, mediaType, entity);
1148
1149     // Check the status code of the response: does it match
1150     // the expected response(s)?
1151     if(logger.isDebugEnabled()){
1152         logger.debug(testName + ": url=" + url +
1153             " status=" + statusCode);
1154      }
1155     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1156     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1157     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1158     }
1159
1160     @Override
1161     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1162         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1163     public void updateWithMalformedXml(String testName) throws Exception {
1164
1165     // Perform setup.
1166     setupUpdateWithMalformedXml(testName);
1167
1168     // Submit the request to the service and store the response.
1169     String method = REQUEST_TYPE.httpMethodName();
1170     String url = getResourceURL(knownResourceId);
1171     String mediaType = MediaType.APPLICATION_XML;
1172     final String entity = MALFORMED_XML_DATA;
1173     int statusCode = submitRequest(method, url, mediaType, entity);
1174
1175     // Check the status code of the response: does it match
1176     // the expected response(s)?
1177     if(logger.isDebugEnabled()){
1178         logger.debug(testName + ": url=" + url +
1179            " status=" + statusCode);
1180      }
1181     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1182     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1183     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1184     }
1185
1186     @Override
1187     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1188         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1189     public void updateWithWrongXmlSchema(String testName) throws Exception {
1190
1191     // Perform setup.
1192     setupUpdateWithWrongXmlSchema(testName);
1193
1194     // Submit the request to the service and store the response.
1195     String method = REQUEST_TYPE.httpMethodName();
1196     String url = getResourceURL(knownResourceId);
1197     String mediaType = MediaType.APPLICATION_XML;
1198     final String entity = WRONG_XML_SCHEMA_DATA;
1199     int statusCode = submitRequest(method, url, mediaType, entity);
1200
1201     // Check the status code of the response: does it match
1202     // the expected response(s)?
1203     if(logger.isDebugEnabled()){
1204         logger.debug("updateWithWrongXmlSchema: url=" + url +
1205             " status=" + statusCode);
1206      }
1207     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1208     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1209     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1210     }
1211 */
1212
1213     @Override
1214     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1215         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1216     public void updateNonExistent(String testName) throws Exception {
1217
1218         // Perform setup.
1219         setupUpdateNonExistent(testName);
1220
1221         // Submit the request to the service and store the response.
1222         // Note: The ID used in this 'create' call may be arbitrary.
1223         // The only relevant ID may be the one used in update(), below.
1224         MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
1225         ClientResponse<MultipartInput> res =
1226                 client.update(NON_EXISTENT_ID, multipart);
1227         int statusCode = res.getStatus();
1228
1229         // Check the status code of the response: does it match
1230         // the expected response(s)?
1231         if(logger.isDebugEnabled()){
1232             logger.debug(testName + ": status = " + statusCode);
1233         }
1234         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1235                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1236         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1237     }
1238
1239     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1240         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1241     public void updateNonExistentItem(String testName) throws Exception {
1242
1243         // Perform setup.
1244         setupUpdateNonExistent(testName);
1245
1246         // Submit the request to the service and store the response.
1247         // Note: The ID(s) used when creating the request payload may be arbitrary.
1248         // The only relevant ID may be the one used in update(), below.
1249         Map<String, String> nonexOrgMap = new HashMap<String,String>();
1250         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
1251         String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
1252         MultipartOutput multipart = 
1253                 OrgAuthorityClientUtils.createOrganizationInstance(
1254                         NON_EXISTENT_ID, refName,
1255                         nonexOrgMap, client.getItemCommonPartName() );
1256         ClientResponse<MultipartInput> res =
1257                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1258         int statusCode = res.getStatus();
1259
1260         // Check the status code of the response: does it match
1261         // the expected response(s)?
1262         if(logger.isDebugEnabled()){
1263             logger.debug(testName + ": status = " + statusCode);
1264         }
1265         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1266                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1267         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1268     }
1269
1270     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1271         groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1272     public void updateNonExistentContact(String testName) throws Exception {
1273         // Currently a no-op test
1274     }
1275
1276     // ---------------------------------------------------------------
1277     // CRUD tests : DELETE tests
1278     // ---------------------------------------------------------------
1279     // Success outcomes
1280
1281     // Note: delete sub-resources in ascending hierarchical order,
1282     // before deleting their parents.
1283
1284     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1285         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1286     public void deleteContact(String testName) throws Exception {
1287
1288         // Perform setup.
1289         setupDelete(testName);
1290
1291          if(logger.isDebugEnabled()){
1292             logger.debug("parentcsid =" + knownResourceId +
1293                 " itemcsid = " + knownItemResourceId +
1294                 " csid = " + knownContactResourceId);
1295         }
1296
1297         // Submit the request to the service and store the response.
1298         ClientResponse<Response> res =
1299             client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1300         int statusCode = res.getStatus();
1301
1302         // Check the status code of the response: does it match
1303         // the expected response(s)?
1304         if(logger.isDebugEnabled()){
1305             logger.debug(testName + ": status = " + statusCode);
1306         }
1307         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1308                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1309         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1310     }
1311
1312    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1313         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1314     public void deleteItem(String testName) throws Exception {
1315
1316         // Perform setup.
1317         setupDelete(testName);
1318
1319         if(logger.isDebugEnabled()){
1320             logger.debug("parentcsid =" + knownResourceId +
1321                 " itemcsid = " + knownItemResourceId);
1322         }
1323
1324         // Submit the request to the service and store the response.
1325         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1326         int statusCode = res.getStatus();
1327
1328         // Check the status code of the response: does it match
1329         // the expected response(s)?
1330         if(logger.isDebugEnabled()){
1331             logger.debug(testName + ": status = " + statusCode);
1332         }
1333         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1334                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1335         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1336     }
1337
1338     @Override
1339     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1340         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1341     public void delete(String testName) throws Exception {
1342
1343         // Perform setup.
1344         setupDelete(testName);
1345
1346         if(logger.isDebugEnabled()){
1347             logger.debug("parentcsid =" + knownResourceId);
1348         }
1349
1350         // Submit the request to the service and store the response.
1351         ClientResponse<Response> res = client.delete(knownResourceId);
1352         int statusCode = res.getStatus();
1353
1354         // Check the status code of the response: does it match
1355         // the expected response(s)?
1356         if(logger.isDebugEnabled()){
1357             logger.debug(testName + ": status = " + statusCode);
1358         }
1359         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1360                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1361         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1362     }
1363
1364     // Failure outcomes
1365     @Override
1366     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1367         groups = {"delete"}, dependsOnMethods = {"delete"})
1368     public void deleteNonExistent(String testName) throws Exception {
1369
1370         // Perform setup.
1371         setupDeleteNonExistent(testName);
1372
1373         // Submit the request to the service and store the response.
1374         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1375         int statusCode = res.getStatus();
1376
1377         // Check the status code of the response: does it match
1378         // the expected response(s)?
1379         if(logger.isDebugEnabled()){
1380             logger.debug(testName + ": status = " + statusCode);
1381         }
1382         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1383                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1384         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1385     }
1386
1387     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1388         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1389     public void deleteNonExistentItem(String testName) {
1390
1391         // Perform setup.
1392         setupDeleteNonExistent(testName);
1393
1394         // Submit the request to the service and store the response.
1395         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1396         int statusCode = res.getStatus();
1397
1398         // Check the status code of the response: does it match
1399         // the expected response(s)?
1400         if(logger.isDebugEnabled()){
1401             logger.debug(testName + ": status = " + statusCode);
1402         }
1403         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1404                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1405         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1406     }
1407
1408     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1409         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1410     public void deleteNonExistentContact(String testName) {
1411
1412         // Perform setup.
1413         setupDeleteNonExistent(testName);
1414
1415         // Submit the request to the service and store the response.
1416         ClientResponse<Response> res =
1417             client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1418         int statusCode = res.getStatus();
1419
1420         // Check the status code of the response: does it match
1421         // the expected response(s)?
1422         if(logger.isDebugEnabled()){
1423             logger.debug(testName + ": status = " + statusCode);
1424         }
1425         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1426                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1427         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1428     }
1429
1430     // ---------------------------------------------------------------
1431     // Utility tests : tests of code used in tests above
1432     // ---------------------------------------------------------------
1433     /**
1434      * Tests the code for manually submitting data that is used by several
1435      * of the methods above.
1436      */
1437     @Test(dependsOnMethods = {"create", "read"})
1438     public void testSubmitRequest() {
1439
1440         // Expected status code: 200 OK
1441         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1442
1443         // Submit the request to the service and store the response.
1444         String method = ServiceRequestType.READ.httpMethodName();
1445         String url = getResourceURL(knownResourceId);
1446         int statusCode = submitRequest(method, url);
1447
1448         // Check the status code of the response: does it match
1449         // the expected response(s)?
1450         if(logger.isDebugEnabled()){
1451             logger.debug("testSubmitRequest: url=" + url +
1452                 " status=" + statusCode);
1453         }
1454         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1455
1456     }
1457
1458     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1459     public void testItemSubmitRequest() {
1460
1461         // Expected status code: 200 OK
1462         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1463
1464         // Submit the request to the service and store the response.
1465         String method = ServiceRequestType.READ.httpMethodName();
1466         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1467         int statusCode = submitRequest(method, url);
1468
1469         // Check the status code of the response: does it match
1470         // the expected response(s)?
1471         if(logger.isDebugEnabled()){
1472             logger.debug("testItemSubmitRequest: url=" + url +
1473                 " status=" + statusCode);
1474         }
1475         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1476
1477     }
1478
1479     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1480     public void testContactSubmitRequest() {
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 = getContactResourceURL(knownResourceId,
1488             knownItemResourceId, knownContactResourceId);
1489         int statusCode = submitRequest(method, url);
1490
1491         // Check the status code of the response: does it match
1492         // the expected response(s)?
1493         if(logger.isDebugEnabled()){
1494             logger.debug("testItemSubmitRequest: url=" + url +
1495                 " status=" + statusCode);
1496         }
1497         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1498
1499     }
1500
1501
1502     // ---------------------------------------------------------------
1503     // Cleanup of resources created during testing
1504     // ---------------------------------------------------------------
1505     
1506     /**
1507      * Deletes all resources created by tests, after all tests have been run.
1508      *
1509      * This cleanup method will always be run, even if one or more tests fail.
1510      * For this reason, it attempts to remove all resources created
1511      * at any point during testing, even if some of those resources
1512      * may be expected to be deleted by certain tests.
1513      */
1514
1515     @AfterClass(alwaysRun=true)
1516     public void cleanUp() {
1517         if (logger.isDebugEnabled()) {
1518             logger.debug("Cleaning up temporary resources created for testing ...");
1519         }
1520         String parentResourceId;
1521         String itemResourceId;
1522         String contactResourceId;
1523         // Clean up contact resources.
1524         parentResourceId = knownResourceId;
1525         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1526             contactResourceId = entry.getKey();
1527             itemResourceId = entry.getValue();
1528             // Note: Any non-success responses from the delete operation
1529             // below are ignored and not reported.
1530             ClientResponse<Response> res =
1531                 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
1532         }
1533         // Clean up item resources.
1534         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1535             itemResourceId = entry.getKey();
1536             parentResourceId = entry.getValue();
1537             // Note: Any non-success responses from the delete operation
1538             // below are ignored and not reported.
1539             ClientResponse<Response> res =
1540                 client.deleteItem(parentResourceId, itemResourceId);
1541         }
1542         // Clean up parent resources.
1543         for (String resourceId : allResourceIdsCreated) {
1544             // Note: Any non-success responses from the delete operation
1545             // below are ignored and not reported.
1546             ClientResponse<Response> res = client.delete(resourceId);
1547         }
1548     }
1549
1550     // ---------------------------------------------------------------
1551     // Utility methods used by tests above
1552     // ---------------------------------------------------------------
1553     @Override
1554     public String getServicePathComponent() {
1555         return SERVICE_PATH_COMPONENT;
1556     }
1557
1558     public String getItemServicePathComponent() {
1559         return ITEM_SERVICE_PATH_COMPONENT;
1560     }
1561
1562     public String getContactServicePathComponent() {
1563         return CONTACT_SERVICE_PATH_COMPONENT;
1564     }
1565
1566     /**
1567      * Returns the root URL for the item service.
1568      *
1569      * This URL consists of a base URL for all services, followed by
1570      * a path component for the owning parent, followed by the
1571      * path component for the items.
1572      *
1573      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1574      * parent authority resource of the relevant item resource.
1575      *
1576      * @return The root URL for the item service.
1577      */
1578     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1579         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1580     }
1581
1582     /**
1583      * Returns the URL of a specific item resource managed by a service, and
1584      * designated by an identifier (such as a universally unique ID, or UUID).
1585      *
1586      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1587      * parent authority resource of the relevant item resource.
1588      *
1589      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1590      * item resource.
1591      *
1592      * @return The URL of a specific item resource managed by a service.
1593      */
1594     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1595         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1596     }
1597
1598
1599     /**
1600      * Returns the root URL for the contact service.
1601      *
1602      * This URL consists of a base URL for all services, followed by
1603      * a path component for the owning authority, followed by the
1604      * path component for the owning item, followed by the path component
1605      * for the contact service.
1606      *
1607      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1608      * parent authority resource of the relevant item resource.
1609      *
1610      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1611      * item resource.
1612      *
1613      * @return The root URL for the contact service.
1614      */
1615     protected String getContactServiceRootURL(String parentResourceIdentifier,
1616         String itemResourceIdentifier) {
1617         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/" +
1618                 getContactServicePathComponent();
1619     }
1620
1621     /**
1622      * Returns the URL of a specific contact resource managed by a service, and
1623      * designated by an identifier (such as a universally unique ID, or UUID).
1624      *
1625      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1626      * parent resource of the relevant item resource.
1627      *
1628      * @param  resourceIdentifier  An identifier (such as a UUID) for an
1629      * item resource.
1630      *
1631      * @return The URL of a specific resource managed by a service.
1632      */
1633     protected String getContactResourceURL(String parentResourceIdentifier,
1634         String itemResourceIdentifier, String contactResourceIdentifier) {
1635         return getContactServiceRootURL(parentResourceIdentifier,
1636             itemResourceIdentifier) + "/" + contactResourceIdentifier;
1637     }
1638
1639     private MultipartOutput createOrgAuthorityInstance(String identifier) {
1640         String displayName = "displayName-" + identifier;
1641         String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1642         return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1643                                 displayName, refName, 
1644                                 client.getCommonPartName());
1645     }
1646 }