]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f49bcf89992a9bb47663a73a997dfca3b51bd68f
[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.HashMap;\r
29 import java.util.List;\r
30 \r
31 import javax.ws.rs.core.MediaType;\r
32 import javax.ws.rs.core.MultivaluedMap;\r
33 import javax.ws.rs.core.Response;\r
34 \r
35 import org.apache.log4j.BasicConfigurator;\r
36 //import org.collectionspace.services.VocabularyItemJAXBSchema; \r
37 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;\r
38 \r
39 import org.collectionspace.services.client.VocabularyClient;\r
40 import org.collectionspace.services.client.VocabularyClientUtils;\r
41 import org.collectionspace.services.client.test.ServiceRequestType;\r
42 import org.collectionspace.services.vocabulary.VocabulariesCommon;\r
43 import org.collectionspace.services.vocabulary.VocabulariesCommonList;\r
44 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;\r
45 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;\r
46 import org.jboss.resteasy.client.ClientResponse;\r
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
49 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
50 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
51 import org.slf4j.Logger;\r
52 import org.slf4j.LoggerFactory;\r
53 \r
54 /**\r
55  * VocabularyServiceTest, carries out tests against a\r
56  * deployed and running Vocabulary Service.\r
57  *\r
58  * $LastChangedRevision$\r
59  * $LastChangedDate$\r
60  */\r
61 public class Sample {\r
62     private static final Logger logger =\r
63         LoggerFactory.getLogger(Sample.class);\r
64 \r
65     // Instance variables specific to this test.\r
66     private VocabularyClient client = new VocabularyClient();\r
67     final String SERVICE_PATH_COMPONENT = "vocabularies";\r
68     final String ITEM_SERVICE_PATH_COMPONENT = "items";\r
69 \r
70 \r
71     // ---------------------------------------------------------------\r
72     // Create\r
73     // ---------------------------------------------------------------\r
74 \r
75     public void createEnumeration(String vocabName, List<String> enumValues ) {\r
76 \r
77         // Expected status code: 201 Created\r
78         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
79         // Type of service request being tested\r
80         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
81 \r
82         if(logger.isDebugEnabled()){\r
83                 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");\r
84         }\r
85 \r
86         String displaySuffix = "displayName-" + System.currentTimeMillis(); //TODO: Laramie20100728 temp fix, made-up displaySuffix.\r
87         String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, displaySuffix);   //TODO: Laramie20100728 temp fix  was vocabName, false\r
88         String fullVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, displaySuffix);   //TODO: Laramie20100728 temp fix  was vocabName, true\r
89         MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(\r
90                         vocabName, fullVocabRefName, client.getCommonPartName());\r
91         ClientResponse<Response> res = client.create(multipart);\r
92 \r
93         int statusCode = res.getStatus();\r
94 \r
95         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
96                 throw new RuntimeException("Could not create enumeration: \""+vocabName\r
97                                 +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
98         }\r
99         if(statusCode != EXPECTED_STATUS_CODE) {\r
100                 throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
101                                 +vocabName +"\", Status:"+ statusCode);\r
102         }\r
103 \r
104         // Store the ID returned from this create operation\r
105         // for additional tests below.\r
106         String newVocabId = VocabularyClientUtils.extractId(res);\r
107         if(logger.isDebugEnabled()){\r
108                 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
109                                 +newVocabId );\r
110         }\r
111         for(String itemName : enumValues){\r
112             HashMap<String, String> itemInfo = new HashMap<String, String>();\r
113             itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, itemName);\r
114                 VocabularyClientUtils.createItemInVocabulary(newVocabId, \r
115                                 baseVocabRefName, itemInfo, client);\r
116         }\r
117     }\r
118     \r
119    // ---------------------------------------------------------------\r
120    // Read\r
121    // ---------------------------------------------------------------\r
122 \r
123    private VocabulariesCommonList readVocabularies() {\r
124 \r
125         // Expected status code: 200 OK\r
126         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
127         // Type of service request being tested\r
128         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
129 \r
130         // Submit the request to the service and store the response.\r
131         ClientResponse<VocabulariesCommonList> res = client.readList();\r
132         VocabulariesCommonList list = res.getEntity();\r
133 \r
134         int statusCode = res.getStatus();\r
135         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
136                 throw new RuntimeException("Could not read list of vocabularies: "\r
137                 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
138         }\r
139         if(statusCode != EXPECTED_STATUS_CODE) {\r
140                 throw new RuntimeException("Unexpected Status when reading " +\r
141                 "list of vocabularies, Status:"+ statusCode);\r
142         }\r
143 \r
144         return list;\r
145    }\r
146 \r
147     private List<String> readVocabularyIds(VocabulariesCommonList list) {\r
148 \r
149         List<String> ids = new ArrayList<String>();\r
150         List<VocabulariesCommonList.VocabularyListItem> vocabularies =\r
151             list.getVocabularyListItem();\r
152         for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {\r
153             ids.add(vocabulary.getCsid());\r
154         }\r
155         return ids;\r
156    }\r
157     \r
158    private VocabulariesCommon readVocabulary(String vocabId) {\r
159 \r
160         // Expected status code: 200 OK\r
161         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
162         // Type of service request being tested\r
163         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
164 \r
165         // Submit the request to the service and store the response.\r
166         VocabulariesCommon vocabulary = null;\r
167         try {\r
168             ClientResponse<MultipartInput> res = client.read(vocabId);\r
169             int statusCode = res.getStatus();\r
170             if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
171                 throw new RuntimeException("Could not read vocabulary"\r
172                     + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
173             }\r
174             if(statusCode != EXPECTED_STATUS_CODE) {\r
175                 throw new RuntimeException("Unexpected Status when reading " +\r
176                     "vocabulary, Status:"+ statusCode);\r
177             }\r
178             MultipartInput input = (MultipartInput) res.getEntity();\r
179             vocabulary = (VocabulariesCommon) extractPart(input,\r
180                     client.getCommonPartName(), VocabulariesCommon.class);\r
181         } catch (Exception e) {\r
182             throw new RuntimeException("Could not read vocabulary: ", e);\r
183         }\r
184 \r
185         return vocabulary;\r
186     }\r
187 \r
188     private VocabularyitemsCommonList readItemsInVocab(String vocabId) {\r
189 \r
190         // Expected status code: 200 OK\r
191         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
192         // Type of service request being tested\r
193         ServiceRequestType REQUEST_TYPE = ServiceRequestType.READ;\r
194 \r
195         // Submit the request to the service and store the response.\r
196                           //  readItemList(String inAuthority, String partialTerm, String keywords)\r
197         ClientResponse<VocabularyitemsCommonList> res =  client.readItemList(vocabId, "", ""); //TODO: figure out these params.  I just put in empty string to make it recompile after refactoring.  Laramie20100728\r
198         VocabularyitemsCommonList list = res.getEntity();\r
199 \r
200         int statusCode = res.getStatus();\r
201 \r
202         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
203                 throw new RuntimeException("Could not read items in vocabulary: "\r
204                 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
205         }\r
206         if(statusCode != EXPECTED_STATUS_CODE) {\r
207                 throw new RuntimeException("Unexpected Status when reading " +\r
208                 "items in vocabulary, Status:"+ statusCode);\r
209         }\r
210 \r
211         return list;\r
212     }\r
213 \r
214     private List<String> readVocabularyItemIds(VocabularyitemsCommonList list) {\r
215 \r
216         List<String> ids = new ArrayList<String>();\r
217         List<VocabularyitemsCommonList.VocabularyitemListItem> items =\r
218             list.getVocabularyitemListItem();\r
219         for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {\r
220             ids.add(item.getCsid());\r
221         }\r
222         return ids;\r
223    }\r
224 \r
225     // ---------------------------------------------------------------\r
226     // Delete\r
227     // ---------------------------------------------------------------\r
228 \r
229     private void deleteVocabulary(String vcsid) {\r
230          // Expected status code: 200 OK\r
231         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
232         // Type of service request being tested\r
233         ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;\r
234 \r
235         ClientResponse<Response> res = client.delete(vcsid);\r
236         int statusCode = res.getStatus();\r
237 \r
238         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
239                 throw new RuntimeException("Could not delete vocabulary: "\r
240                 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
241         }\r
242         if(statusCode != EXPECTED_STATUS_CODE) {\r
243                 throw new RuntimeException("Unexpected Status when deleting " +\r
244                 "vocabulary, Status:"+ statusCode);\r
245         }\r
246     }\r
247 \r
248     private void deleteAllVocabularies() {\r
249         List<String> ids = readVocabularyIds(readVocabularies());\r
250         for (String id : ids) {\r
251             deleteVocabulary(id);\r
252         }\r
253     }\r
254 \r
255         private void deleteVocabularyItem(String vcsid, String itemcsid) {\r
256          // Expected status code: 200 OK\r
257         int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();\r
258         // Type of service request being tested\r
259         ServiceRequestType REQUEST_TYPE = ServiceRequestType.DELETE;\r
260 \r
261         ClientResponse<Response> res = client.deleteItem(vcsid, itemcsid);\r
262         int statusCode = res.getStatus();\r
263 \r
264         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
265                 throw new RuntimeException("Could not delete vocabulary item: "\r
266                 + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
267         }\r
268         if(statusCode != EXPECTED_STATUS_CODE) {\r
269                 throw new RuntimeException("Unexpected Status when deleting " +\r
270                 "vocabulary item, Status:"+ statusCode);\r
271         }\r
272     }\r
273 \r
274     private void deleteAllItemsForVocab(String vocabId) {\r
275         List<String> itemIds = readVocabularyItemIds(readItemsInVocab(vocabId));\r
276         for (String itemId : itemIds) {\r
277             deleteVocabularyItem(vocabId, itemId);\r
278         }\r
279     }\r
280 \r
281     // ---------------------------------------------------------------\r
282     // Utility methods used by tests above\r
283     // ---------------------------------------------------------------\r
284 \r
285     // Retrieve individual fields of vocabulary records.\r
286 \r
287     private String displayAllVocabularies(VocabulariesCommonList list) {\r
288         StringBuffer sb = new StringBuffer();\r
289             List<VocabulariesCommonList.VocabularyListItem> vocabularies =\r
290                     list.getVocabularyListItem();\r
291             int i = 0;\r
292         for (VocabulariesCommonList.VocabularyListItem vocabulary : vocabularies) {\r
293             sb.append("vocabulary [" + i + "]" + "\n");\r
294             sb.append(displayVocabularyDetails(vocabulary));\r
295             i++;\r
296         }\r
297         return sb.toString();\r
298     }\r
299 \r
300     private String displayVocabularyDetails(\r
301         VocabulariesCommonList.VocabularyListItem vocabulary) {\r
302             StringBuffer sb = new StringBuffer();\r
303             sb.append("displayName=" + vocabulary.getDisplayName() + "\n");\r
304             sb.append("vocabType=" + vocabulary.getVocabType() + "\n");\r
305             // sb.append("csid=" + vocabulary.getCsid() + "\n");\r
306             sb.append("URI=" + vocabulary.getUri() + "\n");\r
307         return sb.toString();\r
308     }\r
309 \r
310     // Retrieve individual fields of vocabulary item records.\r
311 \r
312     private String displayAllVocabularyItems(VocabularyitemsCommonList list) {\r
313         StringBuffer sb = new StringBuffer();\r
314         List<VocabularyitemsCommonList.VocabularyitemListItem> items =\r
315                 list.getVocabularyitemListItem();\r
316         int i = 0;\r
317         for (VocabularyitemsCommonList.VocabularyitemListItem item : items) {\r
318             sb.append("vocabulary item [" + i + "]" + "\n");\r
319             sb.append(displayVocabularyItemDetails(item));\r
320             i++;\r
321         }\r
322         return sb.toString();\r
323     }\r
324 \r
325     private String displayVocabularyItemDetails(\r
326         VocabularyitemsCommonList.VocabularyitemListItem item) {\r
327             StringBuffer sb = new StringBuffer();\r
328             sb.append("csid=" + item.getCsid() + "\n");\r
329             sb.append("displayName=" + item.getDisplayName() + "\n");\r
330             // sb.append("URI=" + item.getUri() + "\n");\r
331         return sb.toString();\r
332     }\r
333 \r
334     // TODO this should be moved to a common utils class\r
335     private Object extractPart(MultipartInput input, String label,\r
336         Class clazz) throws Exception {\r
337         Object obj = null;\r
338         for(InputPart part : input.getParts()){\r
339             String partLabel = part.getHeaders().getFirst("label");\r
340             if(label.equalsIgnoreCase(partLabel)){\r
341                 String partStr = part.getBodyAsString();\r
342                 if(logger.isDebugEnabled()){\r
343                     logger.debug("extracted part str=\n" + partStr);\r
344                 }\r
345                 obj = part.getBody(clazz, null);\r
346                 if(logger.isDebugEnabled()){\r
347                     logger.debug("extracted part obj=\n", obj, clazz);\r
348                 }\r
349                 break;\r
350             }\r
351         }\r
352         return obj;\r
353     }\r
354 \r
355         public static void main(String[] args) {\r
356 \r
357         // Configure logging.\r
358                 BasicConfigurator.configure();\r
359 \r
360         logger.info("VocabularyBaseImport starting...");\r
361 \r
362                 Sample vbi = new Sample();\r
363         VocabulariesCommonList vocabularies;\r
364         List<String> vocabIds;\r
365         String details = "";\r
366 \r
367         // Optionally delete all vocabularies and vocabulary items.\r
368 \r
369         boolean ENABLE_DELETE_ALL = false;\r
370         if (ENABLE_DELETE_ALL) {\r
371 \r
372             logger.info("Deleting all vocabulary items and vocabularies ...");\r
373 \r
374             // For each vocabulary ...\r
375             vocabularies = vbi.readVocabularies();\r
376             vocabIds = vbi.readVocabularyIds(vocabularies);\r
377             for (String vocabId : vocabIds) {\r
378                 logger.info("Deleting all vocabulary items for vocabulary ...");\r
379                 vbi.deleteAllItemsForVocab(vocabId);\r
380                 logger.info("Deleting vocabulary ...");\r
381                 vbi.deleteVocabulary(vocabId);\r
382             }\r
383 \r
384             logger.info("Reading vocabularies after deletion ...");\r
385             vocabularies = vbi.readVocabularies();\r
386             details = vbi.displayAllVocabularies(vocabularies);\r
387             logger.info(details);\r
388 \r
389             logger.info("Reading items in each vocabulary after deletion ...");\r
390             vocabIds = vbi.readVocabularyIds(vocabularies);\r
391             for (String vocabId : vocabIds) {\r
392                 VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);\r
393                 details = vbi.displayAllVocabularyItems(items);\r
394                 logger.info(details);\r
395             }\r
396 \r
397         }\r
398 \r
399         // Create new vocabularies, each populated with vocabulary items.\r
400 \r
401                 final String acquisitionMethodsVocabName = "Acquisition Methods";\r
402                 final String entryMethodsVocabName = "Entry Methods";\r
403                 final String entryReasonsVocabName = "Entry Reasons";\r
404                 final String responsibleDeptsVocabName = "Responsible Departments";\r
405 \r
406                 List<String> acquisitionMethodsEnumValues = \r
407                         Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");\r
408                 List<String> entryMethodsEnumValues = \r
409                         Arrays.asList("In person","Post","Found on doorstep");\r
410                 List<String> entryReasonsEnumValues = \r
411                         Arrays.asList("Enquiry","Commission","Loan");\r
412                 List<String> respDeptNamesEnumValues = \r
413                         Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",\r
414                                                                         "Ethnography","Herpetology","Media and Performance Art",\r
415                                                                         "Paintings and Sculpture","Paleobotany","Photographs",\r
416                                                                         "Prints and Drawings");\r
417         \r
418                 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);\r
419                 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);\r
420                 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);\r
421                 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);\r
422 \r
423                 logger.info("VocabularyBaseImport complete.");\r
424 \r
425         logger.info("Reading vocabularies and items ...");\r
426         // Get a list of vocabularies.\r
427         vocabularies = vbi.readVocabularies();\r
428         // For each vocabulary ...\r
429         for (VocabulariesCommonList.VocabularyListItem\r
430             vocabulary : vocabularies.getVocabularyListItem()) {\r
431             // Get its display name.\r
432             logger.info(vocabulary.getDisplayName());\r
433             // Get a list of the vocabulary items in this vocabulary.\r
434             VocabularyitemsCommonList items =\r
435                 vbi.readItemsInVocab(vocabulary.getCsid());\r
436             // For each vocabulary item ...\r
437             for (VocabularyitemsCommonList.VocabularyitemListItem\r
438                 item : items.getVocabularyitemListItem()) {\r
439                 // Get its display name.\r
440                 logger.info(" " + item.getDisplayName());\r
441             }\r
442         }\r
443 \r
444         // Sample alternate methods of reading all vocabularies and\r
445         // vocabulary items separately.\r
446         boolean RUN_ADDITIONAL_SAMPLES = false;\r
447         if (RUN_ADDITIONAL_SAMPLES) {\r
448 \r
449             logger.info("Reading all vocabularies ...");\r
450             details = vbi.displayAllVocabularies(vocabularies);\r
451             logger.info(details);\r
452 \r
453             logger.info("Reading all vocabulary items ...");\r
454             vocabIds = vbi.readVocabularyIds(vocabularies);\r
455             for (String vocabId : vocabIds) {\r
456                 VocabularyitemsCommonList items = vbi.readItemsInVocab(vocabId);\r
457                 details = vbi.displayAllVocabularyItems(items);\r
458                 logger.info(details);\r
459             }\r
460 \r
461         }\r
462 \r
463         }\r
464 \r
465 }\r