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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
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;
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;
45 * VocabularyServiceTest, carries out tests against a
46 * deployed and running Vocabulary Service.
48 * $LastChangedRevision: 753 $
49 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
51 public class VocabularyServiceTest extends AbstractServiceTest {
53 private final Logger logger =
54 LoggerFactory.getLogger(VocabularyServiceTest.class);
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;
64 protected String createRefName(String displayName) {
65 return displayName.replaceAll("\\W", "");
68 // ---------------------------------------------------------------
69 // CRUD tests : CREATE tests
70 // ---------------------------------------------------------------
73 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
74 public void create(String testName) throws Exception {
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);
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();
91 // Check the status code of the response: does it match
92 // the expected response(s)?
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);
100 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
101 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
102 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
114 dependsOnMethods = {"create"})
115 public void createItem(String testName) {
116 setupCreate(testName);
118 knownItemResourceId = createItemInVocab(knownResourceId);
119 if(logger.isDebugEnabled()){
120 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
124 private String createItemInVocab(String vcsid) {
126 final String testName = "createItemInVocab";
127 if(logger.isDebugEnabled()){
128 logger.debug(testName + ":...");
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();
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);
143 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
144 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
147 return extractId(res);
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++) {
156 // Add 3 items to each vocab
157 for (int j = 0; j < 3; j++) {
158 createItem(testName);
164 // Placeholders until the three tests below can be uncommented.
165 // See Issue CSPACE-401.
167 public void createWithEmptyEntityBody(String testName) throws Exception {
171 public void createWithMalformedXml(String testName) throws Exception {
175 public void createWithWrongXmlSchema(String testName) throws Exception {
180 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
181 dependsOnMethods = {"create", "testSubmitRequest"})
182 public void createWithEmptyEntityBody(String testName) throws Exception {
185 setupCreateWithEmptyEntityBody(testName);
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);
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);
200 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
206 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
207 dependsOnMethods = {"create", "testSubmitRequest"})
208 public void createWithMalformedXml(String testName) throws Exception {
211 setupCreateWithMalformedXml(testName);
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);
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);
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
233 dependsOnMethods = {"create", "testSubmitRequest"})
234 public void createWithWrongXmlSchema(String testName) throws Exception {
237 setupCreateWithWrongXmlSchema(testName);
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);
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);
252 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
258 // ---------------------------------------------------------------
259 // CRUD tests : READ tests
260 // ---------------------------------------------------------------
263 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
264 dependsOnMethods = {"create"})
265 public void read(String testName) throws Exception {
270 // Submit the request to the service and store the response.
271 ClientResponse<MultipartInput> res = client.read(knownResourceId);
272 int statusCode = res.getStatus();
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);
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
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);
294 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
295 dependsOnMethods = {"read"})
296 public void readByName(String testName) throws Exception {
301 // Submit the request to the service and store the response.
302 ClientResponse<MultipartInput> res = client.read(knownResourceId);
303 int statusCode = res.getStatus();
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);
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
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);
325 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
326 dependsOnMethods = {"createItem", "read"})
327 public void readItem(String testName) throws Exception {
332 // Submit the request to the service and store the response.
333 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
334 int statusCode = res.getStatus();
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);
341 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
342 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
343 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
355 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
356 dependsOnMethods = {"read"})
357 public void readNonExistent(String testName) {
360 setupReadNonExistent(testName);
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();
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);
371 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
372 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
373 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
376 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
377 dependsOnMethods = {"readItem", "readNonExistent"})
378 public void readItemNonExistent(String testName) {
381 setupReadNonExistent(testName);
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();
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);
392 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
396 // ---------------------------------------------------------------
397 // CRUD tests : READ_LIST tests
398 // ---------------------------------------------------------------
402 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
403 dependsOnMethods = {"read"})
404 public void readList(String testName) throws Exception {
407 setupReadList(testName);
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();
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);
419 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
429 for (VocabulariesCommonList.VocabularyListItem item : items) {
430 String csid = item.getCsid();
431 logger.debug(testName + ": list-item[" + i + "] csid=" +
433 logger.debug(testName + ": list-item[" + i + "] displayName=" +
434 item.getDisplayName());
435 logger.debug(testName + ": list-item[" + i + "] URI=" +
443 @Test(dependsOnMethods = {"readItem"})
444 public void readItemList() {
445 readItemList(knownResourceId);
448 private void readItemList(String vcsid) {
450 final String testName = "readItemList";
453 setupReadList(testName);
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();
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);
466 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
467 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
468 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
476 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
477 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
479 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
480 item.getDisplayName());
481 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
490 // ---------------------------------------------------------------
491 // CRUD tests : UPDATE tests
492 // ---------------------------------------------------------------
495 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
496 dependsOnMethods = {"read"})
497 public void update(String testName) throws Exception {
500 setupUpdate(testName);
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());
508 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
510 if(logger.isDebugEnabled()){
511 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
513 MultipartInput input = (MultipartInput) res.getEntity();
514 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
515 client.getCommonPartName(), VocabulariesCommon.class);
516 Assert.assertNotNull(vocabulary);
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));
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();
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);
537 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
538 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
539 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
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.");
554 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
555 dependsOnMethods = {"readItem", "update"})
556 public void updateItem(String testName) throws Exception {
559 setupUpdate(testName);
561 ClientResponse<MultipartInput> res =
562 client.readItem(knownResourceId, knownItemResourceId);
563 if(logger.isDebugEnabled()){
564 logger.debug(testName + ": read status = " + res.getStatus());
566 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
568 if(logger.isDebugEnabled()){
569 logger.debug("got VocabularyItem to update with ID: " +
570 knownItemResourceId +
571 " in Vocab: " + knownResourceId );
573 MultipartInput input = (MultipartInput) res.getEntity();
574 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
575 client.getItemCommonPartName(), VocabularyitemsCommon.class);
576 Assert.assertNotNull(vocabularyItem);
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));
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();
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);
597 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
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.");
615 // Placeholders until the three tests below can be uncommented.
616 // See Issue CSPACE-401.
618 public void updateWithEmptyEntityBody(String testName) throws Exception {
622 public void updateWithMalformedXml(String testName) throws Exception {
626 public void updateWithWrongXmlSchema(String testName) throws Exception {
631 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
632 dependsOnMethods = {"create", "update", "testSubmitRequest"})
633 public void updateWithEmptyEntityBody(String testName) throws Exception {
636 setupUpdateWithEmptyEntityBody(testName);
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);
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);
651 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
652 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
653 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
657 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
658 dependsOnMethods = {"create", "update", "testSubmitRequest"})
659 public void updateWithMalformedXml(String testName) throws Exception {
662 setupUpdateWithMalformedXml(testName);
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);
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);
677 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
678 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
679 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
683 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
684 dependsOnMethods = {"create", "update", "testSubmitRequest"})
685 public void updateWithWrongXmlSchema(String testName) throws Exception {
688 setupUpdateWithWrongXmlSchema(testName);
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);
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);
703 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
704 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
705 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
711 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
712 dependsOnMethods = {"update", "testSubmitRequest"})
713 public void updateNonExistent(String testName) throws Exception {
716 setupUpdateNonExistent(testName);
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.
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();
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);
733 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
734 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
735 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
738 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
739 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
740 public void updateNonExistentItem(String testName) throws Exception {
743 setupUpdateNonExistent(testName);
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.
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();
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);
761 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
762 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
763 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
766 // ---------------------------------------------------------------
767 // CRUD tests : DELETE tests
768 // ---------------------------------------------------------------
771 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
772 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
773 public void delete(String testName) throws Exception {
776 setupDelete(testName);
778 // Submit the request to the service and store the response.
779 ClientResponse<Response> res = client.delete(knownResourceId);
780 int statusCode = res.getStatus();
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);
787 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
788 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
789 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
792 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
793 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
795 public void deleteItem(String testName) throws Exception {
798 setupDelete(testName);
800 // Submit the request to the service and store the response.
801 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
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("delete: status = " + statusCode);
809 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
810 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
811 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
816 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
817 dependsOnMethods = {"delete"})
818 public void deleteNonExistent(String testName) throws Exception {
821 setupDeleteNonExistent(testName);
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();
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);
832 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
833 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
834 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
837 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
838 dependsOnMethods = {"deleteItem"})
839 public void deleteNonExistentItem(String testName) {
842 setupDeleteNonExistent(testName);
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();
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);
853 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
854 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
855 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
858 // ---------------------------------------------------------------
859 // Utility tests : tests of code used in tests above
860 // ---------------------------------------------------------------
862 * Tests the code for manually submitting data that is used by several
863 * of the methods above.
865 @Test(dependsOnMethods = {"create", "read"})
866 public void testSubmitRequest() {
868 // Expected status code: 200 OK
869 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
882 Assert.assertEquals(statusCode, EXPECTED_STATUS);
886 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
887 public void testItemSubmitRequest() {
889 // Expected status code: 200 OK
890 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
903 Assert.assertEquals(statusCode, EXPECTED_STATUS);
907 // ---------------------------------------------------------------
908 // Utility methods used by tests above
909 // ---------------------------------------------------------------
911 public String getServicePathComponent() {
912 return SERVICE_PATH_COMPONENT;
915 public String getItemServicePathComponent() {
916 return ITEM_SERVICE_PATH_COMPONENT;
920 * Returns the root URL for a service.
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.
926 * @return The root URL for a service.
928 protected String getItemServiceRootURL(String parentResourceIdentifier) {
929 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
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).
936 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
938 * @return The URL of a specific resource managed by a service.
940 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
941 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
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 );
952 private MultipartOutput createVocabularyInstance(
953 String displayName, String refName, String vocabType) {
954 VocabulariesCommon vocabulary = new VocabulariesCommon();
955 vocabulary.setDisplayName(displayName);
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());
963 if(logger.isDebugEnabled()) {
964 logger.debug("to be created, vocabulary common");
965 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
970 private MultipartOutput createVocabularyItemInstance(String inVocabulary,
971 String displayName, String refName) {
972 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
973 vocabularyItem.setInVocabulary(inVocabulary);
974 vocabularyItem.setDisplayName(displayName);
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());
982 if(logger.isDebugEnabled()){
983 logger.debug("to be created, vocabularyitem common");
984 logger.debug(objectAsXmlString(vocabularyItem,
985 VocabularyitemsCommon.class));