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.common.vocabulary.AuthorityItemJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.VocabularyClient;
35 import org.collectionspace.services.client.VocabularyClientUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.vocabulary.VocabulariesCommon;
38 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
39 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
40 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
53 * VocabularyServiceTest, carries out tests against a
54 * deployed and running Vocabulary Service.
56 * $LastChangedRevision: 753 $
57 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
59 public class VocabularyServiceTest extends AbstractServiceTestImpl {
61 private final String CLASS_NAME = VocabularyServiceTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 // Instance variables specific to this test.
65 final String SERVICE_PATH_COMPONENT = "vocabularies";
66 final String ITEM_SERVICE_PATH_COMPONENT = "items";
67 private String knownResourceId = null;
68 private String knownResourceShortIdentifer = null;
69 private String knownResourceRefName = null;
70 private String knownResourceFullRefName = null;
71 private String knownItemResourceId = null;
72 private int nItemsToCreateInList = 5;
73 private List<String> allResourceIdsCreated = new ArrayList<String>();
74 private Map<String, String> allResourceItemIdsCreated =
75 new HashMap<String, String>();
77 protected void setKnownResource( String id, String shortIdentifer,
78 String refName, String fullRefName ) {
80 knownResourceShortIdentifer = shortIdentifer;
81 knownResourceRefName = refName;
82 knownResourceFullRefName = fullRefName;
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
89 protected CollectionSpaceClient getClientInstance() {
90 return new VocabularyClient();
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
97 protected AbstractCommonList getAbstractCommonList(
98 ClientResponse<AbstractCommonList> response) {
99 return response.getEntity(VocabulariesCommonList.class);
102 // ---------------------------------------------------------------
103 // CRUD tests : CREATE tests
104 // ---------------------------------------------------------------
107 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
108 public void create(String testName) throws Exception {
110 if (logger.isDebugEnabled()) {
111 logger.debug(testBanner(testName, CLASS_NAME));
113 // Perform setup, such as initializing the type of service request
114 // (e.g. CREATE, DELETE), its valid and expected status codes, and
115 // its associated HTTP method name (e.g. POST, DELETE).
118 // Submit the request to the service and store the response.
119 VocabularyClient client = new VocabularyClient();
120 String identifier = createIdentifier();
121 String displayName = "displayName-" + identifier;
122 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
123 displayName, identifier, client.getCommonPartName());
124 ClientResponse<Response> res = client.create(multipart);
125 int statusCode = res.getStatus();
127 // Check the status code of the response: does it match
128 // the expected response(s)?
131 // Does it fall within the set of valid status codes?
132 // Does it exactly match the expected status code?
133 if(logger.isDebugEnabled()){
134 logger.debug(testName + ": status = " + statusCode);
136 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
137 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
138 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
140 // Store the ID returned from the first resource created
141 // for additional tests below.
142 if (knownResourceId == null){
143 setKnownResource(extractId(res), identifier,
144 VocabularyClientUtils.createVocabularyRefName(identifier, null),
145 VocabularyClientUtils.createVocabularyRefName(identifier, displayName));
146 if (logger.isDebugEnabled()) {
147 logger.debug(testName + ": knownResourceId=" + knownResourceId);
150 // Store the IDs from every resource created by tests,
151 // so they can be deleted after tests have been run.
152 allResourceIdsCreated.add(extractId(res));
156 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
157 dependsOnMethods = {"create"})
158 public void createItem(String testName) {
160 if (null!=testName && logger.isDebugEnabled()) {
161 logger.debug(testBanner(testName, CLASS_NAME));
166 VocabularyClient client = new VocabularyClient();
167 HashMap<String, String> itemInfo = new HashMap<String, String>();
168 String shortId = createIdentifier();
169 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
170 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-"+shortId);
171 String newID = VocabularyClientUtils.createItemInVocabulary(knownResourceId,
172 knownResourceRefName, itemInfo, client);
174 // Store the ID returned from the first item resource created
175 // for additional tests below.
176 if (knownItemResourceId == null){
177 knownItemResourceId = newID;
178 if (null!=testName && logger.isDebugEnabled()) {
179 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
182 // Store the IDs from any item resources created
183 // by tests, along with the IDs of their parents, so these items
184 // can be deleted after all tests have been run.
185 allResourceItemIdsCreated.put(newID, knownResourceId);
189 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
190 dependsOnMethods = {"create", "createItem", "readItem"})
191 public void createList(String testName) throws Exception {
192 for (int i = 0; i < 3; i++) {
193 // Force create to reset the known resource info
194 setKnownResource(null, null, null, null);
195 knownItemResourceId = null;
197 // Add nItemsToCreateInList items to each vocab
198 for (int j = 0; j < nItemsToCreateInList; j++) {
205 // Placeholders until the three tests below can be uncommented.
206 // See Issue CSPACE-401.
208 public void createWithEmptyEntityBody(String testName) throws Exception {
212 public void createWithMalformedXml(String testName) throws Exception {
216 public void createWithWrongXmlSchema(String testName) throws Exception {
219 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
220 dependsOnMethods = {"create"})
221 public void createWithBadShortId(String testName) throws Exception {
223 if (logger.isDebugEnabled()) {
224 logger.debug(testBanner(testName, CLASS_NAME));
226 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
228 // Submit the request to the service and store the response.
229 VocabularyClient client = new VocabularyClient();
230 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
231 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
232 ClientResponse<Response> res = client.create(multipart);
233 int statusCode = res.getStatus();
235 // Check the status code of the response: does it match
236 // the expected response(s)?
239 // Does it fall within the set of valid status codes?
240 // Does it exactly match the expected status code?
241 if(logger.isDebugEnabled()){
242 logger.debug(testName + ": status = " + statusCode);
244 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
245 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
246 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
250 dependsOnMethods = {"createItem"})
251 public void createItemWithBadShortId(String testName) throws Exception {
253 if (logger.isDebugEnabled()) {
254 logger.debug(testBanner(testName, CLASS_NAME));
256 setupCreateWithMalformedXml();
258 // Submit the request to the service and store the response.
259 VocabularyClient client = new VocabularyClient();
260 HashMap<String, String> itemInfo = new HashMap<String, String>();
261 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
262 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
263 MultipartOutput multipart =
264 VocabularyClientUtils.createVocabularyItemInstance( knownResourceRefName,
265 itemInfo, client.getItemCommonPartName() );
266 ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
268 int statusCode = res.getStatus();
270 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
271 throw new RuntimeException("Could not create Item: \""+itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
272 +"\" in personAuthority: \"" + knownResourceRefName
273 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
275 if(statusCode != EXPECTED_STATUS_CODE) {
276 throw new RuntimeException("Unexpected Status when creating Item: \""+itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
277 +"\" in personAuthority: \"" + knownResourceRefName +"\", Status:"+ statusCode);
283 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
284 dependsOnMethods = {"create", "testSubmitRequest"})
285 public void createWithEmptyEntityBody(String testName) throws Exception {
287 if (logger.isDebugEnabled()) {
288 logger.debug(testBanner(testName, CLASS_NAME));
291 setupCreateWithEmptyEntityBody(testName, CLASS_NAME);
293 // Submit the request to the service and store the response.
294 String method = REQUEST_TYPE.httpMethodName();
295 String url = getServiceRootURL();
296 String mediaType = MediaType.APPLICATION_XML;
297 final String entity = "";
298 int statusCode = submitRequest(method, url, mediaType, entity);
300 // Check the status code of the response: does it match
301 // the expected response(s)?
302 if(logger.isDebugEnabled()) {
303 logger.debug(testName + ": url=" + url +
304 " status=" + statusCode);
306 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
307 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
308 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
312 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
313 dependsOnMethods = {"create", "testSubmitRequest"})
314 public void createWithMalformedXml(String testName) throws Exception {
316 if (logger.isDebugEnabled()) {
317 logger.debug(testBanner(testName, CLASS_NAME));
320 setupCreateWithMalformedXml();
322 // Submit the request to the service and store the response.
323 String method = REQUEST_TYPE.httpMethodName();
324 String url = getServiceRootURL();
325 String mediaType = MediaType.APPLICATION_XML;
326 final String entity = MALFORMED_XML_DATA; // Constant from base class.
327 int statusCode = submitRequest(method, url, mediaType, entity);
329 // Check the status code of the response: does it match
330 // the expected response(s)?
331 if(logger.isDebugEnabled()){
332 logger.debug(testName + ": url=" + url +
333 " status=" + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
341 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
342 dependsOnMethods = {"create", "testSubmitRequest"})
343 public void createWithWrongXmlSchema(String testName) throws Exception {
345 if (logger.isDebugEnabled()) {
346 logger.debug(testBanner(testName, CLASS_NAME));
349 setupCreateWithWrongXmlSchema();
351 // Submit the request to the service and store the response.
352 String method = REQUEST_TYPE.httpMethodName();
353 String url = getServiceRootURL();
354 String mediaType = MediaType.APPLICATION_XML;
355 final String entity = WRONG_XML_SCHEMA_DATA;
356 int statusCode = submitRequest(method, url, mediaType, entity);
358 // Check the status code of the response: does it match
359 // the expected response(s)?
360 if(logger.isDebugEnabled()){
361 logger.debug(testName + ": url=" + url +
362 " status=" + statusCode);
364 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
365 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
366 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 // ---------------------------------------------------------------
371 // CRUD tests : READ tests
372 // ---------------------------------------------------------------
375 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
376 dependsOnMethods = {"create"})
377 public void read(String testName) throws Exception {
379 if (logger.isDebugEnabled()) {
380 logger.debug(testBanner(testName, CLASS_NAME));
385 // Submit the request to the service and store the response.
386 VocabularyClient client = new VocabularyClient();
387 ClientResponse<MultipartInput> res = client.read(knownResourceId);
388 int statusCode = res.getStatus();
390 // Check the status code of the response: does it match
391 // the expected response(s)?
392 if(logger.isDebugEnabled()){
393 logger.debug(testName + ": status = " + statusCode);
395 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
396 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
397 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
398 //FIXME: remove the following try catch once Aron fixes signatures
400 MultipartInput input = (MultipartInput) res.getEntity();
401 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
402 client.getCommonPartName(), VocabulariesCommon.class);
403 Assert.assertNotNull(vocabulary);
404 Assert.assertEquals(vocabulary.getRefName(), knownResourceFullRefName);
405 } catch (Exception e) {
406 throw new RuntimeException(e);
413 * @param testName the test name
414 * @throws Exception the exception
416 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
417 dependsOnMethods = {"read"})
418 public void readByName(String testName) throws Exception {
420 if (logger.isDebugEnabled()) {
421 logger.debug(testBanner(testName, CLASS_NAME));
426 // Submit the request to the service and store the response.
427 VocabularyClient client = new VocabularyClient();
428 ClientResponse<MultipartInput> res = client.readByName(knownResourceShortIdentifer);
430 int statusCode = res.getStatus();
432 // Check the status code of the response: does it match
433 // the expected response(s)?
434 if(logger.isDebugEnabled()){
435 logger.debug(testName + ": status = " + statusCode);
437 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
438 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
439 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
440 //FIXME: remove the following try catch once Aron fixes signatures
442 MultipartInput input = (MultipartInput) res.getEntity();
443 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
444 client.getCommonPartName(), VocabulariesCommon.class);
445 Assert.assertNotNull(vocabulary);
446 } catch (Exception e) {
447 throw new RuntimeException(e);
450 res.releaseConnection();
455 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
456 dependsOnMethods = {"read"})
457 public void readByName(String testName) throws Exception {
459 if (logger.isDebugEnabled()) {
460 logger.debug(testBanner(testName, CLASS_NAME));
465 // Submit the request to the service and store the response.
466 ClientResponse<MultipartInput> res = client.read(knownResourceId);
467 int statusCode = res.getStatus();
469 // Check the status code of the response: does it match
470 // the expected response(s)?
471 if(logger.isDebugEnabled()){
472 logger.debug(testName + ": status = " + statusCode);
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477 //FIXME: remove the following try catch once Aron fixes signatures
479 MultipartInput input = (MultipartInput) res.getEntity();
480 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
481 client.getCommonPartName(), VocabulariesCommon.class);
482 Assert.assertNotNull(vocabulary);
483 } catch (Exception e) {
484 throw new RuntimeException(e);
489 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
490 dependsOnMethods = {"createItem", "read"})
491 public void readItem(String testName) throws Exception {
493 if (logger.isDebugEnabled()) {
494 logger.debug(testBanner(testName, CLASS_NAME));
499 // Submit the request to the service and store the response.
500 VocabularyClient client = new VocabularyClient();
501 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
502 int statusCode = res.getStatus();
504 // Check the status code of the response: does it match
505 // the expected response(s)?
506 if(logger.isDebugEnabled()){
507 logger.debug(testName + ": status = " + statusCode);
509 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
510 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
511 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
513 // Check whether we've received a vocabulary item.
514 MultipartInput input = (MultipartInput) res.getEntity();
515 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
516 client.getItemCommonPartName(), VocabularyitemsCommon.class);
517 Assert.assertNotNull(vocabularyItem);
518 Assert.assertEquals(vocabularyItem.getInAuthority(), knownResourceId);
522 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
523 dependsOnMethods = {"updateItem"})
524 public void verifyIllegalItemDisplayName(String testName) throws Exception {
526 if (logger.isDebugEnabled()) {
527 logger.debug(testBanner(testName, CLASS_NAME));
530 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
531 // setupUpdateWithWrongXmlSchema(testName);
533 // Submit the request to the service and store the response.
534 VocabularyClient client = new VocabularyClient();
535 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
536 int statusCode = res.getStatus();
538 // Check the status code of the response: does it match
539 // the expected response(s)?
540 if(logger.isDebugEnabled()){
541 logger.debug(testName + ": status = " + statusCode);
543 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
544 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
545 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
547 // Check whether Person has expected displayName.
548 MultipartInput input = (MultipartInput) res.getEntity();
549 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
550 client.getItemCommonPartName(), VocabularyitemsCommon.class);
551 Assert.assertNotNull(vitem);
552 // Try to Update with null displayName
553 vitem.setDisplayName(null);
555 // Submit the updated resource to the service and store the response.
556 MultipartOutput output = new MultipartOutput();
557 OutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
558 commonPart.getHeaders().add("label", client.getItemCommonPartName());
559 res = client.updateItem(knownResourceId, knownItemResourceId, output);
560 statusCode = res.getStatus();
562 // Check the status code of the response: does it match the expected response(s)?
563 if(logger.isDebugEnabled()){
564 logger.debug("updateItem: status = " + statusCode);
566 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
567 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
568 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
569 "Expecting invalid message because of null displayName.");
571 // Now try to Update with 1-char displayName (too short)
572 vitem.setDisplayName("a");
574 // Submit the updated resource to the service and store the response.
575 output = new MultipartOutput();
576 commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
577 commonPart.getHeaders().add("label", client.getItemCommonPartName());
578 res = client.updateItem(knownResourceId, knownItemResourceId, output);
579 statusCode = res.getStatus();
581 // Check the status code of the response: does it match the expected response(s)?
582 if(logger.isDebugEnabled()){
583 logger.debug("updateItem: status = " + statusCode);
585 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
586 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
587 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
588 "Expecting invalid message because of 1-char displayName.");
592 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
593 dependsOnMethods = {"read"})
594 public void readNonExistent(String testName) {
596 if (logger.isDebugEnabled()) {
597 logger.debug(testBanner(testName, CLASS_NAME));
600 setupReadNonExistent();
602 // Submit the request to the service and store the response.
603 VocabularyClient client = new VocabularyClient();
604 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
605 int statusCode = res.getStatus();
607 // Check the status code of the response: does it match
608 // the expected response(s)?
609 if(logger.isDebugEnabled()){
610 logger.debug(testName + ": status = " + statusCode);
612 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
613 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
614 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
617 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
618 dependsOnMethods = {"readItem", "readNonExistent"})
619 public void readItemNonExistent(String testName) {
621 if (logger.isDebugEnabled()) {
622 logger.debug(testBanner(testName, CLASS_NAME));
625 setupReadNonExistent();
627 // Submit the request to the service and store the response.
628 VocabularyClient client = new VocabularyClient();
629 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
630 int statusCode = res.getStatus();
632 // Check the status code of the response: does it match
633 // the expected response(s)?
634 if(logger.isDebugEnabled()){
635 logger.debug(testName + ": status = " + statusCode);
637 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
638 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
639 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
641 // ---------------------------------------------------------------
642 // CRUD tests : READ_LIST tests
643 // ---------------------------------------------------------------
647 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
648 dependsOnMethods = {"createList", "read"})
649 public void readList(String testName) throws Exception {
651 if (logger.isDebugEnabled()) {
652 logger.debug(testBanner(testName, CLASS_NAME));
657 // Submit the request to the service and store the response.
658 VocabularyClient client = new VocabularyClient();
659 ClientResponse<VocabulariesCommonList> res = client.readList();
660 VocabulariesCommonList list = res.getEntity();
661 int statusCode = res.getStatus();
663 // Check the status code of the response: does it match
664 // the expected response(s)?
665 if(logger.isDebugEnabled()){
666 logger.debug(testName + ": status = " + statusCode);
668 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
669 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
670 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
672 // Optionally output additional data about list members for debugging.
673 boolean iterateThroughList = false;
674 if (iterateThroughList && logger.isDebugEnabled()) {
675 List<VocabulariesCommonList.VocabularyListItem> items =
676 list.getVocabularyListItem();
678 for (VocabulariesCommonList.VocabularyListItem item : items) {
679 String csid = item.getCsid();
680 logger.debug(testName + ": list-item[" + i + "] csid=" +
682 logger.debug(testName + ": list-item[" + i + "] displayName=" +
683 item.getDisplayName());
684 logger.debug(testName + ": list-item[" + i + "] URI=" +
686 readItemListInt(csid, null, "readList");
692 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
693 dependsOnMethods = {"createList", "readItem"})
694 public void readItemList(String testName) {
695 readItemListInt(knownResourceId, null, testName);
698 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
699 dependsOnMethods = {"createList", "readItem"})
700 public void readItemListByName(String testName) {
701 readItemListInt(null, knownResourceShortIdentifer, testName);
704 private void readItemListInt(String vcsid, String shortId, String testName) {
709 // Submit the request to the service and store the response.
710 VocabularyClient client = new VocabularyClient();
711 ClientResponse<VocabularyitemsCommonList> res = null;
713 res = client.readItemList(vcsid, null, null);
714 } else if(shortId!=null) {
715 res = client.readItemListForNamedVocabulary(shortId, null, null);
717 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
719 VocabularyitemsCommonList list = res.getEntity();
720 int statusCode = res.getStatus();
722 // Check the status code of the response: does it match
723 // the expected response(s)?
724 if(logger.isDebugEnabled()){
725 logger.debug(" " + testName + ": status = " + statusCode);
727 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
728 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
729 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
731 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
732 list.getVocabularyitemListItem();
733 int nItemsReturned = items.size();
734 long nItemsTotal = list.getTotalItems();
735 if(logger.isDebugEnabled()){
736 logger.debug(" " + testName + ": Expected "
737 + nItemsToCreateInList+" items; got: "+nItemsReturned+" of: "+nItemsTotal);
739 Assert.assertEquals( nItemsTotal, nItemsToCreateInList);
741 // Optionally output additional data about list members for debugging.
742 boolean iterateThroughList = true;
743 if (iterateThroughList && logger.isDebugEnabled()) {
744 logger.debug(" " + testName + ": checking items");
746 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
747 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
749 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
750 item.getDisplayName());
751 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
760 // ---------------------------------------------------------------
761 // CRUD tests : UPDATE tests
762 // ---------------------------------------------------------------
765 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
766 dependsOnMethods = {"read"})
767 public void update(String testName) throws Exception {
769 if (logger.isDebugEnabled()) {
770 logger.debug(testBanner(testName, CLASS_NAME));
775 // Retrieve the contents of a resource to update.
776 VocabularyClient client = new VocabularyClient();
777 ClientResponse<MultipartInput> res =
778 client.read(knownResourceId);
779 if(logger.isDebugEnabled()){
780 logger.debug(testName + ": read status = " + res.getStatus());
782 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
784 if(logger.isDebugEnabled()){
785 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
787 MultipartInput input = (MultipartInput) res.getEntity();
788 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
789 client.getCommonPartName(), VocabulariesCommon.class);
790 Assert.assertNotNull(vocabulary);
792 // Update the contents of this resource.
793 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
794 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
795 if(logger.isDebugEnabled()){
796 logger.debug("to be updated Vocabulary");
797 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
800 // Submit the updated resource to the service and store the response.
801 MultipartOutput output = new MultipartOutput();
802 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
803 commonPart.getHeaders().add("label", client.getCommonPartName());
804 res = client.update(knownResourceId, output);
805 int statusCode = res.getStatus();
807 // Check the status code of the response: does it match the expected response(s)?
808 if(logger.isDebugEnabled()){
809 logger.debug("update: status = " + statusCode);
811 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
812 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
813 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
815 // Retrieve the updated resource and verify that its contents exist.
816 input = (MultipartInput) res.getEntity();
817 VocabulariesCommon updatedVocabulary =
818 (VocabulariesCommon) extractPart(input,
819 client.getCommonPartName(), VocabulariesCommon.class);
820 Assert.assertNotNull(updatedVocabulary);
822 // Verify that the updated resource received the correct data.
823 Assert.assertEquals(updatedVocabulary.getDisplayName(),
824 vocabulary.getDisplayName(),
825 "Data in updated object did not match submitted data.");
828 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
829 dependsOnMethods = {"readItem", "update", "verifyIgnoredUpdateWithInAuthority"})
830 public void updateItem(String testName) throws Exception {
832 if (logger.isDebugEnabled()) {
833 logger.debug(testBanner(testName, CLASS_NAME));
838 // Retrieve the contents of a resource to update.
839 VocabularyClient client = new VocabularyClient();
840 ClientResponse<MultipartInput> res =
841 client.readItem(knownResourceId, knownItemResourceId);
842 if(logger.isDebugEnabled()){
843 logger.debug(testName + ": read status = " + res.getStatus());
845 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
847 if(logger.isDebugEnabled()){
848 logger.debug("got VocabularyItem to update with ID: " +
849 knownItemResourceId +
850 " in Vocab: " + knownResourceId );
852 MultipartInput input = (MultipartInput) res.getEntity();
853 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
854 client.getItemCommonPartName(), VocabularyitemsCommon.class);
855 Assert.assertNotNull(vocabularyItem);
857 // Update the contents of this resource.
858 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
859 if(logger.isDebugEnabled()){
860 logger.debug("to be updated VocabularyItem");
861 logger.debug(objectAsXmlString(vocabularyItem,
862 VocabularyitemsCommon.class));
865 // Submit the updated resource to the service and store the response.
866 MultipartOutput output = new MultipartOutput();
867 OutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
868 commonPart.getHeaders().add("label", client.getItemCommonPartName());
869 res = client.updateItem(knownResourceId, knownItemResourceId, output);
870 int statusCode = res.getStatus();
872 // Check the status code of the response: does it match the expected response(s)?
873 if(logger.isDebugEnabled()){
874 logger.debug("updateItem: status = " + statusCode);
876 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
877 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
878 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
880 // Retrieve the updated resource and verify that its contents exist.
881 input = (MultipartInput) res.getEntity();
882 VocabularyitemsCommon updatedVocabularyItem =
883 (VocabularyitemsCommon) extractPart(input,
884 client.getItemCommonPartName(), VocabularyitemsCommon.class);
885 Assert.assertNotNull(updatedVocabularyItem);
887 // Verify that the updated resource received the correct data.
888 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
889 vocabularyItem.getDisplayName(),
890 "Data in updated VocabularyItem did not match submitted data.");
893 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
894 dependsOnMethods = {"readItem"})
895 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
897 if (logger.isDebugEnabled()) {
898 logger.debug(testBanner(testName, CLASS_NAME));
903 // Submit the request to the service and store the response.
904 VocabularyClient client = new VocabularyClient();
905 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
906 int statusCode = res.getStatus();
908 // Check the status code of the response: does it match
909 // the expected response(s)?
910 if(logger.isDebugEnabled()){
911 logger.debug(testName + " read Vocab:"+knownResourceId+"/Item:"
912 +knownItemResourceId+" status = " + statusCode);
914 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
915 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
916 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
918 MultipartInput input = (MultipartInput) res.getEntity();
919 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
920 client.getItemCommonPartName(), VocabularyitemsCommon.class);
921 Assert.assertNotNull(vitem);
922 // Try to Update with new parent vocab (use self, for test).
923 Assert.assertEquals(vitem.getInAuthority(),
925 "VocabularyItem inAuthority does not match knownResourceId.");
926 vitem.setInAuthority(knownItemResourceId);
928 // Submit the updated resource to the service and store the response.
929 MultipartOutput output = new MultipartOutput();
930 OutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
931 commonPart.getHeaders().add("label", client.getItemCommonPartName());
932 res = client.updateItem(knownResourceId, knownItemResourceId, output);
933 statusCode = res.getStatus();
935 // Check the status code of the response: does it match the expected response(s)?
936 if(logger.isDebugEnabled()){
937 logger.debug(testName + ": status = " + statusCode);
939 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
940 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
941 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
943 // Retrieve the updated resource and verify that the parent did not change
944 res = client.readItem(knownResourceId, knownItemResourceId);
945 input = (MultipartInput) res.getEntity();
946 VocabularyitemsCommon updatedVocabularyItem =
947 (VocabularyitemsCommon) extractPart(input,
948 client.getItemCommonPartName(), VocabularyitemsCommon.class);
949 Assert.assertNotNull(updatedVocabularyItem);
951 // Verify that the updated resource received the correct data.
952 Assert.assertEquals(updatedVocabularyItem.getInAuthority(),
954 "VocabularyItem allowed update to the parent (inAuthority).");
958 // Placeholders until the three tests below can be uncommented.
959 // See Issue CSPACE-401.
961 public void updateWithEmptyEntityBody(String testName) throws Exception {
965 public void updateWithMalformedXml(String testName) throws Exception {
969 public void updateWithWrongXmlSchema(String testName) throws Exception {
974 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
975 dependsOnMethods = {"create", "update", "testSubmitRequest"})
976 public void updateWithEmptyEntityBody(String testName) throws Exception {
978 if (logger.isDebugEnabled()) {
979 logger.debug(testBanner(testName, CLASS_NAME));
982 setupUpdateWithEmptyEntityBody();
984 // Submit the request to the service and store the response.
985 String method = REQUEST_TYPE.httpMethodName();
986 String url = getResourceURL(knownResourceId);
987 String mediaType = MediaType.APPLICATION_XML;
988 final String entity = "";
989 int statusCode = submitRequest(method, url, mediaType, entity);
991 // Check the status code of the response: does it match
992 // the expected response(s)?
993 if(logger.isDebugEnabled()){
994 logger.debug(testName + ": url=" + url +
995 " status=" + statusCode);
997 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
998 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
999 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1003 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1004 dependsOnMethods = {"create", "update", "testSubmitRequest"})
1005 public void updateWithMalformedXml(String testName) throws Exception {
1007 if (logger.isDebugEnabled()) {
1008 logger.debug(testBanner(testName, CLASS_NAME));
1011 setupUpdateWithMalformedXml();
1013 // Submit the request to the service and store the response.
1014 String method = REQUEST_TYPE.httpMethodName();
1015 String url = getResourceURL(knownResourceId);
1016 String mediaType = MediaType.APPLICATION_XML;
1017 final String entity = MALFORMED_XML_DATA;
1018 int statusCode = submitRequest(method, url, mediaType, entity);
1020 // Check the status code of the response: does it match
1021 // the expected response(s)?
1022 if(logger.isDebugEnabled()){
1023 logger.debug(testName + ": url=" + url +
1024 " status=" + statusCode);
1026 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1027 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1028 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1032 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1033 dependsOnMethods = {"create", "update", "testSubmitRequest"})
1034 public void updateWithWrongXmlSchema(String testName) throws Exception {
1036 if (logger.isDebugEnabled()) {
1037 logger.debug(testBanner(testName, CLASS_NAME));
1040 setupUpdateWithWrongXmlSchema();
1042 // Submit the request to the service and store the response.
1043 String method = REQUEST_TYPE.httpMethodName();
1044 String url = getResourceURL(knownResourceId);
1045 String mediaType = MediaType.APPLICATION_XML;
1046 final String entity = WRONG_XML_SCHEMA_DATA;
1047 int statusCode = submitRequest(method, url, mediaType, entity);
1049 // Check the status code of the response: does it match
1050 // the expected response(s)?
1051 if(logger.isDebugEnabled()){
1052 logger.debug("updateWithWrongXmlSchema: url=" + url +
1053 " status=" + statusCode);
1055 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1056 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1057 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1063 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1064 dependsOnMethods = {"update", "testSubmitRequest"})
1065 public void updateNonExistent(String testName) throws Exception {
1067 if (logger.isDebugEnabled()) {
1068 logger.debug(testBanner(testName, CLASS_NAME));
1071 setupUpdateNonExistent();
1073 // Submit the request to the service and store the response.
1074 // Note: The ID used in this 'create' call may be arbitrary.
1075 // The only relevant ID may be the one used in update(), below.
1076 VocabularyClient client = new VocabularyClient();
1077 String displayName = "displayName-" + NON_EXISTENT_ID;
1078 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
1079 displayName, NON_EXISTENT_ID, client.getCommonPartName());
1080 ClientResponse<MultipartInput> res =
1081 client.update(NON_EXISTENT_ID, multipart);
1082 int statusCode = res.getStatus();
1084 // Check the status code of the response: does it match
1085 // the expected response(s)?
1086 if(logger.isDebugEnabled()){
1087 logger.debug(testName + ": status = " + statusCode);
1089 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1090 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1091 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1094 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1095 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1096 public void updateNonExistentItem(String testName) throws Exception {
1098 if (logger.isDebugEnabled()) {
1099 logger.debug(testBanner(testName, CLASS_NAME));
1102 setupUpdateNonExistent();
1104 // Submit the request to the service and store the response.
1105 // Note: The ID used in this 'create' call may be arbitrary.
1106 // The only relevant ID may be the one used in update(), below.
1107 VocabularyClient client = new VocabularyClient();
1108 HashMap<String, String> itemInfo = new HashMap<String, String>();
1109 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
1110 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
1111 MultipartOutput multipart =
1112 VocabularyClientUtils.createVocabularyItemInstance(
1113 VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
1114 itemInfo, client.getItemCommonPartName());
1115 ClientResponse<MultipartInput> res =
1116 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1117 int statusCode = res.getStatus();
1119 // Check the status code of the response: does it match
1120 // the expected response(s)?
1121 if(logger.isDebugEnabled()){
1122 logger.debug(testName + ": status = " + statusCode);
1124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1129 // ---------------------------------------------------------------
1130 // CRUD tests : DELETE tests
1131 // ---------------------------------------------------------------
1134 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1135 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "deleteItem"})
1136 public void delete(String testName) throws Exception {
1138 if (logger.isDebugEnabled()) {
1139 logger.debug(testBanner(testName, CLASS_NAME));
1144 // Submit the request to the service and store the response.
1145 VocabularyClient client = new VocabularyClient();
1146 ClientResponse<Response> res = client.delete(knownResourceId);
1147 int statusCode = res.getStatus();
1149 // Check the status code of the response: does it match
1150 // the expected response(s)?
1151 if(logger.isDebugEnabled()){
1152 logger.debug(testName + ": status = " + statusCode);
1154 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1155 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1156 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1159 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1160 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
1161 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
1162 public void deleteItem(String testName) throws Exception {
1164 if (logger.isDebugEnabled()) {
1165 logger.debug(testBanner(testName, CLASS_NAME));
1170 // Submit the request to the service and store the response.
1171 VocabularyClient client = new VocabularyClient();
1172 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1173 int statusCode = res.getStatus();
1175 // Check the status code of the response: does it match
1176 // the expected response(s)?
1177 if(logger.isDebugEnabled()){
1178 logger.debug("delete: status = " + statusCode);
1180 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1181 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1182 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1187 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1188 dependsOnMethods = {"delete"})
1189 public void deleteNonExistent(String testName) throws Exception {
1191 if (logger.isDebugEnabled()) {
1192 logger.debug(testBanner(testName, CLASS_NAME));
1195 setupDeleteNonExistent();
1197 // Submit the request to the service and store the response.
1198 VocabularyClient client = new VocabularyClient();
1199 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1200 int statusCode = res.getStatus();
1202 // Check the status code of the response: does it match
1203 // the expected response(s)?
1204 if(logger.isDebugEnabled()){
1205 logger.debug(testName + ": status = " + statusCode);
1207 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1208 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1209 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1212 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1213 dependsOnMethods = {"deleteItem"})
1214 public void deleteNonExistentItem(String testName) {
1216 if (logger.isDebugEnabled()) {
1217 logger.debug(testBanner(testName, CLASS_NAME));
1220 setupDeleteNonExistent();
1222 // Submit the request to the service and store the response.
1223 VocabularyClient client = new VocabularyClient();
1224 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1225 int statusCode = res.getStatus();
1227 // Check the status code of the response: does it match
1228 // the expected response(s)?
1229 if(logger.isDebugEnabled()){
1230 logger.debug(testName + ": status = " + statusCode);
1232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1237 // ---------------------------------------------------------------
1238 // Utility tests : tests of code used in tests above
1239 // ---------------------------------------------------------------
1241 * Tests the code for manually submitting data that is used by several
1242 * of the methods above.
1244 @Test(dependsOnMethods = {"create", "read"})
1245 public void testSubmitRequest() {
1247 // Expected status code: 200 OK
1250 // Submit the request to the service and store the response.
1251 String method = ServiceRequestType.READ.httpMethodName();
1252 String url = getResourceURL(knownResourceId);
1253 int statusCode = submitRequest(method, url);
1255 // Check the status code of the response: does it match
1256 // the expected response(s)?
1257 if(logger.isDebugEnabled()){
1258 logger.debug("testSubmitRequest: url=" + url +
1259 " status=" + statusCode);
1261 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1265 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1266 public void testItemSubmitRequest() {
1268 // Expected status code: 200 OK
1269 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1271 // Submit the request to the service and store the response.
1272 String method = ServiceRequestType.READ.httpMethodName();
1273 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1274 int statusCode = submitRequest(method, url);
1276 // Check the status code of the response: does it match
1277 // the expected response(s)?
1278 if(logger.isDebugEnabled()){
1279 logger.debug("testItemSubmitRequest: url=" + url +
1280 " status=" + statusCode);
1282 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1286 // ---------------------------------------------------------------
1287 // Cleanup of resources created during testing
1288 // ---------------------------------------------------------------
1291 * Deletes all resources created by tests, after all tests have been run.
1293 * This cleanup method will always be run, even if one or more tests fail.
1294 * For this reason, it attempts to remove all resources created
1295 * at any point during testing, even if some of those resources
1296 * may be expected to be deleted by certain tests.
1298 @AfterClass(alwaysRun=true)
1299 public void cleanUp() {
1300 String noTest = System.getProperty("noTestCleanup");
1301 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1302 if (logger.isDebugEnabled()) {
1303 logger.debug("Skipping Cleanup phase ...");
1307 if (logger.isDebugEnabled()) {
1308 logger.debug("Cleaning up temporary resources created for testing ...");
1310 VocabularyClient client = new VocabularyClient();
1311 String vocabularyResourceId;
1312 String vocabularyItemResourceId;
1313 // Clean up vocabulary item resources.
1314 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1315 vocabularyItemResourceId = entry.getKey();
1316 vocabularyResourceId = entry.getValue();
1317 // Note: Any non-success responses are ignored and not reported.
1318 client.deleteItem(vocabularyResourceId, vocabularyItemResourceId).releaseConnection();
1320 // Clean up vocabulary resources.
1321 for (String resourceId : allResourceIdsCreated) {
1322 // Note: Any non-success responses are ignored and not reported.
1323 client.delete(resourceId).releaseConnection();
1328 // ---------------------------------------------------------------
1329 // Utility methods used by tests above
1330 // ---------------------------------------------------------------
1332 public String getServicePathComponent() {
1333 return SERVICE_PATH_COMPONENT;
1336 public String getItemServicePathComponent() {
1337 return ITEM_SERVICE_PATH_COMPONENT;
1341 * Returns the root URL for a service.
1343 * This URL consists of a base URL for all services, followed by
1344 * a path component for the owning vocabulary, followed by the
1345 * path component for the items.
1347 * @return The root URL for a service.
1349 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1350 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1354 * Returns the URL of a specific resource managed by a service, and
1355 * designated by an identifier (such as a universally unique ID, or UUID).
1357 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1359 * @return The URL of a specific resource managed by a service.
1361 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1362 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;