]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9befe6b01e3923770827837408c3e79ca26f9e19
[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.client.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, "enum");
72         ClientResponse<Response> res = client.create(multipart);
73
74         int statusCode = res.getStatus();
75
76         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
77                                         throw new RuntimeException("Could not create enumeration: \""+vocabName
78                                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
79                                 }
80         if(statusCode != EXPECTED_STATUS_CODE) {
81                                         throw new RuntimeException("Unexpected Status when creating enumeration: \""
82                                                         +vocabName +"\", Status:"+ statusCode);
83                                 }
84
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:"
90                                                                 +newVocabId );
91                                 }
92                                 for(String itemName : enumValues){
93                                         createItemInVocab(newVocabId, vocabName, itemName);
94                                 }
95     }
96     
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;
102
103         if(logger.isDebugEnabled()){
104             logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");
105                                 }
106         MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName);
107         ClientResponse<Response> res = client.createItem(vcsid, multipart);
108
109         int statusCode = res.getStatus();
110
111         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
112                                         throw new RuntimeException("Could not create Item: \""+itemName
113                                                         +"\" in vocabulary: \"" + vocabName
114                                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
115                                 }
116         if(statusCode != EXPECTED_STATUS_CODE) {
117                                         throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
118                                                         +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);
119                                 }
120
121         return extractId(res);
122     }
123
124     // ---------------------------------------------------------------
125     // Utility methods used by tests above
126     // ---------------------------------------------------------------
127
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());
135
136         if(logger.isDebugEnabled()){
137                 logger.debug("to be created, vocabulary common ", 
138                                         vocabulary, VocabulariesCommon.class);
139         }
140
141         return multipart;
142     }
143
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());
151
152         if(logger.isDebugEnabled()){
153                 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);
154         }
155
156         return multipart;
157     }
158
159
160     /**
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.
164      *
165      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
166      *
167      * @param statusCode  The invalid status code that was returned in the response,
168      *                    from submitting that type of request to the service.
169      *
170      * @return An error message.
171      */
172     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
173         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
174                 requestType.validStatusCodesAsString();
175     }
176
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);
182         }
183         String[] segments = uri.split("/");
184         String id = segments[segments.length - 1];
185         if(logger.isDebugEnabled()){
186                 logger.debug("id=" + id);
187         }
188         return id;
189     }
190
191         public static void main(String[] args) {
192                 
193                 BasicConfigurator.configure();
194                 logger.info("VocabularyBaseImport starting...");
195
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";
201
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");
213
214                 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
215                 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
216                 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
217                 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
218
219                 logger.info("VocabularyBaseImport complete.");
220         }
221 }