]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ae878daa891520c17417270efe3ae2946be1a40d
[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.TERM_STATUS))!=null)\r
87                 location.setTermStatus(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         String newID = null;\r
136         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
137         try {\r
138                 int statusCode = res.getStatus();\r
139         \r
140                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
141                         throw new RuntimeException("Could not create Item: \""\r
142                                         +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
143                                         +"\" in locationAuthority: \"" + locationAuthorityRefName\r
144                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
145                 }\r
146                 if(statusCode != EXPECTED_STATUS_CODE) {\r
147                         throw new RuntimeException("Unexpected Status when creating Item: \""\r
148                                         +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
149                                         +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);\r
150                 }\r
151                 newID = extractId(res);\r
152         } finally {\r
153                 res.releaseConnection();\r
154         }\r
155 \r
156         return newID;\r
157     }\r
158 \r
159     public static MultipartOutput createLocationInstance(\r
160                 String commonPartXML, String headerLabel){\r
161         MultipartOutput multipart = new MultipartOutput();\r
162         OutputPart commonPart = multipart.addPart(commonPartXML,\r
163             MediaType.APPLICATION_XML_TYPE);\r
164         commonPart.getHeaders().add("label", headerLabel);\r
165 \r
166         if(logger.isDebugEnabled()){\r
167                 logger.debug("to be created, location common ", commonPartXML);\r
168         }\r
169 \r
170         return multipart;\r
171     }\r
172     \r
173     public static String createItemInAuthority(String vcsid,\r
174                 String commonPartXML,\r
175                 LocationAuthorityClient client ) {\r
176         // Expected status code: 201 Created\r
177         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
178         // Type of service request being tested\r
179         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
180         \r
181         MultipartOutput multipart = \r
182                 createLocationInstance( commonPartXML, client.getItemCommonPartName() );\r
183         String newID = null;\r
184         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
185         try {\r
186                 int statusCode = res.getStatus();\r
187         \r
188                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
189                         throw new RuntimeException("Could not create Item: \""+commonPartXML\r
190                                         +"\" in locationAuthority: \"" + vcsid\r
191                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
192                 }\r
193                 if(statusCode != EXPECTED_STATUS_CODE) {\r
194                         throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML\r
195                                         +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);\r
196                 }\r
197                 newID = extractId(res);\r
198         } finally {\r
199                 res.releaseConnection();\r
200         }\r
201 \r
202         return newID;\r
203     }\r
204     \r
205     /**\r
206      * Creates the from xml file.\r
207      *\r
208      * @param fileName the file name\r
209      * @return new CSID as string\r
210      * @throws Exception the exception\r
211      */\r
212     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, \r
213                 LocationAuthorityClient client) throws Exception {\r
214         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));\r
215         String commonPartXML = new String(b);\r
216         return createItemInAuthority(vcsid, commonPartXML, client );\r
217     }    \r
218 \r
219     /**\r
220      * Creates the locationAuthority ref name.\r
221      *\r
222      * @param shortId the locationAuthority shortIdentifier\r
223      * @param displaySuffix displayName to be appended, if non-null\r
224      * @return the string\r
225      */\r
226     public static String createLocationAuthRefName(String shortId, String displaySuffix) {\r
227         String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("\r
228                         +shortId+")";\r
229                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
230                         refName += "'"+displaySuffix+"'";\r
231         return refName;\r
232     }\r
233 \r
234     /**\r
235      * Creates the location ref name.\r
236      *\r
237      * @param locationAuthRefName the locationAuthority ref name\r
238      * @param shortId the location shortIdentifier\r
239      * @param displaySuffix displayName to be appended, if non-null\r
240      * @return the string\r
241      */\r
242     public static String createLocationRefName(\r
243                                                 String locationAuthRefName, String shortId, String displaySuffix) {\r
244         String refName = locationAuthRefName+":location:name("+shortId+")";\r
245                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
246                         refName += "'"+displaySuffix+"'";\r
247         return refName;\r
248     }\r
249 \r
250     public static String extractId(ClientResponse<Response> res) {\r
251         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
252         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
253         if(logger.isDebugEnabled()){\r
254                 logger.debug("extractId:uri=" + uri);\r
255         }\r
256         String[] segments = uri.split("/");\r
257         String id = segments[segments.length - 1];\r
258         if(logger.isDebugEnabled()){\r
259                 logger.debug("id=" + id);\r
260         }\r
261         return id;\r
262     }\r
263     \r
264     /**\r
265      * Returns an error message indicating that the status code returned by a\r
266      * specific call to a service does not fall within a set of valid status\r
267      * codes for that service.\r
268      *\r
269      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
270      *\r
271      * @param statusCode  The invalid status code that was returned in the response,\r
272      *                    from submitting that type of request to the service.\r
273      *\r
274      * @return An error message.\r
275      */\r
276     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
277         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
278                 requestType.validStatusCodesAsString();\r
279     }\r
280 \r
281 \r
282     \r
283     /**\r
284      * Produces a default displayName from the basic name and dates fields.\r
285      * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which\r
286      * duplicates this logic, until we define a service-general utils package\r
287      * that is neither client nor service specific.\r
288      * @param name      \r
289      * @return\r
290      */\r
291     public static String prepareDefaultDisplayName(\r
292                 String name ) {\r
293         StringBuilder newStr = new StringBuilder();\r
294                         newStr.append(name);\r
295                 return newStr.toString();\r
296     }\r
297     \r
298 \r
299 \r
300 }\r