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 knownItemResourceId = null;
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
69 public void create(String testName) throws Exception {
71 // Perform setup, such as initializing the type of service request
72 // (e.g. CREATE, DELETE), its valid and expected status codes, and
73 // its associated HTTP method name (e.g. POST, DELETE).
74 setupCreate(testName);
76 // Submit the request to the service and store the response.
77 String identifier = createIdentifier();
78 MultipartOutput multipart = createVocabularyInstance(identifier);
79 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 // Check the status code of the response: does it match
83 // the expected response(s)?
86 // Does it fall within the set of valid status codes?
87 // Does it exactly match the expected status code?
88 if(logger.isDebugEnabled()){
89 logger.debug(testName + ": status = " + statusCode);
91 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95 // Store the ID returned from this create operation
96 // for additional tests below.
97 knownResourceId = extractId(res);
98 if(logger.isDebugEnabled()){
99 logger.debug("create: knownResourceId=" + knownResourceId);
103 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
104 dependsOnMethods = {"create"})
105 public void createItem(String testName) {
106 setupCreate(testName);
108 knownItemResourceId = createItemInVocab(knownResourceId);
109 if(logger.isDebugEnabled()){
110 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
114 private String createItemInVocab(String vcsid) {
116 final String testName = "createItemInVocab";
117 if(logger.isDebugEnabled()){
118 logger.debug(testName + ":...");
121 // Submit the request to the service and store the response.
122 String identifier = createIdentifier();
123 MultipartOutput multipart = createVocabularyItemInstance(vcsid, identifier);
124 ClientResponse<Response> res = client.createItem(vcsid, multipart);
125 int statusCode = res.getStatus();
127 // Check the status code of the response: does it match
128 // the expected response(s)?
129 if(logger.isDebugEnabled()){
130 logger.debug(testName + ": status = " + statusCode);
132 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
133 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
134 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
136 return extractId(res);
140 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
141 dependsOnMethods = {"create", "createItem"})
142 public void createList(String testName) throws Exception {
143 for (int i = 0; i < 3; i++) {
145 // Add 3 items to each vocab
146 for (int j = 0; j < 3; j++) {
147 createItem(testName);
153 // Placeholders until the three tests below can be uncommented.
154 // See Issue CSPACE-401.
156 public void createWithEmptyEntityBody(String testName) throws Exception {
160 public void createWithMalformedXml(String testName) throws Exception {
164 public void createWithWrongXmlSchema(String testName) throws Exception {
169 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
170 dependsOnMethods = {"create", "testSubmitRequest"})
171 public void createWithEmptyEntityBody(String testName) throws Exception {
174 setupCreateWithEmptyEntityBody(testName);
176 // Submit the request to the service and store the response.
177 String method = REQUEST_TYPE.httpMethodName();
178 String url = getServiceRootURL();
179 String mediaType = MediaType.APPLICATION_XML;
180 final String entity = "";
181 int statusCode = submitRequest(method, url, mediaType, entity);
183 // Check the status code of the response: does it match
184 // the expected response(s)?
185 if(logger.isDebugEnabled()) {
186 logger.debug(testName + ": url=" + url +
187 " status=" + statusCode);
189 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
195 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
196 dependsOnMethods = {"create", "testSubmitRequest"})
197 public void createWithMalformedXml(String testName) throws Exception {
200 setupCreateWithMalformedXml(testName);
202 // Submit the request to the service and store the response.
203 String method = REQUEST_TYPE.httpMethodName();
204 String url = getServiceRootURL();
205 String mediaType = MediaType.APPLICATION_XML;
206 final String entity = MALFORMED_XML_DATA; // Constant from base class.
207 int statusCode = submitRequest(method, url, mediaType, entity);
209 // Check the status code of the response: does it match
210 // the expected response(s)?
211 if(logger.isDebugEnabled()){
212 logger.debug(testName + ": url=" + url +
213 " status=" + statusCode);
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create", "testSubmitRequest"})
223 public void createWithWrongXmlSchema(String testName) throws Exception {
226 setupCreateWithWrongXmlSchema(testName);
228 // Submit the request to the service and store the response.
229 String method = REQUEST_TYPE.httpMethodName();
230 String url = getServiceRootURL();
231 String mediaType = MediaType.APPLICATION_XML;
232 final String entity = WRONG_XML_SCHEMA_DATA;
233 int statusCode = submitRequest(method, url, mediaType, entity);
235 // Check the status code of the response: does it match
236 // the expected response(s)?
237 if(logger.isDebugEnabled()){
238 logger.debug(testName + ": url=" + url +
239 " status=" + statusCode);
241 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
243 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
247 // ---------------------------------------------------------------
248 // CRUD tests : READ tests
249 // ---------------------------------------------------------------
252 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
253 dependsOnMethods = {"create"})
254 public void read(String testName) throws Exception {
259 // Submit the request to the service and store the response.
260 ClientResponse<MultipartInput> res = client.read(knownResourceId);
261 int statusCode = res.getStatus();
263 // Check the status code of the response: does it match
264 // the expected response(s)?
265 if(logger.isDebugEnabled()){
266 logger.debug(testName + ": status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271 //FIXME: remove the following try catch once Aron fixes signatures
273 MultipartInput input = (MultipartInput) res.getEntity();
274 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
275 client.getCommonPartName(), VocabulariesCommon.class);
276 Assert.assertNotNull(vocabulary);
277 } catch (Exception e) {
278 throw new RuntimeException(e);
282 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
283 dependsOnMethods = {"createItem", "read"})
284 public void readItem(String testName) throws Exception {
289 // Submit the request to the service and store the response.
290 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
291 int statusCode = res.getStatus();
293 // Check the status code of the response: does it match
294 // the expected response(s)?
295 if(logger.isDebugEnabled()){
296 logger.debug(testName + ": status = " + statusCode);
298 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
299 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
300 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
302 // Check whether we've received a vocabulary item.
303 MultipartInput input = (MultipartInput) res.getEntity();
304 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
305 client.getItemCommonPartName(), VocabularyitemsCommon.class);
306 Assert.assertNotNull(vocabularyItem);
312 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
313 dependsOnMethods = {"read"})
314 public void readNonExistent(String testName) {
317 setupReadNonExistent(testName);
319 // Submit the request to the service and store the response.
320 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
321 int statusCode = res.getStatus();
323 // Check the status code of the response: does it match
324 // the expected response(s)?
325 if(logger.isDebugEnabled()){
326 logger.debug(testName + ": status = " + statusCode);
328 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
329 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
330 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
333 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
334 dependsOnMethods = {"readItem", "readNonExistent"})
335 public void readItemNonExistent(String testName) {
338 setupReadNonExistent(testName);
340 // Submit the request to the service and store the response.
341 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
342 int statusCode = res.getStatus();
344 // Check the status code of the response: does it match
345 // the expected response(s)?
346 if(logger.isDebugEnabled()){
347 logger.debug(testName + ": status = " + statusCode);
349 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
350 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
351 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
353 // ---------------------------------------------------------------
354 // CRUD tests : READ_LIST tests
355 // ---------------------------------------------------------------
359 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
360 dependsOnMethods = {"read"})
361 public void readList(String testName) throws Exception {
364 setupReadList(testName);
366 // Submit the request to the service and store the response.
367 ClientResponse<VocabulariesCommonList> res = client.readList();
368 VocabulariesCommonList list = res.getEntity();
369 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match
372 // the expected response(s)?
373 if(logger.isDebugEnabled()){
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
380 // Optionally output additional data about list members for debugging.
381 boolean iterateThroughList = false;
382 if (iterateThroughList && logger.isDebugEnabled()) {
383 List<VocabulariesCommonList.VocabularyListItem> items =
384 list.getVocabularyListItem();
386 for (VocabulariesCommonList.VocabularyListItem item : items) {
387 String csid = item.getCsid();
388 logger.debug(testName + ": list-item[" + i + "] csid=" +
390 logger.debug(testName + ": list-item[" + i + "] displayName=" +
391 item.getDisplayName());
392 logger.debug(testName + ": list-item[" + i + "] URI=" +
400 @Test(dependsOnMethods = {"readItem"})
401 public void readItemList() {
402 readItemList(knownResourceId);
405 private void readItemList(String vcsid) {
407 final String testName = "readItemList";
410 setupReadList(testName);
412 // Submit the request to the service and store the response.
413 ClientResponse<VocabularyitemsCommonList> res =
414 client.readItemList(vcsid);
415 VocabularyitemsCommonList list = res.getEntity();
416 int statusCode = res.getStatus();
418 // Check the status code of the response: does it match
419 // the expected response(s)?
420 if(logger.isDebugEnabled()){
421 logger.debug(" " + testName + ": status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
427 // Optionally output additional data about list members for debugging.
428 boolean iterateThroughList = false;
429 if (iterateThroughList && logger.isDebugEnabled()) {
430 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
431 list.getVocabularyitemListItem();
433 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
434 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
436 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
437 item.getDisplayName());
438 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
447 // ---------------------------------------------------------------
448 // CRUD tests : UPDATE tests
449 // ---------------------------------------------------------------
452 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
453 dependsOnMethods = {"read"})
454 public void update(String testName) throws Exception {
457 setupUpdate(testName);
459 // Retrieve the contents of a resource to update.
460 ClientResponse<MultipartInput> res =
461 client.read(knownResourceId);
462 if(logger.isDebugEnabled()){
463 logger.debug(testName + ": read status = " + res.getStatus());
465 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
467 if(logger.isDebugEnabled()){
468 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
470 MultipartInput input = (MultipartInput) res.getEntity();
471 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
472 client.getCommonPartName(), VocabulariesCommon.class);
473 Assert.assertNotNull(vocabulary);
475 // Update the contents of this resource.
476 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
477 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
478 if(logger.isDebugEnabled()){
479 logger.debug("to be updated Vocabulary");
480 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
483 // Submit the updated resource to the service and store the response.
484 MultipartOutput output = new MultipartOutput();
485 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
486 commonPart.getHeaders().add("label", client.getCommonPartName());
487 res = client.update(knownResourceId, output);
488 int statusCode = res.getStatus();
490 // Check the status code of the response: does it match the expected response(s)?
491 if(logger.isDebugEnabled()){
492 logger.debug("update: status = " + statusCode);
494 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
495 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
496 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
498 // Retrieve the updated resource and verify that its contents exist.
499 input = (MultipartInput) res.getEntity();
500 VocabulariesCommon updatedVocabulary =
501 (VocabulariesCommon) extractPart(input,
502 client.getCommonPartName(), VocabulariesCommon.class);
503 Assert.assertNotNull(updatedVocabulary);
505 // Verify that the updated resource received the correct data.
506 Assert.assertEquals(updatedVocabulary.getDisplayName(),
507 vocabulary.getDisplayName(),
508 "Data in updated object did not match submitted data.");
511 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
512 dependsOnMethods = {"readItem", "update"})
513 public void updateItem(String testName) throws Exception {
516 setupUpdate(testName);
518 ClientResponse<MultipartInput> res =
519 client.readItem(knownResourceId, knownItemResourceId);
520 if(logger.isDebugEnabled()){
521 logger.debug(testName + ": read status = " + res.getStatus());
523 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
525 if(logger.isDebugEnabled()){
526 logger.debug("got VocabularyItem to update with ID: " +
527 knownItemResourceId +
528 " in Vocab: " + knownResourceId );
530 MultipartInput input = (MultipartInput) res.getEntity();
531 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
532 client.getItemCommonPartName(), VocabularyitemsCommon.class);
533 Assert.assertNotNull(vocabularyItem);
535 // Update the contents of this resource.
536 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
537 if(logger.isDebugEnabled()){
538 logger.debug("to be updated VocabularyItem");
539 logger.debug(objectAsXmlString(vocabularyItem,
540 VocabularyitemsCommon.class));
543 // Submit the updated resource to the service and store the response.
544 MultipartOutput output = new MultipartOutput();
545 OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
546 commonPart.getHeaders().add("label", client.getItemCommonPartName());
547 res = client.updateItem(knownResourceId, knownItemResourceId, output);
548 int statusCode = res.getStatus();
550 // Check the status code of the response: does it match the expected response(s)?
551 if(logger.isDebugEnabled()){
552 logger.debug("updateItem: status = " + statusCode);
554 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
555 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
556 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
558 // Retrieve the updated resource and verify that its contents exist.
559 input = (MultipartInput) res.getEntity();
560 VocabularyitemsCommon updatedVocabularyItem =
561 (VocabularyitemsCommon) extractPart(input,
562 client.getItemCommonPartName(), VocabularyitemsCommon.class);
563 Assert.assertNotNull(updatedVocabularyItem);
565 // Verify that the updated resource received the correct data.
566 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
567 vocabularyItem.getDisplayName(),
568 "Data in updated VocabularyItem did not match submitted data.");
572 // Placeholders until the three tests below can be uncommented.
573 // See Issue CSPACE-401.
575 public void updateWithEmptyEntityBody(String testName) throws Exception {
579 public void updateWithMalformedXml(String testName) throws Exception {
583 public void updateWithWrongXmlSchema(String testName) throws Exception {
588 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
589 dependsOnMethods = {"create", "update", "testSubmitRequest"})
590 public void updateWithEmptyEntityBody(String testName) throws Exception {
593 setupUpdateWithEmptyEntityBody(testName);
595 // Submit the request to the service and store the response.
596 String method = REQUEST_TYPE.httpMethodName();
597 String url = getResourceURL(knownResourceId);
598 String mediaType = MediaType.APPLICATION_XML;
599 final String entity = "";
600 int statusCode = submitRequest(method, url, mediaType, entity);
602 // Check the status code of the response: does it match
603 // the expected response(s)?
604 if(logger.isDebugEnabled()){
605 logger.debug(testName + ": url=" + url +
606 " status=" + statusCode);
608 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
609 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
610 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
615 dependsOnMethods = {"create", "update", "testSubmitRequest"})
616 public void updateWithMalformedXml(String testName) throws Exception {
619 setupUpdateWithMalformedXml(testName);
621 // Submit the request to the service and store the response.
622 String method = REQUEST_TYPE.httpMethodName();
623 String url = getResourceURL(knownResourceId);
624 String mediaType = MediaType.APPLICATION_XML;
625 final String entity = MALFORMED_XML_DATA;
626 int statusCode = submitRequest(method, url, mediaType, entity);
628 // Check the status code of the response: does it match
629 // the expected response(s)?
630 if(logger.isDebugEnabled()){
631 logger.debug(testName + ": url=" + url +
632 " status=" + statusCode);
634 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
635 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
636 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
640 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
641 dependsOnMethods = {"create", "update", "testSubmitRequest"})
642 public void updateWithWrongXmlSchema(String testName) throws Exception {
645 setupUpdateWithWrongXmlSchema(testName);
647 // Submit the request to the service and store the response.
648 String method = REQUEST_TYPE.httpMethodName();
649 String url = getResourceURL(knownResourceId);
650 String mediaType = MediaType.APPLICATION_XML;
651 final String entity = WRONG_XML_SCHEMA_DATA;
652 int statusCode = submitRequest(method, url, mediaType, entity);
654 // Check the status code of the response: does it match
655 // the expected response(s)?
656 if(logger.isDebugEnabled()){
657 logger.debug("updateWithWrongXmlSchema: url=" + url +
658 " status=" + statusCode);
660 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
661 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
662 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
668 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
669 dependsOnMethods = {"update", "testSubmitRequest"})
670 public void updateNonExistent(String testName) throws Exception {
673 setupUpdateNonExistent(testName);
675 // Submit the request to the service and store the response.
676 // Note: The ID used in this 'create' call may be arbitrary.
677 // The only relevant ID may be the one used in update(), below.
679 // The only relevant ID may be the one used in update(), below.
680 MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
681 ClientResponse<MultipartInput> res =
682 client.update(NON_EXISTENT_ID, multipart);
683 int statusCode = res.getStatus();
685 // Check the status code of the response: does it match
686 // the expected response(s)?
687 if(logger.isDebugEnabled()){
688 logger.debug(testName + ": status = " + statusCode);
690 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
691 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
692 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
695 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
696 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
697 public void updateNonExistentItem(String testName) throws Exception {
700 setupUpdateNonExistent(testName);
702 // Submit the request to the service and store the response.
703 // Note: The ID used in this 'create' call may be arbitrary.
704 // The only relevant ID may be the one used in update(), below.
706 // The only relevant ID may be the one used in update(), below.
707 MultipartOutput multipart = createVocabularyItemInstance(knownResourceId, NON_EXISTENT_ID);
708 ClientResponse<MultipartInput> res =
709 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
710 int statusCode = res.getStatus();
712 // Check the status code of the response: does it match
713 // the expected response(s)?
714 if(logger.isDebugEnabled()){
715 logger.debug(testName + ": status = " + statusCode);
717 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
718 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
719 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
722 // ---------------------------------------------------------------
723 // CRUD tests : DELETE tests
724 // ---------------------------------------------------------------
727 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
728 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
729 public void delete(String testName) throws Exception {
732 setupDelete(testName);
734 // Submit the request to the service and store the response.
735 ClientResponse<Response> res = client.delete(knownResourceId);
736 int statusCode = res.getStatus();
738 // Check the status code of the response: does it match
739 // the expected response(s)?
740 if(logger.isDebugEnabled()){
741 logger.debug(testName + ": status = " + statusCode);
743 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
744 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
745 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
748 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
749 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
751 public void deleteItem(String testName) throws Exception {
754 setupDelete(testName);
756 // Submit the request to the service and store the response.
757 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
758 int statusCode = res.getStatus();
760 // Check the status code of the response: does it match
761 // the expected response(s)?
762 if(logger.isDebugEnabled()){
763 logger.debug("delete: status = " + statusCode);
765 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
766 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
767 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
772 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
773 dependsOnMethods = {"delete"})
774 public void deleteNonExistent(String testName) throws Exception {
777 setupDeleteNonExistent(testName);
779 // Submit the request to the service and store the response.
780 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
781 int statusCode = res.getStatus();
783 // Check the status code of the response: does it match
784 // the expected response(s)?
785 if(logger.isDebugEnabled()){
786 logger.debug(testName + ": status = " + statusCode);
788 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
789 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
790 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
793 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
794 dependsOnMethods = {"deleteItem"})
795 public void deleteNonExistentItem(String testName) {
798 setupDeleteNonExistent(testName);
800 // Submit the request to the service and store the response.
801 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
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 // Utility tests : tests of code used in tests above
816 // ---------------------------------------------------------------
818 * Tests the code for manually submitting data that is used by several
819 * of the methods above.
821 @Test(dependsOnMethods = {"create", "read"})
822 public void testSubmitRequest() {
824 // Expected status code: 200 OK
825 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
827 // Submit the request to the service and store the response.
828 String method = ServiceRequestType.READ.httpMethodName();
829 String url = getResourceURL(knownResourceId);
830 int statusCode = submitRequest(method, url);
832 // Check the status code of the response: does it match
833 // the expected response(s)?
834 if(logger.isDebugEnabled()){
835 logger.debug("testSubmitRequest: url=" + url +
836 " status=" + statusCode);
838 Assert.assertEquals(statusCode, EXPECTED_STATUS);
842 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
843 public void testItemSubmitRequest() {
845 // Expected status code: 200 OK
846 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
848 // Submit the request to the service and store the response.
849 String method = ServiceRequestType.READ.httpMethodName();
850 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
851 int statusCode = submitRequest(method, url);
853 // Check the status code of the response: does it match
854 // the expected response(s)?
855 if(logger.isDebugEnabled()){
856 logger.debug("testItemSubmitRequest: url=" + url +
857 " status=" + statusCode);
859 Assert.assertEquals(statusCode, EXPECTED_STATUS);
863 // ---------------------------------------------------------------
864 // Utility methods used by tests above
865 // ---------------------------------------------------------------
867 public String getServicePathComponent() {
868 return SERVICE_PATH_COMPONENT;
871 public String getItemServicePathComponent() {
872 return ITEM_SERVICE_PATH_COMPONENT;
876 * Returns the root URL for a service.
878 * This URL consists of a base URL for all services, followed by
879 * a path component for the owning vocabulary, followed by the
880 * path component for the items.
882 * @return The root URL for a service.
884 protected String getItemServiceRootURL(String parentResourceIdentifier) {
885 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
889 * Returns the URL of a specific resource managed by a service, and
890 * designated by an identifier (such as a universally unique ID, or UUID).
892 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
894 * @return The URL of a specific resource managed by a service.
896 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
897 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
900 private MultipartOutput createVocabularyInstance(String identifier) {
901 return createVocabularyInstance(
902 "displayName-" + identifier,
903 "vocabType-" + identifier);
906 private MultipartOutput createVocabularyInstance(String displayName, String vocabType) {
907 VocabulariesCommon vocabulary = new VocabulariesCommon();
908 vocabulary.setDisplayName(displayName);
909 vocabulary.setVocabType(vocabType);
910 MultipartOutput multipart = new MultipartOutput();
911 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
912 commonPart.getHeaders().add("label", client.getCommonPartName());
914 if(logger.isDebugEnabled()) {
915 logger.debug("to be created, vocabulary common");
916 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
921 private MultipartOutput createVocabularyItemInstance(String inVocabulary,
922 String displayName) {
923 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
924 vocabularyItem.setInVocabulary(inVocabulary);
925 vocabularyItem.setDisplayName(displayName);
926 MultipartOutput multipart = new MultipartOutput();
927 OutputPart commonPart = multipart.addPart(vocabularyItem,
928 MediaType.APPLICATION_XML_TYPE);
929 commonPart.getHeaders().add("label", client.getItemCommonPartName());
931 if(logger.isDebugEnabled()){
932 logger.debug("to be created, vocabularyitem common");
933 logger.debug(objectAsXmlString(vocabularyItem,
934 VocabularyitemsCommon.class));