]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8df271be864d2f7bb66d080f091102698a8083f2
[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.collectionspace.services.client.VocabularyClient;
35 import org.collectionspace.services.client.test.ServiceRequestType;
36 import org.collectionspace.services.vocabulary.VocabulariesCommon;
37 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
38 import org.jboss.resteasy.client.ClientResponse;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
40 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * VocabularyServiceTest, carries out tests against a
46  * deployed and running Vocabulary Service.
47  *
48  * $LastChangedRevision: 753 $
49  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
50  */
51 public class VocabularyBaseImport {
52     private final Logger logger =
53         LoggerFactory.getLogger(VocabularyBaseImport.class);
54
55     // Instance variables specific to this test.
56     private VocabularyClient client = new VocabularyClient();
57     final String SERVICE_PATH_COMPONENT = "vocabularies";
58     final String ITEM_SERVICE_PATH_COMPONENT = "items";
59
60     public void createEnumeration(String vocabName, List<String> enumValues ) {
61
62         // Expected status code: 201 Created
63         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
64         // Type of service request being tested
65         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
66
67         if(logger.isDebugEnabled()){
68             logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
69                                 }
70         MultipartOutput multipart = createVocabularyInstance(vocabName, "enum");
71         ClientResponse<Response> res = client.create(multipart);
72
73         int statusCode = res.getStatus();
74
75         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
76                                         throw new RuntimeException("Could not create enumeration: \""+vocabName
77                                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
78                                 }
79         if(statusCode != EXPECTED_STATUS_CODE) {
80                                         throw new RuntimeException("Unexpected Status when creating enumeration: \""
81                                                         +vocabName +"\", Status:"+ statusCode);
82                                 }
83
84         // Store the ID returned from this create operation
85         // for additional tests below.
86         String newVocabId = extractId(res);
87         if(logger.isDebugEnabled()){
88             logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
89                                                                 +newVocabId );
90                                 }
91                                 for(String itemName : enumValues){
92                                         createItemInVocab(newVocabId, vocabName, itemName);
93                                 }
94     }
95     
96     private String createItemInVocab(String vcsid, String vocabName, String itemName) {
97         // Expected status code: 201 Created
98         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
99         // Type of service request being tested
100         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
101
102         if(logger.isDebugEnabled()){
103             logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");
104                                 }
105         MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName);
106         ClientResponse<Response> res = client.createItem(vcsid, multipart);
107
108         int statusCode = res.getStatus();
109
110         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
111                                         throw new RuntimeException("Could not create Item: \""+itemName
112                                                         +"\" in vocabulary: \"" + vocabName
113                                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
114                                 }
115         if(statusCode != EXPECTED_STATUS_CODE) {
116                                         throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
117                                                         +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);
118                                 }
119
120         return extractId(res);
121     }
122
123     // ---------------------------------------------------------------
124     // Utility methods used by tests above
125     // ---------------------------------------------------------------
126
127     private MultipartOutput createVocabularyInstance(String displayName, String vocabType) {
128         VocabulariesCommon vocabulary = new VocabulariesCommon();
129         vocabulary.setDisplayName(displayName);
130         vocabulary.setVocabType(vocabType);
131         MultipartOutput multipart = new MultipartOutput();
132         OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
133         commonPart.getHeaders().add("label", client.getCommonPartName());
134
135         if(logger.isDebugEnabled()){
136                 logger.debug("to be created, vocabulary common ", 
137                                         vocabulary, VocabulariesCommon.class);
138         }
139
140         return multipart;
141     }
142
143     private MultipartOutput createVocabularyItemInstance(String inVocabulary, String displayName) {
144         VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
145         vocabularyItem.setInVocabulary(inVocabulary);
146         vocabularyItem.setDisplayName(displayName);
147         MultipartOutput multipart = new MultipartOutput();
148         OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);
149         commonPart.getHeaders().add("label", client.getItemCommonPartName());
150
151         if(logger.isDebugEnabled()){
152                 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);
153         }
154
155         return multipart;
156     }
157
158
159     /**
160      * Returns an error message indicating that the status code returned by a
161      * specific call to a service does not fall within a set of valid status
162      * codes for that service.
163      *
164      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
165      *
166      * @param statusCode  The invalid status code that was returned in the response,
167      *                    from submitting that type of request to the service.
168      *
169      * @return An error message.
170      */
171     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
172         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
173                 requestType.validStatusCodesAsString();
174     }
175
176     protected String extractId(ClientResponse<Response> res) {
177         MultivaluedMap<String, Object> mvm = res.getMetadata();
178         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
179         if(logger.isDebugEnabled()){
180                 logger.debug("extractId:uri=" + uri);
181         }
182         String[] segments = uri.split("/");
183         String id = segments[segments.length - 1];
184         if(logger.isDebugEnabled()){
185                 logger.debug("id=" + id);
186         }
187         return id;
188     }
189
190         public static void main(String[] args) {
191                         VocabularyBaseImport vbi = new VocabularyBaseImport();
192                         final String acquisitionMethodsVocabName = "Acquisition Methods";
193                         final String entryMethodsVocabName = "Entry Methods";
194                         final String entryReasonsVocabName = "Entry Reasons";
195                         final String responsibleDeptsVocabName = "Responsible Departments";
196
197                         List<String> acquisitionMethodsEnumValues = 
198                                 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
199                         List<String> entryMethodsEnumValues = 
200                                 Arrays.asList("In person","Post","Found on doorstep");
201                         List<String> entryReasonsEnumValues = 
202                                 Arrays.asList("Enquiry","Commission","Loan");
203                         List<String> respDeptNamesEnumValues = 
204                                 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
205                                                                                 "Ethnography","Herpetology","Media and Performance Art",
206                                                                                 "Paintings and Sculpture","Paleobotany","Photographs",
207                                                                                 "Prints and Drawings");
208
209                         vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
210                         vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
211                         vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
212                         vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
213                 }
214 }