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