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.HashMap;
27 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
28 import org.collectionspace.services.client.AuthorityClient;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.client.VocabularyClient;
34 import org.collectionspace.services.client.VocabularyClientUtils;
35 import org.collectionspace.services.vocabulary.VocabulariesCommon;
36 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
42 import javax.ws.rs.core.Response;
45 * VocabularyServiceTest, carries out tests against a
46 * deployed and running Vocabulary Service.
48 * $LastChangedRevision: 753 $
49 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
51 public class VocabularyServiceTest extends AbstractAuthorityServiceTest<VocabulariesCommon, VocabularyitemsCommon> {
53 private final String CLASS_NAME = VocabularyServiceTest.class.getName();
54 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
55 // Instance variables specific to this test.
56 final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
57 final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
58 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
62 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
65 protected CollectionSpaceClient getClientInstance() {
66 return new VocabularyClient();
70 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
71 return new VocabularyClient(clientPropertiesFilename);
75 protected String createItemInAuthority(AuthorityClient client, String authorityId) {
78 HashMap<String, String> itemInfo = new HashMap<String, String>();
79 String shortId = createIdentifier();
80 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
81 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
82 result = VocabularyClientUtils.createItemInVocabulary(authorityId,
83 null /*knownResourceRefName*/, itemInfo, (VocabularyClient) client);
84 allResourceItemIdsCreated.put(result, authorityId);
89 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
90 dependsOnMethods = {"CRUDTests"})
91 public void createWithBadShortId(String testName) throws Exception {
92 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
94 // Submit the request to the service and store the response.
95 VocabularyClient client = new VocabularyClient();
96 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
97 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
98 Response res = client.create(multipart);
100 assertStatusCode(res, testName);
109 public void createItemList(String testName) throws Exception {
110 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
111 for (int j = 0; j < nItemsToCreateInList; j++) {
112 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems);
116 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
117 dependsOnMethods = {"CRUDTests"})
118 public void createWithNonuniqueShortId(String testName) throws Exception {
119 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
121 // Create a new vocabulary
122 VocabularyClient client = new VocabularyClient();
123 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
124 "Vocab with non-unique Short Id", "nonunique", client.getCommonPartName());
125 Response res = client.create(multipart);
127 assertStatusCode(res, testName);
128 String newId = extractId(res);
129 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
137 // Now try to create a duplicate, we should fail because we're using a non-unique short id
139 res = client.create(multipart);
141 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
142 } catch (AssertionError ae) {
143 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
144 // the vocabulary we just created.
145 String newId = extractId(res);
146 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
147 throw ae; // rethrow the exception
156 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
157 dependsOnMethods = {"authorityTests"})
158 public void createItemWithBadShortId(String testName) throws Exception {
159 setupCreateWithMalformedXml();
161 // Submit the request to the service and store the response.
162 VocabularyClient client = new VocabularyClient();
163 HashMap<String, String> itemInfo = new HashMap<String, String>();
164 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
165 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
166 PoxPayloadOut multipart =
167 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
168 itemInfo, client.getItemCommonPartName());
169 Response res = client.createItem(knownResourceId, multipart);
171 int statusCode = res.getStatus();
173 if (!testRequestType.isValidStatusCode(statusCode)) {
174 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
175 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
176 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
178 if (statusCode != testExpectedStatusCode) {
179 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
180 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
190 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
191 dependsOnMethods = {"updateItem"})
192 public void verifyIllegalItemDisplayName(String testName) throws Exception {
193 // Perform setup for read.
196 // Submit the request to the service and store the response.
197 VocabularyClient client = new VocabularyClient();
198 Response res = client.readItem(knownResourceId, knownItemResourceId);
199 VocabularyitemsCommon vitem = null;
201 assertStatusCode(res, testName);
202 // Check whether Person has expected displayName.
203 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
204 vitem = (VocabularyitemsCommon) extractPart(input,
205 client.getItemCommonPartName(), VocabularyitemsCommon.class);
206 Assert.assertNotNull(vitem);
213 // Try to Update with null displayName
215 setupUpdateWithInvalidBody();
216 vitem.setDisplayName(null);
217 // Submit the updated resource to the service and store the response.
218 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
219 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
220 res = client.updateItem(knownResourceId, knownItemResourceId, output);
222 assertStatusCode(res, testName);
229 // Now try to Update with 1-char displayName (too short)
231 setupUpdateWithInvalidBody();
232 vitem.setDisplayName("a");
233 // Submit the updated resource to the service and store the response.
234 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
235 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
236 res = client.updateItem(knownResourceId, knownItemResourceId, output);
238 assertStatusCode(res, testName);
246 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
247 public void localDelete(String testName) throws Exception {
248 super.delete(testName);
252 public void delete(String testName) throws Exception {
254 // This overrides the base test. We don't want to do anything at this point
255 // in the test suite. See the localDelete() method for the actual "delete" test
260 public void deleteItem(String testName) throws Exception {
261 //Do nothing. We don't want to delete the known item until all the dependencies of the
262 // localDeleteItem() test have been fulfilled.
265 @Test(dataProvider = "testName",
266 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
267 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
268 public void localDeleteItem(String testName) throws Exception {
269 super.deleteItem(testName);
273 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
274 * refer to this method in their @Test annotation declarations.
277 @Test(dataProvider = "testName",
279 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
280 public void authorityTests(String testName) {
281 // This method only exists as a dependency target for TestNG
284 // ---------------------------------------------------------------
285 // Vocabulary test specific overrides
286 // ---------------------------------------------------------------
289 public String getServicePathComponent() {
290 return SERVICE_PATH_COMPONENT;
294 protected String getServiceName() {
295 return VocabularyClient.SERVICE_NAME;
299 protected PoxPayloadOut createInstance(String commonPartName,
301 String displayName = "displayName-" + identifier;
302 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
303 displayName, identifier, commonPartName);
308 protected PoxPayloadOut createInstance(String identifier) {
309 VocabularyClient client = new VocabularyClient();
310 return createInstance(client.getCommonPartName(), identifier);
314 protected VocabulariesCommon updateInstance(
315 VocabulariesCommon vocabulariesCommon) {
316 VocabulariesCommon result = new VocabulariesCommon();
318 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
319 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
325 protected void compareUpdatedInstances(VocabulariesCommon original,
326 VocabulariesCommon updated) throws Exception {
327 Assert.assertEquals(updated.getDisplayName(),
328 original.getDisplayName(),
329 "Display name in updated object did not match submitted data.");
330 Assert.assertEquals(updated.getVocabType(),
331 original.getVocabType(),
332 "Vocabulary tyype name in updated object did not match submitted data.");
336 // Vocabulary item specific overrides
340 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
341 String headerLabel = new VocabularyClient().getItemCommonPartName();
342 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
343 String shortId = identifier;
344 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
345 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
347 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
351 protected VocabularyitemsCommon updateItemInstance(
352 VocabularyitemsCommon authorityItem) {
353 VocabularyitemsCommon result = new VocabularyitemsCommon();
354 result.setDisplayName("updated-" + authorityItem.getDisplayName());
359 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
360 VocabularyitemsCommon updated) throws Exception {
361 Assert.assertEquals(updated.getDisplayName(),
362 original.getDisplayName(),
363 "Display name in updated VocabularyItem did not match submitted data.");
367 protected void verifyReadItemInstance(VocabularyitemsCommon item)
369 // TODO Auto-generated method stub
374 protected PoxPayloadOut createNonExistenceItemInstance(
375 String commonPartName, String identifier) {
376 HashMap<String, String> itemInfo = new HashMap<String, String>();
377 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
378 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
379 PoxPayloadOut result =
380 VocabularyClientUtils.createVocabularyItemInstance(
381 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
382 itemInfo, commonPartName);