]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f2a117c83f4af5dff18ec1526cea6332b4e04793
[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         boolean showFull = true;
403         if(showFull && logger.isDebugEnabled()){
404             logger.debug(testName + ": returned payload:");
405             logger.debug(objectAsXmlString(organization,
406                     OrganizationsCommon.class));
407         }
408         Assert.assertEquals(organization.getInAuthority(), knownResourceId);
409     }
410
411     // Failure outcomes
412     @Override
413     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
414         dependsOnMethods = {"read"})
415     public void readNonExistent(String testName) {
416
417         // Perform setup.
418         setupReadNonExistent(testName);
419
420         // Submit the request to the service and store the response.
421         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
422         int statusCode = res.getStatus();
423
424         // Check the status code of the response: does it match
425         // the expected response(s)?
426         if(logger.isDebugEnabled()){
427             logger.debug(testName + ": status = " + statusCode);
428         }
429         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
430                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
431         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
432     }
433
434     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
435         dependsOnMethods = {"readItem", "readNonExistent"})
436     public void readItemNonExistent(String testName) {
437
438         // Perform setup.
439         setupReadNonExistent(testName);
440
441         // Submit the request to the service and store the response.
442         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
443         int statusCode = res.getStatus();
444
445         // Check the status code of the response: does it match
446         // the expected response(s)?
447         if(logger.isDebugEnabled()){
448             logger.debug(testName + ": status = " + statusCode);
449         }
450         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
453     }
454     // ---------------------------------------------------------------
455     // CRUD tests : READ_LIST tests
456     // ---------------------------------------------------------------
457     // Success outcomes
458
459     @Override
460     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
461         dependsOnMethods = {"createList", "read"})
462     public void readList(String testName) throws Exception {
463
464         // Perform setup.
465         setupReadList(testName);
466
467         // Submit the request to the service and store the response.
468         ClientResponse<OrgauthoritiesCommonList> res = client.readList();
469         OrgauthoritiesCommonList list = res.getEntity();
470         int statusCode = res.getStatus();
471
472         // Check the status code of the response: does it match
473         // the expected response(s)?
474         if(logger.isDebugEnabled()){
475             logger.debug(testName + ": status = " + statusCode);
476         }
477         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
480
481         // Optionally output additional data about list members for debugging.
482         boolean iterateThroughList = false;
483         if (iterateThroughList && logger.isDebugEnabled()) {
484             List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
485                     list.getOrgauthorityListItem();
486             int i = 0;
487             for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
488                 String csid = item.getCsid();
489                 logger.debug(testName + ": list-item[" + i + "] csid=" +
490                         csid);
491                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
492                         item.getDisplayName());
493                 logger.debug(testName + ": list-item[" + i + "] URI=" +
494                         item.getUri());
495                 readItemList(csid);
496                 i++;
497             }
498         }
499     }
500
501     @Test(dependsOnMethods = {"createList", "readItem"})
502     public void readItemList() {
503         readItemList(knownResourceId);
504     }
505
506     private void readItemList(String vcsid) {
507
508         final String testName = "readItemList";
509
510         // Perform setup.
511         setupReadList(testName);
512
513         // Submit the request to the service and store the response.
514         ClientResponse<OrganizationsCommonList> res =
515                 client.readItemList(vcsid);
516         OrganizationsCommonList list = res.getEntity();
517         int statusCode = res.getStatus();
518
519         // Check the status code of the response: does it match
520         // the expected response(s)?
521         if(logger.isDebugEnabled()){
522             logger.debug("  " + testName + ": status = " + statusCode);
523         }
524         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
525                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
526         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
527
528         List<OrganizationsCommonList.OrganizationListItem> items =
529             list.getOrganizationListItem();
530         int nItemsReturned = items.size();
531         if(logger.isDebugEnabled()){
532             logger.debug("  " + testName + ": Expected "
533                         + nItemsToCreateInList+" items; got: "+nItemsReturned);
534         }
535         Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
536
537         int i = 0;
538         for (OrganizationsCommonList.OrganizationListItem item : items) {
539                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
540                 Assert.assertTrue((null != item.getDisplayName()), "Item refName is null!");
541                 // Optionally output additional data about list members for debugging.
542                 boolean showDetails = true;
543                 if (showDetails && logger.isDebugEnabled()) {
544                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
545                         item.getCsid());
546                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
547                         item.getRefName());
548                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
549                         item.getDisplayName());
550                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
551                         item.getUri());
552             }
553             i++;
554         }
555     }
556
557     // Failure outcomes
558     // None at present.
559     // ---------------------------------------------------------------
560     // CRUD tests : UPDATE tests
561     // ---------------------------------------------------------------
562     // Success outcomes
563     @Override
564     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
565         dependsOnMethods = {"read"})
566     public void update(String testName) throws Exception {
567
568         // Perform setup.
569         setupUpdate(testName);
570
571         // Retrieve the contents of a resource to update.
572         ClientResponse<MultipartInput> res =
573                 client.read(knownResourceId);
574         if(logger.isDebugEnabled()){
575             logger.debug(testName + ": read status = " + res.getStatus());
576         }
577         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
578
579         if(logger.isDebugEnabled()){
580             logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
581         }
582         MultipartInput input = (MultipartInput) res.getEntity();
583         OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
584                 client.getCommonPartName(), OrgauthoritiesCommon.class);
585         Assert.assertNotNull(orgAuthority);
586
587         // Update the contents of this resource.
588         orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
589         orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
590         if(logger.isDebugEnabled()){
591             logger.debug("to be updated OrgAuthority");
592             logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
593         }
594
595         // Submit the updated resource to the service and store the response.
596         MultipartOutput output = new MultipartOutput();
597         OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
598         commonPart.getHeaders().add("label", client.getCommonPartName());
599         res = client.update(knownResourceId, output);
600         int statusCode = res.getStatus();
601
602         // Check the status code of the response: does it match the expected response(s)?
603         if(logger.isDebugEnabled()){
604             logger.debug("update: status = " + statusCode);
605         }
606         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
607                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
608         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
609
610         // Retrieve the updated resource and verify that its contents exist.
611         input = (MultipartInput) res.getEntity();
612         OrgauthoritiesCommon updatedOrgAuthority =
613                 (OrgauthoritiesCommon) extractPart(input,
614                         client.getCommonPartName(), OrgauthoritiesCommon.class);
615         Assert.assertNotNull(updatedOrgAuthority);
616
617         // Verify that the updated resource received the correct data.
618         Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
619                 orgAuthority.getDisplayName(),
620                 "Data in updated object did not match submitted data.");
621     }
622
623     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
624         dependsOnMethods = {"readItem", "update"})
625     public void updateItem(String testName) throws Exception {
626
627         // Perform setup.
628         setupUpdate(testName);
629
630         ClientResponse<MultipartInput> res =
631                 client.readItem(knownResourceId, knownItemResourceId);
632         if(logger.isDebugEnabled()){
633             logger.debug(testName + ": read status = " + res.getStatus());
634         }
635         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
636
637         if(logger.isDebugEnabled()){
638             logger.debug("got Organization to update with ID: " +
639                 knownItemResourceId +
640                 " in OrgAuthority: " + knownResourceId );
641         }
642         MultipartInput input = (MultipartInput) res.getEntity();
643         OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
644                 client.getItemCommonPartName(), OrganizationsCommon.class);
645         Assert.assertNotNull(organization);
646
647         // Update the contents of this resource.
648         organization.setShortName("updated-" + organization.getShortName());
649         if(logger.isDebugEnabled()){
650             logger.debug("to be updated Organization");
651             logger.debug(objectAsXmlString(organization,
652                 OrganizationsCommon.class));
653         }
654
655         // Submit the updated resource to the service and store the response.
656         MultipartOutput output = new MultipartOutput();
657         OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
658         commonPart.getHeaders().add("label", client.getItemCommonPartName());
659         res = client.updateItem(knownResourceId, knownItemResourceId, output);
660         int statusCode = res.getStatus();
661
662         // Check the status code of the response: does it match the expected response(s)?
663         if(logger.isDebugEnabled()){
664             logger.debug("updateItem: status = " + statusCode);
665         }
666         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
667                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
668         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
669
670         // Retrieve the updated resource and verify that its contents exist.
671         input = (MultipartInput) res.getEntity();
672         OrganizationsCommon updatedOrganization =
673                 (OrganizationsCommon) extractPart(input,
674                         client.getItemCommonPartName(), OrganizationsCommon.class);
675         Assert.assertNotNull(updatedOrganization);
676
677         // Verify that the updated resource received the correct data.
678         Assert.assertEquals(updatedOrganization.getShortName(),
679                 organization.getShortName(),
680                 "Data in updated Organization did not match submitted data.");
681     }
682
683     // Failure outcomes
684     // Placeholders until the three tests below can be uncommented.
685     // See Issue CSPACE-401.
686     @Override
687     public void updateWithEmptyEntityBody(String testName) throws Exception {
688     }
689
690     @Override
691     public void updateWithMalformedXml(String testName) throws Exception {
692     }
693
694     @Override
695     public void updateWithWrongXmlSchema(String testName) throws Exception {
696     }
697
698     /*
699     @Override
700     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
701         dependsOnMethods = {"create", "update", "testSubmitRequest"})
702     public void updateWithEmptyEntityBody(String testName) throws Exception {
703
704     // Perform setup.
705     setupUpdateWithEmptyEntityBody(testName);
706
707     // Submit the request to the service and store the response.
708     String method = REQUEST_TYPE.httpMethodName();
709     String url = getResourceURL(knownResourceId);
710     String mediaType = MediaType.APPLICATION_XML;
711     final String entity = "";
712     int statusCode = submitRequest(method, url, mediaType, entity);
713
714     // Check the status code of the response: does it match
715     // the expected response(s)?
716     if(logger.isDebugEnabled()){
717         logger.debug(testName + ": url=" + url +
718             " status=" + statusCode);
719      }
720     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
721     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
722     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
723     }
724
725     @Override
726     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
727         dependsOnMethods = {"create", "update", "testSubmitRequest"})
728     public void updateWithMalformedXml(String testName) throws Exception {
729
730     // Perform setup.
731     setupUpdateWithMalformedXml(testName);
732
733     // Submit the request to the service and store the response.
734     String method = REQUEST_TYPE.httpMethodName();
735     String url = getResourceURL(knownResourceId);
736     String mediaType = MediaType.APPLICATION_XML;
737     final String entity = MALFORMED_XML_DATA;
738     int statusCode = submitRequest(method, url, mediaType, entity);
739
740     // Check the status code of the response: does it match
741     // the expected response(s)?
742     if(logger.isDebugEnabled()){
743         logger.debug(testName + ": url=" + url +
744            " status=" + statusCode);
745      }
746     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
747     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
748     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
749     }
750
751     @Override
752     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
753         dependsOnMethods = {"create", "update", "testSubmitRequest"})
754     public void updateWithWrongXmlSchema(String testName) throws Exception {
755
756     // Perform setup.
757     setupUpdateWithWrongXmlSchema(testName);
758
759     // Submit the request to the service and store the response.
760     String method = REQUEST_TYPE.httpMethodName();
761     String url = getResourceURL(knownResourceId);
762     String mediaType = MediaType.APPLICATION_XML;
763     final String entity = WRONG_XML_SCHEMA_DATA;
764     int statusCode = submitRequest(method, url, mediaType, entity);
765
766     // Check the status code of the response: does it match
767     // the expected response(s)?
768     if(logger.isDebugEnabled()){
769         logger.debug("updateWithWrongXmlSchema: url=" + url +
770             " status=" + statusCode);
771      }
772     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
773     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
774     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
775     }
776      */
777
778
779     @Override
780     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
781         dependsOnMethods = {"update", "testSubmitRequest"})
782     public void updateNonExistent(String testName) throws Exception {
783
784         // Perform setup.
785         setupUpdateNonExistent(testName);
786
787         // Submit the request to the service and store the response.
788         // Note: The ID used in this 'create' call may be arbitrary.
789         // The only relevant ID may be the one used in update(), below.
790
791         // The only relevant ID may be the one used in update(), below.
792         MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
793         ClientResponse<MultipartInput> res =
794                 client.update(NON_EXISTENT_ID, multipart);
795         int statusCode = res.getStatus();
796
797         // Check the status code of the response: does it match
798         // the expected response(s)?
799         if(logger.isDebugEnabled()){
800             logger.debug(testName + ": status = " + statusCode);
801         }
802         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
803                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
804         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
805     }
806
807     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
808         dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
809     public void updateNonExistentItem(String testName) throws Exception {
810
811         // Perform setup.
812         setupUpdateNonExistent(testName);
813
814         // Submit the request to the service and store the response.
815         // Note: The ID used in this 'create' call may be arbitrary.
816         // The only relevant ID may be the one used in update(), below.
817
818         // The only relevant ID may be the one used in update(), below.
819         Map<String, String> nonexOrgMap = new HashMap<String,String>();
820         nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
821         MultipartOutput multipart = 
822                 OrgAuthorityClientUtils.createOrganizationInstance(
823                         knownResourceId, createRefName(NON_EXISTENT_ID),
824                         nonexOrgMap, client.getItemCommonPartName() );
825         ClientResponse<MultipartInput> res =
826                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
827         int statusCode = res.getStatus();
828
829         // Check the status code of the response: does it match
830         // the expected response(s)?
831         if(logger.isDebugEnabled()){
832             logger.debug(testName + ": status = " + statusCode);
833         }
834         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
835                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
836         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
837     }
838
839     // ---------------------------------------------------------------
840     // CRUD tests : DELETE tests
841     // ---------------------------------------------------------------
842     // Success outcomes
843     @Override
844     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
845         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
846     public void delete(String testName) throws Exception {
847
848         // Perform setup.
849         setupDelete(testName);
850
851         // Submit the request to the service and store the response.
852         ClientResponse<Response> res = client.delete(knownResourceId);
853         int statusCode = res.getStatus();
854
855         // Check the status code of the response: does it match
856         // the expected response(s)?
857         if(logger.isDebugEnabled()){
858             logger.debug(testName + ": status = " + statusCode);
859         }
860         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
861                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
862         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
863     }
864
865    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
866         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
867             "updateItem"})
868     public void deleteItem(String testName) throws Exception {
869
870         // Perform setup.
871         setupDelete(testName);
872
873         // Submit the request to the service and store the response.
874         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
875         int statusCode = res.getStatus();
876
877         // Check the status code of the response: does it match
878         // the expected response(s)?
879         if(logger.isDebugEnabled()){
880             logger.debug("delete: status = " + statusCode);
881         }
882         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
883                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
884         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
885     }
886
887     // Failure outcomes
888     @Override
889     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
890         dependsOnMethods = {"delete"})
891     public void deleteNonExistent(String testName) throws Exception {
892
893         // Perform setup.
894         setupDeleteNonExistent(testName);
895
896         // Submit the request to the service and store the response.
897         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
898         int statusCode = res.getStatus();
899
900         // Check the status code of the response: does it match
901         // the expected response(s)?
902         if(logger.isDebugEnabled()){
903             logger.debug(testName + ": status = " + statusCode);
904         }
905         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
906                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
907         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
908     }
909
910     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
911         dependsOnMethods = {"deleteItem"})
912     public void deleteNonExistentItem(String testName) {
913
914         // Perform setup.
915         setupDeleteNonExistent(testName);
916
917         // Submit the request to the service and store the response.
918         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
919         int statusCode = res.getStatus();
920
921         // Check the status code of the response: does it match
922         // the expected response(s)?
923         if(logger.isDebugEnabled()){
924             logger.debug(testName + ": status = " + statusCode);
925         }
926         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
927                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
928         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
929     }
930
931     // ---------------------------------------------------------------
932     // Utility tests : tests of code used in tests above
933     // ---------------------------------------------------------------
934     /**
935      * Tests the code for manually submitting data that is used by several
936      * of the methods above.
937      */
938     @Test(dependsOnMethods = {"create", "read"})
939     public void testSubmitRequest() {
940
941         // Expected status code: 200 OK
942         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
943
944         // Submit the request to the service and store the response.
945         String method = ServiceRequestType.READ.httpMethodName();
946         String url = getResourceURL(knownResourceId);
947         int statusCode = submitRequest(method, url);
948
949         // Check the status code of the response: does it match
950         // the expected response(s)?
951         if(logger.isDebugEnabled()){
952             logger.debug("testSubmitRequest: url=" + url +
953                 " status=" + statusCode);
954         }
955         Assert.assertEquals(statusCode, EXPECTED_STATUS);
956
957     }
958
959     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
960     public void testItemSubmitRequest() {
961
962         // Expected status code: 200 OK
963         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
964
965         // Submit the request to the service and store the response.
966         String method = ServiceRequestType.READ.httpMethodName();
967         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
968         int statusCode = submitRequest(method, url);
969
970         // Check the status code of the response: does it match
971         // the expected response(s)?
972         if(logger.isDebugEnabled()){
973             logger.debug("testItemSubmitRequest: url=" + url +
974                 " status=" + statusCode);
975         }
976         Assert.assertEquals(statusCode, EXPECTED_STATUS);
977
978     }
979
980     // ---------------------------------------------------------------
981     // Cleanup of resources created during testing
982     // ---------------------------------------------------------------
983     
984     /**
985      * Deletes all resources created by tests, after all tests have been run.
986      *
987      * This cleanup method will always be run, even if one or more tests fail.
988      * For this reason, it attempts to remove all resources created
989      * at any point during testing, even if some of those resources
990      * may be expected to be deleted by certain tests.
991      */
992     @AfterClass(alwaysRun=true)
993     public void cleanUp() {
994         if (logger.isDebugEnabled()) {
995             logger.debug("Cleaning up temporary resources created for testing ...");
996         }
997         // Clean up organization resources.
998         String orgAuthorityResourceId;
999         String organizationResourceId;
1000         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1001             organizationResourceId = entry.getKey();
1002             orgAuthorityResourceId = entry.getValue();
1003             // Note: Any non-success responses are ignored and not reported.
1004             ClientResponse<Response> res =
1005                 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1006         }
1007         // Clean up orgAuthority resources.
1008         for (String resourceId : allResourceIdsCreated) {
1009             // Note: Any non-success responses are ignored and not reported.
1010             ClientResponse<Response> res = client.delete(resourceId);
1011         }
1012     }
1013
1014     // ---------------------------------------------------------------
1015     // Utility methods used by tests above
1016     // ---------------------------------------------------------------
1017     @Override
1018     public String getServicePathComponent() {
1019         return SERVICE_PATH_COMPONENT;
1020     }
1021
1022     public String getItemServicePathComponent() {
1023         return ITEM_SERVICE_PATH_COMPONENT;
1024     }
1025
1026     /**
1027      * Returns the root URL for a service.
1028      *
1029      * This URL consists of a base URL for all services, followed by
1030      * a path component for the owning orgAuthority, followed by the 
1031      * path component for the items.
1032      *
1033      * @return The root URL for a service.
1034      */
1035     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1036         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1037     }
1038
1039     /**
1040      * Returns the URL of a specific resource managed by a service, and
1041      * designated by an identifier (such as a universally unique ID, or UUID).
1042      *
1043      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
1044      *
1045      * @return The URL of a specific resource managed by a service.
1046      */
1047     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1048         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1049     }
1050
1051     private MultipartOutput createOrgAuthorityInstance(String identifier) {
1052         String displayName = "displayName-" + identifier;
1053         String refName = createRefName(displayName);
1054         return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1055                                 displayName, refName, 
1056                                 client.getCommonPartName());
1057     }
1058 }