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;
26 import javax.ws.rs.core.Response;
28 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
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.jboss.resteasy.client.ClientResponse;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
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 private final String CLASS_NAME = VocabularyServiceTest.class.getName();
55 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
56 // Instance variables specific to this test.
57 final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
58 final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
59 final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
63 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
66 protected CollectionSpaceClient getClientInstance() {
67 return new VocabularyClient();
71 protected String createItemInAuthority(String authorityId) {
74 VocabularyClient client = new VocabularyClient();
75 HashMap<String, String> itemInfo = new HashMap<String, String>();
76 String shortId = createIdentifier();
77 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
78 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
79 result = VocabularyClientUtils.createItemInVocabulary(authorityId,
80 null /*knownResourceRefName*/, itemInfo, client);
81 allResourceItemIdsCreated.put(result, authorityId);
86 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
87 dependsOnMethods = {"CRUDTests"})
88 public void createWithBadShortId(String testName) throws Exception {
89 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
91 // Submit the request to the service and store the response.
92 VocabularyClient client = new VocabularyClient();
93 PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
94 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
95 ClientResponse<Response> res = client.create(multipart);
97 assertStatusCode(res, testName);
100 res.releaseConnection();
105 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
106 dependsOnMethods = {"authorityTests"})
107 public void createItemWithBadShortId(String testName) throws Exception {
108 setupCreateWithMalformedXml();
110 // Submit the request to the service and store the response.
111 VocabularyClient client = new VocabularyClient();
112 HashMap<String, String> itemInfo = new HashMap<String, String>();
113 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
114 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
115 PoxPayloadOut multipart =
116 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
117 itemInfo, client.getItemCommonPartName());
118 ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
120 int statusCode = res.getStatus();
122 if (!testRequestType.isValidStatusCode(statusCode)) {
123 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
124 + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
125 + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
127 if (statusCode != testExpectedStatusCode) {
128 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
129 + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
133 res.releaseConnection();
139 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
140 dependsOnMethods = {"updateItem"})
141 public void verifyIllegalItemDisplayName(String testName) throws Exception {
142 // Perform setup for read.
145 // Submit the request to the service and store the response.
146 VocabularyClient client = new VocabularyClient();
147 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
148 VocabularyitemsCommon vitem = null;
150 assertStatusCode(res, testName);
151 // Check whether Person has expected displayName.
152 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
153 vitem = (VocabularyitemsCommon) extractPart(input,
154 client.getItemCommonPartName(), VocabularyitemsCommon.class);
155 Assert.assertNotNull(vitem);
158 res.releaseConnection();
162 // Try to Update with null displayName
164 setupUpdateWithInvalidBody();
165 vitem.setDisplayName(null);
166 // Submit the updated resource to the service and store the response.
167 PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
168 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
169 res = client.updateItem(knownResourceId, knownItemResourceId, output);
171 assertStatusCode(res, testName);
174 res.releaseConnection();
178 // Now try to Update with 1-char displayName (too short)
180 setupUpdateWithInvalidBody();
181 vitem.setDisplayName("a");
182 // Submit the updated resource to the service and store the response.
183 output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
184 commonPart = output.addPart(client.getItemCommonPartName(), vitem);
185 res = client.updateItem(knownResourceId, knownItemResourceId, output);
187 assertStatusCode(res, testName);
190 res.releaseConnection();
195 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
196 public void localDelete(String testName) throws Exception {
197 super.delete(testName);
201 public void delete(String testName) throws Exception {
203 // This overrides the base test. We don't want to do anything at this point
204 // in the test suite. See the localDelete() method for the actual "delete" test
209 public void deleteItem(String testName) throws Exception {
210 //Do nothing. We don't want to delete the known item until all the dependencies of the
211 // localDeleteItem() test have been fulfilled.
214 @Test(dataProvider = "testName",
215 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
216 "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
217 public void localDeleteItem(String testName) throws Exception {
218 super.deleteItem(testName);
222 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
223 * refer to this method in their @Test annotation declarations.
226 @Test(dataProvider = "testName",
228 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})
229 public void authorityTests(String testName) {
230 // This method only exists as a dependency target for TestNG
233 // ---------------------------------------------------------------
234 // Vocabulary test specific overrides
235 // ---------------------------------------------------------------
238 public String getServicePathComponent() {
239 return SERVICE_PATH_COMPONENT;
243 protected String getServiceName() {
244 return VocabularyClient.SERVICE_NAME;
248 protected PoxPayloadOut createInstance(String commonPartName,
250 String displayName = "displayName-" + identifier;
251 PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
252 displayName, identifier, commonPartName);
257 protected PoxPayloadOut createInstance(String identifier) {
258 VocabularyClient client = new VocabularyClient();
259 return createInstance(client.getCommonPartName(), identifier);
263 protected VocabulariesCommon updateInstance(
264 VocabulariesCommon vocabulariesCommon) {
265 VocabulariesCommon result = new VocabulariesCommon();
267 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
268 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
274 protected void compareUpdatedInstances(VocabulariesCommon original,
275 VocabulariesCommon updated) throws Exception {
276 Assert.assertEquals(updated.getDisplayName(),
277 original.getDisplayName(),
278 "Display name in updated object did not match submitted data.");
279 Assert.assertEquals(updated.getVocabType(),
280 original.getVocabType(),
281 "Vocabulary tyype name in updated object did not match submitted data.");
285 // Vocabulary item specific overrides
289 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
290 String headerLabel = new VocabularyClient().getItemCommonPartName();
291 HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
292 String shortId = createIdentifier();
293 vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
294 vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
296 return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
300 protected VocabularyitemsCommon updateItemInstance(
301 VocabularyitemsCommon authorityItem) {
302 VocabularyitemsCommon result = new VocabularyitemsCommon();
303 result.setDisplayName("updated-" + authorityItem.getDisplayName());
308 protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
309 VocabularyitemsCommon updated) throws Exception {
310 Assert.assertEquals(updated.getDisplayName(),
311 original.getDisplayName(),
312 "Display name in updated VocabularyItem did not match submitted data.");
316 protected void verifyReadItemInstance(VocabularyitemsCommon item)
318 // TODO Auto-generated method stub
323 protected PoxPayloadOut createNonExistenceItemInstance(
324 String commonPartName, String identifier) {
325 HashMap<String, String> itemInfo = new HashMap<String, String>();
326 itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
327 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
328 PoxPayloadOut result =
329 VocabularyClientUtils.createVocabularyItemInstance(
330 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
331 itemInfo, commonPartName);