]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0ea5fea498fe01264ede3f985226e9e859190c96
[tmp/jakarta-migration.git] /
1 /**\r
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
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c)) 2009 Regents of the University of California\r
10  *\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
13  *\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
16  *\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
22  */\r
23 \r
24 package org.collectionspace.services.vocabulary.client.sample;\r
25 \r
26 import java.util.ArrayList;\r
27 import java.util.Arrays;\r
28 import java.util.List;\r
29 \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
33 \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.VocabulariesCommonList;\r
39 import org.collectionspace.services.vocabulary.VocabulariesCommonList.VocabularyListItem;\r
40 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;\r
41 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;\r
42 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList.VocabularyitemListItem;\r
43 import org.jboss.resteasy.client.ClientResponse;\r
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
46 import org.slf4j.Logger;\r
47 import org.slf4j.LoggerFactory;\r
48 \r
49 /**\r
50  * VocabularyServiceTest, carries out tests against a\r
51  * deployed and running Vocabulary Service.\r
52  *\r
53  * $LastChangedRevision: 753 $\r
54  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $\r
55  */\r
56 public class Sample {\r
57     private static final Logger logger =\r
58         LoggerFactory.getLogger(Sample.class);\r
59 \r
60     // Instance variables specific to this test.\r
61     private VocabularyClient client = new VocabularyClient();\r
62     final String SERVICE_PATH_COMPONENT = "vocabularies";\r
63     final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
64 \r
65 \r
66     // Create\r
67 \r
68     public void createEnumeration(String vocabName, List<String> enumValues ) {\r
69 \r
70         // Expected status code: 201 Created\r
71         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
72         // Type of service request being tested\r
73         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
74 \r
75         logger.info("Import: Create vocabulary: \"" + vocabName +"\"");\r
76         MultipartOutput multipart = createVocabularyInstance(vocabName, \r
77                         createRefName(vocabName), "enum");\r
78         ClientResponse<Response> res = client.create(multipart);\r
79 \r
80         int statusCode = res.getStatus();\r
81 \r
82         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
83                 throw new RuntimeException("Could not create enumeration: \""+vocabName\r
84                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
85         }\r
86         if(statusCode != EXPECTED_STATUS_CODE) {\r
87                 throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
88                                 +vocabName +"\", Status:"+ statusCode);\r
89         }\r
90 \r
91         // Store the ID returned from this create operation\r
92         // for additional tests below.\r
93         String newVocabId = extractId(res);\r
94         logger.info("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
95                                 +newVocabId );\r
96         \r
97         // Add items to the vocabulary\r
98         for(String itemName : enumValues){\r
99                 createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));\r
100         }\r
101         \r
102     }\r
103     \r
104     private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {\r
105         // Expected status code: 201 Created\r
106         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
107         // Type of service request being tested\r
108         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
109 \r
110         logger.info("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");\r
111         MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);\r
112         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
113 \r
114         int statusCode = res.getStatus();\r
115 \r
116         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
117                 throw new RuntimeException("Could not create Item: \""+itemName\r
118                                 +"\" in vocabulary: \"" + vocabName\r
119                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
120         }\r
121         if(statusCode != EXPECTED_STATUS_CODE) {\r
122                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName\r
123                                 +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);\r
124         }\r
125 \r
126         return extractId(res);\r
127     }\r
128 \r
129    // Read\r
130 \r
131    private VocabulariesCommonList readVocabularies() {\r
132 \r
133         // Expected status code: 200 OK\r
134         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
135         // Type of service request being tested\r
136         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
137 \r
138         // Submit the request to the service and store the response.\r
139         ClientResponse<VocabulariesCommonList> res = client.readList();\r
140         VocabulariesCommonList list = res.getEntity();\r
141 \r
142         int statusCode = res.getStatus();\r
143         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
144                 throw new RuntimeException("Could not read list of vocabularies: "\r
145                 + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
146         }\r
147         if(statusCode != EXPECTED_STATUS_CODE) {\r
148                 throw new RuntimeException("Unexpected Status when reading " +\r
149                 "list of vocabularies, Status:"+ statusCode);\r
150         }\r
151 \r
152         return list;\r
153 \r
154     }\r
155 /*\r
156     private List<String> readVocabularyIds() {\r
157 \r
158         VocabulariesCommonList items = readVocabularies();\r
159 \r
160         List<String> ids;\r
161         for (VocabulariesCommonList.VocabularyListItem item : items) {\r
162             ids.add(item.getCsid());\r
163         }\r
164 \r
165         return list;\r
166 \r
167     }\r
168 */\r
169     private VocabularyitemsCommonList readItemsInVocab(String vocabId) {\r
170 \r
171         // Expected status code: 200 OK\r
172         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
173         // Type of service request being tested\r
174         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
175 \r
176         // Submit the request to the service and store the response.\r
177         ClientResponse<VocabularyitemsCommonList> res =\r
178                 client.readItemList(vocabId);\r
179         VocabularyitemsCommonList list = res.getEntity();\r
180 \r
181         int statusCode = res.getStatus();\r
182 \r
183         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
184                 throw new RuntimeException("Could not read items in vocabulary: "\r
185                 + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
186         }\r
187         if(statusCode != EXPECTED_STATUS_CODE) {\r
188                 throw new RuntimeException("Unexpected Status when reading " +\r
189                 "items in vocabulary, Status:"+ statusCode);\r
190         }\r
191 \r
192         return list;\r
193 \r
194     }\r
195 \r
196     // ---------------------------------------------------------------\r
197     // Utility methods used by tests above\r
198     // ---------------------------------------------------------------\r
199 \r
200     private MultipartOutput createVocabularyInstance(\r
201                 String displayName, String refName, String vocabType) {\r
202         VocabulariesCommon vocabulary = new VocabulariesCommon();\r
203         vocabulary.setDisplayName(displayName);\r
204         vocabulary.setRefName(refName);\r
205         vocabulary.setVocabType(vocabType);\r
206         MultipartOutput multipart = new MultipartOutput();\r
207         OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);\r
208         commonPart.getHeaders().add("label", client.getCommonPartName());\r
209 \r
210         if(logger.isDebugEnabled()){\r
211                 logger.debug("to be created, vocabulary common ",\r
212                                         vocabulary, VocabulariesCommon.class);\r
213         }\r
214 \r
215         return multipart;\r
216     }\r
217 \r
218     private MultipartOutput createVocabularyItemInstance(\r
219                 String inVocabulary, String displayName, String refName) {\r
220         VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();\r
221         vocabularyItem.setInVocabulary(inVocabulary);\r
222         vocabularyItem.setDisplayName(displayName);\r
223         vocabularyItem.setRefName(refName);\r
224         MultipartOutput multipart = new MultipartOutput();\r
225         OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);\r
226         commonPart.getHeaders().add("label", client.getItemCommonPartName());\r
227 \r
228         if(logger.isDebugEnabled()){\r
229                 logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);\r
230         }\r
231 \r
232         return multipart;\r
233     }\r
234 \r
235     // Retrieve individual fields of vocabulary records.\r
236 \r
237     private String displayVocabularyListDetails(VocabulariesCommonList list) {\r
238         StringBuffer sb = new StringBuffer();\r
239             List<VocabulariesCommonList.VocabularyListItem> items =\r
240                     list.getVocabularyListItem();\r
241             int i = 0;\r
242         for (VocabulariesCommonList.VocabularyListItem item : items) {\r
243             sb.append("vocabulary [" + i + "]" + "\n");\r
244             sb.append(displayVocabularyDetails(item));\r
245             i++;\r
246         }\r
247         return sb.toString();\r
248     }\r
249 \r
250     private String displayVocabularyDetails(\r
251         VocabulariesCommonList.VocabularyListItem item) {\r
252             StringBuffer sb = new StringBuffer();\r
253             sb.append("csid=" + item.getCsid() + "\n");\r
254             sb.append("displayName=" + item.getDisplayName() + "\n");\r
255             sb.append("URI=" + item.getUri() + "\n");\r
256         return sb.toString();\r
257     }\r
258 \r
259     // Retrieve individual fields of vocabulary item records.\r
260 \r
261     private String displayVocabularyItemListDetails(VocabularyitemsCommonList list) {\r
262         StringBuffer sb = new StringBuffer();\r
263         List<VocabularyitemListItem> items =\r
264                 list.getVocabularyitemListItem();\r
265         int i = 0;\r
266         for (VocabularyitemListItem item : items) {\r
267             sb.append("vocabulary item [" + i + "]" + "\n");\r
268             sb.append(displayVocabularyItemDetails(item));\r
269             i++;\r
270         }\r
271         return sb.toString();\r
272     }\r
273 \r
274     private String displayVocabularyItemDetails(\r
275         VocabularyitemsCommonList.VocabularyitemListItem item) {\r
276             StringBuffer sb = new StringBuffer();\r
277             sb.append("csid=" + item.getCsid() + "\n");\r
278             sb.append("displayName=" + item.getDisplayName() + "\n");\r
279             sb.append("URI=" + item.getUri() + "\n");\r
280         return sb.toString();\r
281     }\r
282 \r
283     /**\r
284      * Returns an error message indicating that the status code returned by a\r
285      * specific call to a service does not fall within a set of valid status\r
286      * codes for that service.\r
287      *\r
288      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
289      *\r
290      * @param statusCode  The invalid status code that was returned in the response,\r
291      *                    from submitting that type of request to the service.\r
292      *\r
293      * @return An error message.\r
294      */\r
295     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
296         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
297                 requestType.validStatusCodesAsString();\r
298     }\r
299 \r
300     protected String extractId(ClientResponse<Response> res) {\r
301         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
302         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
303         if(logger.isDebugEnabled()){\r
304                 logger.info("extractId:uri=" + uri);\r
305         }\r
306         String[] segments = uri.split("/");\r
307         String id = segments[segments.length - 1];\r
308         if(logger.isDebugEnabled()){\r
309                 logger.debug("id=" + id);\r
310         }\r
311         return id;\r
312     }\r
313     \r
314     protected String createRefName(String displayName) {\r
315         return displayName.replaceAll("\\W", "");\r
316     }\r
317 \r
318         public static void main(String[] args) {\r
319                 \r
320                 BasicConfigurator.configure();\r
321                 logger.info("VocabularyBaseImport starting...");\r
322 \r
323 \r
324                 Sample vbi = new Sample();\r
325                 final String acquisitionMethodsVocabName = "Acquisition Methods";\r
326                 final String entryMethodsVocabName = "Entry Methods";\r
327                 final String entryReasonsVocabName = "Entry Reasons";\r
328                 final String responsibleDeptsVocabName = "Responsible Departments";\r
329 \r
330                 List<String> acquisitionMethodsEnumValues = \r
331                         Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");\r
332                 List<String> entryMethodsEnumValues = \r
333                         Arrays.asList("In person","Post","Found on doorstep");\r
334                 List<String> entryReasonsEnumValues = \r
335                         Arrays.asList("Enquiry","Commission","Loan");\r
336                 List<String> respDeptNamesEnumValues = \r
337                         Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",\r
338                                                                         "Ethnography","Herpetology","Media and Performance Art",\r
339                                                                         "Paintings and Sculpture","Paleobotany","Photographs",\r
340                                                                         "Prints and Drawings");\r
341 \r
342                 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);\r
343                 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);\r
344                 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);\r
345                 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);\r
346 \r
347                 logger.info("VocabularyBaseImport complete.");\r
348 \r
349         logger.info("Reading newly-created vocabularies ...");\r
350 \r
351         // Read vocabularies.\r
352         VocabulariesCommonList vocabularies = vbi.readVocabularies();\r
353         String details = vbi.displayVocabularyListDetails(vocabularies);\r
354         logger.info(details);\r
355 \r
356         // Read items in each vocabulary.\r
357 //        readItemsInVocab(newVocabId);\r
358 \r
359         // Delete vocabulary items.\r
360 \r
361         // Delete vocabularies.\r
362         }\r
363 \r
364 }\r