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