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.
24 package org.collectionspace.services.vocabulary.importer;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
35 import org.apache.log4j.BasicConfigurator;
36 import org.collectionspace.services.VocabularyItemJAXBSchema;
37 import org.collectionspace.services.client.VocabularyClient;
38 import org.collectionspace.services.client.VocabularyClientUtils;
39 import org.collectionspace.services.client.test.ServiceRequestType;
40 import org.collectionspace.services.vocabulary.VocabulariesCommon;
41 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
44 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * VocabularyServiceTest, carries out tests against a
50 * deployed and running Vocabulary Service.
52 * $LastChangedRevision: 753 $
53 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
55 public class VocabularyBaseImport {
56 private static final Logger logger =
57 LoggerFactory.getLogger(VocabularyBaseImport.class);
59 // Instance variables specific to this test.
60 private VocabularyClient client = new VocabularyClient();
61 final String SERVICE_PATH_COMPONENT = "vocabularies";
62 final String ITEM_SERVICE_PATH_COMPONENT = "items";
64 public void createEnumeration(String vocabName, List<String> enumValues ) {
66 // Expected status code: 201 Created
67 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
68 // Type of service request being tested
69 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
71 if(logger.isDebugEnabled()){
72 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
74 String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, false);
75 String fullVocabRefName = baseVocabRefName+"'"+vocabName+"'";
76 MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
77 vocabName, fullVocabRefName, client.getCommonPartName());
78 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
83 throw new RuntimeException("Could not create enumeration: \""+vocabName
84 +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86 if(statusCode != EXPECTED_STATUS_CODE) {
87 throw new RuntimeException("Unexpected Status when creating enumeration: \""
88 +vocabName +"\", Status:"+ statusCode);
91 // Store the ID returned from this create operation
92 // for additional tests below.
93 String newVocabId = VocabularyClientUtils.extractId(res);
94 if(logger.isDebugEnabled()){
95 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
98 for(String itemName : enumValues){
99 HashMap<String, String> itemInfo = new HashMap<String, String>();
100 itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, itemName);
101 VocabularyClientUtils.createItemInVocabulary(newVocabId,
102 baseVocabRefName, itemInfo, client);
106 public static void main(String[] args) {
108 BasicConfigurator.configure();
109 logger.info("VocabularyBaseImport starting...");
111 VocabularyBaseImport vbi = new VocabularyBaseImport();
112 final String acquisitionMethodsVocabName = "Acquisition Methods";
113 final String entryMethodsVocabName = "Entry Methods";
114 final String entryReasonsVocabName = "Entry Reasons";
115 final String responsibleDeptsVocabName = "Responsible Departments";
117 List<String> acquisitionMethodsEnumValues =
118 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
119 List<String> entryMethodsEnumValues =
120 Arrays.asList("In person","Post","Found on doorstep");
121 List<String> entryReasonsEnumValues =
122 Arrays.asList("Enquiry","Commission","Loan");
123 List<String> respDeptNamesEnumValues =
124 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
125 "Ethnography","Herpetology","Media and Performance Art",
126 "Paintings and Sculpture","Paleobotany","Photographs",
127 "Prints and Drawings");
129 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
130 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
131 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
132 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
134 logger.info("VocabularyBaseImport complete.");