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.CollectionSpaceClient;
29 import org.collectionspace.services.client.PayloadOutputPart;
30 import org.collectionspace.services.client.PoxPayloadIn;
31 import org.collectionspace.services.client.PoxPayloadOut;
32 import org.collectionspace.services.client.VocabularyClient;
33 import org.collectionspace.services.client.VocabularyClientUtils;
34 import org.collectionspace.services.vocabulary.VocabulariesCommon;
35 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
41 import javax.ws.rs.core.Response;
44 * VocabularyServiceTest, carries out tests against a
45 * deployed and running Vocabulary Service.
47 * $LastChangedRevision: 753 $
48 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
50 public class VocabularyServiceTest extends AbstractAuthorityServiceTest<VocabulariesCommon, VocabularyitemsCommon> {
52 private final String CLASS_NAME = VocabularyServiceTest.class.getName();
53 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
54 // Instance variables specific to this test.
55 final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
56 final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
57 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
61 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
64 protected CollectionSpaceClient getClientInstance() {
65 return new VocabularyClient();
69 protected String createItemInAuthority(String authorityId) {
72 VocabularyClient client = new VocabularyClient();
73 HashMap<String, String> itemInfo = new HashMap<String, String>();
74 String shortId = createIdentifier();
75 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
76 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
77 result = VocabularyClientUtils.createItemInVocabulary(authorityId,
78 null /*knownResourceRefName*/, itemInfo, client);
79 allResourceItemIdsCreated.put(result, authorityId);
84 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
85 dependsOnMethods = {"CRUDTests"})
86 public void createWithBadShortId(String testName) throws Exception {
87 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
89 // Submit the request to the service and store the response.
90 VocabularyClient client = new VocabularyClient();
91 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
92 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
93 Response res = client.create(multipart);
95 assertStatusCode(res, testName);
103 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
104 dependsOnMethods = {"createItem"})
105 public void createItemList(String testName) throws Exception {
106 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
107 for (int j = 0; j < nItemsToCreateInList; j++) {
108 createItemInAuthority(knownAuthorityWithItems);
112 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
113 dependsOnMethods = {"CRUDTests"})
114 public void createWithNonuniqueShortId(String testName) throws Exception {
115 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117 // Create a new vocabulary
118 VocabularyClient client = new VocabularyClient();
119 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
120 "Vocab with non-unique Short Id", "nonunique", client.getCommonPartName());
121 Response res = client.create(multipart);
123 assertStatusCode(res, testName);
124 String newId = extractId(res);
125 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
133 // Now try to create a duplicate, we should fail because we're using a non-unique short id
135 res = client.create(multipart);
137 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
138 } catch (AssertionError ae) {
139 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
140 // the vocabulary we just created.
141 String newId = extractId(res);
142 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
143 throw ae; // rethrow the exception
152 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
153 dependsOnMethods = {"authorityTests"})
154 public void createItemWithBadShortId(String testName) throws Exception {
155 setupCreateWithMalformedXml();
157 // Submit the request to the service and store the response.
158 VocabularyClient client = new VocabularyClient();
159 HashMap<String, String> itemInfo = new HashMap<String, String>();
160 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
161 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
162 PoxPayloadOut multipart =
163 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
164 itemInfo, client.getItemCommonPartName());
165 Response res = client.createItem(knownResourceId, multipart);
167 int statusCode = res.getStatus();
169 if (!testRequestType.isValidStatusCode(statusCode)) {
170 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
171 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
172 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
174 if (statusCode != testExpectedStatusCode) {
175 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
176 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
186 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
187 dependsOnMethods = {"updateItem"})
188 public void verifyIllegalItemDisplayName(String testName) throws Exception {
189 // Perform setup for read.
192 // Submit the request to the service and store the response.
193 VocabularyClient client = new VocabularyClient();
194 Response res = client.readItem(knownResourceId, knownItemResourceId);
195 VocabularyitemsCommon vitem = null;
197 assertStatusCode(res, testName);
198 // Check whether Person has expected displayName.
199 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
200 vitem = (VocabularyitemsCommon) extractPart(input,
201 client.getItemCommonPartName(), VocabularyitemsCommon.class);
202 Assert.assertNotNull(vitem);
209 // Try to Update with null displayName
211 setupUpdateWithInvalidBody();
212 vitem.setDisplayName(null);
213 // Submit the updated resource to the service and store the response.
214 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
215 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
216 res = client.updateItem(knownResourceId, knownItemResourceId, output);
218 assertStatusCode(res, testName);
225 // Now try to Update with 1-char displayName (too short)
227 setupUpdateWithInvalidBody();
228 vitem.setDisplayName("a");
229 // Submit the updated resource to the service and store the response.
230 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
231 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
232 res = client.updateItem(knownResourceId, knownItemResourceId, output);
234 assertStatusCode(res, testName);
242 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
243 public void localDelete(String testName) throws Exception {
244 super.delete(testName);
248 public void delete(String testName) throws Exception {
250 // This overrides the base test. We don't want to do anything at this point
251 // in the test suite. See the localDelete() method for the actual "delete" test
256 public void deleteItem(String testName) throws Exception {
257 //Do nothing. We don't want to delete the known item until all the dependencies of the
258 // localDeleteItem() test have been fulfilled.
261 @Test(dataProvider = "testName",
262 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
263 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
264 public void localDeleteItem(String testName) throws Exception {
265 super.deleteItem(testName);
269 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
270 * refer to this method in their @Test annotation declarations.
273 @Test(dataProvider = "testName",
275 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
276 public void authorityTests(String testName) {
277 // This method only exists as a dependency target for TestNG
280 // ---------------------------------------------------------------
281 // Vocabulary test specific overrides
282 // ---------------------------------------------------------------
285 public String getServicePathComponent() {
286 return SERVICE_PATH_COMPONENT;
290 protected String getServiceName() {
291 return VocabularyClient.SERVICE_NAME;
295 protected PoxPayloadOut createInstance(String commonPartName,
297 String displayName = "displayName-" + identifier;
298 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
299 displayName, identifier, commonPartName);
304 protected PoxPayloadOut createInstance(String identifier) {
305 VocabularyClient client = new VocabularyClient();
306 return createInstance(client.getCommonPartName(), identifier);
310 protected VocabulariesCommon updateInstance(
311 VocabulariesCommon vocabulariesCommon) {
312 VocabulariesCommon result = new VocabulariesCommon();
314 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
315 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
321 protected void compareUpdatedInstances(VocabulariesCommon original,
322 VocabulariesCommon updated) throws Exception {
323 Assert.assertEquals(updated.getDisplayName(),
324 original.getDisplayName(),
325 "Display name in updated object did not match submitted data.");
326 Assert.assertEquals(updated.getVocabType(),
327 original.getVocabType(),
328 "Vocabulary tyype name in updated object did not match submitted data.");
332 // Vocabulary item specific overrides
336 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
337 String headerLabel = new VocabularyClient().getItemCommonPartName();
338 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
339 String shortId = createIdentifier();
340 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
341 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
343 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
347 protected VocabularyitemsCommon updateItemInstance(
348 VocabularyitemsCommon authorityItem) {
349 VocabularyitemsCommon result = new VocabularyitemsCommon();
350 result.setDisplayName("updated-" + authorityItem.getDisplayName());
355 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
356 VocabularyitemsCommon updated) throws Exception {
357 Assert.assertEquals(updated.getDisplayName(),
358 original.getDisplayName(),
359 "Display name in updated VocabularyItem did not match submitted data.");
363 protected void verifyReadItemInstance(VocabularyitemsCommon item)
365 // TODO Auto-generated method stub
370 protected PoxPayloadOut createNonExistenceItemInstance(
371 String commonPartName, String identifier) {
372 HashMap<String, String> itemInfo = new HashMap<String, String>();
373 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
374 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
375 PoxPayloadOut result =
376 VocabularyClientUtils.createVocabularyItemInstance(
377 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
378 itemInfo, commonPartName);