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;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
43 import javax.ws.rs.core.Response;
46 * VocabularyServiceTest, carries out tests against a
47 * deployed and running Vocabulary Service.
49 * $LastChangedRevision: 753 $
50 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
52 public class VocabularyServiceTest extends AbstractAuthorityServiceTest<VocabulariesCommon, VocabularyitemsCommon> {
54 // The class for logging
55 private final Logger logger = LoggerFactory.getLogger(VocabularyServiceTest.class);
57 // Instance variables specific to this test.
58 final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
59 final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
60 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
63 * Default constructor. Used to set the short ID for all tests authority items
65 public VocabularyServiceTest() {
67 TEST_SHORTID = "vocabTest";
71 protected String getTestAuthorityItemShortId() {
72 return getTestAuthorityItemShortId(true); // The short ID of every person item we create should be unique
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79 protected CollectionSpaceClient getClientInstance() {
80 return new VocabularyClient();
84 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
85 return new VocabularyClient(clientPropertiesFilename);
89 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
92 HashMap<String, String> itemInfo = new HashMap<String, String>();
93 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
94 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
95 result = VocabularyClientUtils.createItemInVocabulary(authorityId,
96 null /*knownResourceRefName*/, itemInfo, (VocabularyClient) client);
97 allResourceItemIdsCreated.put(result, authorityId);
102 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
103 dependsOnMethods = {"CRUDTests"})
104 public void createWithBadShortId(String testName) throws Exception {
105 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
107 // Submit the request to the service and store the response.
108 VocabularyClient client = new VocabularyClient();
109 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
110 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
111 Response res = client.create(multipart);
113 assertStatusCode(res, testName);
122 public void createItemList(String testName) throws Exception {
123 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
124 for (int j = 0; j < nItemsToCreateInList; j++) {
125 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, getTestAuthorityItemShortId(true));
129 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
130 dependsOnMethods = {"CRUDTests"})
131 public void createWithNonuniqueShortId(String testName) throws Exception {
132 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
134 // Create a new vocabulary
135 String shortId = "nonunique" + random.nextInt(1000); // Prevent collisions with past test sessions that never cleaned up properly
136 VocabularyClient client = new VocabularyClient();
137 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
138 "Vocab with non-unique Short Id", shortId, client.getCommonPartName());
139 Response res = client.create(multipart);
141 assertStatusCode(res, testName);
142 String newId = extractId(res);
143 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
151 // Now try to create a duplicate, we should fail because we're using a non-unique short id
153 res = client.create(multipart);
155 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
156 } catch (AssertionError ae) {
157 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
158 // the vocabulary we just created.
159 String newId = extractId(res);
160 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
161 throw ae; // rethrow the exception
170 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
171 dependsOnMethods = {"authorityTests"})
172 public void createItemWithBadShortId(String testName) throws Exception {
173 setupCreateWithMalformedXml();
175 // Submit the request to the service and store the response.
176 VocabularyClient client = new VocabularyClient();
177 HashMap<String, String> itemInfo = new HashMap<String, String>();
178 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
179 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
180 PoxPayloadOut multipart =
181 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
182 itemInfo, client.getItemCommonPartName());
183 Response res = client.createItem(knownResourceId, multipart);
185 int statusCode = res.getStatus();
187 if (!testRequestType.isValidStatusCode(statusCode)) {
188 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
189 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
190 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
192 if (statusCode != testExpectedStatusCode) {
193 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
194 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
204 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
205 dependsOnMethods = {"updateItem"})
206 public void verifyIllegalItemDisplayName(String testName) throws Exception {
207 // Perform setup for read.
210 // Submit the request to the service and store the response.
211 VocabularyClient client = new VocabularyClient();
212 Response res = client.readItem(knownResourceId, knownItemResourceId);
213 VocabularyitemsCommon vitem = null;
215 assertStatusCode(res, testName);
216 // Check whether Person has expected displayName.
217 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
218 vitem = (VocabularyitemsCommon) extractPart(input,
219 client.getItemCommonPartName(), VocabularyitemsCommon.class);
220 Assert.assertNotNull(vitem);
227 // Try to Update with null displayName
229 setupUpdateWithInvalidBody();
230 vitem.setDisplayName(null);
231 // Submit the updated resource to the service and store the response.
232 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
233 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
234 res = client.updateItem(knownResourceId, knownItemResourceId, output);
236 assertStatusCode(res, testName);
243 // Now try to Update with 1-char displayName (too short)
245 setupUpdateWithInvalidBody();
246 vitem.setDisplayName("a");
247 // Submit the updated resource to the service and store the response.
248 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
249 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
250 res = client.updateItem(knownResourceId, knownItemResourceId, output);
252 assertStatusCode(res, testName);
260 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
261 public void localDelete(String testName) throws Exception {
262 super.delete(testName);
266 public void delete(String testName) throws Exception {
268 // This overrides the base test. We don't want to do anything at this point
269 // in the test suite. See the localDelete() method for the actual "delete" test
274 public void deleteItem(String testName) throws Exception {
275 //Do nothing. We don't want to delete the known item until all the dependencies of the
276 // localDeleteItem() test have been fulfilled.
279 @Test(dataProvider = "testName",
280 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
281 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
282 public void localDeleteItem(String testName) throws Exception {
283 super.deleteItem(testName);
287 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
288 * refer to this method in their @Test annotation declarations.
291 @Test(dataProvider = "testName",
293 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
294 public void authorityTests(String testName) {
295 // This method only exists as a dependency target for TestNG
298 // ---------------------------------------------------------------
299 // Vocabulary test specific overrides
300 // ---------------------------------------------------------------
303 public String getServicePathComponent() {
304 return SERVICE_PATH_COMPONENT;
308 protected String getServiceName() {
309 return VocabularyClient.SERVICE_NAME;
313 protected PoxPayloadOut createInstance(String commonPartName,
315 String displayName = "displayName-" + identifier;
316 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
317 displayName, identifier, commonPartName);
322 protected PoxPayloadOut createInstance(String identifier) {
323 VocabularyClient client = new VocabularyClient();
324 return createInstance(client.getCommonPartName(), identifier);
328 protected VocabulariesCommon updateInstance(
329 VocabulariesCommon vocabulariesCommon) {
330 VocabulariesCommon result = new VocabulariesCommon();
332 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
333 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
339 protected void compareUpdatedInstances(VocabulariesCommon original,
340 VocabulariesCommon updated) throws Exception {
341 Assert.assertEquals(updated.getDisplayName(),
342 original.getDisplayName(),
343 "Display name in updated object did not match submitted data.");
344 Assert.assertEquals(updated.getVocabType(),
345 original.getVocabType(),
346 "Vocabulary tyype name in updated object did not match submitted data.");
350 // Vocabulary item specific overrides
354 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
355 String headerLabel = new VocabularyClient().getItemCommonPartName();
356 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
357 String shortId = identifier;
358 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
359 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
361 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
365 protected VocabularyitemsCommon updateItemInstance(
366 VocabularyitemsCommon authorityItem) {
367 VocabularyitemsCommon result = new VocabularyitemsCommon();
368 result.setDisplayName("updated-" + authorityItem.getDisplayName());
373 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
374 VocabularyitemsCommon updated) throws Exception {
375 Assert.assertEquals(updated.getDisplayName(),
376 original.getDisplayName(),
377 "Display name in updated VocabularyItem did not match submitted data.");
381 protected void verifyReadItemInstance(VocabularyitemsCommon item)
383 // TODO Auto-generated method stub
388 protected PoxPayloadOut createNonExistenceItemInstance(
389 String commonPartName, String identifier) {
390 HashMap<String, String> itemInfo = new HashMap<String, String>();
391 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
392 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
393 PoxPayloadOut result =
394 VocabularyClientUtils.createVocabularyItemInstance(
395 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
396 itemInfo, commonPartName);