]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bd0c7fb9c60969cb04625edb211820be5fa2df1b
[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.OrgAuthorityClient;
34 import org.collectionspace.services.client.OrgAuthorityClientUtils;
35 import org.collectionspace.services.organization.OrgauthoritiesCommon;
36 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
37 import org.collectionspace.services.organization.OrganizationsCommon;
38 import org.collectionspace.services.organization.OrganizationsCommonList;
39
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
49
50 /**
51  * OrgAuthorityServiceTest, carries out tests against a
52  * deployed and running OrgAuthority Service.
53  *
54  * $LastChangedRevision: 753 $
55  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
56  */
57 public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
58
59     private final Logger logger =
60         LoggerFactory.getLogger(OrgAuthorityServiceTest.class);
61
62     // Instance variables specific to this test.
63     private OrgAuthorityClient client = new OrgAuthorityClient();
64     final String SERVICE_PATH_COMPONENT = "orgauthorities";
65     final String ITEM_SERVICE_PATH_COMPONENT = "items";
66     private final String TEST_ORG_SHORTNAME = "Test Org";
67     private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
68     private String knownResourceId = null;
69     private String lastOrgAuthId = null;
70     private String knownResourceRefName = null;
71     private String knownItemResourceId = null;
72     private int nItemsToCreateInList = 3;
73     private List<String> allResourceIdsCreated = new ArrayList<String>();
74     private Map<String, String> allResourceItemIdsCreated =
75         new HashMap<String, String>();
76     
77     // ---------------------------------------------------------------
78     // CRUD tests : CREATE tests
79     // ---------------------------------------------------------------
80     // Success outcomes
81     @Override
82     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
83     public void create(String testName) throws Exception {
84
85         // Perform setup, such as initializing the type of service request
86         // (e.g. CREATE, DELETE), its valid and expected status codes, and
87         // its associated HTTP method name (e.g. POST, DELETE).
88         setupCreate(testName);
89
90         // Submit the request to the service and store the response.
91         String identifier = createIdentifier();
92         String displayName = "displayName-" + identifier;
93         String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
94         MultipartOutput multipart = 
95                 OrgAuthorityClientUtils.createOrgAuthorityInstance(
96                                 displayName, refName, 
97                                 client.getCommonPartName());
98         ClientResponse<Response> res = client.create(multipart);
99         int statusCode = res.getStatus();
100
101         // Check the status code of the response: does it match
102         // the expected response(s)?
103         //
104         // Specifically:
105         // Does it fall within the set of valid status codes?
106         // Does it exactly match the expected status code?
107         if(logger.isDebugEnabled()){
108             logger.debug(testName + ": status = " + statusCode);
109         }
110         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
111                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
112         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
113
114         // Store the refname from the first resource created
115         // for additional tests below.
116         knownResourceRefName = refName;
117
118         lastOrgAuthId = extractId(res);
119         // Store the ID returned from the first resource created
120         // for additional tests below.
121         if (knownResourceId == null){
122             knownResourceId = lastOrgAuthId;
123             if (logger.isDebugEnabled()) {
124                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
125             }
126         }
127         // Store the IDs from every resource created by tests,
128         // so they can be deleted after tests have been run.
129         allResourceIdsCreated.add(extractId(res));
130
131     }
132
133     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
134         dependsOnMethods = {"create"})
135     public void createItem(String testName) {
136         setupCreate(testName);
137
138         knownItemResourceId = createItemInAuthority(lastOrgAuthId, knownResourceRefName);
139         if(logger.isDebugEnabled()){
140             logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
141         }
142     }
143
144     private String createItemInAuthority(String vcsid, String orgAuthorityRefName) {
145
146         final String testName = "createItemInAuthority";
147         if(logger.isDebugEnabled()){
148             logger.debug(testName + ":...");
149         }
150
151         // Submit the request to the service and store the response.
152         String identifier = createIdentifier();
153         String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, identifier, true);
154         Map<String, String> testOrgMap = new HashMap<String,String>();
155         testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, TEST_ORG_SHORTNAME);
156         testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
157         testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
158         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
159         testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
160         testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
161         String newID = OrgAuthorityClientUtils.createItemInAuthority(
162                         vcsid, orgAuthorityRefName, testOrgMap, client);
163         // Store the ID returned from the first item resource created
164         // for additional tests below.
165         if (knownItemResourceId == null){
166             knownItemResourceId = newID;
167             if (logger.isDebugEnabled()) {
168                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
169             }
170         }
171
172         // Store the IDs from any item resources created
173         // by tests, along with the IDs of their parents, so these items
174         // can be deleted after all tests have been run.
175         //
176         // Item resource IDs are unique, so these are used as keys;
177         // the non-unique IDs of their parents are stored as associated values.
178         allResourceItemIdsCreated.put(newID, vcsid);
179
180         return newID;
181     }
182
183     @Override
184     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
185             dependsOnMethods = {"create", "createItem"})
186     public void createList(String testName) throws Exception {
187         for (int i = 0; i < 3; i++) {
188             create(testName);
189             knownResourceId = lastOrgAuthId;
190             if (logger.isDebugEnabled()) {
191                 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
192             }
193             // Add nItemsToCreateInList items to each orgauthority
194             for (int j = 0; j < nItemsToCreateInList; j++) {
195                 createItem(testName);
196             }
197         }
198     }
199
200     // Failure outcomes
201     // Placeholders until the three tests below can be uncommented.
202     // See Issue CSPACE-401.
203     @Override
204     public void createWithEmptyEntityBody(String testName) throws Exception {
205     }
206
207     @Override
208     public void createWithMalformedXml(String testName) throws Exception {
209     }
210
211     @Override
212     public void createWithWrongXmlSchema(String testName) throws Exception {
213     }
214
215     /*
216     @Override
217     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
218         dependsOnMethods = {"create", "testSubmitRequest"})
219     public void createWithEmptyEntityBody(String testName) throws Exception {
220
221     // Perform setup.
222     setupCreateWithEmptyEntityBody(testName);
223
224     // Submit the request to the service and store the response.
225     String method = REQUEST_TYPE.httpMethodName();
226     String url = getServiceRootURL();
227     String mediaType = MediaType.APPLICATION_XML;
228     final String entity = "";
229     int statusCode = submitRequest(method, url, mediaType, entity);
230
231     // Check the status code of the response: does it match
232     // the expected response(s)?
233     if(logger.isDebugEnabled()) {
234         logger.debug(testName + ": url=" + url +
235             " status=" + statusCode);
236      }
237     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
240     }
241
242     @Override
243     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
244         dependsOnMethods = {"create", "testSubmitRequest"})
245     public void createWithMalformedXml(String testName) throws Exception {
246
247     // Perform setup.
248     setupCreateWithMalformedXml(testName);
249
250     // Submit the request to the service and store the response.
251     String method = REQUEST_TYPE.httpMethodName();
252     String url = getServiceRootURL();
253     String mediaType = MediaType.APPLICATION_XML;
254     final String entity = MALFORMED_XML_DATA; // Constant from base class.
255     int statusCode = submitRequest(method, url, mediaType, entity);
256
257     // Check the status code of the response: does it match
258     // the expected response(s)?
259     if(logger.isDebugEnabled()){
260         logger.debug(testName + ": url=" + url +
261             " status=" + statusCode);
262      }
263     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266     }
267
268     @Override
269     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
270         dependsOnMethods = {"create", "testSubmitRequest"})
271     public void createWithWrongXmlSchema(String testName) throws Exception {
272
273     // Perform setup.
274     setupCreateWithWrongXmlSchema(testName);
275
276     // Submit the request to the service and store the response.
277     String method = REQUEST_TYPE.httpMethodName();
278     String url = getServiceRootURL();
279     String mediaType = MediaType.APPLICATION_XML;
280     final String entity = WRONG_XML_SCHEMA_DATA;
281     int statusCode = submitRequest(method, url, mediaType, entity);
282
283     // Check the status code of the response: does it match
284     // the expected response(s)?
285     if(logger.isDebugEnabled()){
286         logger.debug(testName + ": url=" + url +
287             " status=" + statusCode);
288      }
289     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292     }
293      */
294
295     // ---------------------------------------------------------------
296     // CRUD tests : READ tests
297     // ---------------------------------------------------------------
298     // Success outcomes
299     @Override
300     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
301         dependsOnMethods = {"create"})
302     public void read(String testName) throws Exception {
303
304         // Perform setup.
305         setupRead();
306
307         // Submit the request to the service and store the response.
308         ClientResponse<MultipartInput> res = client.read(knownResourceId);
309         int statusCode = res.getStatus();
310
311         // Check the status code of the response: does it match
312         // the expected response(s)?
313         if(logger.isDebugEnabled()){
314             logger.debug(testName + ": status = " + statusCode);
315         }
316         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
317                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
318         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319         //FIXME: remove the following try catch once Aron fixes signatures
320         try {
321             MultipartInput input = (MultipartInput) res.getEntity();
322             OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
323                     client.getCommonPartName(), OrgauthoritiesCommon.class);
324             Assert.assertNotNull(orgAuthority);
325         } catch (Exception e) {
326             throw new RuntimeException(e);
327         }
328     }
329
330     /*
331     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
332             dependsOnMethods = {"read"})
333         public void readByName(String testName) throws Exception {
334
335             // Perform setup.
336             setupRead();
337
338             // Submit the request to the service and store the response.
339             ClientResponse<MultipartInput> res = client.read(knownResourceId);
340             int statusCode = res.getStatus();
341
342             // Check the status code of the response: does it match
343             // the expected response(s)?
344             if(logger.isDebugEnabled()){
345                 logger.debug(testName + ": status = " + statusCode);
346             }
347             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350             //FIXME: remove the following try catch once Aron fixes signatures
351             try {
352                 MultipartInput input = (MultipartInput) res.getEntity();
353                 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
354                         client.getCommonPartName(), OrgauthoritiesCommon.class);
355                 Assert.assertNotNull(orgAuthority);
356             } catch (Exception e) {
357                 throw new RuntimeException(e);
358             }
359         }
360     */
361
362     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
363         dependsOnMethods = {"createItem", "read"})
364     public void readItem(String testName) throws Exception {
365
366         // Perform setup.
367         setupRead(testName);
368
369         // Submit the request to the service and store the response.
370         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
371         int statusCode = res.getStatus();
372
373         // Check the status code of the response: does it match
374         // the expected response(s)?
375         if(logger.isDebugEnabled()){
376             logger.debug(testName + ": status = " + statusCode);
377         }
378         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
381
382         // Check whether we've received a organization.
383         MultipartInput input = (MultipartInput) res.getEntity();
384         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
385                 client.getItemCommonPartName(), OrganizationsCommon.class);
386         Assert.assertNotNull(organization);
387         boolean showFull = true;
388         if(showFull && logger.isDebugEnabled()){
389             logger.debug(testName + ": returned payload:");
390             logger.debug(objectAsXmlString(organization,
391                     OrganizationsCommon.class));
392         }
393         Assert.assertEquals(organization.getInAuthority(), knownResourceId);
394     }
395
396     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
397             dependsOnMethods = {"readItem", "updateItem"})
398     public void verifyItemDisplayName(String testName) throws Exception {
399
400         // Perform setup.
401         setupRead(testName);
402
403         // Submit the request to the service and store the response.
404         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
405         int statusCode = res.getStatus();
406
407         // Check the status code of the response: does it match
408         // the expected response(s)?
409         if(logger.isDebugEnabled()){
410             logger.debug(testName + ": status = " + statusCode);
411         }
412         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
413                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
414         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
415
416         // Check whether organization has expected displayName.
417         MultipartInput input = (MultipartInput) res.getEntity();
418         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
419                 client.getItemCommonPartName(), OrganizationsCommon.class);
420         Assert.assertNotNull(organization);
421         String displayName = organization.getDisplayName();
422         // Make sure displayName matches computed form
423         String expectedDisplayName = 
424                 OrgAuthorityClientUtils.prepareDefaultDisplayName(
425                                 TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
426         Assert.assertNotNull(displayName, expectedDisplayName);
427         
428         // Update the shortName and verify the computed name is updated.
429         organization.setDisplayNameComputed(true);
430         organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
431         expectedDisplayName = 
432                 OrgAuthorityClientUtils.prepareDefaultDisplayName(
433                                 "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
434
435         // Submit the updated resource to the service and store the response.
436         MultipartOutput output = new MultipartOutput();
437         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
438         commonPart.getHeaders().add("label", client.getItemCommonPartName());
439         res = client.updateItem(knownResourceId, knownItemResourceId, output);
440         statusCode = res.getStatus();
441
442         // Check the status code of the response: does it match the expected response(s)?
443         if(logger.isDebugEnabled()){
444             logger.debug("updateItem: status = " + statusCode);
445         }
446         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449
450         // Retrieve the updated resource and verify that its contents exist.
451         input = (MultipartInput) res.getEntity();
452         OrganizationsCommon updatedOrganization =
453                 (OrganizationsCommon) extractPart(input,
454                         client.getItemCommonPartName(), OrganizationsCommon.class);
455         Assert.assertNotNull(updatedOrganization);
456
457         // Verify that the updated resource received the correct data.
458         Assert.assertEquals(updatedOrganization.getShortName(),
459                 organization.getShortName(),
460                 "Updated ShortName in Organization did not match submitted data.");
461         // Verify that the updated resource computes the right displayName.
462         Assert.assertEquals(updatedOrganization.getDisplayName(),
463                         expectedDisplayName,
464                 "Updated ShortName in Organization not reflected in computed DisplayName.");
465
466         // Now Update the displayName, not computed and verify the computed name is overriden.
467         organization.setDisplayNameComputed(false);
468         expectedDisplayName = "TestName";
469         organization.setDisplayName(expectedDisplayName);
470
471         // Submit the updated resource to the service and store the response.
472         output = new MultipartOutput();
473         commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
474         commonPart.getHeaders().add("label", client.getItemCommonPartName());
475         res = client.updateItem(knownResourceId, knownItemResourceId, output);
476         statusCode = res.getStatus();
477
478         // Check the status code of the response: does it match the expected response(s)?
479         if(logger.isDebugEnabled()){
480             logger.debug("updateItem: status = " + statusCode);
481         }
482         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
483                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
484         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
485
486         // Retrieve the updated resource and verify that its contents exist.
487         input = (MultipartInput) res.getEntity();
488         updatedOrganization =
489                 (OrganizationsCommon) extractPart(input,
490                         client.getItemCommonPartName(), OrganizationsCommon.class);
491         Assert.assertNotNull(updatedOrganization);
492
493         // Verify that the updated resource received the correct data.
494         Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
495                 "Updated displayNameComputed in Organization did not match submitted data.");
496         // Verify that the updated resource computes the right displayName.
497         Assert.assertEquals(updatedOrganization.getDisplayName(),
498                         expectedDisplayName,
499                 "Updated DisplayName (not computed) in Organization not stored.");
500     }
501
502     // Failure outcomes
503     @Override
504     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
505         dependsOnMethods = {"read"})
506     public void readNonExistent(String testName) {
507
508         // Perform setup.
509         setupReadNonExistent(testName);
510
511         // Submit the request to the service and store the response.
512         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
513         int statusCode = res.getStatus();
514
515         // Check the status code of the response: does it match
516         // the expected response(s)?
517         if(logger.isDebugEnabled()){
518             logger.debug(testName + ": status = " + statusCode);
519         }
520         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
521                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
522         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
523     }
524
525     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
526         dependsOnMethods = {"readItem", "readNonExistent"})
527     public void readItemNonExistent(String testName) {
528
529         // Perform setup.
530         setupReadNonExistent(testName);
531
532         // Submit the request to the service and store the response.
533         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
534         int statusCode = res.getStatus();
535
536         // Check the status code of the response: does it match
537         // the expected response(s)?
538         if(logger.isDebugEnabled()){
539             logger.debug(testName + ": status = " + statusCode);
540         }
541         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
542                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
543         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
544     }
545     // ---------------------------------------------------------------
546     // CRUD tests : READ_LIST tests
547     // ---------------------------------------------------------------
548     // Success outcomes
549
550     @Override
551     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
552         dependsOnMethods = {"createList", "read"})
553     public void readList(String testName) throws Exception {
554
555         // Perform setup.
556         setupReadList(testName);
557
558         // Submit the request to the service and store the response.
559         ClientResponse<OrgauthoritiesCommonList> res = client.readList();
560         OrgauthoritiesCommonList list = res.getEntity();
561         int statusCode = res.getStatus();
562
563         // Check the status code of the response: does it match
564         // the expected response(s)?
565         if(logger.isDebugEnabled()){
566             logger.debug(testName + ": status = " + statusCode);
567         }
568         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
569                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
570         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
571
572         // Optionally output additional data about list members for debugging.
573         boolean iterateThroughList = false;
574         if (iterateThroughList && logger.isDebugEnabled()) {
575             List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
576                     list.getOrgauthorityListItem();
577             int i = 0;
578             for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
579                 String csid = item.getCsid();
580                 logger.debug(testName + ": list-item[" + i + "] csid=" +
581                         csid);
582                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
583                         item.getDisplayName());
584                 logger.debug(testName + ": list-item[" + i + "] URI=" +
585                         item.getUri());
586                 readItemList(csid);
587                 i++;
588             }
589         }
590     }
591
592     @Test(dependsOnMethods = {"createList", "readItem"})
593     public void readItemList() {
594         readItemList(knownResourceId);
595     }
596
597     private void readItemList(String vcsid) {
598
599         final String testName = "readItemList";
600
601         // Perform setup.
602         setupReadList(testName);
603
604         // Submit the request to the service and store the response.
605         ClientResponse<OrganizationsCommonList> res =
606                 client.readItemList(vcsid);
607         OrganizationsCommonList list = res.getEntity();
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, EXPECTED_STATUS_CODE);
618
619         List<OrganizationsCommonList.OrganizationListItem> items =
620             list.getOrganizationListItem();
621         int nItemsReturned = items.size();
622         if(logger.isDebugEnabled()){
623             logger.debug("  " + testName + ": Expected "
624                         + nItemsToCreateInList+" items; got: "+nItemsReturned);
625         }
626         Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
627
628         int i = 0;
629         for (OrganizationsCommonList.OrganizationListItem item : items) {
630                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
631                 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
632                 // Optionally output additional data about list members for debugging.
633                 boolean showDetails = true;
634                 if (showDetails && logger.isDebugEnabled()) {
635                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
636                         item.getCsid());
637                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
638                         item.getRefName());
639                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
640                         item.getDisplayName());
641                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
642                         item.getUri());
643             }
644             i++;
645         }
646     }
647
648     // Failure outcomes
649     // None at present.
650     // ---------------------------------------------------------------
651     // CRUD tests : UPDATE tests
652     // ---------------------------------------------------------------
653     // Success outcomes
654     @Override
655     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
656         dependsOnMethods = {"read"})
657     public void update(String testName) throws Exception {
658
659         // Perform setup.
660         setupUpdate(testName);
661
662         // Retrieve the contents of a resource to update.
663         ClientResponse<MultipartInput> res =
664                 client.read(knownResourceId);
665         if(logger.isDebugEnabled()){
666             logger.debug(testName + ": read status = " + res.getStatus());
667         }
668         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
669
670         if(logger.isDebugEnabled()){
671             logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
672         }
673         MultipartInput input = (MultipartInput) res.getEntity();
674         OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
675                 client.getCommonPartName(), OrgauthoritiesCommon.class);
676         Assert.assertNotNull(orgAuthority);
677
678         // Update the contents of this resource.
679         orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
680         orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
681         if(logger.isDebugEnabled()){
682             logger.debug("to be updated OrgAuthority");
683             logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
684         }
685
686         // Submit the updated resource to the service and store the response.
687         MultipartOutput output = new MultipartOutput();
688         OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
689         commonPart.getHeaders().add("label", client.getCommonPartName());
690         res = client.update(knownResourceId, output);
691         int statusCode = res.getStatus();
692
693         // Check the status code of the response: does it match the expected response(s)?
694         if(logger.isDebugEnabled()){
695             logger.debug("update: status = " + statusCode);
696         }
697         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
698                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
699         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
700
701         // Retrieve the updated resource and verify that its contents exist.
702         input = (MultipartInput) res.getEntity();
703         OrgauthoritiesCommon updatedOrgAuthority =
704                 (OrgauthoritiesCommon) extractPart(input,
705                         client.getCommonPartName(), OrgauthoritiesCommon.class);
706         Assert.assertNotNull(updatedOrgAuthority);
707
708         // Verify that the updated resource received the correct data.
709         Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
710                 orgAuthority.getDisplayName(),
711                 "Data in updated object did not match submitted data.");
712     }
713
714     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
715         dependsOnMethods = {"readItem", "update"})
716     public void updateItem(String testName) throws Exception {
717
718         // Perform setup.
719         setupUpdate(testName);
720
721         ClientResponse<MultipartInput> res =
722                 client.readItem(knownResourceId, knownItemResourceId);
723         if(logger.isDebugEnabled()){
724             logger.debug(testName + ": read status = " + res.getStatus());
725         }
726         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
727
728         if(logger.isDebugEnabled()){
729             logger.debug("got Organization to update with ID: " +
730                 knownItemResourceId +
731                 " in OrgAuthority: " + knownResourceId );
732         }
733         MultipartInput input = (MultipartInput) res.getEntity();
734         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
735                 client.getItemCommonPartName(), OrganizationsCommon.class);
736         Assert.assertNotNull(organization);
737
738         // Update the contents of this resource.
739         organization.setShortName("updated-" + organization.getShortName());
740         if(logger.isDebugEnabled()){
741             logger.debug("to be updated Organization");
742             logger.debug(objectAsXmlString(organization,
743                 OrganizationsCommon.class));
744         }
745
746         // Submit the updated resource to the service and store the response.
747         MultipartOutput output = new MultipartOutput();
748         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
749         commonPart.getHeaders().add("label", client.getItemCommonPartName());
750         res = client.updateItem(knownResourceId, knownItemResourceId, output);
751         int statusCode = res.getStatus();
752
753         // Check the status code of the response: does it match the expected response(s)?
754         if(logger.isDebugEnabled()){
755             logger.debug("updateItem: status = " + statusCode);
756         }
757         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
758                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
759         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
760
761         // Retrieve the updated resource and verify that its contents exist.
762         input = (MultipartInput) res.getEntity();
763         OrganizationsCommon updatedOrganization =
764                 (OrganizationsCommon) extractPart(input,
765                         client.getItemCommonPartName(), OrganizationsCommon.class);
766         Assert.assertNotNull(updatedOrganization);
767
768         // Verify that the updated resource received the correct data.
769         Assert.assertEquals(updatedOrganization.getShortName(),
770                 organization.getShortName(),
771                 "Data in updated Organization did not match submitted data.");
772     }
773
774     // Failure outcomes
775     // Placeholders until the three tests below can be uncommented.
776     // See Issue CSPACE-401.
777     @Override
778     public void updateWithEmptyEntityBody(String testName) throws Exception {
779     }
780
781     @Override
782     public void updateWithMalformedXml(String testName) throws Exception {
783     }
784
785     @Override
786     public void updateWithWrongXmlSchema(String testName) throws Exception {
787     }
788
789     /*
790     @Override
791     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
792         dependsOnMethods = {"create", "update", "testSubmitRequest"})
793     public void updateWithEmptyEntityBody(String testName) throws Exception {
794
795     // Perform setup.
796     setupUpdateWithEmptyEntityBody(testName);
797
798     // Submit the request to the service and store the response.
799     String method = REQUEST_TYPE.httpMethodName();
800     String url = getResourceURL(knownResourceId);
801     String mediaType = MediaType.APPLICATION_XML;
802     final String entity = "";
803     int statusCode = submitRequest(method, url, mediaType, entity);
804
805     // Check the status code of the response: does it match
806     // the expected response(s)?
807     if(logger.isDebugEnabled()){
808         logger.debug(testName + ": url=" + url +
809             " status=" + statusCode);
810      }
811     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
812     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
813     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
814     }
815
816     @Override
817     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
818         dependsOnMethods = {"create", "update", "testSubmitRequest"})
819     public void updateWithMalformedXml(String testName) throws Exception {
820
821     // Perform setup.
822     setupUpdateWithMalformedXml(testName);
823
824     // Submit the request to the service and store the response.
825     String method = REQUEST_TYPE.httpMethodName();
826     String url = getResourceURL(knownResourceId);
827     String mediaType = MediaType.APPLICATION_XML;
828     final String entity = MALFORMED_XML_DATA;
829     int statusCode = submitRequest(method, url, mediaType, entity);
830
831     // Check the status code of the response: does it match
832     // the expected response(s)?
833     if(logger.isDebugEnabled()){
834         logger.debug(testName + ": url=" + url +
835            " status=" + statusCode);
836      }
837     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
838     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
839     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
840     }
841
842     @Override
843     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
844         dependsOnMethods = {"create", "update", "testSubmitRequest"})
845     public void updateWithWrongXmlSchema(String testName) throws Exception {
846
847     // Perform setup.
848     setupUpdateWithWrongXmlSchema(testName);
849
850     // Submit the request to the service and store the response.
851     String method = REQUEST_TYPE.httpMethodName();
852     String url = getResourceURL(knownResourceId);
853     String mediaType = MediaType.APPLICATION_XML;
854     final String entity = WRONG_XML_SCHEMA_DATA;
855     int statusCode = submitRequest(method, url, mediaType, entity);
856
857     // Check the status code of the response: does it match
858     // the expected response(s)?
859     if(logger.isDebugEnabled()){
860         logger.debug("updateWithWrongXmlSchema: url=" + url +
861             " status=" + statusCode);
862      }
863     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
864     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
865     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
866     }
867      */
868
869
870     @Override
871     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
872         dependsOnMethods = {"update", "testSubmitRequest"})
873     public void updateNonExistent(String testName) throws Exception {
874
875         // Perform setup.
876         setupUpdateNonExistent(testName);
877
878         // Submit the request to the service and store the response.
879         // Note: The ID used in this 'create' call may be arbitrary.
880         // The only relevant ID may be the one used in update(), below.
881
882         // The only relevant ID may be the one used in update(), below.
883         MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
884         ClientResponse<MultipartInput> res =
885                 client.update(NON_EXISTENT_ID, multipart);
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
898     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
899         dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
900     public void updateNonExistentItem(String testName) throws Exception {
901
902         // Perform setup.
903         setupUpdateNonExistent(testName);
904
905         // Submit the request to the service and store the response.
906         // Note: The ID used in this 'create' call may be arbitrary.
907         // The only relevant ID may be the one used in update(), below.
908
909         // The only relevant ID may be the one used in update(), below.
910         Map<String, String> nonexOrgMap = new HashMap<String,String>();
911         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
912         String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
913         MultipartOutput multipart = 
914                 OrgAuthorityClientUtils.createOrganizationInstance(
915                         knownResourceId, refName,
916                         nonexOrgMap, client.getItemCommonPartName() );
917         ClientResponse<MultipartInput> res =
918                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
919         int statusCode = res.getStatus();
920
921         // Check the status code of the response: does it match
922         // the expected response(s)?
923         if(logger.isDebugEnabled()){
924             logger.debug(testName + ": status = " + statusCode);
925         }
926         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
927                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
928         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
929     }
930
931     // ---------------------------------------------------------------
932     // CRUD tests : DELETE tests
933     // ---------------------------------------------------------------
934     // Success outcomes
935     @Override
936     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
937         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
938     public void delete(String testName) throws Exception {
939
940         // Perform setup.
941         setupDelete(testName);
942
943         // Submit the request to the service and store the response.
944         ClientResponse<Response> res = client.delete(knownResourceId);
945         int statusCode = res.getStatus();
946
947         // Check the status code of the response: does it match
948         // the expected response(s)?
949         if(logger.isDebugEnabled()){
950             logger.debug(testName + ": status = " + statusCode);
951         }
952         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
953                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
954         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
955     }
956
957    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
958         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
959             "updateItem", "verifyItemDisplayName"})
960     public void deleteItem(String testName) throws Exception {
961
962         // Perform setup.
963         setupDelete(testName);
964
965         // Submit the request to the service and store the response.
966         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
967         int statusCode = res.getStatus();
968
969         // Check the status code of the response: does it match
970         // the expected response(s)?
971         if(logger.isDebugEnabled()){
972             logger.debug("delete: status = " + statusCode);
973         }
974         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
975                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
976         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
977     }
978
979     // Failure outcomes
980     @Override
981     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
982         dependsOnMethods = {"delete"})
983     public void deleteNonExistent(String testName) throws Exception {
984
985         // Perform setup.
986         setupDeleteNonExistent(testName);
987
988         // Submit the request to the service and store the response.
989         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
990         int statusCode = res.getStatus();
991
992         // Check the status code of the response: does it match
993         // the expected response(s)?
994         if(logger.isDebugEnabled()){
995             logger.debug(testName + ": status = " + statusCode);
996         }
997         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
998                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
999         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1000     }
1001
1002     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1003         dependsOnMethods = {"deleteItem"})
1004     public void deleteNonExistentItem(String testName) {
1005
1006         // Perform setup.
1007         setupDeleteNonExistent(testName);
1008
1009         // Submit the request to the service and store the response.
1010         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1011         int statusCode = res.getStatus();
1012
1013         // Check the status code of the response: does it match
1014         // the expected response(s)?
1015         if(logger.isDebugEnabled()){
1016             logger.debug(testName + ": status = " + statusCode);
1017         }
1018         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1019                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1020         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1021     }
1022
1023     // ---------------------------------------------------------------
1024     // Utility tests : tests of code used in tests above
1025     // ---------------------------------------------------------------
1026     /**
1027      * Tests the code for manually submitting data that is used by several
1028      * of the methods above.
1029      */
1030     @Test(dependsOnMethods = {"create", "read"})
1031     public void testSubmitRequest() {
1032
1033         // Expected status code: 200 OK
1034         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1035
1036         // Submit the request to the service and store the response.
1037         String method = ServiceRequestType.READ.httpMethodName();
1038         String url = getResourceURL(knownResourceId);
1039         int statusCode = submitRequest(method, url);
1040
1041         // Check the status code of the response: does it match
1042         // the expected response(s)?
1043         if(logger.isDebugEnabled()){
1044             logger.debug("testSubmitRequest: url=" + url +
1045                 " status=" + statusCode);
1046         }
1047         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1048
1049     }
1050
1051     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1052     public void testItemSubmitRequest() {
1053
1054         // Expected status code: 200 OK
1055         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1056
1057         // Submit the request to the service and store the response.
1058         String method = ServiceRequestType.READ.httpMethodName();
1059         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1060         int statusCode = submitRequest(method, url);
1061
1062         // Check the status code of the response: does it match
1063         // the expected response(s)?
1064         if(logger.isDebugEnabled()){
1065             logger.debug("testItemSubmitRequest: url=" + url +
1066                 " status=" + statusCode);
1067         }
1068         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1069
1070     }
1071
1072     // ---------------------------------------------------------------
1073     // Cleanup of resources created during testing
1074     // ---------------------------------------------------------------
1075     
1076     /**
1077      * Deletes all resources created by tests, after all tests have been run.
1078      *
1079      * This cleanup method will always be run, even if one or more tests fail.
1080      * For this reason, it attempts to remove all resources created
1081      * at any point during testing, even if some of those resources
1082      * may be expected to be deleted by certain tests.
1083      */
1084     @AfterClass(alwaysRun=true)
1085     public void cleanUp() {
1086         if (logger.isDebugEnabled()) {
1087             logger.debug("Cleaning up temporary resources created for testing ...");
1088         }
1089         // Clean up organization resources.
1090         String orgAuthorityResourceId;
1091         String organizationResourceId;
1092         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1093             organizationResourceId = entry.getKey();
1094             orgAuthorityResourceId = entry.getValue();
1095             // Note: Any non-success responses are ignored and not reported.
1096             ClientResponse<Response> res =
1097                 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1098         }
1099         // Clean up orgAuthority resources.
1100         for (String resourceId : allResourceIdsCreated) {
1101             // Note: Any non-success responses are ignored and not reported.
1102             ClientResponse<Response> res = client.delete(resourceId);
1103         }
1104     }
1105
1106     // ---------------------------------------------------------------
1107     // Utility methods used by tests above
1108     // ---------------------------------------------------------------
1109     @Override
1110     public String getServicePathComponent() {
1111         return SERVICE_PATH_COMPONENT;
1112     }
1113
1114     public String getItemServicePathComponent() {
1115         return ITEM_SERVICE_PATH_COMPONENT;
1116     }
1117
1118     /**
1119      * Returns the root URL for a service.
1120      *
1121      * This URL consists of a base URL for all services, followed by
1122      * a path component for the owning orgAuthority, followed by the 
1123      * path component for the items.
1124      *
1125      * @return The root URL for a service.
1126      */
1127     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1128         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1129     }
1130
1131     /**
1132      * Returns the URL of a specific resource managed by a service, and
1133      * designated by an identifier (such as a universally unique ID, or UUID).
1134      *
1135      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
1136      *
1137      * @return The URL of a specific resource managed by a service.
1138      */
1139     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1140         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1141     }
1142
1143     private MultipartOutput createOrgAuthorityInstance(String identifier) {
1144         String displayName = "displayName-" + identifier;
1145         String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1146         return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1147                                 displayName, refName, 
1148                                 client.getCommonPartName());
1149     }
1150 }