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