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.client.VocabularyClient;
33 import org.collectionspace.services.vocabulary.VocabulariesCommon;
34 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
35 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
36 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
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;
49 * VocabularyServiceTest, carries out tests against a
50 * deployed and running Vocabulary Service.
52 * $LastChangedRevision: 753 $
53 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
55 public class VocabularyServiceTest extends AbstractServiceTest {
57 private final Logger logger =
58 LoggerFactory.getLogger(VocabularyServiceTest.class);
60 // Instance variables specific to this test.
61 private VocabularyClient client = new VocabularyClient();
62 final String SERVICE_PATH_COMPONENT = "vocabularies";
63 final String ITEM_SERVICE_PATH_COMPONENT = "items";
64 private String knownResourceId = null;
65 private String lastVocabId = 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>();
73 protected String createRefName(String displayName) {
74 return displayName.replaceAll("\\W", "");
77 // ---------------------------------------------------------------
78 // CRUD tests : CREATE tests
79 // ---------------------------------------------------------------
82 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
83 public void create(String testName) throws Exception {
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);
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 createVocabularyInstance(displayName, refName, typeName);
97 ClientResponse<Response> res = client.create(multipart);
98 int statusCode = res.getStatus();
100 // Check the status code of the response: does it match
101 // the expected response(s)?
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);
109 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
110 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
111 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
113 // Store the refname from the first resource created
114 // for additional tests below.
115 knownResourceRefName = refName;
117 lastVocabId = extractId(res);
118 // Store the ID returned from the first resource created
119 // for additional tests below.
120 if (knownResourceId == null){
121 knownResourceId = lastVocabId;
122 if (logger.isDebugEnabled()) {
123 logger.debug(testName + ": knownResourceId=" + knownResourceId);
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));
132 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
133 dependsOnMethods = {"create"})
134 public void createItem(String testName) {
135 setupCreate(testName);
137 knownItemResourceId = createItemInVocab(lastVocabId);
138 if(logger.isDebugEnabled()){
139 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
143 private String createItemInVocab(String vcsid) {
145 final String testName = "createItemInVocab";
146 if(logger.isDebugEnabled()){
147 logger.debug(testName + ":...");
150 // Submit the request to the service and store the response.
151 String identifier = createIdentifier();
152 String refName = createRefName(identifier);
153 MultipartOutput multipart = createVocabularyItemInstance(vcsid, identifier, refName);
154 ClientResponse<Response> res = client.createItem(vcsid, multipart);
155 int statusCode = res.getStatus();
157 // Check the status code of the response: does it match
158 // the expected response(s)?
159 if(logger.isDebugEnabled()){
160 logger.debug(testName + ": status = " + statusCode);
162 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
163 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
164 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
166 // Store the ID returned from the first item resource created
167 // for additional tests below.
168 if (knownItemResourceId == null){
169 knownItemResourceId = extractId(res);
170 if (logger.isDebugEnabled()) {
171 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
175 // Store the IDs from any item resources created
176 // by tests, along with the IDs of their parents, so these items
177 // can be deleted after all tests have been run.
179 // Item resource IDs are unique, so these are used as keys;
180 // the non-unique IDs of their parents are stored as associated values.
181 allResourceItemIdsCreated.put(extractId(res), vcsid);
183 return extractId(res);
187 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
188 dependsOnMethods = {"create", "createItem"})
189 public void createList(String testName) throws Exception {
190 for (int i = 0; i < 3; i++) {
192 knownResourceId = lastVocabId;
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
196 // Add nItemsToCreateInList items to each vocab
197 for (int j = 0; j < nItemsToCreateInList; j++) {
198 createItem(testName);
204 // Placeholders until the three tests below can be uncommented.
205 // See Issue CSPACE-401.
207 public void createWithEmptyEntityBody(String testName) throws Exception {
211 public void createWithMalformedXml(String testName) throws Exception {
215 public void createWithWrongXmlSchema(String testName) throws Exception {
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
221 dependsOnMethods = {"create", "testSubmitRequest"})
222 public void createWithEmptyEntityBody(String testName) throws Exception {
225 setupCreateWithEmptyEntityBody(testName);
227 // Submit the request to the service and store the response.
228 String method = REQUEST_TYPE.httpMethodName();
229 String url = getServiceRootURL();
230 String mediaType = MediaType.APPLICATION_XML;
231 final String entity = "";
232 int statusCode = submitRequest(method, url, mediaType, entity);
234 // Check the status code of the response: does it match
235 // the expected response(s)?
236 if(logger.isDebugEnabled()) {
237 logger.debug(testName + ": url=" + url +
238 " status=" + statusCode);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
246 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
247 dependsOnMethods = {"create", "testSubmitRequest"})
248 public void createWithMalformedXml(String testName) throws Exception {
251 setupCreateWithMalformedXml(testName);
253 // Submit the request to the service and store the response.
254 String method = REQUEST_TYPE.httpMethodName();
255 String url = getServiceRootURL();
256 String mediaType = MediaType.APPLICATION_XML;
257 final String entity = MALFORMED_XML_DATA; // Constant from base class.
258 int statusCode = submitRequest(method, url, mediaType, entity);
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if(logger.isDebugEnabled()){
263 logger.debug(testName + ": url=" + url +
264 " status=" + statusCode);
266 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
267 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
268 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
273 dependsOnMethods = {"create", "testSubmitRequest"})
274 public void createWithWrongXmlSchema(String testName) throws Exception {
277 setupCreateWithWrongXmlSchema(testName);
279 // Submit the request to the service and store the response.
280 String method = REQUEST_TYPE.httpMethodName();
281 String url = getServiceRootURL();
282 String mediaType = MediaType.APPLICATION_XML;
283 final String entity = WRONG_XML_SCHEMA_DATA;
284 int statusCode = submitRequest(method, url, mediaType, entity);
286 // Check the status code of the response: does it match
287 // the expected response(s)?
288 if(logger.isDebugEnabled()){
289 logger.debug(testName + ": url=" + url +
290 " status=" + statusCode);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 // ---------------------------------------------------------------
299 // CRUD tests : READ tests
300 // ---------------------------------------------------------------
303 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
304 dependsOnMethods = {"create"})
305 public void read(String testName) throws Exception {
310 // Submit the request to the service and store the response.
311 ClientResponse<MultipartInput> res = client.read(knownResourceId);
312 int statusCode = res.getStatus();
314 // Check the status code of the response: does it match
315 // the expected response(s)?
316 if(logger.isDebugEnabled()){
317 logger.debug(testName + ": status = " + statusCode);
319 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 //FIXME: remove the following try catch once Aron fixes signatures
324 MultipartInput input = (MultipartInput) res.getEntity();
325 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
326 client.getCommonPartName(), VocabulariesCommon.class);
327 Assert.assertNotNull(vocabulary);
328 } catch (Exception e) {
329 throw new RuntimeException(e);
334 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
335 dependsOnMethods = {"read"})
336 public void readByName(String testName) throws Exception {
341 // Submit the request to the service and store the response.
342 ClientResponse<MultipartInput> res = client.read(knownResourceId);
343 int statusCode = res.getStatus();
345 // Check the status code of the response: does it match
346 // the expected response(s)?
347 if(logger.isDebugEnabled()){
348 logger.debug(testName + ": status = " + statusCode);
350 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
351 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
352 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
353 //FIXME: remove the following try catch once Aron fixes signatures
355 MultipartInput input = (MultipartInput) res.getEntity();
356 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
357 client.getCommonPartName(), VocabulariesCommon.class);
358 Assert.assertNotNull(vocabulary);
359 } catch (Exception e) {
360 throw new RuntimeException(e);
365 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
366 dependsOnMethods = {"createItem", "read"})
367 public void readItem(String testName) throws Exception {
372 // Submit the request to the service and store the response.
373 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
374 int statusCode = res.getStatus();
376 // Check the status code of the response: does it match
377 // the expected response(s)?
378 if(logger.isDebugEnabled()){
379 logger.debug(testName + ": status = " + statusCode);
381 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
382 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
383 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
385 // Check whether we've received a vocabulary item.
386 MultipartInput input = (MultipartInput) res.getEntity();
387 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
388 client.getItemCommonPartName(), VocabularyitemsCommon.class);
389 Assert.assertNotNull(vocabularyItem);
395 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
396 dependsOnMethods = {"read"})
397 public void readNonExistent(String testName) {
400 setupReadNonExistent(testName);
402 // Submit the request to the service and store the response.
403 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
404 int statusCode = res.getStatus();
406 // Check the status code of the response: does it match
407 // the expected response(s)?
408 if(logger.isDebugEnabled()){
409 logger.debug(testName + ": status = " + statusCode);
411 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
412 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
413 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
416 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
417 dependsOnMethods = {"readItem", "readNonExistent"})
418 public void readItemNonExistent(String testName) {
421 setupReadNonExistent(testName);
423 // Submit the request to the service and store the response.
424 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
425 int statusCode = res.getStatus();
427 // Check the status code of the response: does it match
428 // the expected response(s)?
429 if(logger.isDebugEnabled()){
430 logger.debug(testName + ": status = " + statusCode);
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 // ---------------------------------------------------------------
437 // CRUD tests : READ_LIST tests
438 // ---------------------------------------------------------------
442 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
443 dependsOnMethods = {"createList", "read"})
444 public void readList(String testName) throws Exception {
447 setupReadList(testName);
449 // Submit the request to the service and store the response.
450 ClientResponse<VocabulariesCommonList> res = client.readList();
451 VocabulariesCommonList list = res.getEntity();
452 int statusCode = res.getStatus();
454 // Check the status code of the response: does it match
455 // the expected response(s)?
456 if(logger.isDebugEnabled()){
457 logger.debug(testName + ": status = " + statusCode);
459 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
463 // Optionally output additional data about list members for debugging.
464 boolean iterateThroughList = false;
465 if (iterateThroughList && logger.isDebugEnabled()) {
466 List<VocabulariesCommonList.VocabularyListItem> items =
467 list.getVocabularyListItem();
469 for (VocabulariesCommonList.VocabularyListItem item : items) {
470 String csid = item.getCsid();
471 logger.debug(testName + ": list-item[" + i + "] csid=" +
473 logger.debug(testName + ": list-item[" + i + "] displayName=" +
474 item.getDisplayName());
475 logger.debug(testName + ": list-item[" + i + "] URI=" +
483 @Test(dependsOnMethods = {"createList", "readItem"})
484 public void readItemList() {
485 readItemList(knownResourceId);
488 private void readItemList(String vcsid) {
490 final String testName = "readItemList";
493 setupReadList(testName);
495 // Submit the request to the service and store the response.
496 ClientResponse<VocabularyitemsCommonList> res =
497 client.readItemList(vcsid);
498 VocabularyitemsCommonList list = res.getEntity();
499 int statusCode = res.getStatus();
501 // Check the status code of the response: does it match
502 // the expected response(s)?
503 if(logger.isDebugEnabled()){
504 logger.debug(" " + testName + ": status = " + statusCode);
506 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
507 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
508 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
510 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
511 list.getVocabularyitemListItem();
512 int nItemsReturned = items.size();
513 if(logger.isDebugEnabled()){
514 logger.debug(" " + testName + ": Expected "
515 + nItemsToCreateInList+" items; got: "+nItemsReturned);
517 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
519 // Optionally output additional data about list members for debugging.
520 boolean iterateThroughList = true;
521 if (iterateThroughList && logger.isDebugEnabled()) {
522 logger.debug(" " + testName + ": checking items");
524 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
525 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
527 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
528 item.getDisplayName());
529 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
538 // ---------------------------------------------------------------
539 // CRUD tests : UPDATE tests
540 // ---------------------------------------------------------------
543 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
544 dependsOnMethods = {"read"})
545 public void update(String testName) throws Exception {
548 setupUpdate(testName);
550 // Retrieve the contents of a resource to update.
551 ClientResponse<MultipartInput> res =
552 client.read(knownResourceId);
553 if(logger.isDebugEnabled()){
554 logger.debug(testName + ": read status = " + res.getStatus());
556 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
558 if(logger.isDebugEnabled()){
559 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
561 MultipartInput input = (MultipartInput) res.getEntity();
562 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
563 client.getCommonPartName(), VocabulariesCommon.class);
564 Assert.assertNotNull(vocabulary);
566 // Update the contents of this resource.
567 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
568 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
569 if(logger.isDebugEnabled()){
570 logger.debug("to be updated Vocabulary");
571 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
574 // Submit the updated resource to the service and store the response.
575 MultipartOutput output = new MultipartOutput();
576 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
577 commonPart.getHeaders().add("label", client.getCommonPartName());
578 res = client.update(knownResourceId, output);
579 int statusCode = res.getStatus();
581 // Check the status code of the response: does it match the expected response(s)?
582 if(logger.isDebugEnabled()){
583 logger.debug("update: status = " + statusCode);
585 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
586 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
587 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
589 // Retrieve the updated resource and verify that its contents exist.
590 input = (MultipartInput) res.getEntity();
591 VocabulariesCommon updatedVocabulary =
592 (VocabulariesCommon) extractPart(input,
593 client.getCommonPartName(), VocabulariesCommon.class);
594 Assert.assertNotNull(updatedVocabulary);
596 // Verify that the updated resource received the correct data.
597 Assert.assertEquals(updatedVocabulary.getDisplayName(),
598 vocabulary.getDisplayName(),
599 "Data in updated object did not match submitted data.");
602 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
603 dependsOnMethods = {"readItem", "update"})
604 public void updateItem(String testName) throws Exception {
607 setupUpdate(testName);
609 ClientResponse<MultipartInput> res =
610 client.readItem(knownResourceId, knownItemResourceId);
611 if(logger.isDebugEnabled()){
612 logger.debug(testName + ": read status = " + res.getStatus());
614 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
616 if(logger.isDebugEnabled()){
617 logger.debug("got VocabularyItem to update with ID: " +
618 knownItemResourceId +
619 " in Vocab: " + knownResourceId );
621 MultipartInput input = (MultipartInput) res.getEntity();
622 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
623 client.getItemCommonPartName(), VocabularyitemsCommon.class);
624 Assert.assertNotNull(vocabularyItem);
626 // Update the contents of this resource.
627 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
628 if(logger.isDebugEnabled()){
629 logger.debug("to be updated VocabularyItem");
630 logger.debug(objectAsXmlString(vocabularyItem,
631 VocabularyitemsCommon.class));
634 // Submit the updated resource to the service and store the response.
635 MultipartOutput output = new MultipartOutput();
636 OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
637 commonPart.getHeaders().add("label", client.getItemCommonPartName());
638 res = client.updateItem(knownResourceId, knownItemResourceId, output);
639 int statusCode = res.getStatus();
641 // Check the status code of the response: does it match the expected response(s)?
642 if(logger.isDebugEnabled()){
643 logger.debug("updateItem: status = " + statusCode);
645 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
646 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
647 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
649 // Retrieve the updated resource and verify that its contents exist.
650 input = (MultipartInput) res.getEntity();
651 VocabularyitemsCommon updatedVocabularyItem =
652 (VocabularyitemsCommon) extractPart(input,
653 client.getItemCommonPartName(), VocabularyitemsCommon.class);
654 Assert.assertNotNull(updatedVocabularyItem);
656 // Verify that the updated resource received the correct data.
657 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
658 vocabularyItem.getDisplayName(),
659 "Data in updated VocabularyItem did not match submitted data.");
663 // Placeholders until the three tests below can be uncommented.
664 // See Issue CSPACE-401.
666 public void updateWithEmptyEntityBody(String testName) throws Exception {
670 public void updateWithMalformedXml(String testName) throws Exception {
674 public void updateWithWrongXmlSchema(String testName) throws Exception {
679 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
680 dependsOnMethods = {"create", "update", "testSubmitRequest"})
681 public void updateWithEmptyEntityBody(String testName) throws Exception {
684 setupUpdateWithEmptyEntityBody(testName);
686 // Submit the request to the service and store the response.
687 String method = REQUEST_TYPE.httpMethodName();
688 String url = getResourceURL(knownResourceId);
689 String mediaType = MediaType.APPLICATION_XML;
690 final String entity = "";
691 int statusCode = submitRequest(method, url, mediaType, entity);
693 // Check the status code of the response: does it match
694 // the expected response(s)?
695 if(logger.isDebugEnabled()){
696 logger.debug(testName + ": url=" + url +
697 " status=" + statusCode);
699 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
700 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
701 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
705 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
706 dependsOnMethods = {"create", "update", "testSubmitRequest"})
707 public void updateWithMalformedXml(String testName) throws Exception {
710 setupUpdateWithMalformedXml(testName);
712 // Submit the request to the service and store the response.
713 String method = REQUEST_TYPE.httpMethodName();
714 String url = getResourceURL(knownResourceId);
715 String mediaType = MediaType.APPLICATION_XML;
716 final String entity = MALFORMED_XML_DATA;
717 int statusCode = submitRequest(method, url, mediaType, entity);
719 // Check the status code of the response: does it match
720 // the expected response(s)?
721 if(logger.isDebugEnabled()){
722 logger.debug(testName + ": url=" + url +
723 " status=" + statusCode);
725 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
726 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
727 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
731 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
732 dependsOnMethods = {"create", "update", "testSubmitRequest"})
733 public void updateWithWrongXmlSchema(String testName) throws Exception {
736 setupUpdateWithWrongXmlSchema(testName);
738 // Submit the request to the service and store the response.
739 String method = REQUEST_TYPE.httpMethodName();
740 String url = getResourceURL(knownResourceId);
741 String mediaType = MediaType.APPLICATION_XML;
742 final String entity = WRONG_XML_SCHEMA_DATA;
743 int statusCode = submitRequest(method, url, mediaType, entity);
745 // Check the status code of the response: does it match
746 // the expected response(s)?
747 if(logger.isDebugEnabled()){
748 logger.debug("updateWithWrongXmlSchema: url=" + url +
749 " status=" + statusCode);
751 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
752 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
753 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
759 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
760 dependsOnMethods = {"update", "testSubmitRequest"})
761 public void updateNonExistent(String testName) throws Exception {
764 setupUpdateNonExistent(testName);
766 // Submit the request to the service and store the response.
767 // Note: The ID used in this 'create' call may be arbitrary.
768 // The only relevant ID may be the one used in update(), below.
770 // The only relevant ID may be the one used in update(), below.
771 MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
772 ClientResponse<MultipartInput> res =
773 client.update(NON_EXISTENT_ID, multipart);
774 int statusCode = res.getStatus();
776 // Check the status code of the response: does it match
777 // the expected response(s)?
778 if(logger.isDebugEnabled()){
779 logger.debug(testName + ": status = " + statusCode);
781 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
782 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
783 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
786 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
787 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
788 public void updateNonExistentItem(String testName) throws Exception {
791 setupUpdateNonExistent(testName);
793 // Submit the request to the service and store the response.
794 // Note: The ID used in this 'create' call may be arbitrary.
795 // The only relevant ID may be the one used in update(), below.
797 // The only relevant ID may be the one used in update(), below.
798 MultipartOutput multipart = createVocabularyItemInstance(
799 knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID));
800 ClientResponse<MultipartInput> res =
801 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
802 int statusCode = res.getStatus();
804 // Check the status code of the response: does it match
805 // the expected response(s)?
806 if(logger.isDebugEnabled()){
807 logger.debug(testName + ": status = " + statusCode);
809 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
810 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
811 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
814 // ---------------------------------------------------------------
815 // CRUD tests : DELETE tests
816 // ---------------------------------------------------------------
819 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
820 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
821 public void delete(String testName) throws Exception {
824 setupDelete(testName);
826 // Submit the request to the service and store the response.
827 ClientResponse<Response> res = client.delete(knownResourceId);
828 int statusCode = res.getStatus();
830 // Check the status code of the response: does it match
831 // the expected response(s)?
832 if(logger.isDebugEnabled()){
833 logger.debug(testName + ": status = " + statusCode);
835 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
836 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
837 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
840 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
841 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
843 public void deleteItem(String testName) throws Exception {
846 setupDelete(testName);
848 // Submit the request to the service and store the response.
849 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
850 int statusCode = res.getStatus();
852 // Check the status code of the response: does it match
853 // the expected response(s)?
854 if(logger.isDebugEnabled()){
855 logger.debug("delete: status = " + statusCode);
857 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
858 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
859 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
864 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
865 dependsOnMethods = {"delete"})
866 public void deleteNonExistent(String testName) throws Exception {
869 setupDeleteNonExistent(testName);
871 // Submit the request to the service and store the response.
872 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
873 int statusCode = res.getStatus();
875 // Check the status code of the response: does it match
876 // the expected response(s)?
877 if(logger.isDebugEnabled()){
878 logger.debug(testName + ": status = " + statusCode);
880 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
881 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
882 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
885 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
886 dependsOnMethods = {"deleteItem"})
887 public void deleteNonExistentItem(String testName) {
890 setupDeleteNonExistent(testName);
892 // Submit the request to the service and store the response.
893 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
894 int statusCode = res.getStatus();
896 // Check the status code of the response: does it match
897 // the expected response(s)?
898 if(logger.isDebugEnabled()){
899 logger.debug(testName + ": status = " + statusCode);
901 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
902 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
903 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
906 // ---------------------------------------------------------------
907 // Utility tests : tests of code used in tests above
908 // ---------------------------------------------------------------
910 * Tests the code for manually submitting data that is used by several
911 * of the methods above.
913 @Test(dependsOnMethods = {"create", "read"})
914 public void testSubmitRequest() {
916 // Expected status code: 200 OK
917 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
919 // Submit the request to the service and store the response.
920 String method = ServiceRequestType.READ.httpMethodName();
921 String url = getResourceURL(knownResourceId);
922 int statusCode = submitRequest(method, url);
924 // Check the status code of the response: does it match
925 // the expected response(s)?
926 if(logger.isDebugEnabled()){
927 logger.debug("testSubmitRequest: url=" + url +
928 " status=" + statusCode);
930 Assert.assertEquals(statusCode, EXPECTED_STATUS);
934 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
935 public void testItemSubmitRequest() {
937 // Expected status code: 200 OK
938 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
940 // Submit the request to the service and store the response.
941 String method = ServiceRequestType.READ.httpMethodName();
942 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
943 int statusCode = submitRequest(method, url);
945 // Check the status code of the response: does it match
946 // the expected response(s)?
947 if(logger.isDebugEnabled()){
948 logger.debug("testItemSubmitRequest: url=" + url +
949 " status=" + statusCode);
951 Assert.assertEquals(statusCode, EXPECTED_STATUS);
955 // ---------------------------------------------------------------
956 // Cleanup of resources created during testing
957 // ---------------------------------------------------------------
960 * Deletes all resources created by tests, after all tests have been run.
962 * This cleanup method will always be run, even if one or more tests fail.
963 * For this reason, it attempts to remove all resources created
964 * at any point during testing, even if some of those resources
965 * may be expected to be deleted by certain tests.
967 @AfterClass(alwaysRun=true)
968 public void cleanUp() {
970 if (logger.isDebugEnabled()) {
971 logger.debug("Cleaning up temporary resources created for testing ...");
973 // Clean up vocabulary item resources.
974 String vocabularyResourceId;
975 String vocabularyItemResourceId;
976 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
977 vocabularyItemResourceId = entry.getKey();
978 vocabularyResourceId = entry.getValue();
979 // Note: Any non-success responses are ignored and not reported.
980 ClientResponse<Response> res =
981 client.deleteItem(vocabularyResourceId, vocabularyItemResourceId);
983 // Clean up vocabulary resources.
984 for (String resourceId : allResourceIdsCreated) {
985 // Note: Any non-success responses are ignored and not reported.
986 ClientResponse<Response> res = client.delete(resourceId);
991 // ---------------------------------------------------------------
992 // Utility methods used by tests above
993 // ---------------------------------------------------------------
995 public String getServicePathComponent() {
996 return SERVICE_PATH_COMPONENT;
999 public String getItemServicePathComponent() {
1000 return ITEM_SERVICE_PATH_COMPONENT;
1004 * Returns the root URL for a service.
1006 * This URL consists of a base URL for all services, followed by
1007 * a path component for the owning vocabulary, followed by the
1008 * path component for the items.
1010 * @return The root URL for a service.
1012 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1013 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1017 * Returns the URL of a specific resource managed by a service, and
1018 * designated by an identifier (such as a universally unique ID, or UUID).
1020 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1022 * @return The URL of a specific resource managed by a service.
1024 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1025 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1028 private MultipartOutput createVocabularyInstance(String identifier) {
1029 String displayName = "displayName-" + identifier;
1030 String refName = createRefName(displayName);
1031 String typeName = "vocabType-" + identifier;
1032 return createVocabularyInstance(
1033 displayName, refName,typeName );
1036 private MultipartOutput createVocabularyInstance(
1037 String displayName, String refName, String vocabType) {
1038 VocabulariesCommon vocabulary = new VocabulariesCommon();
1039 vocabulary.setDisplayName(displayName);
1041 vocabulary.setRefName(refName);
1042 vocabulary.setVocabType(vocabType);
1043 MultipartOutput multipart = new MultipartOutput();
1044 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
1045 commonPart.getHeaders().add("label", client.getCommonPartName());
1047 if(logger.isDebugEnabled()) {
1048 logger.debug("to be created, vocabulary common");
1049 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
1054 private MultipartOutput createVocabularyItemInstance(String inVocabulary,
1055 String displayName, String refName) {
1056 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
1057 vocabularyItem.setInVocabulary(inVocabulary);
1058 vocabularyItem.setDisplayName(displayName);
1060 vocabularyItem.setRefName(refName);
1061 MultipartOutput multipart = new MultipartOutput();
1062 OutputPart commonPart = multipart.addPart(vocabularyItem,
1063 MediaType.APPLICATION_XML_TYPE);
1064 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1066 if(logger.isDebugEnabled()){
1067 logger.debug("to be created, vocabularyitem common");
1068 logger.debug(objectAsXmlString(vocabularyItem,
1069 VocabularyitemsCommon.class));