]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e448af7d8574b8e04884034cb840f1a555cc778a
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23
24 package org.collectionspace.services.vocabulary.importer;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
33
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;
44
45 /**
46  * VocabularyServiceTest, carries out tests against a
47  * deployed and running Vocabulary Service.
48  *
49  * $LastChangedRevision: 753 $
50  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
51  */
52 public class VocabularyBaseImport {
53     private static final Logger logger =
54         LoggerFactory.getLogger(VocabularyBaseImport.class);
55
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";
60
61     public void createEnumeration(String vocabName, List<String> enumValues ) {
62
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;
67
68         if(logger.isDebugEnabled()){
69                 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
70         }
71         MultipartOutput multipart = createVocabularyInstance(vocabName, 
72                         createRefName(vocabName), "enum");
73         ClientResponse<Response> res = client.create(multipart);
74
75         int statusCode = res.getStatus();
76
77         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
78                 throw new RuntimeException("Could not create enumeration: \""+vocabName
79                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
80         }
81         if(statusCode != EXPECTED_STATUS_CODE) {
82                 throw new RuntimeException("Unexpected Status when creating enumeration: \""
83                                 +vocabName +"\", Status:"+ statusCode);
84         }
85
86         // Store the ID returned from this create operation
87         // for additional tests below.
88         String newVocabId = extractId(res);
89         if(logger.isDebugEnabled()){
90                 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
91                                 +newVocabId );
92         }
93         for(String itemName : enumValues){
94                 createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));
95         }
96     }
97     
98     private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {
99         // Expected status code: 201 Created
100         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
101         // Type of service request being tested
102         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
103
104         if(logger.isDebugEnabled()){
105                 logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");
106         }
107         MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);
108         ClientResponse<Response> res = client.createItem(vcsid, multipart);
109
110         int statusCode = res.getStatus();
111
112         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
113                 throw new RuntimeException("Could not create Item: \""+itemName
114                                 +"\" in vocabulary: \"" + vocabName
115                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
116         }
117         if(statusCode != EXPECTED_STATUS_CODE) {
118                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
119                                 +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);
120         }
121
122         return extractId(res);
123     }
124
125     // ---------------------------------------------------------------
126     // Utility methods used by tests above
127     // ---------------------------------------------------------------
128
129     private MultipartOutput createVocabularyInstance(
130                 String displayName, String refName, String vocabType) {
131         VocabulariesCommon vocabulary = new VocabulariesCommon();
132         vocabulary.setDisplayName(displayName);
133         vocabulary.setRefName(refName);
134         vocabulary.setVocabType(vocabType);
135         MultipartOutput multipart = new MultipartOutput();
136         OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
137         commonPart.getHeaders().add("label", client.getCommonPartName());
138
139         if(logger.isDebugEnabled()){
140                 logger.debug("to be created, vocabulary common ", 
141                                         vocabulary, VocabulariesCommon.class);
142         }
143
144         return multipart;
145     }
146
147     private MultipartOutput createVocabularyItemInstance(
148                 String inVocabulary, String displayName, String refName) {
149         VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
150         vocabularyItem.setInVocabulary(inVocabulary);
151         vocabularyItem.setDisplayName(displayName);
152         vocabularyItem.setRefName(refName);
153         MultipartOutput multipart = new MultipartOutput();
154         OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
155         commonPart.getHeaders().add("label", client.getItemCommonPartName());
156
157         if(logger.isDebugEnabled()){
158                 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);
159         }
160
161         return multipart;
162     }
163
164
165     /**
166      * Returns an error message indicating that the status code returned by a
167      * specific call to a service does not fall within a set of valid status
168      * codes for that service.
169      *
170      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
171      *
172      * @param statusCode  The invalid status code that was returned in the response,
173      *                    from submitting that type of request to the service.
174      *
175      * @return An error message.
176      */
177     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
178         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
179                 requestType.validStatusCodesAsString();
180     }
181
182     protected String extractId(ClientResponse<Response> res) {
183         MultivaluedMap<String, Object> mvm = res.getMetadata();
184         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
185         if(logger.isDebugEnabled()){
186                 logger.debug("extractId:uri=" + uri);
187         }
188         String[] segments = uri.split("/");
189         String id = segments[segments.length - 1];
190         if(logger.isDebugEnabled()){
191                 logger.debug("id=" + id);
192         }
193         return id;
194     }
195     
196     protected String createRefName(String displayName) {
197         return displayName.replaceAll("\\W", "");
198     }
199
200         public static void main(String[] args) {
201                 
202                 BasicConfigurator.configure();
203                 logger.info("VocabularyBaseImport starting...");
204
205                 VocabularyBaseImport vbi = new VocabularyBaseImport();
206                 final String acquisitionMethodsVocabName = "Acquisition Methods";
207                 final String entryMethodsVocabName = "Entry Methods";
208                 final String entryReasonsVocabName = "Entry Reasons";
209                 final String responsibleDeptsVocabName = "Responsible Departments";
210
211                 List<String> acquisitionMethodsEnumValues = 
212                         Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
213                 List<String> entryMethodsEnumValues = 
214                         Arrays.asList("In person","Post","Found on doorstep");
215                 List<String> entryReasonsEnumValues = 
216                         Arrays.asList("Enquiry","Commission","Loan");
217                 List<String> respDeptNamesEnumValues = 
218                         Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
219                                                                         "Ethnography","Herpetology","Media and Performance Art",
220                                                                         "Paintings and Sculpture","Paleobotany","Photographs",
221                                                                         "Prints and Drawings");
222
223                 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
224                 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
225                 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
226                 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
227
228                 logger.info("VocabularyBaseImport complete.");
229         }
230 }