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