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"})
106 public void createItemList(String testName) throws Exception {
107 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
108 for (int j = 0; j < nItemsToCreateInList; j++) {
109 createItemInAuthority(knownAuthorityWithItems);
113 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
114 dependsOnMethods = {"CRUDTests"})
115 public void createWithNonuniqueShortId(String testName) throws Exception {
116 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
118 // Create a new vocabulary
119 VocabularyClient client = new VocabularyClient();
120 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
121 "Vocab with non-unique Short Id", "nonunique", client.getCommonPartName());
122 Response res = client.create(multipart);
124 assertStatusCode(res, testName);
125 String newId = extractId(res);
126 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
134 // Now try to create a duplicate, we should fail because we're using a non-unique short id
136 res = client.create(multipart);
138 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
139 } catch (AssertionError ae) {
140 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
141 // the vocabulary we just created.
142 String newId = extractId(res);
143 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
144 throw ae; // rethrow the exception
153 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
154 dependsOnMethods = {"authorityTests"})
155 public void createItemWithBadShortId(String testName) throws Exception {
156 setupCreateWithMalformedXml();
158 // Submit the request to the service and store the response.
159 VocabularyClient client = new VocabularyClient();
160 HashMap<String, String> itemInfo = new HashMap<String, String>();
161 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
162 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
163 PoxPayloadOut multipart =
164 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
165 itemInfo, client.getItemCommonPartName());
166 Response res = client.createItem(knownResourceId, multipart);
168 int statusCode = res.getStatus();
170 if (!testRequestType.isValidStatusCode(statusCode)) {
171 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
172 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
173 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
175 if (statusCode != testExpectedStatusCode) {
176 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
177 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
187 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
188 dependsOnMethods = {"updateItem"})
189 public void verifyIllegalItemDisplayName(String testName) throws Exception {
190 // Perform setup for read.
193 // Submit the request to the service and store the response.
194 VocabularyClient client = new VocabularyClient();
195 Response res = client.readItem(knownResourceId, knownItemResourceId);
196 VocabularyitemsCommon vitem = null;
198 assertStatusCode(res, testName);
199 // Check whether Person has expected displayName.
200 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
201 vitem = (VocabularyitemsCommon) extractPart(input,
202 client.getItemCommonPartName(), VocabularyitemsCommon.class);
203 Assert.assertNotNull(vitem);
210 // Try to Update with null displayName
212 setupUpdateWithInvalidBody();
213 vitem.setDisplayName(null);
214 // Submit the updated resource to the service and store the response.
215 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
216 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
217 res = client.updateItem(knownResourceId, knownItemResourceId, output);
219 assertStatusCode(res, testName);
226 // Now try to Update with 1-char displayName (too short)
228 setupUpdateWithInvalidBody();
229 vitem.setDisplayName("a");
230 // Submit the updated resource to the service and store the response.
231 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
232 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
233 res = client.updateItem(knownResourceId, knownItemResourceId, output);
235 assertStatusCode(res, testName);
243 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
244 public void localDelete(String testName) throws Exception {
245 super.delete(testName);
249 public void delete(String testName) throws Exception {
251 // This overrides the base test. We don't want to do anything at this point
252 // in the test suite. See the localDelete() method for the actual "delete" test
257 public void deleteItem(String testName) throws Exception {
258 //Do nothing. We don't want to delete the known item until all the dependencies of the
259 // localDeleteItem() test have been fulfilled.
262 @Test(dataProvider = "testName",
263 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
264 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
265 public void localDeleteItem(String testName) throws Exception {
266 super.deleteItem(testName);
270 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
271 * refer to this method in their @Test annotation declarations.
274 @Test(dataProvider = "testName",
276 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
277 public void authorityTests(String testName) {
278 // This method only exists as a dependency target for TestNG
281 // ---------------------------------------------------------------
282 // Vocabulary test specific overrides
283 // ---------------------------------------------------------------
286 public String getServicePathComponent() {
287 return SERVICE_PATH_COMPONENT;
291 protected String getServiceName() {
292 return VocabularyClient.SERVICE_NAME;
296 protected PoxPayloadOut createInstance(String commonPartName,
298 String displayName = "displayName-" + identifier;
299 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
300 displayName, identifier, commonPartName);
305 protected PoxPayloadOut createInstance(String identifier) {
306 VocabularyClient client = new VocabularyClient();
307 return createInstance(client.getCommonPartName(), identifier);
311 protected VocabulariesCommon updateInstance(
312 VocabulariesCommon vocabulariesCommon) {
313 VocabulariesCommon result = new VocabulariesCommon();
315 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
316 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
322 protected void compareUpdatedInstances(VocabulariesCommon original,
323 VocabulariesCommon updated) throws Exception {
324 Assert.assertEquals(updated.getDisplayName(),
325 original.getDisplayName(),
326 "Display name in updated object did not match submitted data.");
327 Assert.assertEquals(updated.getVocabType(),
328 original.getVocabType(),
329 "Vocabulary tyype name in updated object did not match submitted data.");
333 // Vocabulary item specific overrides
337 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
338 String headerLabel = new VocabularyClient().getItemCommonPartName();
339 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
340 String shortId = createIdentifier();
341 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
342 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
344 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
348 protected VocabularyitemsCommon updateItemInstance(
349 VocabularyitemsCommon authorityItem) {
350 VocabularyitemsCommon result = new VocabularyitemsCommon();
351 result.setDisplayName("updated-" + authorityItem.getDisplayName());
356 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
357 VocabularyitemsCommon updated) throws Exception {
358 Assert.assertEquals(updated.getDisplayName(),
359 original.getDisplayName(),
360 "Display name in updated VocabularyItem did not match submitted data.");
364 protected void verifyReadItemInstance(VocabularyitemsCommon item)
366 // TODO Auto-generated method stub
371 protected PoxPayloadOut createNonExistenceItemInstance(
372 String commonPartName, String identifier) {
373 HashMap<String, String> itemInfo = new HashMap<String, String>();
374 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
375 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
376 PoxPayloadOut result =
377 VocabularyClientUtils.createVocabularyItemInstance(
378 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
379 itemInfo, commonPartName);