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