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