2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c)) 2009 Regents of the University of California
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
24 package org.collectionspace.services.vocabulary.client.sample;
\r
26 import java.util.ArrayList;
\r
27 import java.util.Arrays;
\r
28 import java.util.HashMap;
\r
29 import java.util.List;
\r
31 import javax.ws.rs.core.MediaType;
\r
32 import javax.ws.rs.core.MultivaluedMap;
\r
33 import javax.ws.rs.core.Response;
\r
35 import org.apache.log4j.BasicConfigurator;
\r
36 //import org.collectionspace.services.VocabularyItemJAXBSchema;
\r
37 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
\r
39 import org.collectionspace.services.client.VocabularyClient;
\r
40 import org.collectionspace.services.client.VocabularyClientUtils;
\r
41 import org.collectionspace.services.client.test.ServiceRequestType;
\r
42 import org.collectionspace.services.vocabulary.VocabulariesCommon;
\r
43 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
\r
44 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
\r
45 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
\r
46 import org.jboss.resteasy.client.ClientResponse;
\r
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
\r
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
49 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
\r
50 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
51 import org.slf4j.Logger;
\r
52 import org.slf4j.LoggerFactory;
\r
55 * VocabularyServiceTest, carries out tests against a
\r
56 * deployed and running Vocabulary Service.
\r
58 * $LastChangedRevision$
\r
61 public class Sample {
\r
62 private static final Logger logger =
\r
63 LoggerFactory.getLogger(Sample.class);
\r
65 // Instance variables specific to this test.
\r
66 private VocabularyClient client = new VocabularyClient();
\r
67 final String SERVICE_PATH_COMPONENT = "vocabularies";
\r
68 final String ITEM_SERVICE_PATH_COMPONENT = "items";
\r
71 // ---------------------------------------------------------------
\r
73 // ---------------------------------------------------------------
\r
75 public void createEnumeration(String vocabName, List<String> enumValues ) {
\r
77 // Expected status code: 201 Created
\r
78 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
79 // Type of service request being tested
\r
80 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
82 if(logger.isDebugEnabled()){
\r
83 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
\r
86 String displaySuffix = "displayName-" + System.currentTimeMillis(); //TODO: Laramie20100728 temp fix, made-up displaySuffix.
\r
87 String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, displaySuffix); //TODO: Laramie20100728 temp fix was vocabName, false
\r
88 String fullVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, displaySuffix); //TODO: Laramie20100728 temp fix was vocabName, true
\r
89 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
\r
90 vocabName, fullVocabRefName, client.getCommonPartName());
\r
91 ClientResponse<Response> res = client.create(multipart);
\r
93 int statusCode = res.getStatus();
\r
95 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
96 throw new RuntimeException("Could not create enumeration: \""+vocabName
\r
97 +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
99 if(statusCode != EXPECTED_STATUS_CODE) {
\r
100 throw new RuntimeException("Unexpected Status when creating enumeration: \""
\r
101 +vocabName +"\", Status:"+ statusCode);
\r
104 // Store the ID returned from this create operation
\r
105 // for additional tests below.
\r
106 String newVocabId = VocabularyClientUtils.extractId(res);
\r
107 if(logger.isDebugEnabled()){
\r
108 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
\r
111 for(String itemName : enumValues){
\r
112 HashMap<String, String> itemInfo = new HashMap<String, String>();
\r
113 itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, itemName);
\r
114 VocabularyClientUtils.createItemInVocabulary(newVocabId,
\r
115 baseVocabRefName, itemInfo, client);
\r
119 // ---------------------------------------------------------------
\r
121 // ---------------------------------------------------------------
\r
123 private VocabulariesCommonList readVocabularies() {
\r
125 // Expected status code: 200 OK
\r
126 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
127 // Type of service request being tested
\r
128 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
130 // Submit the request to the service and store the response.
\r
131 ClientResponse<VocabulariesCommonList> res = client.readList();
\r
132 VocabulariesCommonList list = res.getEntity();
\r
134 int statusCode = res.getStatus();
\r
135 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
136 throw new RuntimeException("Could not read list of vocabularies: "
\r
137 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
139 if(statusCode != EXPECTED_STATUS_CODE) {
\r
140 throw new RuntimeException("Unexpected Status when reading " +
\r
141 "list of vocabularies, Status:"+ statusCode);
\r
147 private List<String> readVocabularyIds(VocabulariesCommonList list) {
\r
149 List<String> ids = new ArrayList<String>();
\r
150 List<VocabulariesCommonList.VocabularyListItem> vocabularies =
\r
151 list.getVocabularyListItem();
\r
152 for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {
\r
153 ids.add(vocabulary.getCsid());
\r
158 private VocabulariesCommon readVocabulary(String vocabId) {
\r
160 // Expected status code: 200 OK
\r
161 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
162 // Type of service request being tested
\r
163 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
165 // Submit the request to the service and store the response.
\r
166 VocabulariesCommon vocabulary = null;
\r
168 ClientResponse<MultipartInput> res = client.read(vocabId);
\r
169 int statusCode = res.getStatus();
\r
170 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
171 throw new RuntimeException("Could not read vocabulary"
\r
172 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
174 if(statusCode != EXPECTED_STATUS_CODE) {
\r
175 throw new RuntimeException("Unexpected Status when reading " +
\r
176 "vocabulary, Status:"+ statusCode);
\r
178 MultipartInput input = (MultipartInput) res.getEntity();
\r
179 vocabulary = (VocabulariesCommon) extractPart(input,
\r
180 client.getCommonPartName(), VocabulariesCommon.class);
\r
181 } catch (Exception e) {
\r
182 throw new RuntimeException("Could not read vocabulary: ", e);
\r
188 private VocabularyitemsCommonList readItemsInVocab(String vocabId) {
\r
190 // Expected status code: 200 OK
\r
191 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
192 // Type of service request being tested
\r
193 ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;
\r
195 // Submit the request to the service and store the response.
\r
196 // readItemList(String inAuthority, String partialTerm, String keywords)
\r
197 ClientResponse<VocabularyitemsCommonList> res = client.readItemList(vocabId, "", ""); //TODO: figure out these params. I just put in empty string to make it recompile after refactoring. Laramie20100728
\r
198 VocabularyitemsCommonList list = res.getEntity();
\r
200 int statusCode = res.getStatus();
\r
202 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
203 throw new RuntimeException("Could not read items in vocabulary: "
\r
204 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
206 if(statusCode != EXPECTED_STATUS_CODE) {
\r
207 throw new RuntimeException("Unexpected Status when reading " +
\r
208 "items in vocabulary, Status:"+ statusCode);
\r
214 private List<String> readVocabularyItemIds(VocabularyitemsCommonList list) {
\r
216 List<String> ids = new ArrayList<String>();
\r
217 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
\r
218 list.getVocabularyitemListItem();
\r
219 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
\r
220 ids.add(item.getCsid());
\r
225 // ---------------------------------------------------------------
\r
227 // ---------------------------------------------------------------
\r
229 private void deleteVocabulary(String vcsid) {
\r
230 // Expected status code: 200 OK
\r
231 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
232 // Type of service request being tested
\r
233 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
235 ClientResponse<Response> res = client.delete(vcsid);
\r
236 int statusCode = res.getStatus();
\r
238 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
239 throw new RuntimeException("Could not delete vocabulary: "
\r
240 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
242 if(statusCode != EXPECTED_STATUS_CODE) {
\r
243 throw new RuntimeException("Unexpected Status when deleting " +
\r
244 "vocabulary, Status:"+ statusCode);
\r
248 private void deleteAllVocabularies() {
\r
249 List<String> ids = readVocabularyIds(readVocabularies());
\r
250 for (String id : ids) {
\r
251 deleteVocabulary(id);
\r
255 private void deleteVocabularyItem(String vcsid, String itemcsid) {
\r
256 // Expected status code: 200 OK
\r
257 int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
\r
258 // Type of service request being tested
\r
259 ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;
\r
261 ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);
\r
262 int statusCode = res.getStatus();
\r
264 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
265 throw new RuntimeException("Could not delete vocabulary item: "
\r
266 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
268 if(statusCode != EXPECTED_STATUS_CODE) {
\r
269 throw new RuntimeException("Unexpected Status when deleting " +
\r
270 "vocabulary item, Status:"+ statusCode);
\r
274 private void deleteAllItemsForVocab(String vocabId) {
\r
275 List<String> itemIds = readVocabularyItemIds(readItemsInVocab(vocabId));
\r
276 for (String itemId : itemIds) {
\r
277 deleteVocabularyItem(vocabId, itemId);
\r
281 // ---------------------------------------------------------------
\r
282 // Utility methods used by tests above
\r
283 // ---------------------------------------------------------------
\r
285 // Retrieve individual fields of vocabulary records.
\r
287 private String displayAllVocabularies(VocabulariesCommonList list) {
\r
288 StringBuffer sb = new StringBuffer();
\r
289 List<VocabulariesCommonList.VocabularyListItem> vocabularies =
\r
290 list.getVocabularyListItem();
\r
292 for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {
\r
293 sb.append("vocabulary [" + i + "]" + "\n");
\r
294 sb.append(displayVocabularyDetails(vocabulary));
\r
297 return sb.toString();
\r
300 private String displayVocabularyDetails(
\r
301 VocabulariesCommonList.VocabularyListItem vocabulary) {
\r
302 StringBuffer sb = new StringBuffer();
\r
303 sb.append("displayName=" + vocabulary.getDisplayName() + "\n");
\r
304 sb.append("vocabType=" + vocabulary.getVocabType() + "\n");
\r
305 // sb.append("csid=" + vocabulary.getCsid() + "\n");
\r
306 sb.append("URI=" + vocabulary.getUri() + "\n");
\r
307 return sb.toString();
\r
310 // Retrieve individual fields of vocabulary item records.
\r
312 private String displayAllVocabularyItems(VocabularyitemsCommonList list) {
\r
313 StringBuffer sb = new StringBuffer();
\r
314 List<VocabularyitemsCommonList.VocabularyitemListItem> items =
\r
315 list.getVocabularyitemListItem();
\r
317 for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {
\r
318 sb.append("vocabulary item [" + i + "]" + "\n");
\r
319 sb.append(displayVocabularyItemDetails(item));
\r
322 return sb.toString();
\r
325 private String displayVocabularyItemDetails(
\r
326 VocabularyitemsCommonList.VocabularyitemListItem item) {
\r
327 StringBuffer sb = new StringBuffer();
\r
328 sb.append("csid=" + item.getCsid() + "\n");
\r
329 sb.append("displayName=" + item.getDisplayName() + "\n");
\r
330 // sb.append("URI=" + item.getUri() + "\n");
\r
331 return sb.toString();
\r
334 // TODO this should be moved to a common utils class
\r
335 private Object extractPart(MultipartInput input, String label,
\r
336 Class clazz) throws Exception {
\r
338 for(InputPart part : input.getParts()){
\r
339 String partLabel = part.getHeaders().getFirst("label");
\r
340 if(label.equalsIgnoreCase(partLabel)){
\r
341 String partStr = part.getBodyAsString();
\r
342 if(logger.isDebugEnabled()){
\r
343 logger.debug("extracted part str=\n" + partStr);
\r
345 obj = part.getBody(clazz, null);
\r
346 if(logger.isDebugEnabled()){
\r
347 logger.debug("extracted part obj=\n", obj, clazz);
\r
355 public static void main(String[] args) {
\r
357 // Configure logging.
\r
358 BasicConfigurator.configure();
\r
360 logger.info("VocabularyBaseImport starting...");
\r
362 Sample vbi = new Sample();
\r
363 VocabulariesCommonList vocabularies;
\r
364 List<String> vocabIds;
\r
365 String details = "";
\r
367 // Optionally delete all vocabularies and vocabulary items.
\r
369 boolean ENABLE_DELETE_ALL = false;
\r
370 if (ENABLE_DELETE_ALL) {
\r
372 logger.info("Deleting all vocabulary items and vocabularies ...");
\r
374 // For each vocabulary ...
\r
375 vocabularies = vbi.readVocabularies();
\r
376 vocabIds = vbi.readVocabularyIds(vocabularies);
\r
377 for (String vocabId : vocabIds) {
\r
378 logger.info("Deleting all vocabulary items for vocabulary ...");
\r
379 vbi.deleteAllItemsForVocab(vocabId);
\r
380 logger.info("Deleting vocabulary ...");
\r
381 vbi.deleteVocabulary(vocabId);
\r
384 logger.info("Reading vocabularies after deletion ...");
\r
385 vocabularies = vbi.readVocabularies();
\r
386 details = vbi.displayAllVocabularies(vocabularies);
\r
387 logger.info(details);
\r
389 logger.info("Reading items in each vocabulary after deletion ...");
\r
390 vocabIds = vbi.readVocabularyIds(vocabularies);
\r
391 for (String vocabId : vocabIds) {
\r
392 VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);
\r
393 details = vbi.displayAllVocabularyItems(items);
\r
394 logger.info(details);
\r
399 // Create new vocabularies, each populated with vocabulary items.
\r
401 final String acquisitionMethodsVocabName = "Acquisition Methods";
\r
402 final String entryMethodsVocabName = "Entry Methods";
\r
403 final String entryReasonsVocabName = "Entry Reasons";
\r
404 final String responsibleDeptsVocabName = "Responsible Departments";
\r
406 List<String> acquisitionMethodsEnumValues =
\r
407 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
\r
408 List<String> entryMethodsEnumValues =
\r
409 Arrays.asList("In person","Post","Found on doorstep");
\r
410 List<String> entryReasonsEnumValues =
\r
411 Arrays.asList("Enquiry","Commission","Loan");
\r
412 List<String> respDeptNamesEnumValues =
\r
413 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
\r
414 "Ethnography","Herpetology","Media and Performance Art",
\r
415 "Paintings and Sculpture","Paleobotany","Photographs",
\r
416 "Prints and Drawings");
\r
418 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
\r
419 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
\r
420 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
\r
421 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
\r
423 logger.info("VocabularyBaseImport complete.");
\r
425 logger.info("Reading vocabularies and items ...");
\r
426 // Get a list of vocabularies.
\r
427 vocabularies = vbi.readVocabularies();
\r
428 // For each vocabulary ...
\r
429 for (VocabulariesCommonList.VocabularyListItem
\r
430 vocabulary : vocabularies.getVocabularyListItem()) {
\r
431 // Get its display name.
\r
432 logger.info(vocabulary.getDisplayName());
\r
433 // Get a list of the vocabulary items in this vocabulary.
\r
434 VocabularyitemsCommonList items =
\r
435 vbi.readItemsInVocab(vocabulary.getCsid());
\r
436 // For each vocabulary item ...
\r
437 for (VocabularyitemsCommonList.VocabularyitemListItem
\r
438 item : items.getVocabularyitemListItem()) {
\r
439 // Get its display name.
\r
440 logger.info(" " + item.getDisplayName());
\r
444 // Sample alternate methods of reading all vocabularies and
\r
445 // vocabulary items separately.
\r
446 boolean RUN_ADDITIONAL_SAMPLES = false;
\r
447 if (RUN_ADDITIONAL_SAMPLES) {
\r
449 logger.info("Reading all vocabularies ...");
\r
450 details = vbi.displayAllVocabularies(vocabularies);
\r
451 logger.info(details);
\r
453 logger.info("Reading all vocabulary items ...");
\r
454 vocabIds = vbi.readVocabularyIds(vocabularies);
\r
455 for (String vocabId : vocabIds) {
\r
456 VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);
\r
457 details = vbi.displayAllVocabularyItems(items);
\r
458 logger.info(details);
\r