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.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.client.VocabularyClient;
38 import org.collectionspace.services.client.VocabularyClientUtils;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.vocabulary.VocabulariesCommon;
41 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
42 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
43 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
45 import org.jboss.resteasy.client.ClientResponse;
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);
63 // Instance variables specific to this test.
64 final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
65 final String SERVICE_PATH_ITEMS_COMPONENT = VocabularyClient.SERVICE_PATH_ITEMS_COMPONENT;//"items";
66 final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
67 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
68 private String knownResourceId = null;
69 private String knownResourceShortIdentifer = null;
70 private String knownResourceRefName = null;
71 private String knownResourceFullRefName = null;
72 private String knownItemResourceId = null;
73 private int nItemsToCreateInList = 5;
74 private List<String> allResourceIdsCreated = new ArrayList<String>();
75 private Map<String, String> allResourceItemIdsCreated =
76 new HashMap<String, String>();
78 protected void setKnownResource(String id, String shortIdentifer,
79 String refName, String fullRefName) {
81 knownResourceShortIdentifer = shortIdentifer;
82 knownResourceRefName = refName;
83 knownResourceFullRefName = fullRefName;
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
90 protected CollectionSpaceClient getClientInstance() {
91 return new VocabularyClient();
95 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
98 protected AbstractCommonList getAbstractCommonList(
99 ClientResponse<AbstractCommonList> response) {
100 return response.getEntity(VocabulariesCommonList.class);
103 // ---------------------------------------------------------------
104 // CRUD tests : CREATE tests
105 // ---------------------------------------------------------------
108 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
109 public void create(String testName) throws Exception {
111 if (logger.isDebugEnabled()) {
112 logger.debug(testBanner(testName, CLASS_NAME));
114 // Perform setup, such as initializing the type of service request
115 // (e.g. CREATE, DELETE), its valid and expected status codes, and
116 // its associated HTTP method name (e.g. POST, DELETE).
119 // Submit the request to the service and store the response.
120 VocabularyClient client = new VocabularyClient();
121 String identifier = createIdentifier();
122 String displayName = "displayName-" + identifier;
123 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
124 displayName, identifier, client.getCommonPartName());
125 ClientResponse<Response> res = client.create(multipart);
126 int statusCode = res.getStatus();
128 // Check the status code of the response: does it match
129 // the expected response(s)?
132 // Does it fall within the set of valid status codes?
133 // Does it exactly match the expected status code?
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": status = " + statusCode);
137 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
139 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
141 // Store the ID returned from the first resource created
142 // for additional tests below.
143 if (knownResourceId == null) {
144 setKnownResource(extractId(res), identifier,
145 VocabularyClientUtils.createVocabularyRefName(identifier, null),
146 VocabularyClientUtils.createVocabularyRefName(identifier, displayName));
147 if (logger.isDebugEnabled()) {
148 logger.debug(testName + ": knownResourceId=" + knownResourceId);
151 // Store the IDs from every resource created by tests,
152 // so they can be deleted after tests have been run.
153 allResourceIdsCreated.add(extractId(res));
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
158 dependsOnMethods = {"create"})
159 public void createItem(String testName) {
161 if (null != testName && logger.isDebugEnabled()) {
162 logger.debug(testBanner(testName, CLASS_NAME));
167 VocabularyClient client = new VocabularyClient();
168 HashMap<String, String> itemInfo = new HashMap<String, String>();
169 String shortId = createIdentifier();
170 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
171 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
172 String newID = VocabularyClientUtils.createItemInVocabulary(knownResourceId,
173 knownResourceRefName, itemInfo, client);
175 // Store the ID returned from the first item resource created
176 // for additional tests below.
177 if (knownItemResourceId == null) {
178 knownItemResourceId = newID;
179 if (null != testName && logger.isDebugEnabled()) {
180 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
183 // Store the IDs from any item resources created
184 // by tests, along with the IDs of their parents, so these items
185 // can be deleted after all tests have been run.
186 allResourceItemIdsCreated.put(newID, knownResourceId);
190 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
191 dependsOnMethods = {"create", "createItem", "readItem"})
192 public void createList(String testName) throws Exception {
193 for (int i = 0; i < 3; i++) {
194 // Force create to reset the known resource info
195 setKnownResource(null, null, null, null);
196 knownItemResourceId = null;
198 // Add nItemsToCreateInList items to each vocab
199 for (int j = 0; j < nItemsToCreateInList; j++) {
206 // Placeholders until the three tests below can be uncommented.
207 // See Issue CSPACE-401.
209 public void createWithEmptyEntityBody(String testName) throws Exception {
213 public void createWithMalformedXml(String testName) throws Exception {
217 public void createWithWrongXmlSchema(String testName) throws Exception {
220 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
221 dependsOnMethods = {"create"})
222 public void createWithBadShortId(String testName) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug(testBanner(testName, CLASS_NAME));
227 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
229 // Submit the request to the service and store the response.
230 VocabularyClient client = new VocabularyClient();
231 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
232 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
233 ClientResponse<Response> res = client.create(multipart);
234 int statusCode = res.getStatus();
236 // Check the status code of the response: does it match
237 // the expected response(s)?
240 // Does it fall within the set of valid status codes?
241 // Does it exactly match the expected status code?
242 if (logger.isDebugEnabled()) {
243 logger.debug(testName + ": status = " + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
250 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
251 dependsOnMethods = {"createItem"})
252 public void createItemWithBadShortId(String testName) throws Exception {
254 if (logger.isDebugEnabled()) {
255 logger.debug(testBanner(testName, CLASS_NAME));
257 setupCreateWithMalformedXml();
259 // Submit the request to the service and store the response.
260 VocabularyClient client = new VocabularyClient();
261 HashMap<String, String> itemInfo = new HashMap<String, String>();
262 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
263 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
264 PoxPayloadOut multipart =
265 VocabularyClientUtils.createVocabularyItemInstance(knownResourceRefName,
266 itemInfo, client.getCommonPartItemName());
267 ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
269 int statusCode = res.getStatus();
271 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
272 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
273 + "\" in personAuthority: \"" + knownResourceRefName
274 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 if (statusCode != EXPECTED_STATUS_CODE) {
277 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
278 + "\" in personAuthority: \"" + knownResourceRefName + "\", Status:" + statusCode);
284 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
285 dependsOnMethods = {"create", "testSubmitRequest"})
286 public void createWithEmptyEntityBody(String testName) throws Exception {
288 if (logger.isDebugEnabled()) {
289 logger.debug(testBanner(testName, CLASS_NAME));
292 setupCreateWithEmptyEntityBody(testName, CLASS_NAME);
294 // Submit the request to the service and store the response.
295 String method = REQUEST_TYPE.httpMethodName();
296 String url = getServiceRootURL();
297 String mediaType = MediaType.APPLICATION_XML;
298 final String entity = "";
299 int statusCode = submitRequest(method, url, mediaType, entity);
301 // Check the status code of the response: does it match
302 // the expected response(s)?
303 if(logger.isDebugEnabled()) {
304 logger.debug(testName + ": url=" + url +
305 " status=" + statusCode);
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
313 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
314 dependsOnMethods = {"create", "testSubmitRequest"})
315 public void createWithMalformedXml(String testName) throws Exception {
317 if (logger.isDebugEnabled()) {
318 logger.debug(testBanner(testName, CLASS_NAME));
321 setupCreateWithMalformedXml();
323 // Submit the request to the service and store the response.
324 String method = REQUEST_TYPE.httpMethodName();
325 String url = getServiceRootURL();
326 String mediaType = MediaType.APPLICATION_XML;
327 final String entity = MALFORMED_XML_DATA; // Constant from base class.
328 int statusCode = submitRequest(method, url, mediaType, entity);
330 // Check the status code of the response: does it match
331 // the expected response(s)?
332 if(logger.isDebugEnabled()){
333 logger.debug(testName + ": url=" + url +
334 " status=" + statusCode);
336 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
337 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
338 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
343 dependsOnMethods = {"create", "testSubmitRequest"})
344 public void createWithWrongXmlSchema(String testName) throws Exception {
346 if (logger.isDebugEnabled()) {
347 logger.debug(testBanner(testName, CLASS_NAME));
350 setupCreateWithWrongXmlSchema();
352 // Submit the request to the service and store the response.
353 String method = REQUEST_TYPE.httpMethodName();
354 String url = getServiceRootURL();
355 String mediaType = MediaType.APPLICATION_XML;
356 final String entity = WRONG_XML_SCHEMA_DATA;
357 int statusCode = submitRequest(method, url, mediaType, entity);
359 // Check the status code of the response: does it match
360 // the expected response(s)?
361 if(logger.isDebugEnabled()){
362 logger.debug(testName + ": url=" + url +
363 " status=" + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 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<String> 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 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
399 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
400 client.getCommonPartName(), VocabulariesCommon.class);
402 Assert.assertNotNull(vocabulary);
403 Assert.assertEquals(vocabulary.getRefName(), knownResourceFullRefName);
409 * @param testName the test name
410 * @throws Exception the exception
412 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
413 dependsOnMethods = {"read"})
414 public void readByName(String testName) throws Exception {
416 if (logger.isDebugEnabled()) {
417 logger.debug(testBanner(testName, CLASS_NAME));
422 // Submit the request to the service and store the response.
423 VocabularyClient client = new VocabularyClient();
424 ClientResponse<String> res = client.readByName(knownResourceShortIdentifer);
425 int statusCode = res.getStatus();
427 // Check the status code of the response: does it match
428 // the expected response(s)?
429 if (logger.isDebugEnabled()) {
430 logger.debug(testName + ": status = " + statusCode);
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
436 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
437 client.getCommonPartName(), VocabulariesCommon.class);
439 Assert.assertNotNull(vocabulary);
443 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
444 dependsOnMethods = {"read"})
445 public void readByName(String testName) throws Exception {
447 if (logger.isDebugEnabled()) {
448 logger.debug(testBanner(testName, CLASS_NAME));
453 // Submit the request to the service and store the response.
454 ClientResponse<PoxPayloadIn> res = client.read(knownResourceId);
455 int statusCode = res.getStatus();
457 // Check the status code of the response: does it match
458 // the expected response(s)?
459 if(logger.isDebugEnabled()){
460 logger.debug(testName + ": status = " + statusCode);
462 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
463 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
464 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 //FIXME: remove the following try catch once Aron fixes signatures
467 PoxPayloadIn input = (PoxPayloadIn) res.getEntity();
468 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
469 client.getCommonPartName(), VocabulariesCommon.class);
470 Assert.assertNotNull(vocabulary);
471 } catch (Exception e) {
472 throw new RuntimeException(e);
476 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
477 dependsOnMethods = {"createItem", "read"})
478 public void readItem(String testName) throws Exception {
480 if (logger.isDebugEnabled()) {
481 logger.debug(testBanner(testName, CLASS_NAME));
486 // Submit the request to the service and store the response.
487 VocabularyClient client = new VocabularyClient();
488 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
489 int statusCode = res.getStatus();
491 // Check the status code of the response: does it match
492 // the expected response(s)?
493 if (logger.isDebugEnabled()) {
494 logger.debug(testName + ": status = " + statusCode);
496 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
497 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
498 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
500 // Check whether we've received a vocabulary item.
501 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
502 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
503 client.getCommonPartItemName(), VocabularyitemsCommon.class);
504 Assert.assertNotNull(vocabularyItem);
505 Assert.assertEquals(vocabularyItem.getInAuthority(), knownResourceId);
509 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
510 dependsOnMethods = {"updateItem"})
511 public void verifyIllegalItemDisplayName(String testName) throws Exception {
513 if (logger.isDebugEnabled()) {
514 logger.debug(testBanner(testName, CLASS_NAME));
517 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
518 // setupUpdateWithWrongXmlSchema(testName);
520 // Submit the request to the service and store the response.
521 VocabularyClient client = new VocabularyClient();
522 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
523 int statusCode = res.getStatus();
525 // Check the status code of the response: does it match
526 // the expected response(s)?
527 if (logger.isDebugEnabled()) {
528 logger.debug(testName + ": status = " + statusCode);
530 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
531 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
532 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
534 // Check whether Person has expected displayName.
535 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
536 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
537 client.getCommonPartItemName(), VocabularyitemsCommon.class);
538 Assert.assertNotNull(vitem);
539 // Try to Update with null displayName
540 vitem.setDisplayName(null);
542 // Submit the updated resource to the service and store the response.
543 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
544 PayloadOutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
545 commonPart.setLabel(client.getCommonPartItemName());
546 res = client.updateItem(knownResourceId, knownItemResourceId, output);
547 statusCode = res.getStatus();
549 // Check the status code of the response: does it match the expected response(s)?
550 if (logger.isDebugEnabled()) {
551 logger.debug("updateItem: status = " + statusCode);
553 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
554 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
555 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
556 "Expecting invalid message because of null displayName.");
558 // Now try to Update with 1-char displayName (too short)
559 vitem.setDisplayName("a");
561 // Submit the updated resource to the service and store the response.
562 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
563 commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
564 commonPart.setLabel(client.getCommonPartItemName());
565 res = client.updateItem(knownResourceId, knownItemResourceId, output);
566 statusCode = res.getStatus();
568 // Check the status code of the response: does it match the expected response(s)?
569 if (logger.isDebugEnabled()) {
570 logger.debug("updateItem: status = " + statusCode);
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
575 "Expecting invalid message because of 1-char displayName.");
579 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
580 dependsOnMethods = {"read"})
581 public void readNonExistent(String testName) {
583 if (logger.isDebugEnabled()) {
584 logger.debug(testBanner(testName, CLASS_NAME));
587 setupReadNonExistent();
589 // Submit the request to the service and store the response.
590 VocabularyClient client = new VocabularyClient();
591 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
592 int statusCode = res.getStatus();
594 // Check the status code of the response: does it match
595 // the expected response(s)?
596 if (logger.isDebugEnabled()) {
597 logger.debug(testName + ": status = " + statusCode);
599 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
600 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
601 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
604 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
605 dependsOnMethods = {"readItem", "readNonExistent"})
606 public void readItemNonExistent(String testName) {
608 if (logger.isDebugEnabled()) {
609 logger.debug(testBanner(testName, CLASS_NAME));
612 setupReadNonExistent();
614 // Submit the request to the service and store the response.
615 VocabularyClient client = new VocabularyClient();
616 ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
617 int statusCode = res.getStatus();
619 // Check the status code of the response: does it match
620 // the expected response(s)?
621 if (logger.isDebugEnabled()) {
622 logger.debug(testName + ": status = " + statusCode);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
628 // ---------------------------------------------------------------
629 // CRUD tests : READ_LIST tests
630 // ---------------------------------------------------------------
634 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
635 dependsOnMethods = {"createList", "read"})
636 public void readList(String testName) throws Exception {
638 if (logger.isDebugEnabled()) {
639 logger.debug(testBanner(testName, CLASS_NAME));
644 // Submit the request to the service and store the response.
645 VocabularyClient client = new VocabularyClient();
646 ClientResponse<VocabulariesCommonList> res = client.readList();
647 VocabulariesCommonList list = res.getEntity();
648 int statusCode = res.getStatus();
650 // Check the status code of the response: does it match
651 // the expected response(s)?
652 if (logger.isDebugEnabled()) {
653 logger.debug(testName + ": status = " + statusCode);
655 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
656 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
657 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
659 // Optionally output additional data about list members for debugging.
660 boolean iterateThroughList = false;
661 if (iterateThroughList && logger.isDebugEnabled()) {
662 List<VocabulariesCommonList.VocabularyListItem> items =
663 list.getVocabularyListItem();
665 for (VocabulariesCommonList.VocabularyListItem item : items) {
666 String csid = item.getCsid();
667 logger.debug(testName + ": list-item[" + i + "] csid="
669 logger.debug(testName + ": list-item[" + i + "] displayName="
670 + item.getDisplayName());
671 logger.debug(testName + ": list-item[" + i + "] URI="
673 readItemListInt(csid, null, "readList");
679 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
680 dependsOnMethods = {"createList", "readItem"})
681 public void readItemList(String testName) {
682 readItemListInt(knownResourceId, null, testName);
685 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
686 dependsOnMethods = {"createList", "readItem"})
687 public void readItemListByName(String testName) {
688 readItemListInt(null, knownResourceShortIdentifer, testName);
691 private void readItemListInt(String vcsid, String shortId, String testName) {
696 // Submit the request to the service and store the response.
697 VocabularyClient client = new VocabularyClient();
698 ClientResponse<VocabularyitemsCommonList> res = null;
700 res = client.readItemList(vcsid, null, null);
701 } else if (shortId != null) {
702 res = client.readItemListForNamedAuthority(shortId, null, null);
704 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
706 VocabularyitemsCommonList list = res.getEntity();
707 int statusCode = res.getStatus();
709 // Check the status code of the response: does it match
710 // the expected response(s)?
711 if (logger.isDebugEnabled()) {
712 logger.debug(" " + testName + ": status = " + statusCode);
714 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
715 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
716 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
718 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
719 list.getVocabularyitemListItem();
720 int nItemsReturned = items.size();
721 long nItemsTotal = list.getTotalItems();
722 if (logger.isDebugEnabled()) {
723 logger.debug(" " + testName + ": Expected "
724 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
726 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
728 // Optionally output additional data about list members for debugging.
729 boolean iterateThroughList = true;
730 if (iterateThroughList && logger.isDebugEnabled()) {
731 logger.debug(" " + testName + ": checking items");
733 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
734 logger.debug(" " + testName + ": list-item[" + i + "] csid="
736 logger.debug(" " + testName + ": list-item[" + i + "] displayName="
737 + item.getDisplayName());
738 logger.debug(" " + testName + ": list-item[" + i + "] URI="
747 // ---------------------------------------------------------------
748 // CRUD tests : UPDATE tests
749 // ---------------------------------------------------------------
752 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
753 dependsOnMethods = {"read"})
754 public void update(String testName) throws Exception {
756 if (logger.isDebugEnabled()) {
757 logger.debug(testBanner(testName, CLASS_NAME));
762 // Retrieve the contents of a resource to update.
763 VocabularyClient client = new VocabularyClient();
764 ClientResponse<String> res =
765 client.read(knownResourceId);
766 if (logger.isDebugEnabled()) {
767 logger.debug(testName + ": read status = " + res.getStatus());
769 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
771 if (logger.isDebugEnabled()) {
772 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
774 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
775 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
776 client.getCommonPartName(), VocabulariesCommon.class);
777 Assert.assertNotNull(vocabulary);
779 // Update the contents of this resource.
780 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
781 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
782 if (logger.isDebugEnabled()) {
783 logger.debug("to be updated Vocabulary");
784 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
787 // Submit the updated resource to the service and store the response.
788 PoxPayloadOut output = new PoxPayloadOut(SERVICE_PAYLOAD_NAME);
790 PayloadOutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
791 commonPart.setLabel(client.getCommonPartName());
792 res = client.update(knownResourceId, output);
793 int statusCode = res.getStatus();
795 // Check the status code of the response: does it match the expected response(s)?
796 if (logger.isDebugEnabled()) {
797 logger.debug("update: status = " + statusCode);
799 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
800 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
801 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803 // Retrieve the updated resource and verify that its contents exist.
804 input = new PoxPayloadIn(res.getEntity());
805 VocabulariesCommon updatedVocabulary =
806 (VocabulariesCommon) extractPart(input,
807 client.getCommonPartName(), VocabulariesCommon.class);
808 Assert.assertNotNull(updatedVocabulary);
810 // Verify that the updated resource received the correct data.
811 Assert.assertEquals(updatedVocabulary.getDisplayName(),
812 vocabulary.getDisplayName(),
813 "Data in updated object did not match submitted data.");
816 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
817 dependsOnMethods = {"readItem", "update", "verifyIgnoredUpdateWithInAuthority"})
818 public void updateItem(String testName) throws Exception {
820 if (logger.isDebugEnabled()) {
821 logger.debug(testBanner(testName, CLASS_NAME));
826 // Retrieve the contents of a resource to update.
827 VocabularyClient client = new VocabularyClient();
828 ClientResponse<String> res =
829 client.readItem(knownResourceId, knownItemResourceId);
830 if (logger.isDebugEnabled()) {
831 logger.debug(testName + ": read status = " + res.getStatus());
833 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
835 if (logger.isDebugEnabled()) {
836 logger.debug("got VocabularyItem to update with ID: "
837 + knownItemResourceId
838 + " in Vocab: " + knownResourceId);
840 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
841 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
842 client.getCommonPartItemName(), VocabularyitemsCommon.class);
843 Assert.assertNotNull(vocabularyItem);
845 // Update the contents of this resource.
846 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
847 if (logger.isDebugEnabled()) {
848 logger.debug("to be updated VocabularyItem");
849 logger.debug(objectAsXmlString(vocabularyItem,
850 VocabularyitemsCommon.class));
853 // Submit the updated resource to the service and store the response.
854 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
855 PayloadOutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
856 commonPart.setLabel(client.getCommonPartItemName());
857 res = client.updateItem(knownResourceId, knownItemResourceId, output);
858 int statusCode = res.getStatus();
860 // Check the status code of the response: does it match the expected response(s)?
861 if (logger.isDebugEnabled()) {
862 logger.debug("updateItem: status = " + statusCode);
864 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
865 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
866 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
868 // Retrieve the updated resource and verify that its contents exist.
869 input = new PoxPayloadIn(res.getEntity());
870 VocabularyitemsCommon updatedVocabularyItem =
871 (VocabularyitemsCommon) extractPart(input,
872 client.getCommonPartItemName(), VocabularyitemsCommon.class);
873 Assert.assertNotNull(updatedVocabularyItem);
875 // Verify that the updated resource received the correct data.
876 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
877 vocabularyItem.getDisplayName(),
878 "Data in updated VocabularyItem did not match submitted data.");
881 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
882 dependsOnMethods = {"readItem"})
883 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
885 if (logger.isDebugEnabled()) {
886 logger.debug(testBanner(testName, CLASS_NAME));
891 // Submit the request to the service and store the response.
892 VocabularyClient client = new VocabularyClient();
893 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
894 int statusCode = res.getStatus();
896 // Check the status code of the response: does it match
897 // the expected response(s)?
898 if (logger.isDebugEnabled()) {
899 logger.debug(testName + " read Vocab:" + knownResourceId + "/Item:"
900 + knownItemResourceId + " status = " + statusCode);
902 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
903 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
904 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
906 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
907 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
908 client.getCommonPartItemName(), VocabularyitemsCommon.class);
909 Assert.assertNotNull(vitem);
910 // Try to Update with new parent vocab (use self, for test).
911 Assert.assertEquals(vitem.getInAuthority(),
913 "VocabularyItem inAuthority does not match knownResourceId.");
914 vitem.setInAuthority(knownItemResourceId);
916 // Submit the updated resource to the service and store the response.
917 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
918 PayloadOutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
919 commonPart.setLabel(client.getCommonPartItemName());
920 res = client.updateItem(knownResourceId, knownItemResourceId, output);
921 statusCode = res.getStatus();
923 // Check the status code of the response: does it match the expected response(s)?
924 if (logger.isDebugEnabled()) {
925 logger.debug(testName + ": status = " + statusCode);
927 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
928 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
929 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
931 // Retrieve the updated resource and verify that the parent did not change
932 res = client.readItem(knownResourceId, knownItemResourceId);
933 input = new PoxPayloadIn(res.getEntity());
934 VocabularyitemsCommon updatedVocabularyItem =
935 (VocabularyitemsCommon) extractPart(input,
936 client.getCommonPartItemName(), VocabularyitemsCommon.class);
937 Assert.assertNotNull(updatedVocabularyItem);
939 // Verify that the updated resource received the correct data.
940 Assert.assertEquals(updatedVocabularyItem.getInAuthority(),
942 "VocabularyItem allowed update to the parent (inAuthority).");
946 // Placeholders until the three tests below can be uncommented.
947 // See Issue CSPACE-401.
949 public void updateWithEmptyEntityBody(String testName) throws Exception {
953 public void updateWithMalformedXml(String testName) throws Exception {
957 public void updateWithWrongXmlSchema(String testName) throws Exception {
962 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
963 dependsOnMethods = {"create", "update", "testSubmitRequest"})
964 public void updateWithEmptyEntityBody(String testName) throws Exception {
966 if (logger.isDebugEnabled()) {
967 logger.debug(testBanner(testName, CLASS_NAME));
970 setupUpdateWithEmptyEntityBody();
972 // Submit the request to the service and store the response.
973 String method = REQUEST_TYPE.httpMethodName();
974 String url = getResourceURL(knownResourceId);
975 String mediaType = MediaType.APPLICATION_XML;
976 final String entity = "";
977 int statusCode = submitRequest(method, url, mediaType, entity);
979 // Check the status code of the response: does it match
980 // the expected response(s)?
981 if(logger.isDebugEnabled()){
982 logger.debug(testName + ": url=" + url +
983 " status=" + statusCode);
985 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
986 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
987 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
991 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
992 dependsOnMethods = {"create", "update", "testSubmitRequest"})
993 public void updateWithMalformedXml(String testName) throws Exception {
995 if (logger.isDebugEnabled()) {
996 logger.debug(testBanner(testName, CLASS_NAME));
999 setupUpdateWithMalformedXml();
1001 // Submit the request to the service and store the response.
1002 String method = REQUEST_TYPE.httpMethodName();
1003 String url = getResourceURL(knownResourceId);
1004 String mediaType = MediaType.APPLICATION_XML;
1005 final String entity = MALFORMED_XML_DATA;
1006 int statusCode = submitRequest(method, url, mediaType, entity);
1008 // Check the status code of the response: does it match
1009 // the expected response(s)?
1010 if(logger.isDebugEnabled()){
1011 logger.debug(testName + ": url=" + url +
1012 " status=" + statusCode);
1014 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1015 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1016 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1020 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1021 dependsOnMethods = {"create", "update", "testSubmitRequest"})
1022 public void updateWithWrongXmlSchema(String testName) throws Exception {
1024 if (logger.isDebugEnabled()) {
1025 logger.debug(testBanner(testName, CLASS_NAME));
1028 setupUpdateWithWrongXmlSchema();
1030 // Submit the request to the service and store the response.
1031 String method = REQUEST_TYPE.httpMethodName();
1032 String url = getResourceURL(knownResourceId);
1033 String mediaType = MediaType.APPLICATION_XML;
1034 final String entity = WRONG_XML_SCHEMA_DATA;
1035 int statusCode = submitRequest(method, url, mediaType, entity);
1037 // Check the status code of the response: does it match
1038 // the expected response(s)?
1039 if(logger.isDebugEnabled()){
1040 logger.debug("updateWithWrongXmlSchema: url=" + url +
1041 " status=" + statusCode);
1043 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1044 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1045 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1049 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1050 dependsOnMethods = {"update", "testSubmitRequest"})
1051 public void updateNonExistent(String testName) throws Exception {
1053 if (logger.isDebugEnabled()) {
1054 logger.debug(testBanner(testName, CLASS_NAME));
1057 setupUpdateNonExistent();
1059 // Submit the request to the service and store the response.
1060 // Note: The ID used in this 'create' call may be arbitrary.
1061 // The only relevant ID may be the one used in update(), below.
1062 VocabularyClient client = new VocabularyClient();
1063 String displayName = "displayName-" + NON_EXISTENT_ID;
1064 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
1065 displayName, NON_EXISTENT_ID, client.getCommonPartName());
1066 ClientResponse<String> res =
1067 client.update(NON_EXISTENT_ID, multipart);
1068 int statusCode = res.getStatus();
1070 // Check the status code of the response: does it match
1071 // the expected response(s)?
1072 if (logger.isDebugEnabled()) {
1073 logger.debug(testName + ": status = " + statusCode);
1075 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1076 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1077 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1080 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1081 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1082 public void updateNonExistentItem(String testName) throws Exception {
1084 if (logger.isDebugEnabled()) {
1085 logger.debug(testBanner(testName, CLASS_NAME));
1088 setupUpdateNonExistent();
1090 // Submit the request to the service and store the response.
1091 // Note: The ID used in this 'create' call may be arbitrary.
1092 // The only relevant ID may be the one used in update(), below.
1093 VocabularyClient client = new VocabularyClient();
1094 HashMap<String, String> itemInfo = new HashMap<String, String>();
1095 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
1096 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
1097 PoxPayloadOut multipart =
1098 VocabularyClientUtils.createVocabularyItemInstance(
1099 VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
1100 itemInfo, client.getCommonPartItemName());
1101 ClientResponse<String> res =
1102 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1103 int statusCode = res.getStatus();
1105 // Check the status code of the response: does it match
1106 // the expected response(s)?
1107 if (logger.isDebugEnabled()) {
1108 logger.debug(testName + ": status = " + statusCode);
1110 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1111 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1112 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1115 // ---------------------------------------------------------------
1116 // CRUD tests : DELETE tests
1117 // ---------------------------------------------------------------
1120 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1121 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "deleteItem"})
1122 public void delete(String testName) throws Exception {
1124 if (logger.isDebugEnabled()) {
1125 logger.debug(testBanner(testName, CLASS_NAME));
1130 // Submit the request to the service and store the response.
1131 VocabularyClient client = new VocabularyClient();
1132 ClientResponse<Response> res = client.delete(knownResourceId);
1133 int statusCode = res.getStatus();
1135 // Check the status code of the response: does it match
1136 // the expected response(s)?
1137 if (logger.isDebugEnabled()) {
1138 logger.debug(testName + ": status = " + statusCode);
1140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1145 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1146 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
1147 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
1148 public void deleteItem(String testName) throws Exception {
1150 if (logger.isDebugEnabled()) {
1151 logger.debug(testBanner(testName, CLASS_NAME));
1156 // Submit the request to the service and store the response.
1157 VocabularyClient client = new VocabularyClient();
1158 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1159 int statusCode = res.getStatus();
1161 // Check the status code of the response: does it match
1162 // the expected response(s)?
1163 if (logger.isDebugEnabled()) {
1164 logger.debug("delete: status = " + statusCode);
1166 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1167 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1168 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1173 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1174 dependsOnMethods = {"delete"})
1175 public void deleteNonExistent(String testName) throws Exception {
1177 if (logger.isDebugEnabled()) {
1178 logger.debug(testBanner(testName, CLASS_NAME));
1181 setupDeleteNonExistent();
1183 // Submit the request to the service and store the response.
1184 VocabularyClient client = new VocabularyClient();
1185 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1186 int statusCode = res.getStatus();
1188 // Check the status code of the response: does it match
1189 // the expected response(s)?
1190 if (logger.isDebugEnabled()) {
1191 logger.debug(testName + ": status = " + statusCode);
1193 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1194 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1195 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1198 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1199 dependsOnMethods = {"deleteItem"})
1200 public void deleteNonExistentItem(String testName) {
1202 if (logger.isDebugEnabled()) {
1203 logger.debug(testBanner(testName, CLASS_NAME));
1206 setupDeleteNonExistent();
1208 // Submit the request to the service and store the response.
1209 VocabularyClient client = new VocabularyClient();
1210 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1211 int statusCode = res.getStatus();
1213 // Check the status code of the response: does it match
1214 // the expected response(s)?
1215 if (logger.isDebugEnabled()) {
1216 logger.debug(testName + ": status = " + statusCode);
1218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1223 // ---------------------------------------------------------------
1224 // Utility tests : tests of code used in tests above
1225 // ---------------------------------------------------------------
1227 * Tests the code for manually submitting data that is used by several
1228 * of the methods above.
1230 @Test(dependsOnMethods = {"create", "read"})
1231 public void testSubmitRequest() {
1233 // Expected status code: 200 OK
1236 // Submit the request to the service and store the response.
1237 String method = ServiceRequestType.READ.httpMethodName();
1238 String url = getResourceURL(knownResourceId);
1239 int statusCode = submitRequest(method, url);
1241 // Check the status code of the response: does it match
1242 // the expected response(s)?
1243 if (logger.isDebugEnabled()) {
1244 logger.debug("testSubmitRequest: url=" + url
1245 + " status=" + statusCode);
1247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1251 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1252 public void testItemSubmitRequest() {
1254 // Expected status code: 200 OK
1255 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1257 // Submit the request to the service and store the response.
1258 String method = ServiceRequestType.READ.httpMethodName();
1259 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1260 int statusCode = submitRequest(method, url);
1262 // Check the status code of the response: does it match
1263 // the expected response(s)?
1264 if (logger.isDebugEnabled()) {
1265 logger.debug("testItemSubmitRequest: url=" + url
1266 + " status=" + statusCode);
1268 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1272 // ---------------------------------------------------------------
1273 // Cleanup of resources created during testing
1274 // ---------------------------------------------------------------
1276 * Deletes all resources created by tests, after all tests have been run.
1278 * This cleanup method will always be run, even if one or more tests fail.
1279 * For this reason, it attempts to remove all resources created
1280 * at any point during testing, even if some of those resources
1281 * may be expected to be deleted by certain tests.
1283 @AfterClass(alwaysRun = true)
1284 public void cleanUp() {
1285 String noTest = System.getProperty("noTestCleanup");
1286 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1287 if (logger.isDebugEnabled()) {
1288 logger.debug("Skipping Cleanup phase ...");
1292 if (logger.isDebugEnabled()) {
1293 logger.debug("Cleaning up temporary resources created for testing ...");
1295 VocabularyClient client = new VocabularyClient();
1296 String vocabularyResourceId;
1297 String vocabularyItemResourceId;
1298 // Clean up vocabulary item resources.
1299 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1300 vocabularyItemResourceId = entry.getKey();
1301 vocabularyResourceId = entry.getValue();
1302 // Note: Any non-success responses are ignored and not reported.
1303 client.deleteItem(vocabularyResourceId, vocabularyItemResourceId).releaseConnection();
1305 // Clean up vocabulary resources.
1306 for (String resourceId : allResourceIdsCreated) {
1307 // Note: Any non-success responses are ignored and not reported.
1308 client.delete(resourceId).releaseConnection();
1313 // ---------------------------------------------------------------
1314 // Utility methods used by tests above
1315 // ---------------------------------------------------------------
1317 public String getServicePathComponent() {
1318 return SERVICE_PATH_COMPONENT;
1321 public String getServicePathItemsComponent() {
1322 return this.SERVICE_PATH_ITEMS_COMPONENT;
1326 * Returns the root URL for a service.
1328 * This URL consists of a base URL for all services, followed by
1329 * a path component for the owning vocabulary, followed by the
1330 * path component for the items.
1332 * @return The root URL for a service.
1334 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1335 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
1339 * Returns the URL of a specific resource managed by a service, and
1340 * designated by an identifier (such as a universally unique ID, or UUID).
1342 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1344 * @return The URL of a specific resource managed by a service.
1346 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1347 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1351 protected String getServiceName() {
1352 return VocabularyClient.SERVICE_NAME;