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