]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6da36afc06f16bd27186554032c9356d4614f14a
[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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.VocabularyClient;
30 import org.collectionspace.services.vocabulary.VocabulariesCommon;
31 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
32 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
33 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
34
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43
44 /**
45  * VocabularyServiceTest, carries out tests against a
46  * deployed and running Vocabulary Service.
47  *
48  * $LastChangedRevision: 753 $
49  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
50  */
51 public class VocabularyServiceTest extends AbstractServiceTest {
52
53     private final Logger logger =
54         LoggerFactory.getLogger(VocabularyServiceTest.class);
55
56     // Instance variables specific to this test.
57     private VocabularyClient client = new VocabularyClient();
58     final String SERVICE_PATH_COMPONENT = "vocabularies";
59     final String ITEM_SERVICE_PATH_COMPONENT = "items";
60     private String knownResourceId = null;
61     private String knownResourceRefName = null;
62     private String knownItemResourceId = null;
63     
64     protected String createRefName(String displayName) {
65         return displayName.replaceAll("\\W", "");
66     }    
67
68     // ---------------------------------------------------------------
69     // CRUD tests : CREATE tests
70     // ---------------------------------------------------------------
71     // Success outcomes
72     @Override
73     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
74     public void create(String testName) throws Exception {
75
76         // Perform setup, such as initializing the type of service request
77         // (e.g. CREATE, DELETE), its valid and expected status codes, and
78         // its associated HTTP method name (e.g. POST, DELETE).
79         setupCreate(testName);
80
81         // Submit the request to the service and store the response.
82         String identifier = createIdentifier();
83         String displayName = "displayName-" + identifier;
84         String refName = createRefName(displayName);
85         String typeName = "vocabType-" + identifier;
86         MultipartOutput multipart = 
87                 createVocabularyInstance(displayName, refName, typeName);
88         ClientResponse<Response> res = client.create(multipart);
89         int statusCode = res.getStatus();
90
91         // Check the status code of the response: does it match
92         // the expected response(s)?
93         //
94         // Specifically:
95         // Does it fall within the set of valid status codes?
96         // Does it exactly match the expected status code?
97         if(logger.isDebugEnabled()){
98             logger.debug(testName + ": status = " + statusCode);
99         }
100         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
101                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
102         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
103
104         // Store the ID returned from this create operation
105         // for additional tests below.
106         knownResourceId = extractId(res);
107         knownResourceRefName = refName;
108         if(logger.isDebugEnabled()){
109             logger.debug("create: knownResourceId=" + knownResourceId);
110         }
111     }
112
113     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
114         dependsOnMethods = {"create"})
115     public void createItem(String testName) {
116         setupCreate(testName);
117
118         knownItemResourceId = createItemInVocab(knownResourceId);
119         if(logger.isDebugEnabled()){
120             logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
121         }
122     }
123
124     private String createItemInVocab(String vcsid) {
125
126         final String testName = "createItemInVocab";
127         if(logger.isDebugEnabled()){
128             logger.debug(testName + ":...");
129         }
130
131         // Submit the request to the service and store the response.
132         String identifier = createIdentifier();
133         String refName = createRefName(identifier);
134         MultipartOutput multipart = createVocabularyItemInstance(vcsid, identifier, refName);
135         ClientResponse<Response> res = client.createItem(vcsid, multipart);
136         int statusCode = res.getStatus();
137
138         // Check the status code of the response: does it match
139         // the expected response(s)?
140         if(logger.isDebugEnabled()){
141             logger.debug(testName + ": status = " + statusCode);
142         }
143         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
144                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
146
147         return extractId(res);
148     }
149
150     @Override
151     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
152             dependsOnMethods = {"create", "createItem"})
153     public void createList(String testName) throws Exception {
154         for (int i = 0; i < 3; i++) {
155             create(testName);
156             // Add 3 items to each vocab
157             for (int j = 0; j < 3; j++) {
158                 createItem(testName);
159             }
160         }
161     }
162
163     // Failure outcomes
164     // Placeholders until the three tests below can be uncommented.
165     // See Issue CSPACE-401.
166     @Override
167     public void createWithEmptyEntityBody(String testName) throws Exception {
168     }
169
170     @Override
171     public void createWithMalformedXml(String testName) throws Exception {
172     }
173
174     @Override
175     public void createWithWrongXmlSchema(String testName) throws Exception {
176     }
177
178     /*
179     @Override
180     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
181         dependsOnMethods = {"create", "testSubmitRequest"})
182     public void createWithEmptyEntityBody(String testName) throws Exception {
183
184     // Perform setup.
185     setupCreateWithEmptyEntityBody(testName);
186
187     // Submit the request to the service and store the response.
188     String method = REQUEST_TYPE.httpMethodName();
189     String url = getServiceRootURL();
190     String mediaType = MediaType.APPLICATION_XML;
191     final String entity = "";
192     int statusCode = submitRequest(method, url, mediaType, entity);
193
194     // Check the status code of the response: does it match
195     // the expected response(s)?
196     if(logger.isDebugEnabled()) {
197         logger.debug(testName + ": url=" + url +
198             " status=" + statusCode);
199      }
200     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203     }
204
205     @Override
206     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
207         dependsOnMethods = {"create", "testSubmitRequest"})
208     public void createWithMalformedXml(String testName) throws Exception {
209
210     // Perform setup.
211     setupCreateWithMalformedXml(testName);
212
213     // Submit the request to the service and store the response.
214     String method = REQUEST_TYPE.httpMethodName();
215     String url = getServiceRootURL();
216     String mediaType = MediaType.APPLICATION_XML;
217     final String entity = MALFORMED_XML_DATA; // Constant from base class.
218     int statusCode = submitRequest(method, url, mediaType, entity);
219
220     // Check the status code of the response: does it match
221     // the expected response(s)?
222     if(logger.isDebugEnabled()){
223         logger.debug(testName + ": url=" + url +
224             " status=" + statusCode);
225      }
226     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
229     }
230
231     @Override
232     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
233         dependsOnMethods = {"create", "testSubmitRequest"})
234     public void createWithWrongXmlSchema(String testName) throws Exception {
235
236     // Perform setup.
237     setupCreateWithWrongXmlSchema(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 = WRONG_XML_SCHEMA_DATA;
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
258     // ---------------------------------------------------------------
259     // CRUD tests : READ tests
260     // ---------------------------------------------------------------
261     // Success outcomes
262     @Override
263     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
264         dependsOnMethods = {"create"})
265     public void read(String testName) throws Exception {
266
267         // Perform setup.
268         setupRead();
269
270         // Submit the request to the service and store the response.
271         ClientResponse<MultipartInput> res = client.read(knownResourceId);
272         int statusCode = res.getStatus();
273
274         // Check the status code of the response: does it match
275         // the expected response(s)?
276         if(logger.isDebugEnabled()){
277             logger.debug(testName + ": status = " + statusCode);
278         }
279         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
280                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
281         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
282         //FIXME: remove the following try catch once Aron fixes signatures
283         try {
284             MultipartInput input = (MultipartInput) res.getEntity();
285             VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
286                     client.getCommonPartName(), VocabulariesCommon.class);
287             Assert.assertNotNull(vocabulary);
288         } catch (Exception e) {
289             throw new RuntimeException(e);
290         }
291     }
292
293     /*
294     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
295             dependsOnMethods = {"read"})
296         public void readByName(String testName) throws Exception {
297
298             // Perform setup.
299             setupRead();
300
301             // Submit the request to the service and store the response.
302             ClientResponse<MultipartInput> res = client.read(knownResourceId);
303             int statusCode = res.getStatus();
304
305             // Check the status code of the response: does it match
306             // the expected response(s)?
307             if(logger.isDebugEnabled()){
308                 logger.debug(testName + ": status = " + statusCode);
309             }
310             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
313             //FIXME: remove the following try catch once Aron fixes signatures
314             try {
315                 MultipartInput input = (MultipartInput) res.getEntity();
316                 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
317                         client.getCommonPartName(), VocabulariesCommon.class);
318                 Assert.assertNotNull(vocabulary);
319             } catch (Exception e) {
320                 throw new RuntimeException(e);
321             }
322         }
323     */
324
325     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
326         dependsOnMethods = {"createItem", "read"})
327     public void readItem(String testName) throws Exception {
328
329         // Perform setup.
330         setupRead(testName);
331
332         // Submit the request to the service and store the response.
333         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
334         int statusCode = res.getStatus();
335
336         // Check the status code of the response: does it match
337         // the expected response(s)?
338         if(logger.isDebugEnabled()){
339             logger.debug(testName + ": status = " + statusCode);
340         }
341         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
342                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
343         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
344
345         // Check whether we've received a vocabulary item.
346         MultipartInput input = (MultipartInput) res.getEntity();
347         VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
348                 client.getItemCommonPartName(), VocabularyitemsCommon.class);
349         Assert.assertNotNull(vocabularyItem);
350
351     }
352
353     // Failure outcomes
354     @Override
355     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
356         dependsOnMethods = {"read"})
357     public void readNonExistent(String testName) {
358
359         // Perform setup.
360         setupReadNonExistent(testName);
361
362         // Submit the request to the service and store the response.
363         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
364         int statusCode = res.getStatus();
365
366         // Check the status code of the response: does it match
367         // the expected response(s)?
368         if(logger.isDebugEnabled()){
369             logger.debug(testName + ": status = " + statusCode);
370         }
371         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
372                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
373         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
374     }
375
376     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
377         dependsOnMethods = {"readItem", "readNonExistent"})
378     public void readItemNonExistent(String testName) {
379
380         // Perform setup.
381         setupReadNonExistent(testName);
382
383         // Submit the request to the service and store the response.
384         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
385         int statusCode = res.getStatus();
386
387         // Check the status code of the response: does it match
388         // the expected response(s)?
389         if(logger.isDebugEnabled()){
390             logger.debug(testName + ": status = " + statusCode);
391         }
392         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395     }
396     // ---------------------------------------------------------------
397     // CRUD tests : READ_LIST tests
398     // ---------------------------------------------------------------
399     // Success outcomes
400
401     @Override
402     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
403         dependsOnMethods = {"read"})
404     public void readList(String testName) throws Exception {
405
406         // Perform setup.
407         setupReadList(testName);
408
409         // Submit the request to the service and store the response.
410         ClientResponse<VocabulariesCommonList> res = client.readList();
411         VocabulariesCommonList list = res.getEntity();
412         int statusCode = res.getStatus();
413
414         // Check the status code of the response: does it match
415         // the expected response(s)?
416         if(logger.isDebugEnabled()){
417             logger.debug(testName + ": status = " + statusCode);
418         }
419         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
422
423         // Optionally output additional data about list members for debugging.
424         boolean iterateThroughList = false;
425         if (iterateThroughList && logger.isDebugEnabled()) {
426             List<VocabulariesCommonList.VocabularyListItem> items =
427                     list.getVocabularyListItem();
428             int i = 0;
429             for (VocabulariesCommonList.VocabularyListItem item : items) {
430                 String csid = item.getCsid();
431                 logger.debug(testName + ": list-item[" + i + "] csid=" +
432                         csid);
433                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
434                         item.getDisplayName());
435                 logger.debug(testName + ": list-item[" + i + "] URI=" +
436                         item.getUri());
437                 readItemList(csid);
438                 i++;
439             }
440         }
441     }
442
443     @Test(dependsOnMethods = {"readItem"})
444     public void readItemList() {
445         readItemList(knownResourceId);
446     }
447
448     private void readItemList(String vcsid) {
449
450         final String testName = "readItemList";
451
452         // Perform setup.
453         setupReadList(testName);
454
455         // Submit the request to the service and store the response.
456         ClientResponse<VocabularyitemsCommonList> res =
457                 client.readItemList(vcsid);
458         VocabularyitemsCommonList list = res.getEntity();
459         int statusCode = res.getStatus();
460
461         // Check the status code of the response: does it match
462         // the expected response(s)?
463         if(logger.isDebugEnabled()){
464             logger.debug("  " + testName + ": status = " + statusCode);
465         }
466         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
467                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
468         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
469
470         // Optionally output additional data about list members for debugging.
471         boolean iterateThroughList = false;
472         if (iterateThroughList && logger.isDebugEnabled()) {
473             List<VocabularyitemsCommonList.VocabularyitemListItem> items =
474                     list.getVocabularyitemListItem();
475             int i = 0;
476             for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
477                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
478                         item.getCsid());
479                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
480                         item.getDisplayName());
481                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
482                         item.getUri());
483                 i++;
484             }
485         }
486     }
487
488     // Failure outcomes
489     // None at present.
490     // ---------------------------------------------------------------
491     // CRUD tests : UPDATE tests
492     // ---------------------------------------------------------------
493     // Success outcomes
494     @Override
495     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
496         dependsOnMethods = {"read"})
497     public void update(String testName) throws Exception {
498
499         // Perform setup.
500         setupUpdate(testName);
501
502         // Retrieve the contents of a resource to update.
503         ClientResponse<MultipartInput> res =
504                 client.read(knownResourceId);
505         if(logger.isDebugEnabled()){
506             logger.debug(testName + ": read status = " + res.getStatus());
507         }
508         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
509
510         if(logger.isDebugEnabled()){
511             logger.debug("got Vocabulary to update with ID: " + knownResourceId);
512         }
513         MultipartInput input = (MultipartInput) res.getEntity();
514         VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
515                 client.getCommonPartName(), VocabulariesCommon.class);
516         Assert.assertNotNull(vocabulary);
517
518         // Update the contents of this resource.
519         vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
520         vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
521         if(logger.isDebugEnabled()){
522             logger.debug("to be updated Vocabulary");
523             logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
524         }
525
526         // Submit the updated resource to the service and store the response.
527         MultipartOutput output = new MultipartOutput();
528         OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
529         commonPart.getHeaders().add("label", client.getCommonPartName());
530         res = client.update(knownResourceId, output);
531         int statusCode = res.getStatus();
532
533         // Check the status code of the response: does it match the expected response(s)?
534         if(logger.isDebugEnabled()){
535             logger.debug("update: status = " + statusCode);
536         }
537         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
538                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
539         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
540
541         // Retrieve the updated resource and verify that its contents exist.
542         input = (MultipartInput) res.getEntity();
543         VocabulariesCommon updatedVocabulary =
544                 (VocabulariesCommon) extractPart(input,
545                         client.getCommonPartName(), VocabulariesCommon.class);
546         Assert.assertNotNull(updatedVocabulary);
547
548         // Verify that the updated resource received the correct data.
549         Assert.assertEquals(updatedVocabulary.getDisplayName(),
550                 vocabulary.getDisplayName(),
551                 "Data in updated object did not match submitted data.");
552     }
553
554     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
555         dependsOnMethods = {"readItem", "update"})
556     public void updateItem(String testName) throws Exception {
557
558         // Perform setup.
559         setupUpdate(testName);
560
561         ClientResponse<MultipartInput> res =
562                 client.readItem(knownResourceId, knownItemResourceId);
563         if(logger.isDebugEnabled()){
564             logger.debug(testName + ": read status = " + res.getStatus());
565         }
566         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
567
568         if(logger.isDebugEnabled()){
569             logger.debug("got VocabularyItem to update with ID: " +
570                 knownItemResourceId +
571                 " in Vocab: " + knownResourceId );
572         }
573         MultipartInput input = (MultipartInput) res.getEntity();
574         VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
575                 client.getItemCommonPartName(), VocabularyitemsCommon.class);
576         Assert.assertNotNull(vocabularyItem);
577
578         // Update the contents of this resource.
579         vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
580         if(logger.isDebugEnabled()){
581             logger.debug("to be updated VocabularyItem");
582             logger.debug(objectAsXmlString(vocabularyItem,
583                 VocabularyitemsCommon.class));
584         }
585
586         // Submit the updated resource to the service and store the response.
587         MultipartOutput output = new MultipartOutput();
588         OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
589         commonPart.getHeaders().add("label", client.getItemCommonPartName());
590         res = client.updateItem(knownResourceId, knownItemResourceId, output);
591         int statusCode = res.getStatus();
592
593         // Check the status code of the response: does it match the expected response(s)?
594         if(logger.isDebugEnabled()){
595             logger.debug("updateItem: status = " + statusCode);
596         }
597         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
600
601         // Retrieve the updated resource and verify that its contents exist.
602         input = (MultipartInput) res.getEntity();
603         VocabularyitemsCommon updatedVocabularyItem =
604                 (VocabularyitemsCommon) extractPart(input,
605                         client.getItemCommonPartName(), VocabularyitemsCommon.class);
606         Assert.assertNotNull(updatedVocabularyItem);
607
608         // Verify that the updated resource received the correct data.
609         Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
610                 vocabularyItem.getDisplayName(),
611                 "Data in updated VocabularyItem did not match submitted data.");
612     }
613
614     // Failure outcomes
615     // Placeholders until the three tests below can be uncommented.
616     // See Issue CSPACE-401.
617     @Override
618     public void updateWithEmptyEntityBody(String testName) throws Exception {
619     }
620
621     @Override
622     public void updateWithMalformedXml(String testName) throws Exception {
623     }
624
625     @Override
626     public void updateWithWrongXmlSchema(String testName) throws Exception {
627     }
628
629     /*
630     @Override
631     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
632         dependsOnMethods = {"create", "update", "testSubmitRequest"})
633     public void updateWithEmptyEntityBody(String testName) throws Exception {
634
635     // Perform setup.
636     setupUpdateWithEmptyEntityBody(testName);
637
638     // Submit the request to the service and store the response.
639     String method = REQUEST_TYPE.httpMethodName();
640     String url = getResourceURL(knownResourceId);
641     String mediaType = MediaType.APPLICATION_XML;
642     final String entity = "";
643     int statusCode = submitRequest(method, url, mediaType, entity);
644
645     // Check the status code of the response: does it match
646     // the expected response(s)?
647     if(logger.isDebugEnabled()){
648         logger.debug(testName + ": url=" + url +
649             " status=" + statusCode);
650      }
651     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
652     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
653     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
654     }
655
656     @Override
657     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
658         dependsOnMethods = {"create", "update", "testSubmitRequest"})
659     public void updateWithMalformedXml(String testName) throws Exception {
660
661     // Perform setup.
662     setupUpdateWithMalformedXml(testName);
663
664     // Submit the request to the service and store the response.
665     String method = REQUEST_TYPE.httpMethodName();
666     String url = getResourceURL(knownResourceId);
667     String mediaType = MediaType.APPLICATION_XML;
668     final String entity = MALFORMED_XML_DATA;
669     int statusCode = submitRequest(method, url, mediaType, entity);
670
671     // Check the status code of the response: does it match
672     // the expected response(s)?
673     if(logger.isDebugEnabled()){
674         logger.debug(testName + ": url=" + url +
675            " status=" + statusCode);
676      }
677     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
678     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
679     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
680     }
681
682     @Override
683     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
684         dependsOnMethods = {"create", "update", "testSubmitRequest"})
685     public void updateWithWrongXmlSchema(String testName) throws Exception {
686
687     // Perform setup.
688     setupUpdateWithWrongXmlSchema(testName);
689
690     // Submit the request to the service and store the response.
691     String method = REQUEST_TYPE.httpMethodName();
692     String url = getResourceURL(knownResourceId);
693     String mediaType = MediaType.APPLICATION_XML;
694     final String entity = WRONG_XML_SCHEMA_DATA;
695     int statusCode = submitRequest(method, url, mediaType, entity);
696
697     // Check the status code of the response: does it match
698     // the expected response(s)?
699     if(logger.isDebugEnabled()){
700         logger.debug("updateWithWrongXmlSchema: url=" + url +
701             " status=" + statusCode);
702      }
703     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
704     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
705     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
706     }
707      */
708
709
710     @Override
711     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
712         dependsOnMethods = {"update", "testSubmitRequest"})
713     public void updateNonExistent(String testName) throws Exception {
714
715         // Perform setup.
716         setupUpdateNonExistent(testName);
717
718         // Submit the request to the service and store the response.
719         // Note: The ID used in this 'create' call may be arbitrary.
720         // The only relevant ID may be the one used in update(), below.
721
722         // The only relevant ID may be the one used in update(), below.
723         MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
724         ClientResponse<MultipartInput> res =
725                 client.update(NON_EXISTENT_ID, multipart);
726         int statusCode = res.getStatus();
727
728         // Check the status code of the response: does it match
729         // the expected response(s)?
730         if(logger.isDebugEnabled()){
731             logger.debug(testName + ": status = " + statusCode);
732         }
733         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
734                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
735         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
736     }
737
738     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
739         dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
740     public void updateNonExistentItem(String testName) throws Exception {
741
742         // Perform setup.
743         setupUpdateNonExistent(testName);
744
745         // Submit the request to the service and store the response.
746         // Note: The ID used in this 'create' call may be arbitrary.
747         // The only relevant ID may be the one used in update(), below.
748
749         // The only relevant ID may be the one used in update(), below.
750         MultipartOutput multipart = createVocabularyItemInstance(
751                         knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID));
752         ClientResponse<MultipartInput> res =
753                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
754         int statusCode = res.getStatus();
755
756         // Check the status code of the response: does it match
757         // the expected response(s)?
758         if(logger.isDebugEnabled()){
759             logger.debug(testName + ": status = " + statusCode);
760         }
761         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
762                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
763         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
764     }
765
766     // ---------------------------------------------------------------
767     // CRUD tests : DELETE tests
768     // ---------------------------------------------------------------
769     // Success outcomes
770     @Override
771     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
772         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
773     public void delete(String testName) throws Exception {
774
775         // Perform setup.
776         setupDelete(testName);
777
778         // Submit the request to the service and store the response.
779         ClientResponse<Response> res = client.delete(knownResourceId);
780         int statusCode = res.getStatus();
781
782         // Check the status code of the response: does it match
783         // the expected response(s)?
784         if(logger.isDebugEnabled()){
785             logger.debug(testName + ": status = " + statusCode);
786         }
787         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
788                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
789         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
790     }
791
792    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
793         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
794             "updateItem"})
795     public void deleteItem(String testName) throws Exception {
796
797         // Perform setup.
798         setupDelete(testName);
799
800         // Submit the request to the service and store the response.
801         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
802         int statusCode = res.getStatus();
803
804         // Check the status code of the response: does it match
805         // the expected response(s)?
806         if(logger.isDebugEnabled()){
807             logger.debug("delete: status = " + statusCode);
808         }
809         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
810                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
811         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
812     }
813
814     // Failure outcomes
815     @Override
816     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
817         dependsOnMethods = {"delete"})
818     public void deleteNonExistent(String testName) throws Exception {
819
820         // Perform setup.
821         setupDeleteNonExistent(testName);
822
823         // Submit the request to the service and store the response.
824         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
825         int statusCode = res.getStatus();
826
827         // Check the status code of the response: does it match
828         // the expected response(s)?
829         if(logger.isDebugEnabled()){
830             logger.debug(testName + ": status = " + statusCode);
831         }
832         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
833                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
834         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
835     }
836
837     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
838         dependsOnMethods = {"deleteItem"})
839     public void deleteNonExistentItem(String testName) {
840
841         // Perform setup.
842         setupDeleteNonExistent(testName);
843
844         // Submit the request to the service and store the response.
845         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
846         int statusCode = res.getStatus();
847
848         // Check the status code of the response: does it match
849         // the expected response(s)?
850         if(logger.isDebugEnabled()){
851             logger.debug(testName + ": status = " + statusCode);
852         }
853         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
854                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
855         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
856     }
857
858     // ---------------------------------------------------------------
859     // Utility tests : tests of code used in tests above
860     // ---------------------------------------------------------------
861     /**
862      * Tests the code for manually submitting data that is used by several
863      * of the methods above.
864      */
865     @Test(dependsOnMethods = {"create", "read"})
866     public void testSubmitRequest() {
867
868         // Expected status code: 200 OK
869         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
870
871         // Submit the request to the service and store the response.
872         String method = ServiceRequestType.READ.httpMethodName();
873         String url = getResourceURL(knownResourceId);
874         int statusCode = submitRequest(method, url);
875
876         // Check the status code of the response: does it match
877         // the expected response(s)?
878         if(logger.isDebugEnabled()){
879             logger.debug("testSubmitRequest: url=" + url +
880                 " status=" + statusCode);
881         }
882         Assert.assertEquals(statusCode, EXPECTED_STATUS);
883
884     }
885
886     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
887     public void testItemSubmitRequest() {
888
889         // Expected status code: 200 OK
890         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
891
892         // Submit the request to the service and store the response.
893         String method = ServiceRequestType.READ.httpMethodName();
894         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
895         int statusCode = submitRequest(method, url);
896
897         // Check the status code of the response: does it match
898         // the expected response(s)?
899         if(logger.isDebugEnabled()){
900             logger.debug("testItemSubmitRequest: url=" + url +
901                 " status=" + statusCode);
902         }
903         Assert.assertEquals(statusCode, EXPECTED_STATUS);
904
905     }
906
907     // ---------------------------------------------------------------
908     // Utility methods used by tests above
909     // ---------------------------------------------------------------
910     @Override
911     public String getServicePathComponent() {
912         return SERVICE_PATH_COMPONENT;
913     }
914
915     public String getItemServicePathComponent() {
916         return ITEM_SERVICE_PATH_COMPONENT;
917     }
918
919     /**
920      * Returns the root URL for a service.
921      *
922      * This URL consists of a base URL for all services, followed by
923      * a path component for the owning vocabulary, followed by the 
924      * path component for the items.
925      *
926      * @return The root URL for a service.
927      */
928     protected String getItemServiceRootURL(String parentResourceIdentifier) {
929         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
930     }
931
932     /**
933      * Returns the URL of a specific resource managed by a service, and
934      * designated by an identifier (such as a universally unique ID, or UUID).
935      *
936      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
937      *
938      * @return The URL of a specific resource managed by a service.
939      */
940     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
941         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
942     }
943
944     private MultipartOutput createVocabularyInstance(String identifier) {
945         String displayName = "displayName-" + identifier;
946         String refName = createRefName(displayName);
947         String typeName = "vocabType-" + identifier;
948         return createVocabularyInstance(
949                 displayName, refName,typeName );
950     }
951
952     private MultipartOutput createVocabularyInstance(
953                 String displayName, String refName, String vocabType) {
954         VocabulariesCommon vocabulary = new VocabulariesCommon();
955         vocabulary.setDisplayName(displayName);
956         if(refName!=null)
957             vocabulary.setRefName(refName);
958         vocabulary.setVocabType(vocabType);
959         MultipartOutput multipart = new MultipartOutput();
960         OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
961         commonPart.getHeaders().add("label", client.getCommonPartName());
962
963         if(logger.isDebugEnabled()) {
964             logger.debug("to be created, vocabulary common");
965             logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
966         }
967         return multipart;
968     }
969
970     private MultipartOutput createVocabularyItemInstance(String inVocabulary,
971         String displayName, String refName) {
972         VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
973         vocabularyItem.setInVocabulary(inVocabulary);
974         vocabularyItem.setDisplayName(displayName);
975         if(refName!=null)
976                 vocabularyItem.setRefName(refName);
977         MultipartOutput multipart = new MultipartOutput();
978         OutputPart commonPart = multipart.addPart(vocabularyItem,
979             MediaType.APPLICATION_XML_TYPE);
980         commonPart.getHeaders().add("label", client.getItemCommonPartName());
981
982         if(logger.isDebugEnabled()){
983             logger.debug("to be created, vocabularyitem common");
984             logger.debug(objectAsXmlString(vocabularyItem,
985                 VocabularyitemsCommon.class));
986         }
987
988         return multipart;
989     }
990 }