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.client.importer;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
34 import org.apache.log4j.BasicConfigurator;
35 import org.collectionspace.services.client.VocabularyClient;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.collectionspace.services.vocabulary.VocabulariesCommon;
38 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
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 VocabularyBaseImport {
53 private static final Logger logger =
54 LoggerFactory.getLogger(VocabularyBaseImport.class);
56 // Instance variables specific to this test.
57 private VocabularyClient client = new VocabularyClient();
58 final String SERVICE_PATH_COMPONENT = "vocabularies";
59 final String ITEM_SERVICE_PATH_COMPONENT = "items";
61 public void createEnumeration(String vocabName, List<String> enumValues ) {
63 // Expected status code: 201 Created
64 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
65 // Type of service request being tested
66 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
68 if(logger.isDebugEnabled()){
69 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
71 MultipartOutput multipart = createVocabularyInstance(vocabName, "enum");
72 ClientResponse<Response> res = client.create(multipart);
74 int statusCode = res.getStatus();
76 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
77 throw new RuntimeException("Could not create enumeration: \""+vocabName
78 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
80 if(statusCode != EXPECTED_STATUS_CODE) {
81 throw new RuntimeException("Unexpected Status when creating enumeration: \""
82 +vocabName +"\", Status:"+ statusCode);
85 // Store the ID returned from this create operation
86 // for additional tests below.
87 String newVocabId = extractId(res);
88 if(logger.isDebugEnabled()){
89 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
92 for(String itemName : enumValues){
93 createItemInVocab(newVocabId, vocabName, itemName);
97 private String createItemInVocab(String vcsid, String vocabName, String itemName) {
98 // Expected status code: 201 Created
99 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
100 // Type of service request being tested
101 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
103 if(logger.isDebugEnabled()){
104 logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");
106 MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName);
107 ClientResponse<Response> res = client.createItem(vcsid, multipart);
109 int statusCode = res.getStatus();
111 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
112 throw new RuntimeException("Could not create Item: \""+itemName
113 +"\" in vocabulary: \"" + vocabName
114 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
116 if(statusCode != EXPECTED_STATUS_CODE) {
117 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
118 +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);
121 return extractId(res);
124 // ---------------------------------------------------------------
125 // Utility methods used by tests above
126 // ---------------------------------------------------------------
128 private MultipartOutput createVocabularyInstance(String displayName, String vocabType) {
129 VocabulariesCommon vocabulary = new VocabulariesCommon();
130 vocabulary.setDisplayName(displayName);
131 vocabulary.setVocabType(vocabType);
132 MultipartOutput multipart = new MultipartOutput();
133 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
134 commonPart.getHeaders().add("label", client.getCommonPartName());
136 if(logger.isDebugEnabled()){
137 logger.debug("to be created, vocabulary common ",
138 vocabulary, VocabulariesCommon.class);
144 private MultipartOutput createVocabularyItemInstance(String inVocabulary, String displayName) {
145 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
146 vocabularyItem.setInVocabulary(inVocabulary);
147 vocabularyItem.setDisplayName(displayName);
148 MultipartOutput multipart = new MultipartOutput();
149 OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
150 commonPart.getHeaders().add("label", client.getItemCommonPartName());
152 if(logger.isDebugEnabled()){
153 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);
161 * Returns an error message indicating that the status code returned by a
162 * specific call to a service does not fall within a set of valid status
163 * codes for that service.
165 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
167 * @param statusCode The invalid status code that was returned in the response,
168 * from submitting that type of request to the service.
170 * @return An error message.
172 protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
173 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
174 requestType.validStatusCodesAsString();
177 protected String extractId(ClientResponse<Response> res) {
178 MultivaluedMap<String, Object> mvm = res.getMetadata();
179 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
180 if(logger.isDebugEnabled()){
181 logger.debug("extractId:uri=" + uri);
183 String[] segments = uri.split("/");
184 String id = segments[segments.length - 1];
185 if(logger.isDebugEnabled()){
186 logger.debug("id=" + id);
191 public static void main(String[] args) {
193 BasicConfigurator.configure();
194 logger.info("VocabularyBaseImport starting...");
196 VocabularyBaseImport vbi = new VocabularyBaseImport();
197 final String acquisitionMethodsVocabName = "Acquisition Methods";
198 final String entryMethodsVocabName = "Entry Methods";
199 final String entryReasonsVocabName = "Entry Reasons";
200 final String responsibleDeptsVocabName = "Responsible Departments";
202 List<String> acquisitionMethodsEnumValues =
203 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
204 List<String> entryMethodsEnumValues =
205 Arrays.asList("In person","Post","Found on doorstep");
206 List<String> entryReasonsEnumValues =
207 Arrays.asList("Enquiry","Commission","Loan");
208 List<String> respDeptNamesEnumValues =
209 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
210 "Ethnography","Herpetology","Media and Performance Art",
211 "Paintings and Sculpture","Paleobotany","Photographs",
212 "Prints and Drawings");
214 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
215 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
216 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
217 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
219 logger.info("VocabularyBaseImport complete.");