]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
09ca4808eb0371c0693a022ffc80c79562d5bcbb
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;\r
2 \r
3 import java.io.File;\r
4 import java.util.ArrayList;\r
5 import java.util.Arrays;\r
6 import java.util.List;\r
7 import java.util.Map;\r
8 \r
9 import javax.ws.rs.core.MediaType;\r
10 import javax.ws.rs.core.MultivaluedMap;\r
11 import javax.ws.rs.core.Response;\r
12 \r
13 import org.apache.commons.io.FileUtils;\r
14 import org.collectionspace.services.LocationJAXBSchema;\r
15 import org.collectionspace.services.client.test.ServiceRequestType;\r
16 import org.collectionspace.services.location.LocationsCommon;\r
17 import org.collectionspace.services.location.LocationauthoritiesCommon;\r
18 import org.jboss.resteasy.client.ClientResponse;\r
19 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
20 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 import org.testng.Assert;\r
24 \r
25 public class LocationAuthorityClientUtils {\r
26     private static final Logger logger =\r
27         LoggerFactory.getLogger(LocationAuthorityClientUtils.class);\r
28 \r
29     /**\r
30      * Creates a new Location Authority\r
31      * @param displayName       The displayName used in UI, etc.\r
32      * @param refName           The proper refName for this authority\r
33      * @param headerLabel       The common part label\r
34      * @return  The MultipartOutput payload for the create call\r
35      */\r
36     public static MultipartOutput createLocationAuthorityInstance(\r
37                 String displayName, String shortIdentifier, String headerLabel ) {\r
38         LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();\r
39         locationAuthority.setDisplayName(displayName);\r
40         locationAuthority.setShortIdentifier(shortIdentifier);\r
41         String refName = createLocationAuthRefName(shortIdentifier, displayName);\r
42         locationAuthority.setRefName(refName);\r
43         locationAuthority.setVocabType("LocationAuthority");\r
44         MultipartOutput multipart = new MultipartOutput();\r
45         OutputPart commonPart = multipart.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);\r
46         commonPart.getHeaders().add("label", headerLabel);\r
47 \r
48         if(logger.isDebugEnabled()){\r
49                 logger.debug("to be created, locationAuthority common ", \r
50                                         locationAuthority, LocationauthoritiesCommon.class);\r
51         }\r
52 \r
53         return multipart;\r
54     }\r
55 \r
56     /**\r
57      * @param inAuthority CSID of the authority to create a new location in\r
58      * @param locationRefName  The proper refName for this authority\r
59      * @param locationInfo the properties for the new Location\r
60      * @param headerLabel       The common part label\r
61      * @return  The MultipartOutput payload for the create call\r
62      */\r
63     public static MultipartOutput createLocationInstance(String inAuthority, \r
64                 String locationAuthRefName, Map<String, String> locationInfo, String headerLabel){\r
65         LocationsCommon location = new LocationsCommon();\r
66         String shortId = locationInfo.get(LocationJAXBSchema.SHORT_IDENTIFIER);\r
67         String displayName = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME);\r
68         location.setShortIdentifier(shortId);\r
69         location.setInAuthority(inAuthority);\r
70         String locationRefName = createLocationRefName(locationAuthRefName, shortId, displayName);\r
71         location.setRefName(locationRefName);\r
72         String value = null;\r
73         value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
74         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
75         location.setDisplayNameComputed(displayNameComputed);\r
76         if((value = (String)locationInfo.get(LocationJAXBSchema.NAME))!=null)\r
77                 location.setName(value);\r
78         if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null)\r
79                 location.setConditionNote(value);\r
80         if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)\r
81                 location.setConditionNoteDate(value);\r
82         if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)\r
83                 location.setSecurityNote(value);\r
84         if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)\r
85                 location.setLocationType(value);\r
86         if((value = (String)locationInfo.get(LocationJAXBSchema.STATUS))!=null)\r
87                 location.setStatus(value);\r
88         MultipartOutput multipart = new MultipartOutput();\r
89         OutputPart commonPart = multipart.addPart(location,\r
90             MediaType.APPLICATION_XML_TYPE);\r
91         commonPart.getHeaders().add("label", headerLabel);\r
92 \r
93         if(logger.isDebugEnabled()){\r
94                 logger.debug("to be created, location common ", location, LocationsCommon.class);\r
95         }\r
96 \r
97         return multipart;\r
98     }\r
99     \r
100     /**\r
101      * @param vcsid CSID of the authority to create a new location in\r
102      * @param locationAuthorityRefName The refName for the authority\r
103      * @param locationMap the properties for the new Location\r
104      * @param client the service client\r
105      * @return the CSID of the new item\r
106      */\r
107     public static String createItemInAuthority(String vcsid, \r
108                 String locationAuthorityRefName, Map<String,String> locationMap,\r
109                 LocationAuthorityClient client ) {\r
110         // Expected status code: 201 Created\r
111         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
112         // Type of service request being tested\r
113         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
114         \r
115         String displayName = locationMap.get(LocationJAXBSchema.DISPLAY_NAME);\r
116         String displayNameComputedStr = locationMap.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
117         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
118         if( displayName == null ) {\r
119                 if(!displayNameComputed) {\r
120                         throw new RuntimeException(\r
121                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
122                 }\r
123                 displayName = \r
124                         prepareDefaultDisplayName(\r
125                         locationMap.get(LocationJAXBSchema.NAME));\r
126         }\r
127         \r
128         if(logger.isDebugEnabled()){\r
129                 logger.debug("Import: Create Item: \""+displayName\r
130                                 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\"");\r
131         }\r
132         MultipartOutput multipart = \r
133                 createLocationInstance( vcsid, locationAuthorityRefName,\r
134                         locationMap, client.getItemCommonPartName() );\r
135         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
136 \r
137         int statusCode = res.getStatus();\r
138 \r
139         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
140                 throw new RuntimeException("Could not create Item: \""\r
141                                 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
142                                 +"\" in locationAuthority: \"" + locationAuthorityRefName\r
143                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
144         }\r
145         if(statusCode != EXPECTED_STATUS_CODE) {\r
146                 throw new RuntimeException("Unexpected Status when creating Item: \""\r
147                                 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
148                                 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);\r
149         }\r
150 \r
151         return extractId(res);\r
152     }\r
153 \r
154     public static MultipartOutput createLocationInstance(\r
155                 String commonPartXML, String headerLabel){\r
156         MultipartOutput multipart = new MultipartOutput();\r
157         OutputPart commonPart = multipart.addPart(commonPartXML,\r
158             MediaType.APPLICATION_XML_TYPE);\r
159         commonPart.getHeaders().add("label", headerLabel);\r
160 \r
161         if(logger.isDebugEnabled()){\r
162                 logger.debug("to be created, location common ", commonPartXML);\r
163         }\r
164 \r
165         return multipart;\r
166     }\r
167     \r
168     public static String createItemInAuthority(String vcsid,\r
169                 String commonPartXML,\r
170                 LocationAuthorityClient client ) {\r
171         // Expected status code: 201 Created\r
172         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
173         // Type of service request being tested\r
174         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
175         \r
176         MultipartOutput multipart = \r
177                 createLocationInstance( commonPartXML, client.getItemCommonPartName() );\r
178         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
179 \r
180         int statusCode = res.getStatus();\r
181 \r
182         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
183                 throw new RuntimeException("Could not create Item: \""+commonPartXML\r
184                                 +"\" in locationAuthority: \"" + vcsid\r
185                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
186         }\r
187         if(statusCode != EXPECTED_STATUS_CODE) {\r
188                 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML\r
189                                 +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);\r
190         }\r
191 \r
192         return extractId(res);\r
193     }\r
194     \r
195     /**\r
196      * Creates the from xml file.\r
197      *\r
198      * @param fileName the file name\r
199      * @return new CSID as string\r
200      * @throws Exception the exception\r
201      */\r
202     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, \r
203                 LocationAuthorityClient client) throws Exception {\r
204         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));\r
205         String commonPartXML = new String(b);\r
206         return createItemInAuthority(vcsid, commonPartXML, client );\r
207     }    \r
208 \r
209     /**\r
210      * Creates the locationAuthority ref name.\r
211      *\r
212      * @param shortId the locationAuthority shortIdentifier\r
213      * @param displaySuffix displayName to be appended, if non-null\r
214      * @return the string\r
215      */\r
216     public static String createLocationAuthRefName(String shortId, String displaySuffix) {\r
217         String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("\r
218                         +shortId+")";\r
219                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
220                         refName += "'"+displaySuffix+"'";\r
221         return refName;\r
222     }\r
223 \r
224     /**\r
225      * Creates the location ref name.\r
226      *\r
227      * @param locationAuthRefName the locationAuthority ref name\r
228      * @param shortId the location shortIdentifier\r
229      * @param displaySuffix displayName to be appended, if non-null\r
230      * @return the string\r
231      */\r
232     public static String createLocationRefName(\r
233                                                 String locationAuthRefName, String shortId, String displaySuffix) {\r
234         String refName = locationAuthRefName+":location:name("+shortId+")";\r
235                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
236                         refName += "'"+displaySuffix+"'";\r
237         return refName;\r
238     }\r
239 \r
240     public static String extractId(ClientResponse<Response> res) {\r
241         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
242         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
243         if(logger.isDebugEnabled()){\r
244                 logger.debug("extractId:uri=" + uri);\r
245         }\r
246         String[] segments = uri.split("/");\r
247         String id = segments[segments.length - 1];\r
248         if(logger.isDebugEnabled()){\r
249                 logger.debug("id=" + id);\r
250         }\r
251         return id;\r
252     }\r
253     \r
254     /**\r
255      * Returns an error message indicating that the status code returned by a\r
256      * specific call to a service does not fall within a set of valid status\r
257      * codes for that service.\r
258      *\r
259      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
260      *\r
261      * @param statusCode  The invalid status code that was returned in the response,\r
262      *                    from submitting that type of request to the service.\r
263      *\r
264      * @return An error message.\r
265      */\r
266     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
267         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
268                 requestType.validStatusCodesAsString();\r
269     }\r
270 \r
271 \r
272     \r
273     /**\r
274      * Produces a default displayName from the basic name and dates fields.\r
275      * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which\r
276      * duplicates this logic, until we define a service-general utils package\r
277      * that is neither client nor service specific.\r
278      * @param name      \r
279      * @return\r
280      */\r
281     public static String prepareDefaultDisplayName(\r
282                 String name ) {\r
283         StringBuilder newStr = new StringBuilder();\r
284                         newStr.append(name);\r
285                 return newStr.toString();\r
286     }\r
287     \r
288 \r
289 \r
290 }\r