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.AfterClass;
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() throws Exception {
80 return new VocabularyClient();
84 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
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);
121 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
122 dependsOnMethods = {"CRUDTests"})
123 public void createWithNonuniqueShortId(String testName) throws Exception {
124 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
126 // Create a new vocabulary
127 String shortId = "nonunique" + random.nextInt(1000); // Prevent collisions with past test sessions that never cleaned up properly
128 VocabularyClient client = new VocabularyClient();
129 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
130 "Vocab with non-unique Short Id", shortId, client.getCommonPartName());
131 Response res = client.create(multipart);
133 assertStatusCode(res, testName);
134 String newId = extractId(res);
135 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
143 // Now try to create a duplicate, we should fail because we're using a non-unique short id
145 res = client.create(multipart);
147 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
148 } catch (AssertionError ae) {
149 // We expected a failure, but we didn't get it. Therefore, we need to cleanup
150 // the vocabulary we just created.
151 String newId = extractId(res);
152 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
153 throw ae; // rethrow the exception
162 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
163 dependsOnMethods = {"authorityTests"})
164 public void createItemWithBadShortId(String testName) throws Exception {
165 setupCreateWithMalformedXml();
167 // Submit the request to the service and store the response.
168 VocabularyClient client = new VocabularyClient();
169 HashMap<String, String> itemInfo = new HashMap<String, String>();
170 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
171 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
172 PoxPayloadOut multipart =
173 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
174 itemInfo, client.getItemCommonPartName());
175 Response res = client.createItem(knownResourceId, multipart);
177 int statusCode = res.getStatus();
179 if (!testRequestType.isValidStatusCode(statusCode)) {
180 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
181 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
182 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
184 if (statusCode != testExpectedStatusCode) {
185 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
186 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
196 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
197 dependsOnMethods = {"updateItem"})
198 public void verifyIllegalItemDisplayName(String testName) throws Exception {
199 // Perform setup for read.
202 // Submit the request to the service and store the response.
203 VocabularyClient client = new VocabularyClient();
204 Response res = client.readItem(knownResourceId, knownItemResourceId);
205 VocabularyitemsCommon vitem = null;
207 assertStatusCode(res, testName);
208 // Check whether Person has expected displayName.
209 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
210 vitem = (VocabularyitemsCommon) extractPart(input,
211 client.getItemCommonPartName(), VocabularyitemsCommon.class);
212 Assert.assertNotNull(vitem);
219 // Try to Update with null displayName
221 setupUpdateWithInvalidBody();
222 vitem.setDisplayName(null);
223 // Submit the updated resource to the service and store the response.
224 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
225 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
226 res = client.updateItem(knownResourceId, knownItemResourceId, output);
228 assertStatusCode(res, testName);
235 // Now try to Update with 1-char displayName (too short)
237 setupUpdateWithInvalidBody();
238 vitem.setDisplayName("a");
239 // Submit the updated resource to the service and store the response.
240 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
241 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
242 res = client.updateItem(knownResourceId, knownItemResourceId, output);
244 assertStatusCode(res, testName);
252 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
253 public void localDelete(String testName) throws Exception {
254 super.delete(testName);
258 public void delete(String testName) throws Exception {
260 // This overrides the base test. We don't want to do anything at this point
261 // in the test suite. See the localDelete() method for the actual "delete" test
266 public void deleteItem(String testName) throws Exception {
267 //Do nothing. We don't want to delete the known item until all the dependencies of the
268 // localDeleteItem() test have been fulfilled.
271 @Test(dataProvider = "testName",
272 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
273 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
274 public void localDeleteItem(String testName) throws Exception {
275 super.deleteItem(testName);
279 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
280 * refer to this method in their @Test annotation declarations.
283 @Test(dataProvider = "testName",
285 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
286 public void authorityTests(String testName) {
287 // This method only exists as a dependency target for TestNG
290 // ---------------------------------------------------------------
291 // Vocabulary test specific overrides
292 // ---------------------------------------------------------------
295 public String getServicePathComponent() {
296 return SERVICE_PATH_COMPONENT;
300 protected String getServiceName() {
301 return VocabularyClient.SERVICE_NAME;
305 protected PoxPayloadOut createInstance(String commonPartName,
307 String displayName = "displayName-" + identifier;
308 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
309 displayName, identifier, commonPartName);
314 protected PoxPayloadOut createInstance(String identifier) throws Exception {
315 VocabularyClient client = new VocabularyClient();
316 return createInstance(client.getCommonPartName(), identifier);
320 protected VocabulariesCommon updateInstance(
321 VocabulariesCommon vocabulariesCommon) {
322 VocabulariesCommon result = new VocabulariesCommon();
324 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
325 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
331 protected void compareUpdatedInstances(VocabulariesCommon original,
332 VocabulariesCommon updated) throws Exception {
333 Assert.assertEquals(updated.getDisplayName(),
334 original.getDisplayName(),
335 "Display name in updated object did not match submitted data.");
336 Assert.assertEquals(updated.getVocabType(),
337 original.getVocabType(),
338 "Vocabulary tyype name in updated object did not match submitted data.");
342 // Vocabulary item specific overrides
346 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) throws Exception {
347 String headerLabel = new VocabularyClient().getItemCommonPartName();
348 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
349 String shortId = identifier;
350 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
351 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
353 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
357 protected VocabularyitemsCommon updateItemInstance(
358 VocabularyitemsCommon authorityItem) {
359 VocabularyitemsCommon result = new VocabularyitemsCommon();
360 result.setDisplayName("updated-" + authorityItem.getDisplayName());
365 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
366 VocabularyitemsCommon updated,
367 boolean compareRevNumbers) throws Exception {
368 Assert.assertEquals(updated.getDisplayName(),
369 original.getDisplayName(),
370 "Display name in updated VocabularyItem did not match submitted data.");
372 if (compareRevNumbers == true) {
373 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
378 protected void verifyReadItemInstance(VocabularyitemsCommon item)
380 // TODO Auto-generated method stub
385 protected PoxPayloadOut createNonExistenceItemInstance(
386 String commonPartName, String identifier) {
387 HashMap<String, String> itemInfo = new HashMap<String, String>();
388 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
389 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
390 PoxPayloadOut result =
391 VocabularyClientUtils.createVocabularyItemInstance(
392 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
393 itemInfo, commonPartName);
397 @AfterClass(alwaysRun = true)
399 public void cleanUp() throws Exception {