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 knownResourceRefName = null;
66 private String knownItemResourceId = null;
67 private List<String> allResourceIdsCreated = new ArrayList<String>();
68 private Map<String, String> allResourceItemIdsCreated =
69 new HashMap<String, String>();
71 protected String createRefName(String displayName) {
72 return displayName.replaceAll("\\W", "");
75 // ---------------------------------------------------------------
76 // CRUD tests : CREATE tests
77 // ---------------------------------------------------------------
80 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
81 public void create(String testName) throws Exception {
83 // Perform setup, such as initializing the type of service request
84 // (e.g. CREATE, DELETE), its valid and expected status codes, and
85 // its associated HTTP method name (e.g. POST, DELETE).
86 setupCreate(testName);
88 // Submit the request to the service and store the response.
89 String identifier = createIdentifier();
90 String displayName = "displayName-" + identifier;
91 String refName = createRefName(displayName);
92 String typeName = "vocabType-" + identifier;
93 MultipartOutput multipart =
94 createVocabularyInstance(displayName, refName, typeName);
95 ClientResponse<Response> res = client.create(multipart);
96 int statusCode = res.getStatus();
98 // Check the status code of the response: does it match
99 // the expected response(s)?
102 // Does it fall within the set of valid status codes?
103 // Does it exactly match the expected status code?
104 if(logger.isDebugEnabled()){
105 logger.debug(testName + ": status = " + statusCode);
107 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
108 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
109 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
111 // Store the refname from the first resource created
112 // for additional tests below.
113 knownResourceRefName = refName;
115 // Store the ID returned from the first resource created
116 // for additional tests below.
117 if (knownResourceId == null){
118 knownResourceId = extractId(res);
119 if (logger.isDebugEnabled()) {
120 logger.debug(testName + ": knownResourceId=" + knownResourceId);
123 // Store the IDs from every resource created by tests,
124 // so they can be deleted after tests have been run.
125 allResourceIdsCreated.add(extractId(res));
129 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
130 dependsOnMethods = {"create"})
131 public void createItem(String testName) {
132 setupCreate(testName);
134 knownItemResourceId = createItemInVocab(knownResourceId);
135 if(logger.isDebugEnabled()){
136 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
140 private String createItemInVocab(String vcsid) {
142 final String testName = "createItemInVocab";
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ":...");
147 // Submit the request to the service and store the response.
148 String identifier = createIdentifier();
149 String refName = createRefName(identifier);
150 MultipartOutput multipart = createVocabularyItemInstance(vcsid, identifier, refName);
151 ClientResponse<Response> res = client.createItem(vcsid, multipart);
152 int statusCode = res.getStatus();
154 // Check the status code of the response: does it match
155 // the expected response(s)?
156 if(logger.isDebugEnabled()){
157 logger.debug(testName + ": status = " + statusCode);
159 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
160 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
161 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
163 // Store the ID returned from the first item resource created
164 // for additional tests below.
165 if (knownItemResourceId == null){
166 knownItemResourceId = extractId(res);
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
172 // Store the IDs from any item resources created
173 // by tests, along with the IDs of their parents, so these items
174 // can be deleted after all tests have been run.
176 // Item resource IDs are unique, so these are used as keys;
177 // the non-unique IDs of their parents are stored as associated values.
178 allResourceItemIdsCreated.put(extractId(res), vcsid);
180 return extractId(res);
184 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
185 dependsOnMethods = {"create", "createItem"})
186 public void createList(String testName) throws Exception {
187 for (int i = 0; i < 3; i++) {
189 // Add 3 items to each vocab
190 for (int j = 0; j < 3; j++) {
191 createItem(testName);
197 // Placeholders until the three tests below can be uncommented.
198 // See Issue CSPACE-401.
200 public void createWithEmptyEntityBody(String testName) throws Exception {
204 public void createWithMalformedXml(String testName) throws Exception {
208 public void createWithWrongXmlSchema(String testName) throws Exception {
213 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
214 dependsOnMethods = {"create", "testSubmitRequest"})
215 public void createWithEmptyEntityBody(String testName) throws Exception {
218 setupCreateWithEmptyEntityBody(testName);
220 // Submit the request to the service and store the response.
221 String method = REQUEST_TYPE.httpMethodName();
222 String url = getServiceRootURL();
223 String mediaType = MediaType.APPLICATION_XML;
224 final String entity = "";
225 int statusCode = submitRequest(method, url, mediaType, entity);
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 if(logger.isDebugEnabled()) {
230 logger.debug(testName + ": url=" + url +
231 " status=" + statusCode);
233 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
240 dependsOnMethods = {"create", "testSubmitRequest"})
241 public void createWithMalformedXml(String testName) throws Exception {
244 setupCreateWithMalformedXml(testName);
246 // Submit the request to the service and store the response.
247 String method = REQUEST_TYPE.httpMethodName();
248 String url = getServiceRootURL();
249 String mediaType = MediaType.APPLICATION_XML;
250 final String entity = MALFORMED_XML_DATA; // Constant from base class.
251 int statusCode = submitRequest(method, url, mediaType, entity);
253 // Check the status code of the response: does it match
254 // the expected response(s)?
255 if(logger.isDebugEnabled()){
256 logger.debug(testName + ": url=" + url +
257 " status=" + statusCode);
259 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
260 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
261 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
266 dependsOnMethods = {"create", "testSubmitRequest"})
267 public void createWithWrongXmlSchema(String testName) throws Exception {
270 setupCreateWithWrongXmlSchema(testName);
272 // Submit the request to the service and store the response.
273 String method = REQUEST_TYPE.httpMethodName();
274 String url = getServiceRootURL();
275 String mediaType = MediaType.APPLICATION_XML;
276 final String entity = WRONG_XML_SCHEMA_DATA;
277 int statusCode = submitRequest(method, url, mediaType, entity);
279 // Check the status code of the response: does it match
280 // the expected response(s)?
281 if(logger.isDebugEnabled()){
282 logger.debug(testName + ": url=" + url +
283 " status=" + statusCode);
285 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
286 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
287 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 // ---------------------------------------------------------------
292 // CRUD tests : READ tests
293 // ---------------------------------------------------------------
296 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
297 dependsOnMethods = {"create"})
298 public void read(String testName) throws Exception {
303 // Submit the request to the service and store the response.
304 ClientResponse<MultipartInput> res = client.read(knownResourceId);
305 int statusCode = res.getStatus();
307 // Check the status code of the response: does it match
308 // the expected response(s)?
309 if(logger.isDebugEnabled()){
310 logger.debug(testName + ": status = " + statusCode);
312 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
313 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
314 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
315 //FIXME: remove the following try catch once Aron fixes signatures
317 MultipartInput input = (MultipartInput) res.getEntity();
318 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
319 client.getCommonPartName(), VocabulariesCommon.class);
320 Assert.assertNotNull(vocabulary);
321 } catch (Exception e) {
322 throw new RuntimeException(e);
327 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
328 dependsOnMethods = {"read"})
329 public void readByName(String testName) throws Exception {
334 // Submit the request to the service and store the response.
335 ClientResponse<MultipartInput> res = client.read(knownResourceId);
336 int statusCode = res.getStatus();
338 // Check the status code of the response: does it match
339 // the expected response(s)?
340 if(logger.isDebugEnabled()){
341 logger.debug(testName + ": status = " + statusCode);
343 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
344 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
345 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
346 //FIXME: remove the following try catch once Aron fixes signatures
348 MultipartInput input = (MultipartInput) res.getEntity();
349 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
350 client.getCommonPartName(), VocabulariesCommon.class);
351 Assert.assertNotNull(vocabulary);
352 } catch (Exception e) {
353 throw new RuntimeException(e);
358 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
359 dependsOnMethods = {"createItem", "read"})
360 public void readItem(String testName) throws Exception {
365 // Submit the request to the service and store the response.
366 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
367 int statusCode = res.getStatus();
369 // Check the status code of the response: does it match
370 // the expected response(s)?
371 if(logger.isDebugEnabled()){
372 logger.debug(testName + ": status = " + statusCode);
374 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
375 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
376 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
378 // Check whether we've received a vocabulary item.
379 MultipartInput input = (MultipartInput) res.getEntity();
380 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
381 client.getItemCommonPartName(), VocabularyitemsCommon.class);
382 Assert.assertNotNull(vocabularyItem);
388 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
389 dependsOnMethods = {"read"})
390 public void readNonExistent(String testName) {
393 setupReadNonExistent(testName);
395 // Submit the request to the service and store the response.
396 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
397 int statusCode = res.getStatus();
399 // Check the status code of the response: does it match
400 // the expected response(s)?
401 if(logger.isDebugEnabled()){
402 logger.debug(testName + ": status = " + statusCode);
404 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
405 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
406 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
409 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
410 dependsOnMethods = {"readItem", "readNonExistent"})
411 public void readItemNonExistent(String testName) {
414 setupReadNonExistent(testName);
416 // Submit the request to the service and store the response.
417 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
418 int statusCode = res.getStatus();
420 // Check the status code of the response: does it match
421 // the expected response(s)?
422 if(logger.isDebugEnabled()){
423 logger.debug(testName + ": status = " + statusCode);
425 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
426 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
427 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
429 // ---------------------------------------------------------------
430 // CRUD tests : READ_LIST tests
431 // ---------------------------------------------------------------
435 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
436 dependsOnMethods = {"read"})
437 public void readList(String testName) throws Exception {
440 setupReadList(testName);
442 // Submit the request to the service and store the response.
443 ClientResponse<VocabulariesCommonList> res = client.readList();
444 VocabulariesCommonList list = res.getEntity();
445 int statusCode = res.getStatus();
447 // Check the status code of the response: does it match
448 // the expected response(s)?
449 if(logger.isDebugEnabled()){
450 logger.debug(testName + ": status = " + statusCode);
452 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
453 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
454 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
456 // Optionally output additional data about list members for debugging.
457 boolean iterateThroughList = false;
458 if (iterateThroughList && logger.isDebugEnabled()) {
459 List<VocabulariesCommonList.VocabularyListItem> items =
460 list.getVocabularyListItem();
462 for (VocabulariesCommonList.VocabularyListItem item : items) {
463 String csid = item.getCsid();
464 logger.debug(testName + ": list-item[" + i + "] csid=" +
466 logger.debug(testName + ": list-item[" + i + "] displayName=" +
467 item.getDisplayName());
468 logger.debug(testName + ": list-item[" + i + "] URI=" +
476 @Test(dependsOnMethods = {"readItem"})
477 public void readItemList() {
478 readItemList(knownResourceId);
481 private void readItemList(String vcsid) {
483 final String testName = "readItemList";
486 setupReadList(testName);
488 // Submit the request to the service and store the response.
489 ClientResponse<VocabularyitemsCommonList> res =
490 client.readItemList(vcsid);
491 VocabularyitemsCommonList list = res.getEntity();
492 int statusCode = res.getStatus();
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if(logger.isDebugEnabled()){
497 logger.debug(" " + testName + ": status = " + statusCode);
499 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
503 // Optionally output additional data about list members for debugging.
504 boolean iterateThroughList = false;
505 if (iterateThroughList && logger.isDebugEnabled()) {
506 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
507 list.getVocabularyitemListItem();
509 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
510 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
512 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
513 item.getDisplayName());
514 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
523 // ---------------------------------------------------------------
524 // CRUD tests : UPDATE tests
525 // ---------------------------------------------------------------
528 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
529 dependsOnMethods = {"read"})
530 public void update(String testName) throws Exception {
533 setupUpdate(testName);
535 // Retrieve the contents of a resource to update.
536 ClientResponse<MultipartInput> res =
537 client.read(knownResourceId);
538 if(logger.isDebugEnabled()){
539 logger.debug(testName + ": read status = " + res.getStatus());
541 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
543 if(logger.isDebugEnabled()){
544 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
546 MultipartInput input = (MultipartInput) res.getEntity();
547 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
548 client.getCommonPartName(), VocabulariesCommon.class);
549 Assert.assertNotNull(vocabulary);
551 // Update the contents of this resource.
552 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
553 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
554 if(logger.isDebugEnabled()){
555 logger.debug("to be updated Vocabulary");
556 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
559 // Submit the updated resource to the service and store the response.
560 MultipartOutput output = new MultipartOutput();
561 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
562 commonPart.getHeaders().add("label", client.getCommonPartName());
563 res = client.update(knownResourceId, output);
564 int statusCode = res.getStatus();
566 // Check the status code of the response: does it match the expected response(s)?
567 if(logger.isDebugEnabled()){
568 logger.debug("update: status = " + statusCode);
570 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
571 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
572 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
574 // Retrieve the updated resource and verify that its contents exist.
575 input = (MultipartInput) res.getEntity();
576 VocabulariesCommon updatedVocabulary =
577 (VocabulariesCommon) extractPart(input,
578 client.getCommonPartName(), VocabulariesCommon.class);
579 Assert.assertNotNull(updatedVocabulary);
581 // Verify that the updated resource received the correct data.
582 Assert.assertEquals(updatedVocabulary.getDisplayName(),
583 vocabulary.getDisplayName(),
584 "Data in updated object did not match submitted data.");
587 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
588 dependsOnMethods = {"readItem", "update"})
589 public void updateItem(String testName) throws Exception {
592 setupUpdate(testName);
594 ClientResponse<MultipartInput> res =
595 client.readItem(knownResourceId, knownItemResourceId);
596 if(logger.isDebugEnabled()){
597 logger.debug(testName + ": read status = " + res.getStatus());
599 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
601 if(logger.isDebugEnabled()){
602 logger.debug("got VocabularyItem to update with ID: " +
603 knownItemResourceId +
604 " in Vocab: " + knownResourceId );
606 MultipartInput input = (MultipartInput) res.getEntity();
607 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
608 client.getItemCommonPartName(), VocabularyitemsCommon.class);
609 Assert.assertNotNull(vocabularyItem);
611 // Update the contents of this resource.
612 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
613 if(logger.isDebugEnabled()){
614 logger.debug("to be updated VocabularyItem");
615 logger.debug(objectAsXmlString(vocabularyItem,
616 VocabularyitemsCommon.class));
619 // Submit the updated resource to the service and store the response.
620 MultipartOutput output = new MultipartOutput();
621 OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
622 commonPart.getHeaders().add("label", client.getItemCommonPartName());
623 res = client.updateItem(knownResourceId, knownItemResourceId, output);
624 int statusCode = res.getStatus();
626 // Check the status code of the response: does it match the expected response(s)?
627 if(logger.isDebugEnabled()){
628 logger.debug("updateItem: status = " + statusCode);
630 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
631 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
632 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
634 // Retrieve the updated resource and verify that its contents exist.
635 input = (MultipartInput) res.getEntity();
636 VocabularyitemsCommon updatedVocabularyItem =
637 (VocabularyitemsCommon) extractPart(input,
638 client.getItemCommonPartName(), VocabularyitemsCommon.class);
639 Assert.assertNotNull(updatedVocabularyItem);
641 // Verify that the updated resource received the correct data.
642 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
643 vocabularyItem.getDisplayName(),
644 "Data in updated VocabularyItem did not match submitted data.");
648 // Placeholders until the three tests below can be uncommented.
649 // See Issue CSPACE-401.
651 public void updateWithEmptyEntityBody(String testName) throws Exception {
655 public void updateWithMalformedXml(String testName) throws Exception {
659 public void updateWithWrongXmlSchema(String testName) throws Exception {
664 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
665 dependsOnMethods = {"create", "update", "testSubmitRequest"})
666 public void updateWithEmptyEntityBody(String testName) throws Exception {
669 setupUpdateWithEmptyEntityBody(testName);
671 // Submit the request to the service and store the response.
672 String method = REQUEST_TYPE.httpMethodName();
673 String url = getResourceURL(knownResourceId);
674 String mediaType = MediaType.APPLICATION_XML;
675 final String entity = "";
676 int statusCode = submitRequest(method, url, mediaType, entity);
678 // Check the status code of the response: does it match
679 // the expected response(s)?
680 if(logger.isDebugEnabled()){
681 logger.debug(testName + ": url=" + url +
682 " status=" + statusCode);
684 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
685 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
686 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
690 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
691 dependsOnMethods = {"create", "update", "testSubmitRequest"})
692 public void updateWithMalformedXml(String testName) throws Exception {
695 setupUpdateWithMalformedXml(testName);
697 // Submit the request to the service and store the response.
698 String method = REQUEST_TYPE.httpMethodName();
699 String url = getResourceURL(knownResourceId);
700 String mediaType = MediaType.APPLICATION_XML;
701 final String entity = MALFORMED_XML_DATA;
702 int statusCode = submitRequest(method, url, mediaType, entity);
704 // Check the status code of the response: does it match
705 // the expected response(s)?
706 if(logger.isDebugEnabled()){
707 logger.debug(testName + ": url=" + url +
708 " status=" + statusCode);
710 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
711 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
712 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
716 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
717 dependsOnMethods = {"create", "update", "testSubmitRequest"})
718 public void updateWithWrongXmlSchema(String testName) throws Exception {
721 setupUpdateWithWrongXmlSchema(testName);
723 // Submit the request to the service and store the response.
724 String method = REQUEST_TYPE.httpMethodName();
725 String url = getResourceURL(knownResourceId);
726 String mediaType = MediaType.APPLICATION_XML;
727 final String entity = WRONG_XML_SCHEMA_DATA;
728 int statusCode = submitRequest(method, url, mediaType, entity);
730 // Check the status code of the response: does it match
731 // the expected response(s)?
732 if(logger.isDebugEnabled()){
733 logger.debug("updateWithWrongXmlSchema: url=" + url +
734 " status=" + statusCode);
736 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
737 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
738 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
744 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
745 dependsOnMethods = {"update", "testSubmitRequest"})
746 public void updateNonExistent(String testName) throws Exception {
749 setupUpdateNonExistent(testName);
751 // Submit the request to the service and store the response.
752 // Note: The ID used in this 'create' call may be arbitrary.
753 // The only relevant ID may be the one used in update(), below.
755 // The only relevant ID may be the one used in update(), below.
756 MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
757 ClientResponse<MultipartInput> res =
758 client.update(NON_EXISTENT_ID, multipart);
759 int statusCode = res.getStatus();
761 // Check the status code of the response: does it match
762 // the expected response(s)?
763 if(logger.isDebugEnabled()){
764 logger.debug(testName + ": status = " + statusCode);
766 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
767 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
768 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
771 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
772 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
773 public void updateNonExistentItem(String testName) throws Exception {
776 setupUpdateNonExistent(testName);
778 // Submit the request to the service and store the response.
779 // Note: The ID used in this 'create' call may be arbitrary.
780 // The only relevant ID may be the one used in update(), below.
782 // The only relevant ID may be the one used in update(), below.
783 MultipartOutput multipart = createVocabularyItemInstance(
784 knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID));
785 ClientResponse<MultipartInput> res =
786 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
787 int statusCode = res.getStatus();
789 // Check the status code of the response: does it match
790 // the expected response(s)?
791 if(logger.isDebugEnabled()){
792 logger.debug(testName + ": status = " + statusCode);
794 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
795 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
796 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
799 // ---------------------------------------------------------------
800 // CRUD tests : DELETE tests
801 // ---------------------------------------------------------------
804 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
805 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
806 public void delete(String testName) throws Exception {
809 setupDelete(testName);
811 // Submit the request to the service and store the response.
812 ClientResponse<Response> res = client.delete(knownResourceId);
813 int statusCode = res.getStatus();
815 // Check the status code of the response: does it match
816 // the expected response(s)?
817 if(logger.isDebugEnabled()){
818 logger.debug(testName + ": status = " + statusCode);
820 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
821 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
822 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
825 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
826 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
828 public void deleteItem(String testName) throws Exception {
831 setupDelete(testName);
833 // Submit the request to the service and store the response.
834 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
835 int statusCode = res.getStatus();
837 // Check the status code of the response: does it match
838 // the expected response(s)?
839 if(logger.isDebugEnabled()){
840 logger.debug("delete: status = " + statusCode);
842 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
843 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
844 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
849 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
850 dependsOnMethods = {"delete"})
851 public void deleteNonExistent(String testName) throws Exception {
854 setupDeleteNonExistent(testName);
856 // Submit the request to the service and store the response.
857 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
858 int statusCode = res.getStatus();
860 // Check the status code of the response: does it match
861 // the expected response(s)?
862 if(logger.isDebugEnabled()){
863 logger.debug(testName + ": status = " + statusCode);
865 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
866 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
867 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
870 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
871 dependsOnMethods = {"deleteItem"})
872 public void deleteNonExistentItem(String testName) {
875 setupDeleteNonExistent(testName);
877 // Submit the request to the service and store the response.
878 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
879 int statusCode = res.getStatus();
881 // Check the status code of the response: does it match
882 // the expected response(s)?
883 if(logger.isDebugEnabled()){
884 logger.debug(testName + ": status = " + statusCode);
886 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
887 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
888 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
891 // ---------------------------------------------------------------
892 // Utility tests : tests of code used in tests above
893 // ---------------------------------------------------------------
895 * Tests the code for manually submitting data that is used by several
896 * of the methods above.
898 @Test(dependsOnMethods = {"create", "read"})
899 public void testSubmitRequest() {
901 // Expected status code: 200 OK
902 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
904 // Submit the request to the service and store the response.
905 String method = ServiceRequestType.READ.httpMethodName();
906 String url = getResourceURL(knownResourceId);
907 int statusCode = submitRequest(method, url);
909 // Check the status code of the response: does it match
910 // the expected response(s)?
911 if(logger.isDebugEnabled()){
912 logger.debug("testSubmitRequest: url=" + url +
913 " status=" + statusCode);
915 Assert.assertEquals(statusCode, EXPECTED_STATUS);
919 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
920 public void testItemSubmitRequest() {
922 // Expected status code: 200 OK
923 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
925 // Submit the request to the service and store the response.
926 String method = ServiceRequestType.READ.httpMethodName();
927 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
928 int statusCode = submitRequest(method, url);
930 // Check the status code of the response: does it match
931 // the expected response(s)?
932 if(logger.isDebugEnabled()){
933 logger.debug("testItemSubmitRequest: url=" + url +
934 " status=" + statusCode);
936 Assert.assertEquals(statusCode, EXPECTED_STATUS);
940 // ---------------------------------------------------------------
941 // Cleanup of resources created during testing
942 // ---------------------------------------------------------------
945 * Deletes all resources created by tests, after all tests have been run.
947 * This cleanup method will always be run, even if one or more tests fail.
948 * For this reason, it attempts to remove all resources created
949 * at any point during testing, even if some of those resources
950 * may be expected to be deleted by certain tests.
952 @AfterClass(alwaysRun=true)
953 public void cleanUp() {
955 if (logger.isDebugEnabled()) {
956 logger.debug("Cleaning up temporary resources created for testing ...");
958 // Clean up vocabulary item resources.
959 String vocabularyResourceId;
960 String vocabularyItemResourceId;
961 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
962 vocabularyItemResourceId = entry.getKey();
963 vocabularyResourceId = entry.getValue();
964 // Note: Any non-success responses are ignored and not reported.
965 ClientResponse<Response> res =
966 client.deleteItem(vocabularyResourceId, vocabularyItemResourceId);
968 // Clean up vocabulary resources.
969 for (String resourceId : allResourceIdsCreated) {
970 // Note: Any non-success responses are ignored and not reported.
971 ClientResponse<Response> res = client.delete(resourceId);
976 // ---------------------------------------------------------------
977 // Utility methods used by tests above
978 // ---------------------------------------------------------------
980 public String getServicePathComponent() {
981 return SERVICE_PATH_COMPONENT;
984 public String getItemServicePathComponent() {
985 return ITEM_SERVICE_PATH_COMPONENT;
989 * Returns the root URL for a service.
991 * This URL consists of a base URL for all services, followed by
992 * a path component for the owning vocabulary, followed by the
993 * path component for the items.
995 * @return The root URL for a service.
997 protected String getItemServiceRootURL(String parentResourceIdentifier) {
998 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1002 * Returns the URL of a specific resource managed by a service, and
1003 * designated by an identifier (such as a universally unique ID, or UUID).
1005 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1007 * @return The URL of a specific resource managed by a service.
1009 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1010 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1013 private MultipartOutput createVocabularyInstance(String identifier) {
1014 String displayName = "displayName-" + identifier;
1015 String refName = createRefName(displayName);
1016 String typeName = "vocabType-" + identifier;
1017 return createVocabularyInstance(
1018 displayName, refName,typeName );
1021 private MultipartOutput createVocabularyInstance(
1022 String displayName, String refName, String vocabType) {
1023 VocabulariesCommon vocabulary = new VocabulariesCommon();
1024 vocabulary.setDisplayName(displayName);
1026 vocabulary.setRefName(refName);
1027 vocabulary.setVocabType(vocabType);
1028 MultipartOutput multipart = new MultipartOutput();
1029 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
1030 commonPart.getHeaders().add("label", client.getCommonPartName());
1032 if(logger.isDebugEnabled()) {
1033 logger.debug("to be created, vocabulary common");
1034 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
1039 private MultipartOutput createVocabularyItemInstance(String inVocabulary,
1040 String displayName, String refName) {
1041 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
1042 vocabularyItem.setDisplayName(displayName);
1044 vocabularyItem.setRefName(refName);
1045 MultipartOutput multipart = new MultipartOutput();
1046 OutputPart commonPart = multipart.addPart(vocabularyItem,
1047 MediaType.APPLICATION_XML_TYPE);
1048 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1050 if(logger.isDebugEnabled()){
1051 logger.debug("to be created, vocabularyitem common");
1052 logger.debug(objectAsXmlString(vocabularyItem,
1053 VocabularyitemsCommon.class));