2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.client.AuthorityClient;
33 import org.collectionspace.services.common.AbstractCommonListUtils;
34 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.VocabularyClient;
40 import org.collectionspace.services.client.VocabularyClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.vocabulary.VocabulariesCommon;
43 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
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_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
66 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
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 protected PoxPayloadOut createInstance(String identifier) {
95 VocabularyClient client = new VocabularyClient();
96 String displayName = "displayName-" + identifier;
97 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
98 displayName, identifier, client.getCommonPartName());
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 PoxPayloadOut 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, null, null );
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));
157 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
158 String headerLabel = new VocabularyClient().getItemCommonPartName();
159 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
160 String shortId = createIdentifier();
161 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
162 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
164 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
167 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
168 dependsOnMethods = {"create"})
169 public void createItem(String testName) {
171 if (null != testName && logger.isDebugEnabled()) {
172 logger.debug(testBanner(testName, CLASS_NAME));
177 VocabularyClient client = new VocabularyClient();
178 HashMap<String, String> itemInfo = new HashMap<String, String>();
179 String shortId = createIdentifier();
180 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
181 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
182 String newID = VocabularyClientUtils.createItemInVocabulary(knownResourceId,
183 null /*knownResourceRefName*/, itemInfo, client);
185 // Store the ID returned from the first item resource created
186 // for additional tests below.
187 if (knownItemResourceId == null) {
188 knownItemResourceId = newID;
189 if (null != testName && logger.isDebugEnabled()) {
190 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
193 // Store the IDs from any item resources created
194 // by tests, along with the IDs of their parents, so these items
195 // can be deleted after all tests have been run.
196 allResourceItemIdsCreated.put(newID, knownResourceId);
200 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
201 dependsOnMethods = {"create", "createItem", "readItem"})
202 public void createList(String testName) throws Exception {
203 for (int i = 0; i < 3; i++) {
204 // Force create to reset the known resource info
205 setKnownResource(null, null, null, null);
206 knownItemResourceId = null;
208 // Add nItemsToCreateInList items to each vocab
209 for (int j = 0; j < nItemsToCreateInList; j++) {
216 // Placeholders until the three tests below can be uncommented.
217 // See Issue CSPACE-401.
219 public void createWithEmptyEntityBody(String testName) throws Exception {
223 public void createWithMalformedXml(String testName) throws Exception {
227 public void createWithWrongXmlSchema(String testName) throws Exception {
230 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
231 dependsOnMethods = {"create"})
232 public void createWithBadShortId(String testName) throws Exception {
234 if (logger.isDebugEnabled()) {
235 logger.debug(testBanner(testName, CLASS_NAME));
237 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
239 // Submit the request to the service and store the response.
240 VocabularyClient client = new VocabularyClient();
241 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
242 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
243 ClientResponse<Response> res = client.create(multipart);
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
250 // Does it fall within the set of valid status codes?
251 // Does it exactly match the expected status code?
252 if (logger.isDebugEnabled()) {
253 logger.debug(testName + ": status = " + statusCode);
255 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
256 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
257 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
260 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
261 dependsOnMethods = {"createItem"})
262 public void createItemWithBadShortId(String testName) throws Exception {
264 if (logger.isDebugEnabled()) {
265 logger.debug(testBanner(testName, CLASS_NAME));
267 setupCreateWithMalformedXml();
269 // Submit the request to the service and store the response.
270 VocabularyClient client = new VocabularyClient();
271 HashMap<String, String> itemInfo = new HashMap<String, String>();
272 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
273 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
274 PoxPayloadOut multipart =
275 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
276 itemInfo, client.getCommonPartItemName());
277 ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
279 int statusCode = res.getStatus();
281 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
282 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
283 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
284 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
286 if (statusCode != EXPECTED_STATUS_CODE) {
287 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
288 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
294 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
295 dependsOnMethods = {"create", "testSubmitRequest"})
296 public void createWithEmptyEntityBody(String testName) throws Exception {
298 if (logger.isDebugEnabled()) {
299 logger.debug(testBanner(testName, CLASS_NAME));
302 setupCreateWithEmptyEntityBody(testName, CLASS_NAME);
304 // Submit the request to the service and store the response.
305 String method = REQUEST_TYPE.httpMethodName();
306 String url = getServiceRootURL();
307 String mediaType = MediaType.APPLICATION_XML;
308 final String entity = "";
309 int statusCode = submitRequest(method, url, mediaType, entity);
311 // Check the status code of the response: does it match
312 // the expected response(s)?
313 if(logger.isDebugEnabled()) {
314 logger.debug(testName + ": url=" + url +
315 " status=" + statusCode);
317 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
324 dependsOnMethods = {"create", "testSubmitRequest"})
325 public void createWithMalformedXml(String testName) throws Exception {
327 if (logger.isDebugEnabled()) {
328 logger.debug(testBanner(testName, CLASS_NAME));
331 setupCreateWithMalformedXml();
333 // Submit the request to the service and store the response.
334 String method = REQUEST_TYPE.httpMethodName();
335 String url = getServiceRootURL();
336 String mediaType = MediaType.APPLICATION_XML;
337 final String entity = MALFORMED_XML_DATA; // Constant from base class.
338 int statusCode = submitRequest(method, url, mediaType, entity);
340 // Check the status code of the response: does it match
341 // the expected response(s)?
342 if(logger.isDebugEnabled()){
343 logger.debug(testName + ": url=" + url +
344 " status=" + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
352 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
353 dependsOnMethods = {"create", "testSubmitRequest"})
354 public void createWithWrongXmlSchema(String testName) throws Exception {
356 if (logger.isDebugEnabled()) {
357 logger.debug(testBanner(testName, CLASS_NAME));
360 setupCreateWithWrongXmlSchema();
362 // Submit the request to the service and store the response.
363 String method = REQUEST_TYPE.httpMethodName();
364 String url = getServiceRootURL();
365 String mediaType = MediaType.APPLICATION_XML;
366 final String entity = WRONG_XML_SCHEMA_DATA;
367 int statusCode = submitRequest(method, url, mediaType, entity);
369 // Check the status code of the response: does it match
370 // the expected response(s)?
371 if(logger.isDebugEnabled()){
372 logger.debug(testName + ": url=" + url +
373 " status=" + statusCode);
375 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
376 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
377 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
380 // ---------------------------------------------------------------
381 // CRUD tests : READ tests
382 // ---------------------------------------------------------------
385 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
386 dependsOnMethods = {"create"})
387 public void read(String testName) throws Exception {
389 if (logger.isDebugEnabled()) {
390 logger.debug(testBanner(testName, CLASS_NAME));
395 // Submit the request to the service and store the response.
396 VocabularyClient client = new VocabularyClient();
397 ClientResponse<String> res = client.read(knownResourceId);
398 int statusCode = res.getStatus();
400 // Check the status code of the response: does it match
401 // the expected response(s)?
402 if (logger.isDebugEnabled()) {
403 logger.debug(testName + ": status = " + statusCode);
405 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
409 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
410 client.getCommonPartName(), VocabulariesCommon.class);
412 Assert.assertNotNull(vocabulary);
413 //Assert.assertEquals(vocabulary.getRefName(), knownResourceFullRefName);
419 * @param testName the test name
420 * @throws Exception the exception
422 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
423 dependsOnMethods = {"read"})
424 public void readByName(String testName) throws Exception {
426 if (logger.isDebugEnabled()) {
427 logger.debug(testBanner(testName, CLASS_NAME));
432 // Submit the request to the service and store the response.
433 VocabularyClient client = new VocabularyClient();
434 ClientResponse<String> res = client.readByName(knownResourceShortIdentifer);
435 assertStatusCode(res, testName);
437 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
438 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
439 client.getCommonPartName(), VocabulariesCommon.class);
441 Assert.assertNotNull(vocabulary);
445 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
446 dependsOnMethods = {"read"})
447 public void readByName(String testName) throws Exception {
449 if (logger.isDebugEnabled()) {
450 logger.debug(testBanner(testName, CLASS_NAME));
455 // Submit the request to the service and store the response.
456 ClientResponse<PoxPayloadIn> res = client.read(knownResourceId);
457 int statusCode = res.getStatus();
458 assertStatusCode(res, testName);
460 //FIXME: remove the following try catch once Aron fixes signatures
462 PoxPayloadIn input = (PoxPayloadIn) res.getEntity();
463 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
464 client.getCommonPartName(), VocabulariesCommon.class);
465 Assert.assertNotNull(vocabulary);
466 } catch (Exception e) {
467 throw new RuntimeException(e);
471 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
472 dependsOnMethods = {"createItem", "read"})
473 public void readItem(String testName) throws Exception {
475 if (logger.isDebugEnabled()) {
476 logger.debug(testBanner(testName, CLASS_NAME));
481 // Submit the request to the service and store the response.
482 VocabularyClient client = new VocabularyClient();
483 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
484 assertStatusCode(res, testName);
486 // Check whether we've received a vocabulary item.
487 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
488 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
489 client.getCommonPartItemName(), VocabularyitemsCommon.class);
490 Assert.assertNotNull(vocabularyItem);
491 Assert.assertEquals(vocabularyItem.getInAuthority(), knownResourceId);
495 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
496 dependsOnMethods = {"updateItem"})
497 public void verifyIllegalItemDisplayName(String testName) throws Exception {
499 if (logger.isDebugEnabled()) {
500 logger.debug(testBanner(testName, CLASS_NAME));
503 // Perform setup for read.
506 // Submit the request to the service and store the response.
507 VocabularyClient client = new VocabularyClient();
508 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
509 assertStatusCode(res, testName);
511 // Perform setup for update.
512 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
514 // Check whether Person has expected displayName.
515 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
516 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
517 client.getCommonPartItemName(), VocabularyitemsCommon.class);
518 Assert.assertNotNull(vitem);
519 // Try to Update with null displayName
520 vitem.setDisplayName(null);
522 // Submit the updated resource to the service and store the response.
523 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
524 PayloadOutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
525 commonPart.setLabel(client.getCommonPartItemName());
526 res = client.updateItem(knownResourceId, knownItemResourceId, output);
527 int statusCode = res.getStatus();
529 // Check the status code of the response: does it match the expected response(s)?
530 if (logger.isDebugEnabled()) {
531 logger.debug("updateItem: status = " + statusCode);
533 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
534 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
535 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
536 "Expecting invalid message because of null displayName.");
538 // Now try to Update with 1-char displayName (too short)
539 vitem.setDisplayName("a");
541 // Submit the updated resource to the service and store the response.
542 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
543 commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
544 commonPart.setLabel(client.getCommonPartItemName());
545 res = client.updateItem(knownResourceId, knownItemResourceId, output);
546 statusCode = res.getStatus();
548 // Check the status code of the response: does it match the expected response(s)?
549 if (logger.isDebugEnabled()) {
550 logger.debug("updateItem: status = " + statusCode);
552 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
553 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
554 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE,
555 "Expecting invalid message because of 1-char displayName.");
559 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
560 dependsOnMethods = {"read"})
561 public void readNonExistent(String testName) {
563 if (logger.isDebugEnabled()) {
564 logger.debug(testBanner(testName, CLASS_NAME));
567 setupReadNonExistent();
569 // Submit the request to the service and store the response.
570 VocabularyClient client = new VocabularyClient();
571 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
572 int statusCode = res.getStatus();
574 // Check the status code of the response: does it match
575 // the expected response(s)?
576 if (logger.isDebugEnabled()) {
577 logger.debug(testName + ": status = " + statusCode);
579 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
580 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
581 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
584 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
585 dependsOnMethods = {"readItem", "readNonExistent"})
586 public void readItemNonExistent(String testName) {
588 if (logger.isDebugEnabled()) {
589 logger.debug(testBanner(testName, CLASS_NAME));
592 setupReadNonExistent();
594 // Submit the request to the service and store the response.
595 VocabularyClient client = new VocabularyClient();
596 ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
597 int statusCode = res.getStatus();
599 // Check the status code of the response: does it match
600 // the expected response(s)?
601 if (logger.isDebugEnabled()) {
602 logger.debug(testName + ": status = " + statusCode);
604 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
605 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
606 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
608 // ---------------------------------------------------------------
609 // CRUD tests : READ_LIST tests
610 // ---------------------------------------------------------------
614 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
615 dependsOnMethods = {"createList", "read"})
616 public void readList(String testName) throws Exception {
618 if (logger.isDebugEnabled()) {
619 logger.debug(testBanner(testName, CLASS_NAME));
624 // Submit the request to the service and store the response.
625 VocabularyClient client = new VocabularyClient();
626 ClientResponse<AbstractCommonList> res = client.readList();
627 assertStatusCode(res, testName);
628 AbstractCommonList list = res.getEntity();
630 // Optionally output additional data about list members for debugging.
631 if(logger.isTraceEnabled()){
632 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
636 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
637 dependsOnMethods = {"createList", "readItem"})
638 public void readItemList(String testName) {
639 readItemListInt(knownResourceId, null, testName);
642 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
643 dependsOnMethods = {"createList", "readItem"})
644 public void readItemListByName(String testName) {
645 readItemListInt(null, knownResourceShortIdentifer, testName);
648 private void readItemListInt(String vcsid, String shortId, String testName) {
653 // Submit the request to the service and store the response.
654 VocabularyClient client = new VocabularyClient();
655 ClientResponse<AbstractCommonList> res = null;
657 res = client.readItemList(vcsid, null, null);
658 } else if (shortId != null) {
659 res = client.readItemListForNamedAuthority(shortId, null, null);
661 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
663 assertStatusCode(res, testName);
664 AbstractCommonList list = res.getEntity();
666 List<AbstractCommonList.ListItem> items = list.getListItem();
667 int nItemsReturned = items.size();
668 long nItemsTotal = list.getTotalItems();
669 if (logger.isDebugEnabled()) {
670 logger.debug(" " + testName + ": Expected "
671 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
673 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
675 if(logger.isTraceEnabled()){
676 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
682 // ---------------------------------------------------------------
683 // CRUD tests : UPDATE tests
684 // ---------------------------------------------------------------
687 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
688 dependsOnMethods = {"read"})
689 public void update(String testName) throws Exception {
691 if (logger.isDebugEnabled()) {
692 logger.debug(testBanner(testName, CLASS_NAME));
697 // Retrieve the contents of a resource to update.
698 VocabularyClient client = new VocabularyClient();
699 ClientResponse<String> res =
700 client.read(knownResourceId);
701 assertStatusCode(res, testName);
703 if (logger.isDebugEnabled()) {
704 logger.debug("got Vocabulary to update with ID: " + knownResourceId);
706 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
707 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
708 client.getCommonPartName(), VocabulariesCommon.class);
709 Assert.assertNotNull(vocabulary);
711 // Update the contents of this resource.
712 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
713 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
714 if (logger.isDebugEnabled()) {
715 logger.debug("to be updated Vocabulary");
716 logger.debug(objectAsXmlString(vocabulary, VocabulariesCommon.class));
719 // Submit the updated resource to the service and store the response.
720 PoxPayloadOut output = new PoxPayloadOut(SERVICE_PAYLOAD_NAME);
722 PayloadOutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
723 commonPart.setLabel(client.getCommonPartName());
724 res = client.update(knownResourceId, output);
725 assertStatusCode(res, testName);
727 // Retrieve the updated resource and verify that its contents exist.
728 input = new PoxPayloadIn(res.getEntity());
729 VocabulariesCommon updatedVocabulary =
730 (VocabulariesCommon) extractPart(input,
731 client.getCommonPartName(), VocabulariesCommon.class);
732 Assert.assertNotNull(updatedVocabulary);
734 // Verify that the updated resource received the correct data.
735 Assert.assertEquals(updatedVocabulary.getDisplayName(),
736 vocabulary.getDisplayName(),
737 "Data in updated object did not match submitted data.");
740 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
741 dependsOnMethods = {"readItem", "update", "verifyIgnoredUpdateWithInAuthority"})
742 public void updateItem(String testName) throws Exception {
744 if (logger.isDebugEnabled()) {
745 logger.debug(testBanner(testName, CLASS_NAME));
750 // Retrieve the contents of a resource to update.
751 VocabularyClient client = new VocabularyClient();
752 ClientResponse<String> res =
753 client.readItem(knownResourceId, knownItemResourceId);
754 assertStatusCode(res, testName);
756 if (logger.isDebugEnabled()) {
757 logger.debug("got VocabularyItem to update with ID: "
758 + knownItemResourceId
759 + " in Vocab: " + knownResourceId);
761 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
762 VocabularyitemsCommon vocabularyItem = (VocabularyitemsCommon) extractPart(input,
763 client.getCommonPartItemName(), VocabularyitemsCommon.class);
764 Assert.assertNotNull(vocabularyItem);
766 // Update the contents of this resource.
767 vocabularyItem.setDisplayName("updated-" + vocabularyItem.getDisplayName());
768 if (logger.isDebugEnabled()) {
769 logger.debug("to be updated VocabularyItem");
770 logger.debug(objectAsXmlString(vocabularyItem,
771 VocabularyitemsCommon.class));
774 // Submit the updated resource to the service and store the response.
775 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
776 PayloadOutputPart commonPart = output.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
777 commonPart.setLabel(client.getCommonPartItemName());
778 res = client.updateItem(knownResourceId, knownItemResourceId, output);
779 assertStatusCode(res, testName);
781 // Retrieve the updated resource and verify that its contents exist.
782 input = new PoxPayloadIn(res.getEntity());
783 VocabularyitemsCommon updatedVocabularyItem =
784 (VocabularyitemsCommon) extractPart(input,
785 client.getCommonPartItemName(), VocabularyitemsCommon.class);
786 Assert.assertNotNull(updatedVocabularyItem);
788 // Verify that the updated resource received the correct data.
789 Assert.assertEquals(updatedVocabularyItem.getDisplayName(),
790 vocabularyItem.getDisplayName(),
791 "Data in updated VocabularyItem did not match submitted data.");
794 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
795 dependsOnMethods = {"readItem"})
796 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
798 if (logger.isDebugEnabled()) {
799 logger.debug(testBanner(testName, CLASS_NAME));
804 // Submit the request to the service and store the response.
805 VocabularyClient client = new VocabularyClient();
806 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
807 assertStatusCode(res, testName);
809 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
810 VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
811 client.getCommonPartItemName(), VocabularyitemsCommon.class);
812 Assert.assertNotNull(vitem);
813 // Try to Update with new parent vocab (use self, for test).
814 Assert.assertEquals(vitem.getInAuthority(),
816 "VocabularyItem inAuthority does not match knownResourceId.");
817 vitem.setInAuthority(knownItemResourceId);
819 // Submit the updated resource to the service and store the response.
820 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
821 PayloadOutputPart commonPart = output.addPart(vitem, MediaType.APPLICATION_XML_TYPE);
822 commonPart.setLabel(client.getCommonPartItemName());
823 res = client.updateItem(knownResourceId, knownItemResourceId, output);
824 assertStatusCode(res, testName);
826 // Retrieve the updated resource and verify that the parent did not change
827 res = client.readItem(knownResourceId, knownItemResourceId);
828 input = new PoxPayloadIn(res.getEntity());
829 VocabularyitemsCommon updatedVocabularyItem =
830 (VocabularyitemsCommon) extractPart(input,
831 client.getCommonPartItemName(), VocabularyitemsCommon.class);
832 Assert.assertNotNull(updatedVocabularyItem);
834 // Verify that the updated resource received the correct data.
835 Assert.assertEquals(updatedVocabularyItem.getInAuthority(),
837 "VocabularyItem allowed update to the parent (inAuthority).");
841 // Placeholders until the three tests below can be uncommented.
842 // See Issue CSPACE-401.
844 public void updateWithEmptyEntityBody(String testName) throws Exception {
848 public void updateWithMalformedXml(String testName) throws Exception {
852 public void updateWithWrongXmlSchema(String testName) throws Exception {
857 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
858 dependsOnMethods = {"create", "update", "testSubmitRequest"})
859 public void updateWithEmptyEntityBody(String testName) throws Exception {
861 if (logger.isDebugEnabled()) {
862 logger.debug(testBanner(testName, CLASS_NAME));
865 setupUpdateWithEmptyEntityBody();
867 // Submit the request to the service and store the response.
868 String method = REQUEST_TYPE.httpMethodName();
869 String url = getResourceURL(knownResourceId);
870 String mediaType = MediaType.APPLICATION_XML;
871 final String entity = "";
872 int statusCode = submitRequest(method, url, mediaType, entity);
874 // Check the status code of the response: does it match
875 // the expected response(s)?
876 if(logger.isDebugEnabled()){
877 logger.debug(testName + ": url=" + url +
878 " status=" + statusCode);
880 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
881 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
882 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
886 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
887 dependsOnMethods = {"create", "update", "testSubmitRequest"})
888 public void updateWithMalformedXml(String testName) throws Exception {
890 if (logger.isDebugEnabled()) {
891 logger.debug(testBanner(testName, CLASS_NAME));
894 setupUpdateWithMalformedXml();
896 // Submit the request to the service and store the response.
897 String method = REQUEST_TYPE.httpMethodName();
898 String url = getResourceURL(knownResourceId);
899 String mediaType = MediaType.APPLICATION_XML;
900 final String entity = MALFORMED_XML_DATA;
901 int statusCode = submitRequest(method, url, mediaType, entity);
903 // Check the status code of the response: does it match
904 // the expected response(s)?
905 if(logger.isDebugEnabled()){
906 logger.debug(testName + ": url=" + url +
907 " status=" + statusCode);
909 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
910 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
911 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
915 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
916 dependsOnMethods = {"create", "update", "testSubmitRequest"})
917 public void updateWithWrongXmlSchema(String testName) throws Exception {
919 if (logger.isDebugEnabled()) {
920 logger.debug(testBanner(testName, CLASS_NAME));
923 setupUpdateWithWrongXmlSchema();
925 // Submit the request to the service and store the response.
926 String method = REQUEST_TYPE.httpMethodName();
927 String url = getResourceURL(knownResourceId);
928 String mediaType = MediaType.APPLICATION_XML;
929 final String entity = WRONG_XML_SCHEMA_DATA;
930 int statusCode = submitRequest(method, url, mediaType, entity);
932 // Check the status code of the response: does it match
933 // the expected response(s)?
934 if(logger.isDebugEnabled()){
935 logger.debug("updateWithWrongXmlSchema: url=" + url +
936 " status=" + statusCode);
938 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
939 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
940 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
944 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
945 dependsOnMethods = {"update", "testSubmitRequest"})
946 public void updateNonExistent(String testName) throws Exception {
948 if (logger.isDebugEnabled()) {
949 logger.debug(testBanner(testName, CLASS_NAME));
952 setupUpdateNonExistent();
954 // Submit the request to the service and store the response.
955 // Note: The ID used in this 'create' call may be arbitrary.
956 // The only relevant ID may be the one used in update(), below.
957 VocabularyClient client = new VocabularyClient();
958 String displayName = "displayName-" + NON_EXISTENT_ID;
959 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
960 displayName, NON_EXISTENT_ID, client.getCommonPartName());
961 ClientResponse<String> res =
962 client.update(NON_EXISTENT_ID, multipart);
963 int statusCode = res.getStatus();
965 // Check the status code of the response: does it match
966 // the expected response(s)?
967 if (logger.isDebugEnabled()) {
968 logger.debug(testName + ": status = " + statusCode);
970 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
971 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
972 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
975 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
976 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
977 public void updateNonExistentItem(String testName) throws Exception {
979 if (logger.isDebugEnabled()) {
980 logger.debug(testBanner(testName, CLASS_NAME));
983 setupUpdateNonExistent();
985 // Submit the request to the service and store the response.
986 // Note: The ID used in this 'create' call may be arbitrary.
987 // The only relevant ID may be the one used in update(), below.
988 VocabularyClient client = new VocabularyClient();
989 HashMap<String, String> itemInfo = new HashMap<String, String>();
990 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
991 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
992 PoxPayloadOut multipart =
993 VocabularyClientUtils.createVocabularyItemInstance(
994 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
995 itemInfo, client.getCommonPartItemName());
996 ClientResponse<String> res =
997 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
998 int statusCode = res.getStatus();
1000 // Check the status code of the response: does it match
1001 // the expected response(s)?
1002 if (logger.isDebugEnabled()) {
1003 logger.debug(testName + ": status = " + statusCode);
1005 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1006 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1007 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1010 // ---------------------------------------------------------------
1011 // CRUD tests : DELETE tests
1012 // ---------------------------------------------------------------
1015 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1016 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "deleteItem"})
1017 public void delete(String testName) throws Exception {
1019 if (logger.isDebugEnabled()) {
1020 logger.debug(testBanner(testName, CLASS_NAME));
1025 // Submit the request to the service and store the response.
1026 VocabularyClient client = new VocabularyClient();
1027 ClientResponse<Response> res = client.delete(knownResourceId);
1028 int statusCode = res.getStatus();
1030 // Check the status code of the response: does it match
1031 // the expected response(s)?
1032 if (logger.isDebugEnabled()) {
1033 logger.debug(testName + ": status = " + statusCode);
1035 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1036 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1037 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1040 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1041 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
1042 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
1043 public void deleteItem(String testName) throws Exception {
1045 if (logger.isDebugEnabled()) {
1046 logger.debug(testBanner(testName, CLASS_NAME));
1051 // Submit the request to the service and store the response.
1052 VocabularyClient client = new VocabularyClient();
1053 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1054 int statusCode = res.getStatus();
1056 // Check the status code of the response: does it match
1057 // the expected response(s)?
1058 if (logger.isDebugEnabled()) {
1059 logger.debug("delete: status = " + statusCode);
1061 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1062 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1063 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1068 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1069 dependsOnMethods = {"delete"})
1070 public void deleteNonExistent(String testName) throws Exception {
1072 if (logger.isDebugEnabled()) {
1073 logger.debug(testBanner(testName, CLASS_NAME));
1076 setupDeleteNonExistent();
1078 // Submit the request to the service and store the response.
1079 VocabularyClient client = new VocabularyClient();
1080 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1081 int statusCode = res.getStatus();
1083 // Check the status code of the response: does it match
1084 // the expected response(s)?
1085 if (logger.isDebugEnabled()) {
1086 logger.debug(testName + ": status = " + statusCode);
1088 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1089 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1090 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1093 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1094 dependsOnMethods = {"deleteItem"})
1095 public void deleteNonExistentItem(String testName) {
1097 if (logger.isDebugEnabled()) {
1098 logger.debug(testBanner(testName, CLASS_NAME));
1101 setupDeleteNonExistent();
1103 // Submit the request to the service and store the response.
1104 VocabularyClient client = new VocabularyClient();
1105 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1106 int statusCode = res.getStatus();
1108 // Check the status code of the response: does it match
1109 // the expected response(s)?
1110 if (logger.isDebugEnabled()) {
1111 logger.debug(testName + ": status = " + statusCode);
1113 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1114 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1115 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1118 // ---------------------------------------------------------------
1119 // Utility tests : tests of code used in tests above
1120 // ---------------------------------------------------------------
1122 * Tests the code for manually submitting data that is used by several
1123 * of the methods above.
1125 @Test(dependsOnMethods = {"create", "read"})
1126 public void testSubmitRequest() {
1128 // Expected status code: 200 OK
1131 // Submit the request to the service and store the response.
1132 String method = ServiceRequestType.READ.httpMethodName();
1133 String url = getResourceURL(knownResourceId);
1134 int statusCode = submitRequest(method, url);
1136 // Check the status code of the response: does it match
1137 // the expected response(s)?
1138 if (logger.isDebugEnabled()) {
1139 logger.debug("testSubmitRequest: url=" + url
1140 + " status=" + statusCode);
1142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1146 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1147 public void testItemSubmitRequest() {
1149 // Expected status code: 200 OK
1150 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1152 // Submit the request to the service and store the response.
1153 String method = ServiceRequestType.READ.httpMethodName();
1154 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1155 int statusCode = submitRequest(method, url);
1157 // Check the status code of the response: does it match
1158 // the expected response(s)?
1159 if (logger.isDebugEnabled()) {
1160 logger.debug("testItemSubmitRequest: url=" + url
1161 + " status=" + statusCode);
1163 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1167 // ---------------------------------------------------------------
1168 // Cleanup of resources created during testing
1169 // ---------------------------------------------------------------
1171 * Deletes all resources created by tests, after all tests have been run.
1173 * This cleanup method will always be run, even if one or more tests fail.
1174 * For this reason, it attempts to remove all resources created
1175 * at any point during testing, even if some of those resources
1176 * may be expected to be deleted by certain tests.
1178 // @AfterClass(alwaysRun = true)
1179 // public void cleanUp() {
1180 // String noTest = System.getProperty("noTestCleanup");
1181 // if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1182 // if (logger.isDebugEnabled()) {
1183 // logger.debug("Skipping Cleanup phase ...");
1187 // if (logger.isDebugEnabled()) {
1188 // logger.debug("Cleaning up temporary resources created for testing ...");
1190 // VocabularyClient client = new VocabularyClient();
1191 // String vocabularyResourceId;
1192 // String vocabularyItemResourceId;
1193 // // Clean up vocabulary item resources.
1194 // for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1195 // vocabularyItemResourceId = entry.getKey();
1196 // vocabularyResourceId = entry.getValue();
1197 // // Note: Any non-success responses are ignored and not reported.
1198 // client.deleteItem(vocabularyResourceId, vocabularyItemResourceId).releaseConnection();
1200 // // Clean up vocabulary resources.
1201 // for (String resourceId : allResourceIdsCreated) {
1202 // // Note: Any non-success responses are ignored and not reported.
1203 // client.delete(resourceId).releaseConnection();
1208 // ---------------------------------------------------------------
1209 // Utility methods used by tests above
1210 // ---------------------------------------------------------------
1212 public String getServicePathComponent() {
1213 return SERVICE_PATH_COMPONENT;
1216 public String getServicePathItemsComponent() {
1217 return AuthorityClient.ITEMS;
1221 * Returns the root URL for a service.
1223 * This URL consists of a base URL for all services, followed by
1224 * a path component for the owning vocabulary, followed by the
1225 * path component for the items.
1227 * @return The root URL for a service.
1229 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1230 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
1234 * Returns the URL of a specific resource managed by a service, and
1235 * designated by an identifier (such as a universally unique ID, or UUID).
1237 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1239 * @return The URL of a specific resource managed by a service.
1241 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1242 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1246 protected String getServiceName() {
1247 return VocabularyClient.SERVICE_NAME;