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 VocabularyServiceTest() {
66 TEST_SHORTID = "vocabTest";
70 protected String getTestAuthorityItemShortId() {
71 return getTestAuthorityItemShortId(true); // The short ID of every person item we create should be unique
75 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
78 protected CollectionSpaceClient getClientInstance() {
79 return new VocabularyClient();
83 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
84 return new VocabularyClient(clientPropertiesFilename);
88 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
91 HashMap<String, String> itemInfo = new HashMap<String, String>();
92 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
93 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
94 result = VocabularyClientUtils.createItemInVocabulary(authorityId,
95 null /*knownResourceRefName*/, itemInfo, (VocabularyClient) client);
96 allResourceItemIdsCreated.put(result, authorityId);
101 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
102 dependsOnMethods = {"CRUDTests"})
103 public void createWithBadShortId(String testName) throws Exception {
104 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
106 // Submit the request to the service and store the response.
107 VocabularyClient client = new VocabularyClient();
108 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
109 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
110 Response res = client.create(multipart);
112 assertStatusCode(res, testName);
121 public void createItemList(String testName) throws Exception {
122 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
123 for (int j = 0; j < nItemsToCreateInList; j++) {
124 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, getTestAuthorityItemShortId(true));
128 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
129 dependsOnMethods = {"CRUDTests"})
130 public void createWithNonuniqueShortId(String testName) throws Exception {
131 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
133 // Create a new vocabulary
134 VocabularyClient client = new VocabularyClient();
135 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
136 "Vocab with non-unique Short Id", "nonunique", client.getCommonPartName());
137 Response res = client.create(multipart);
139 assertStatusCode(res, testName);
140 String newId = extractId(res);
141 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
149 // Now try to create a duplicate, we should fail because we're using a non-unique short id
151 res = client.create(multipart);
153 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
154 } catch (AssertionError ae) {
155 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
156 // the vocabulary we just created.
157 String newId = extractId(res);
158 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
159 throw ae; // rethrow the exception
168 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
169 dependsOnMethods = {"authorityTests"})
170 public void createItemWithBadShortId(String testName) throws Exception {
171 setupCreateWithMalformedXml();
173 // Submit the request to the service and store the response.
174 VocabularyClient client = new VocabularyClient();
175 HashMap<String, String> itemInfo = new HashMap<String, String>();
176 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
177 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
178 PoxPayloadOut multipart =
179 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
180 itemInfo, client.getItemCommonPartName());
181 Response res = client.createItem(knownResourceId, multipart);
183 int statusCode = res.getStatus();
185 if (!testRequestType.isValidStatusCode(statusCode)) {
186 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
187 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
188 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
190 if (statusCode != testExpectedStatusCode) {
191 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
192 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
202 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
203 dependsOnMethods = {"updateItem"})
204 public void verifyIllegalItemDisplayName(String testName) throws Exception {
205 // Perform setup for read.
208 // Submit the request to the service and store the response.
209 VocabularyClient client = new VocabularyClient();
210 Response res = client.readItem(knownResourceId, knownItemResourceId);
211 VocabularyitemsCommon vitem = null;
213 assertStatusCode(res, testName);
214 // Check whether Person has expected displayName.
215 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
216 vitem = (VocabularyitemsCommon) extractPart(input,
217 client.getItemCommonPartName(), VocabularyitemsCommon.class);
218 Assert.assertNotNull(vitem);
225 // Try to Update with null displayName
227 setupUpdateWithInvalidBody();
228 vitem.setDisplayName(null);
229 // Submit the updated resource to the service and store the response.
230 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
231 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
232 res = client.updateItem(knownResourceId, knownItemResourceId, output);
234 assertStatusCode(res, testName);
241 // Now try to Update with 1-char displayName (too short)
243 setupUpdateWithInvalidBody();
244 vitem.setDisplayName("a");
245 // Submit the updated resource to the service and store the response.
246 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
247 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
248 res = client.updateItem(knownResourceId, knownItemResourceId, output);
250 assertStatusCode(res, testName);
258 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
259 public void localDelete(String testName) throws Exception {
260 super.delete(testName);
264 public void delete(String testName) throws Exception {
266 // This overrides the base test. We don't want to do anything at this point
267 // in the test suite. See the localDelete() method for the actual "delete" test
272 public void deleteItem(String testName) throws Exception {
273 //Do nothing. We don't want to delete the known item until all the dependencies of the
274 // localDeleteItem() test have been fulfilled.
277 @Test(dataProvider = "testName",
278 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
279 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
280 public void localDeleteItem(String testName) throws Exception {
281 super.deleteItem(testName);
285 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
286 * refer to this method in their @Test annotation declarations.
289 @Test(dataProvider = "testName",
291 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
292 public void authorityTests(String testName) {
293 // This method only exists as a dependency target for TestNG
296 // ---------------------------------------------------------------
297 // Vocabulary test specific overrides
298 // ---------------------------------------------------------------
301 public String getServicePathComponent() {
302 return SERVICE_PATH_COMPONENT;
306 protected String getServiceName() {
307 return VocabularyClient.SERVICE_NAME;
311 protected PoxPayloadOut createInstance(String commonPartName,
313 String displayName = "displayName-" + identifier;
314 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
315 displayName, identifier, commonPartName);
320 protected PoxPayloadOut createInstance(String identifier) {
321 VocabularyClient client = new VocabularyClient();
322 return createInstance(client.getCommonPartName(), identifier);
326 protected VocabulariesCommon updateInstance(
327 VocabulariesCommon vocabulariesCommon) {
328 VocabulariesCommon result = new VocabulariesCommon();
330 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
331 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
337 protected void compareUpdatedInstances(VocabulariesCommon original,
338 VocabulariesCommon updated) throws Exception {
339 Assert.assertEquals(updated.getDisplayName(),
340 original.getDisplayName(),
341 "Display name in updated object did not match submitted data.");
342 Assert.assertEquals(updated.getVocabType(),
343 original.getVocabType(),
344 "Vocabulary tyype name in updated object did not match submitted data.");
348 // Vocabulary item specific overrides
352 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
353 String headerLabel = new VocabularyClient().getItemCommonPartName();
354 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
355 String shortId = identifier;
356 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
357 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
359 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
363 protected VocabularyitemsCommon updateItemInstance(
364 VocabularyitemsCommon authorityItem) {
365 VocabularyitemsCommon result = new VocabularyitemsCommon();
366 result.setDisplayName("updated-" + authorityItem.getDisplayName());
371 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
372 VocabularyitemsCommon updated) throws Exception {
373 Assert.assertEquals(updated.getDisplayName(),
374 original.getDisplayName(),
375 "Display name in updated VocabularyItem did not match submitted data.");
379 protected void verifyReadItemInstance(VocabularyitemsCommon item)
381 // TODO Auto-generated method stub
386 protected PoxPayloadOut createNonExistenceItemInstance(
387 String commonPartName, String identifier) {
388 HashMap<String, String> itemInfo = new HashMap<String, String>();
389 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
390 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
391 PoxPayloadOut result =
392 VocabularyClientUtils.createVocabularyItemInstance(
393 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
394 itemInfo, commonPartName);