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.List;
\r
30 import javax.ws.rs.core.MediaType;
\r
31 import javax.ws.rs.core.MultivaluedMap;
\r
32 import javax.ws.rs.core.Response;
\r
34 import org.apache.log4j.BasicConfigurator;
\r
35 import org.collectionspace.services.client.VocabularyClient;
\r
36 import org.collectionspace.services.client.test.ServiceRequestType;
\r
37 import org.collectionspace.services.vocabulary.VocabulariesCommon;
\r
38 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
\r
39 import org.jboss.resteasy.client.ClientResponse;
\r
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
42 import org.slf4j.Logger;
\r
43 import org.slf4j.LoggerFactory;
\r
46 * VocabularyServiceTest, carries out tests against a
\r
47 * deployed and running Vocabulary Service.
\r
49 * $LastChangedRevision: 753 $
\r
50 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
\r
52 public class Sample {
\r
53 private static final Logger logger =
\r
54 LoggerFactory.getLogger(Sample.class);
\r
56 // Instance variables specific to this test.
\r
57 private VocabularyClient client = new VocabularyClient();
\r
58 final String SERVICE_PATH_COMPONENT = "vocabularies";
\r
59 final String ITEM_SERVICE_PATH_COMPONENT = "items";
\r
61 public void createEnumeration(String vocabName, List<String> enumValues ) {
\r
63 // Expected status code: 201 Created
\r
64 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
65 // Type of service request being tested
\r
66 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
68 if(logger.isDebugEnabled()){
\r
69 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
\r
71 MultipartOutput multipart = createVocabularyInstance(vocabName,
\r
72 createRefName(vocabName), "enum");
\r
73 ClientResponse<Response> res = client.create(multipart);
\r
75 int statusCode = res.getStatus();
\r
77 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
78 throw new RuntimeException("Could not create enumeration: \""+vocabName
\r
79 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
81 if(statusCode != EXPECTED_STATUS_CODE) {
\r
82 throw new RuntimeException("Unexpected Status when creating enumeration: \""
\r
83 +vocabName +"\", Status:"+ statusCode);
\r
86 // Store the ID returned from this create operation
\r
87 // for additional tests below.
\r
88 String newVocabId = extractId(res);
\r
89 if(logger.isDebugEnabled()){
\r
90 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
\r
93 for(String itemName : enumValues){
\r
94 createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));
\r
98 private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {
\r
99 // Expected status code: 201 Created
\r
100 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
101 // Type of service request being tested
\r
102 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
104 if(logger.isDebugEnabled()){
\r
105 logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");
\r
107 MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);
\r
108 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
110 int statusCode = res.getStatus();
\r
112 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
113 throw new RuntimeException("Could not create Item: \""+itemName
\r
114 +"\" in vocabulary: \"" + vocabName
\r
115 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
117 if(statusCode != EXPECTED_STATUS_CODE) {
\r
118 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
\r
119 +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);
\r
122 return extractId(res);
\r
125 // ---------------------------------------------------------------
\r
126 // Utility methods used by tests above
\r
127 // ---------------------------------------------------------------
\r
129 private MultipartOutput createVocabularyInstance(
\r
130 String displayName, String refName, String vocabType) {
\r
131 VocabulariesCommon vocabulary = new VocabulariesCommon();
\r
132 vocabulary.setDisplayName(displayName);
\r
133 vocabulary.setRefName(refName);
\r
134 vocabulary.setVocabType(vocabType);
\r
135 MultipartOutput multipart = new MultipartOutput();
\r
136 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
\r
137 commonPart.getHeaders().add("label", client.getCommonPartName());
\r
139 if(logger.isDebugEnabled()){
\r
140 logger.debug("to be created, vocabulary common ",
\r
141 vocabulary, VocabulariesCommon.class);
\r
147 private MultipartOutput createVocabularyItemInstance(
\r
148 String inVocabulary, String displayName, String refName) {
\r
149 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
\r
150 vocabularyItem.setInVocabulary(inVocabulary);
\r
151 vocabularyItem.setDisplayName(displayName);
\r
152 vocabularyItem.setRefName(refName);
\r
153 MultipartOutput multipart = new MultipartOutput();
\r
154 OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
\r
155 commonPart.getHeaders().add("label", client.getItemCommonPartName());
\r
157 if(logger.isDebugEnabled()){
\r
158 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);
\r
166 * Returns an error message indicating that the status code returned by a
\r
167 * specific call to a service does not fall within a set of valid status
\r
168 * codes for that service.
\r
170 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
172 * @param statusCode The invalid status code that was returned in the response,
\r
173 * from submitting that type of request to the service.
\r
175 * @return An error message.
\r
177 protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
178 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
179 requestType.validStatusCodesAsString();
\r
182 protected String extractId(ClientResponse<Response> res) {
\r
183 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
184 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
185 if(logger.isDebugEnabled()){
\r
186 logger.debug("extractId:uri=" + uri);
\r
188 String[] segments = uri.split("/");
\r
189 String id = segments[segments.length - 1];
\r
190 if(logger.isDebugEnabled()){
\r
191 logger.debug("id=" + id);
\r
196 protected String createRefName(String displayName) {
\r
197 return displayName.replaceAll("\\W", "");
\r
200 public static void main(String[] args) {
\r
202 BasicConfigurator.configure();
\r
203 logger.info("VocabularyBaseImport starting...");
\r
205 Sample vbi = new Sample();
\r
206 final String acquisitionMethodsVocabName = "Acquisition Methods";
\r
207 final String entryMethodsVocabName = "Entry Methods";
\r
208 final String entryReasonsVocabName = "Entry Reasons";
\r
209 final String responsibleDeptsVocabName = "Responsible Departments";
\r
211 List<String> acquisitionMethodsEnumValues =
\r
212 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
\r
213 List<String> entryMethodsEnumValues =
\r
214 Arrays.asList("In person","Post","Found on doorstep");
\r
215 List<String> entryReasonsEnumValues =
\r
216 Arrays.asList("Enquiry","Commission","Loan");
\r
217 List<String> respDeptNamesEnumValues =
\r
218 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
\r
219 "Ethnography","Herpetology","Media and Performance Art",
\r
220 "Paintings and Sculpture","Paleobotany","Photographs",
\r
221 "Prints and Drawings");
\r
223 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
\r
224 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
\r
225 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
\r
226 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
\r
228 logger.info("VocabularyBaseImport complete.");
\r