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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.VocabularyItemJAXBSchema;
33 import org.collectionspace.services.client.VocabularyClient;
34 import org.collectionspace.services.client.VocabularyClientUtils;
35 import org.collectionspace.services.vocabulary.VocabulariesCommon;
36 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
37 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
38 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * VocabularyServiceTest, carries out tests against a
52 * deployed and running Vocabulary Service.
54 * $LastChangedRevision: 753 $
55 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
57 public class VocabularyServiceTest extends AbstractServiceTestImpl {
59 private final Logger logger =
60 LoggerFactory.getLogger(VocabularyServiceTest.class);
62 // Instance variables specific to this test.
63 private VocabularyClient client = new VocabularyClient();
64 final String SERVICE_PATH_COMPONENT = "vocabularies";
65 final String ITEM_SERVICE_PATH_COMPONENT = "items";
66 private String knownResourceId = null;
67 private String knownResourceRefName = null;
68 private String knownItemResourceId = null;
69 private int nItemsToCreateInList = 3;
70 private List<String> allResourceIdsCreated = new ArrayList<String>();
71 private Map<String, String> allResourceItemIdsCreated =
72 new HashMap<String, String>();
74 protected void setKnownResource( String id, String refName ) {
76 knownResourceRefName = refName;
79 // ---------------------------------------------------------------
80 // CRUD tests : CREATE tests
81 // ---------------------------------------------------------------
84 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
85 public void create(String testName) throws Exception {
87 // Perform setup, such as initializing the type of service request
88 // (e.g. CREATE, DELETE), its valid and expected status codes, and
89 // its associated HTTP method name (e.g. POST, DELETE).
90 setupCreate(testName);
92 // Submit the request to the service and store the response.
93 String identifier = createIdentifier();
94 String displayName = "displayName-" + identifier;
95 String refName = VocabularyClientUtils.createVocabularyRefName(displayName, false);
96 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
97 displayName, refName, client.getCommonPartName());
98 ClientResponse<Response> res = client.create(multipart);
99 int statusCode = res.getStatus();
101 // Check the status code of the response: does it match
102 // the expected response(s)?
105 // Does it fall within the set of valid status codes?
106 // Does it exactly match the expected status code?
107 if(logger.isDebugEnabled()){
108 logger.debug(testName + ": status = " + statusCode);
110 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
111 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
112 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
114 // Store the ID returned from the first resource created
115 // for additional tests below.
116 if (knownResourceId == null){
117 setKnownResource(extractId(res), refName);
118 if (logger.isDebugEnabled()) {
119 logger.debug(testName + ": knownResourceId=" + knownResourceId);
122 // Store the IDs from every resource created by tests,
123 // so they can be deleted after tests have been run.
124 allResourceIdsCreated.add(extractId(res));
128 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
129 dependsOnMethods = {"create"})
130 public void createItem(String testName) {
131 setupCreate(testName);
133 HashMap<String, String> itemInfo = new HashMap<String, String>();
134 itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, createIdentifier());
135 knownItemResourceId = VocabularyClientUtils.createItemInVocabulary(knownResourceId,
136 knownResourceRefName, itemInfo, client);
137 if(logger.isDebugEnabled()){
138 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
143 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
144 dependsOnMethods = {"create", "createItem"})
145 public void createList(String testName) throws Exception {
146 for (int i = 0; i < 3; i++) {
147 // Force create to reset the known resource info
148 setKnownResource(null, null);
150 // Add nItemsToCreateInList items to each vocab
151 for (int j = 0; j < nItemsToCreateInList; j++) {
152 createItem(testName);
158 // Placeholders until the three tests below can be uncommented.
159 // See Issue CSPACE-401.
161 public void createWithEmptyEntityBody(String testName) throws Exception {
165 public void createWithMalformedXml(String testName) throws Exception {
169 public void createWithWrongXmlSchema(String testName) throws Exception {
174 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
175 dependsOnMethods = {"create", "testSubmitRequest"})
176 public void createWithEmptyEntityBody(String testName) throws Exception {
179 setupCreateWithEmptyEntityBody(testName);
181 // Submit the request to the service and store the response.
182 String method = REQUEST_TYPE.httpMethodName();
183 String url = getServiceRootURL();
184 String mediaType = MediaType.APPLICATION_XML;
185 final String entity = "";
186 int statusCode = submitRequest(method, url, mediaType, entity);
188 // Check the status code of the response: does it match
189 // the expected response(s)?
190 if(logger.isDebugEnabled()) {
191 logger.debug(testName + ": url=" + url +
192 " status=" + statusCode);
194 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
196 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
200 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
201 dependsOnMethods = {"create", "testSubmitRequest"})
202 public void createWithMalformedXml(String testName) throws Exception {
205 setupCreateWithMalformedXml(testName);
207 // Submit the request to the service and store the response.
208 String method = REQUEST_TYPE.httpMethodName();
209 String url = getServiceRootURL();
210 String mediaType = MediaType.APPLICATION_XML;
211 final String entity = MALFORMED_XML_DATA; // Constant from base class.
212 int statusCode = submitRequest(method, url, mediaType, entity);
214 // Check the status code of the response: does it match
215 // the expected response(s)?
216 if(logger.isDebugEnabled()){
217 logger.debug(testName + ": url=" + url +
218 " status=" + statusCode);
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
227 dependsOnMethods = {"create", "testSubmitRequest"})
228 public void createWithWrongXmlSchema(String testName) throws Exception {
231 setupCreateWithWrongXmlSchema(testName);
233 // Submit the request to the service and store the response.
234 String method = REQUEST_TYPE.httpMethodName();
235 String url = getServiceRootURL();
236 String mediaType = MediaType.APPLICATION_XML;
237 final String entity = WRONG_XML_SCHEMA_DATA;
238 int statusCode = submitRequest(method, url, mediaType, entity);
240 // Check the status code of the response: does it match
241 // the expected response(s)?
242 if(logger.isDebugEnabled()){
243 logger.debug(testName + ": url=" + url +
244 " status=" + statusCode);
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
252 // ---------------------------------------------------------------
253 // CRUD tests : READ tests
254 // ---------------------------------------------------------------
257 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
258 dependsOnMethods = {"create"})
259 public void read(String testName) throws Exception {
264 // Submit the request to the service and store the response.
265 ClientResponse<MultipartInput> res = client.read(knownResourceId);
266 int statusCode = res.getStatus();
268 // Check the status code of the response: does it match
269 // the expected response(s)?
270 if(logger.isDebugEnabled()){
271 logger.debug(testName + ": status = " + statusCode);
273 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
274 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
275 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
276 //FIXME: remove the following try catch once Aron fixes signatures
278 MultipartInput input = (MultipartInput) res.getEntity();
279 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
280 client.getCommonPartName(), VocabulariesCommon.class);
281 Assert.assertNotNull(vocabulary);
282 } catch (Exception e) {
283 throw new RuntimeException(e);
288 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
289 dependsOnMethods = {"read"})
290 public void readByName(String testName) throws Exception {
295 // Submit the request to the service and store the response.
296 ClientResponse<MultipartInput> res = client.read(knownResourceId);
297 int statusCode = res.getStatus();
299 // Check the status code of the response: does it match
300 // the expected response(s)?
301 if(logger.isDebugEnabled()){
302 logger.debug(testName + ": status = " + statusCode);
304 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
305 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
306 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
307 //FIXME: remove the following try catch once Aron fixes signatures
309 MultipartInput input = (MultipartInput) res.getEntity();
310 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
311 client.getCommonPartName(), VocabulariesCommon.class);
312 Assert.assertNotNull(vocabulary);
313 } catch (Exception e) {
314 throw new RuntimeException(e);
319 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
320 dependsOnMethods = {"createItem", "read"})
321 public void readItem(String testName) throws Exception {
326 // Submit the request to the service and store the response.
327 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
328 int statusCode = res.getStatus();
330 // Check the status code of the response: does it match
331 // the expected response(s)?
332 if(logger.isDebugEnabled()){
333 logger.debug(testName + ": status = " + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
339 // Check whether we've received a vocabulary item.
340 MultipartInput input = (MultipartInput) res.getEntity();
341 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
342 client.getItemCommonPartName(), VocabularyitemsCommon.class);
343 Assert.assertNotNull(vocabularyItem);
349 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
350 dependsOnMethods = {"read"})
351 public void readNonExistent(String testName) {
354 setupReadNonExistent(testName);
356 // Submit the request to the service and store the response.
357 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
358 int statusCode = res.getStatus();
360 // Check the status code of the response: does it match
361 // the expected response(s)?
362 if(logger.isDebugEnabled()){
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
371 dependsOnMethods = {"readItem", "readNonExistent"})
372 public void readItemNonExistent(String testName) {
375 setupReadNonExistent(testName);
377 // Submit the request to the service and store the response.
378 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
379 int statusCode = res.getStatus();
381 // Check the status code of the response: does it match
382 // the expected response(s)?
383 if(logger.isDebugEnabled()){
384 logger.debug(testName + ": status = " + statusCode);
386 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
387 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
388 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
390 // ---------------------------------------------------------------
391 // CRUD tests : READ_LIST tests
392 // ---------------------------------------------------------------
396 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
397 dependsOnMethods = {"createList", "read"})
398 public void readList(String testName) throws Exception {
401 setupReadList(testName);
403 // Submit the request to the service and store the response.
404 ClientResponse<VocabulariesCommonList> res = client.readList();
405 VocabulariesCommonList list = res.getEntity();
406 int statusCode = res.getStatus();
408 // Check the status code of the response: does it match
409 // the expected response(s)?
410 if(logger.isDebugEnabled()){
411 logger.debug(testName + ": status = " + statusCode);
413 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
414 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
415 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417 // Optionally output additional data about list members for debugging.
418 boolean iterateThroughList = false;
419 if (iterateThroughList && logger.isDebugEnabled()) {
420 List<VocabulariesCommonList.VocabularyListItem> items =
421 list.getVocabularyListItem();
423 for (VocabulariesCommonList.VocabularyListItem item : items) {
424 String csid = item.getCsid();
425 logger.debug(testName + ": list-item[" + i + "] csid=" +
427 logger.debug(testName + ": list-item[" + i + "] displayName=" +
428 item.getDisplayName());
429 logger.debug(testName + ": list-item[" + i + "] URI=" +
437 @Test(dependsOnMethods = {"createList", "readItem"})
438 public void readItemList() {
439 readItemList(knownResourceId);
442 private void readItemList(String vcsid) {
444 final String testName = "readItemList";
447 setupReadList(testName);
449 // Submit the request to the service and store the response.
450 ClientResponse<VocabularyitemsCommonList> res =
451 client.readItemList(vcsid);
452 VocabularyitemsCommonList list = res.getEntity();
453 int statusCode = res.getStatus();
455 // Check the status code of the response: does it match
456 // the expected response(s)?
457 if(logger.isDebugEnabled()){
458 logger.debug(" " + testName + ": status = " + statusCode);
460 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
465 list.getVocabularyitemListItem();
466 int nItemsReturned = items.size();
467 if(logger.isDebugEnabled()){
468 logger.debug(" " + testName + ": Expected "
469 + nItemsToCreateInList+" items; got: "+nItemsReturned);
471 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
473 // Optionally output additional data about list members for debugging.
474 boolean iterateThroughList = true;
475 if (iterateThroughList && logger.isDebugEnabled()) {
476 logger.debug(" " + testName + ": checking items");
478 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
479 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
481 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
482 item.getDisplayName());
483 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
492 // ---------------------------------------------------------------
493 // CRUD tests : UPDATE tests
494 // ---------------------------------------------------------------
497 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
498 dependsOnMethods = {"read"})
499 public void update(String testName) throws Exception {
502 setupUpdate(testName);
504 // Retrieve the contents of a resource to update.
505 ClientResponse<MultipartInput> res =
506 client.read(knownResourceId);
507 if(logger.isDebugEnabled()){
508 logger.debug(testName + ": read status = " + res.getStatus());
510 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
512 if(logger.isDebugEnabled()){
513 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
515 MultipartInput input = (MultipartInput) res.getEntity();
516 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
517 client.getCommonPartName(), VocabulariesCommon.class);
518 Assert.assertNotNull(vocabulary);
520 // Update the contents of this resource.
521 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
522 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
523 if(logger.isDebugEnabled()){
524 logger.debug("to be updated Vocabulary");
525 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
528 // Submit the updated resource to the service and store the response.
529 MultipartOutput output = new MultipartOutput();
530 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
531 commonPart.getHeaders().add("label", client.getCommonPartName());
532 res = client.update(knownResourceId, output);
533 int statusCode = res.getStatus();
535 // Check the status code of the response: does it match the expected response(s)?
536 if(logger.isDebugEnabled()){
537 logger.debug("update: status = " + statusCode);
539 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
543 // Retrieve the updated resource and verify that its contents exist.
544 input = (MultipartInput) res.getEntity();
545 VocabulariesCommon updatedVocabulary =
546 (VocabulariesCommon) extractPart(input,
547 client.getCommonPartName(), VocabulariesCommon.class);
548 Assert.assertNotNull(updatedVocabulary);
550 // Verify that the updated resource received the correct data.
551 Assert.assertEquals(updatedVocabulary.getDisplayName(),
552 vocabulary.getDisplayName(),
553 "Data in updated object did not match submitted data.");
556 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
557 dependsOnMethods = {"readItem", "update"})
558 public void updateItem(String testName) throws Exception {
561 setupUpdate(testName);
563 ClientResponse<MultipartInput> res =
564 client.readItem(knownResourceId, knownItemResourceId);
565 if(logger.isDebugEnabled()){
566 logger.debug(testName + ": read status = " + res.getStatus());
568 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
570 if(logger.isDebugEnabled()){
571 logger.debug("got VocabularyItem to update with ID: " +
572 knownItemResourceId +
573 " in Vocab: " + knownResourceId );
575 MultipartInput input = (MultipartInput) res.getEntity();
576 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
577 client.getItemCommonPartName(), VocabularyitemsCommon.class);
578 Assert.assertNotNull(vocabularyItem);
580 // Update the contents of this resource.
581 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
582 if(logger.isDebugEnabled()){
583 logger.debug("to be updated VocabularyItem");
584 logger.debug(objectAsXmlString(vocabularyItem,
585 VocabularyitemsCommon.class));
588 // Submit the updated resource to the service and store the response.
589 MultipartOutput output = new MultipartOutput();
590 OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
591 commonPart.getHeaders().add("label", client.getItemCommonPartName());
592 res = client.updateItem(knownResourceId, knownItemResourceId, output);
593 int statusCode = res.getStatus();
595 // Check the status code of the response: does it match the expected response(s)?
596 if(logger.isDebugEnabled()){
597 logger.debug("updateItem: status = " + statusCode);
599 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
600 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
601 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
603 // Retrieve the updated resource and verify that its contents exist.
604 input = (MultipartInput) res.getEntity();
605 VocabularyitemsCommon updatedVocabularyItem =
606 (VocabularyitemsCommon) extractPart(input,
607 client.getItemCommonPartName(), VocabularyitemsCommon.class);
608 Assert.assertNotNull(updatedVocabularyItem);
610 // Verify that the updated resource received the correct data.
611 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
612 vocabularyItem.getDisplayName(),
613 "Data in updated VocabularyItem did not match submitted data.");
617 // Placeholders until the three tests below can be uncommented.
618 // See Issue CSPACE-401.
620 public void updateWithEmptyEntityBody(String testName) throws Exception {
624 public void updateWithMalformedXml(String testName) throws Exception {
628 public void updateWithWrongXmlSchema(String testName) throws Exception {
633 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
634 dependsOnMethods = {"create", "update", "testSubmitRequest"})
635 public void updateWithEmptyEntityBody(String testName) throws Exception {
638 setupUpdateWithEmptyEntityBody(testName);
640 // Submit the request to the service and store the response.
641 String method = REQUEST_TYPE.httpMethodName();
642 String url = getResourceURL(knownResourceId);
643 String mediaType = MediaType.APPLICATION_XML;
644 final String entity = "";
645 int statusCode = submitRequest(method, url, mediaType, entity);
647 // Check the status code of the response: does it match
648 // the expected response(s)?
649 if(logger.isDebugEnabled()){
650 logger.debug(testName + ": url=" + url +
651 " status=" + statusCode);
653 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
654 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
655 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
659 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
660 dependsOnMethods = {"create", "update", "testSubmitRequest"})
661 public void updateWithMalformedXml(String testName) throws Exception {
664 setupUpdateWithMalformedXml(testName);
666 // Submit the request to the service and store the response.
667 String method = REQUEST_TYPE.httpMethodName();
668 String url = getResourceURL(knownResourceId);
669 String mediaType = MediaType.APPLICATION_XML;
670 final String entity = MALFORMED_XML_DATA;
671 int statusCode = submitRequest(method, url, mediaType, entity);
673 // Check the status code of the response: does it match
674 // the expected response(s)?
675 if(logger.isDebugEnabled()){
676 logger.debug(testName + ": url=" + url +
677 " status=" + statusCode);
679 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
680 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
681 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
685 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
686 dependsOnMethods = {"create", "update", "testSubmitRequest"})
687 public void updateWithWrongXmlSchema(String testName) throws Exception {
690 setupUpdateWithWrongXmlSchema(testName);
692 // Submit the request to the service and store the response.
693 String method = REQUEST_TYPE.httpMethodName();
694 String url = getResourceURL(knownResourceId);
695 String mediaType = MediaType.APPLICATION_XML;
696 final String entity = WRONG_XML_SCHEMA_DATA;
697 int statusCode = submitRequest(method, url, mediaType, entity);
699 // Check the status code of the response: does it match
700 // the expected response(s)?
701 if(logger.isDebugEnabled()){
702 logger.debug("updateWithWrongXmlSchema: url=" + url +
703 " status=" + statusCode);
705 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
706 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
707 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
713 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
714 dependsOnMethods = {"update", "testSubmitRequest"})
715 public void updateNonExistent(String testName) throws Exception {
718 setupUpdateNonExistent(testName);
720 // Submit the request to the service and store the response.
721 // Note: The ID used in this 'create' call may be arbitrary.
722 // The only relevant ID may be the one used in update(), below.
724 // The only relevant ID may be the one used in update(), below.
725 String displayName = "displayName-" + NON_EXISTENT_ID;
726 String refName = VocabularyClientUtils.createVocabularyRefName(displayName, false);
727 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
728 displayName, refName, client.getCommonPartName());
729 ClientResponse<MultipartInput> res =
730 client.update(NON_EXISTENT_ID, multipart);
731 int statusCode = res.getStatus();
733 // Check the status code of the response: does it match
734 // the expected response(s)?
735 if(logger.isDebugEnabled()){
736 logger.debug(testName + ": status = " + statusCode);
738 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
739 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
740 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
743 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
744 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
745 public void updateNonExistentItem(String testName) throws Exception {
748 setupUpdateNonExistent(testName);
750 // Submit the request to the service and store the response.
751 // Note: The ID used in this 'create' call may be arbitrary.
752 // The only relevant ID may be the one used in update(), below.
754 // The only relevant ID may be the one used in update(), below.
755 HashMap<String, String> itemInfo = new HashMap<String, String>();
756 itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, "nonex");
757 MultipartOutput multipart = VocabularyClientUtils.createVocabularyItemInstance(
758 knownResourceId, NON_EXISTENT_ID, itemInfo, NON_EXISTENT_ID);
759 ClientResponse<MultipartInput> res =
760 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
761 int statusCode = res.getStatus();
763 // Check the status code of the response: does it match
764 // the expected response(s)?
765 if(logger.isDebugEnabled()){
766 logger.debug(testName + ": status = " + statusCode);
768 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
769 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
770 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
773 // ---------------------------------------------------------------
774 // CRUD tests : DELETE tests
775 // ---------------------------------------------------------------
778 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
779 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
780 public void delete(String testName) throws Exception {
783 setupDelete(testName);
785 // Submit the request to the service and store the response.
786 ClientResponse<Response> res = client.delete(knownResourceId);
787 int statusCode = res.getStatus();
789 // Check the status code of the response: does it match
790 // the expected response(s)?
791 if(logger.isDebugEnabled()){
792 logger.debug(testName + ": status = " + statusCode);
794 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
795 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
796 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
799 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
800 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
802 public void deleteItem(String testName) throws Exception {
805 setupDelete(testName);
807 // Submit the request to the service and store the response.
808 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
809 int statusCode = res.getStatus();
811 // Check the status code of the response: does it match
812 // the expected response(s)?
813 if(logger.isDebugEnabled()){
814 logger.debug("delete: status = " + statusCode);
816 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
817 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
818 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
823 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
824 dependsOnMethods = {"delete"})
825 public void deleteNonExistent(String testName) throws Exception {
828 setupDeleteNonExistent(testName);
830 // Submit the request to the service and store the response.
831 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
832 int statusCode = res.getStatus();
834 // Check the status code of the response: does it match
835 // the expected response(s)?
836 if(logger.isDebugEnabled()){
837 logger.debug(testName + ": status = " + statusCode);
839 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
840 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
841 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
844 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
845 dependsOnMethods = {"deleteItem"})
846 public void deleteNonExistentItem(String testName) {
849 setupDeleteNonExistent(testName);
851 // Submit the request to the service and store the response.
852 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
853 int statusCode = res.getStatus();
855 // Check the status code of the response: does it match
856 // the expected response(s)?
857 if(logger.isDebugEnabled()){
858 logger.debug(testName + ": status = " + statusCode);
860 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
861 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
862 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
865 // ---------------------------------------------------------------
866 // Utility tests : tests of code used in tests above
867 // ---------------------------------------------------------------
869 * Tests the code for manually submitting data that is used by several
870 * of the methods above.
872 @Test(dependsOnMethods = {"create", "read"})
873 public void testSubmitRequest() {
875 // Expected status code: 200 OK
876 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
878 // Submit the request to the service and store the response.
879 String method = ServiceRequestType.READ.httpMethodName();
880 String url = getResourceURL(knownResourceId);
881 int statusCode = submitRequest(method, url);
883 // Check the status code of the response: does it match
884 // the expected response(s)?
885 if(logger.isDebugEnabled()){
886 logger.debug("testSubmitRequest: url=" + url +
887 " status=" + statusCode);
889 Assert.assertEquals(statusCode, EXPECTED_STATUS);
893 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
894 public void testItemSubmitRequest() {
896 // Expected status code: 200 OK
897 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
899 // Submit the request to the service and store the response.
900 String method = ServiceRequestType.READ.httpMethodName();
901 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
902 int statusCode = submitRequest(method, url);
904 // Check the status code of the response: does it match
905 // the expected response(s)?
906 if(logger.isDebugEnabled()){
907 logger.debug("testItemSubmitRequest: url=" + url +
908 " status=" + statusCode);
910 Assert.assertEquals(statusCode, EXPECTED_STATUS);
914 // ---------------------------------------------------------------
915 // Cleanup of resources created during testing
916 // ---------------------------------------------------------------
919 * Deletes all resources created by tests, after all tests have been run.
921 * This cleanup method will always be run, even if one or more tests fail.
922 * For this reason, it attempts to remove all resources created
923 * at any point during testing, even if some of those resources
924 * may be expected to be deleted by certain tests.
926 @AfterClass(alwaysRun=true)
927 public void cleanUp() {
929 if (logger.isDebugEnabled()) {
930 logger.debug("Cleaning up temporary resources created for testing ...");
932 // Clean up vocabulary item resources.
933 String vocabularyResourceId;
934 String vocabularyItemResourceId;
935 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
936 vocabularyItemResourceId = entry.getKey();
937 vocabularyResourceId = entry.getValue();
938 // Note: Any non-success responses are ignored and not reported.
939 ClientResponse<Response> res =
940 client.deleteItem(vocabularyResourceId, vocabularyItemResourceId);
942 // Clean up vocabulary resources.
943 for (String resourceId : allResourceIdsCreated) {
944 // Note: Any non-success responses are ignored and not reported.
945 ClientResponse<Response> res = client.delete(resourceId);
950 // ---------------------------------------------------------------
951 // Utility methods used by tests above
952 // ---------------------------------------------------------------
954 public String getServicePathComponent() {
955 return SERVICE_PATH_COMPONENT;
958 public String getItemServicePathComponent() {
959 return ITEM_SERVICE_PATH_COMPONENT;
963 * Returns the root URL for a service.
965 * This URL consists of a base URL for all services, followed by
966 * a path component for the owning vocabulary, followed by the
967 * path component for the items.
969 * @return The root URL for a service.
971 protected String getItemServiceRootURL(String parentResourceIdentifier) {
972 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
976 * Returns the URL of a specific resource managed by a service, and
977 * designated by an identifier (such as a universally unique ID, or UUID).
979 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
981 * @return The URL of a specific resource managed by a service.
983 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
984 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;