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