001package org.kuali.asr; 002 003import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 004import org.apache.commons.httpclient.HttpClient; 005import org.apache.commons.httpclient.HttpException; 006import org.apache.commons.httpclient.HttpStatus; 007import org.apache.commons.httpclient.methods.DeleteMethod; 008import org.apache.commons.httpclient.methods.GetMethod; 009import org.apache.commons.httpclient.params.HttpMethodParams; 010import org.apache.log4j.Logger; 011import org.junit.Test; 012import org.kuali.asr.bo.*; 013import org.kuali.asr.handler.ResponseHandler; 014 015import java.io.*; 016import java.net.HttpURLConnection; 017import java.net.URL; 018 019/** 020 * Created with IntelliJ IDEA. 021 * User: sheiksalahudeenm 022 * Date: 2/13/14 023 * Time: 3:05 PM 024 * To change this template use File | Settings | File Templates. 025 */ 026public class ASRRequestProcessor_UT { 027 private final String USER_AGENT = "Mozilla/5.0"; 028 029 private Logger LOG = org.apache.log4j.Logger.getLogger(ASRRequestProcessor_UT.class); 030 private String baseUrl="http://localhost:8080/olefs/asrService/asr"; 031 ResponseHandler responseHandler = new ResponseHandler(); 032 033 @Test 034 public void lookupNewASRItem() throws Exception { 035 String format="json"; 036 String url =baseUrl+"/lookupNewAsrItems/dev2"; 037 sendGetRequest(url,format); 038 } 039 040 @Test 041 public void lookupNewASRTypeRequest() throws Exception { 042 String format="json"; 043 String url =baseUrl+"/lookupAsrRequests/dev2,ASR"; 044 sendGetRequest(url,format); 045 } 046 047 @Test 048 public void lookupASRRequests() throws Exception { 049 String format="json"; 050 String url =baseUrl+"/lookupAsrItemHolds/dev2,010"; 051 sendGetRequest(url,format); 052 } 053 054 @Test 055 public void placeRequest() throws Exception { 056 String format="json"; 057 String url =baseUrl+"/placeASRTypeRequest"; 058 PlaceASRItemRequestBo placeASRItemRequestBo = new PlaceASRItemRequestBo(); 059 placeASRItemRequestBo.setItemBarcode("0101"); 060 placeASRItemRequestBo.setOperatorId("dev2"); 061 placeASRItemRequestBo.setPatronId("00100055U"); 062 placeASRItemRequestBo.setPickUpLocation("BL_EDUC"); 063 String parameter; 064 if(format.equals("json")) 065 parameter=responseHandler.marshalObjectToJson(placeASRItemRequestBo); 066 else 067 parameter=responseHandler.marshalObjectToXml(placeASRItemRequestBo); 068 069 /*String parameter = "<placeASRRequest>\n" + 070 "<itemBarcode>0101</itemBarcode>\n" + 071 "<patronId>00100055U</patronId>\n" + 072 "<operatorId>dev2</operatorId>\n" + 073 "<pickUpLocation>BL_EDUC</pickUpLocation>\n" + 074 "</placeASRRequest>";*/ 075 sendPostRequest(url, parameter,format); 076 } 077 078 @Test 079 public void cancelRequest() throws Exception { 080 String format="json"; 081 String url =baseUrl+"/cancelHold/2,dev2"; 082 sendDeleteRequest(url,format); 083 } 084 085 @Test 086 public void removeItem() throws Exception { 087 String format="json"; 088 String url =baseUrl+"/removeASRItem/0103"; 089 sendDeleteRequest(url,format); 090 } 091 092 @Test 093 public void updateRequestStatus() throws Exception { 094 String format="json"; 095 String url =baseUrl+"/updateASRTypeRequest"; 096 UpdateASRRequestStatusBo updateASRRequestStatusBo = new UpdateASRRequestStatusBo(); 097 updateASRRequestStatusBo.setHoldId("1"); 098 updateASRRequestStatusBo.setOperatorId("dev2"); 099 updateASRRequestStatusBo.setStatus("3"); 100 String parameter; 101 if(format.equals("json")) 102 parameter=responseHandler.marshalObjectToJson(updateASRRequestStatusBo); 103 else 104 parameter=responseHandler.marshalObjectToXml(updateASRRequestStatusBo); 105 /*String parameter = "<updateASRRequestStatus>\n" + 106 "<holdId>1</holdId>\n" + 107 "<status>3</status>\n" + 108 "<operatorId>dev2</operatorId>\n" + 109 "</updateASRRequestStatus>";*/ 110 sendPostRequest(url, parameter, format); 111 } 112 113 @Test 114 public void receiveTransistOfASRItem() throws Exception { 115 String format="json"; 116 String url =baseUrl; 117 ReceiveTransitRequestBo receiveTransitRequestBo = new ReceiveTransitRequestBo(); 118 receiveTransitRequestBo.setBarcode("0101"); 119 receiveTransitRequestBo.setOperatorId("dev2"); 120 String parameter; 121 if(format.equals("json")) 122 parameter=responseHandler.marshalObjectToJson(receiveTransitRequestBo); 123 else 124 parameter=responseHandler.marshalObjectToXml(receiveTransitRequestBo); 125 126 /*String parameter = "<receiveTransitRequest>\n" + 127 " <barcode>0101</barcode>\n" + 128 " <operatorId>dev2</operatorId>\n" + 129 " </receiveTransitRequest>";*/ 130 sendPostRequest(url, parameter,""); 131 } 132 133 @Test 134 public void updateASRItemStatusAvailable() throws Exception { 135 String format="json"; 136 String url =baseUrl+"/updateASRItemStatusAvailable"; 137 UpdateASRItemRequestBo updateASRItemRequestBo = new UpdateASRItemRequestBo(); 138 updateASRItemRequestBo.setItemBarcode("0101"); 139 updateASRItemRequestBo.setItemStatus("csa"); 140 updateASRItemRequestBo.setOperatorId("dev2"); 141 String parameter; 142 if(format.equals("json")) 143 parameter=responseHandler.marshalObjectToJson(updateASRItemRequestBo); 144 else 145 parameter=responseHandler.marshalObjectToXml(updateASRItemRequestBo); 146 147 /* String parameter = "<updateASRItem>\n" + 148 "<itemBarcode>0101</itemBarcode>\n" + 149 "<itemStatus>csp</itemStatus>\n" + 150 "<operatorId>dev2</operatorId>\n" + 151 "</updateASRItem>";*/ 152 sendPostRequest(url, parameter,format); 153 } 154 155 @Test 156 public void updateASRItemStatusMissing() throws Exception { 157 String format="json"; 158 String url =baseUrl+"/updateASRItemStatusMissing"; 159 UpdateASRItemStatusBo updateASRItemStatusBo = new UpdateASRItemStatusBo(); 160 updateASRItemStatusBo.setItemBarcode("0102"); 161 updateASRItemStatusBo.setItemStatus("csn"); 162 updateASRItemStatusBo.setOperatorId("dev2"); 163 String parameter; 164 if(format.equals("json")) 165 parameter=responseHandler.marshalObjectToJson(updateASRItemStatusBo); 166 else 167 parameter=responseHandler.marshalObjectToXml(updateASRItemStatusBo); 168 169 /* String parameter = "<updateASRItemStatus>\n" + 170 "<itemBarcode>0102</itemBarcode>\n" + 171 "<itemStatus>csn</itemStatus>\n" + 172 "<operatorId>dev2</operatorId>\n" + 173 "</updateASRItemStatus>";*/ 174 sendPostRequest(url,parameter,format); 175 } 176 177 @Test 178 public void updateASRItemStatusRetrive() throws Exception { 179 String format="json"; 180 String url =baseUrl+"/updateASRItemStatusBeingRetrieved"; 181 UpdateASRItemStatusBo updateASRItemStatusBo = new UpdateASRItemStatusBo(); 182 updateASRItemStatusBo.setItemBarcode("0102"); 183 updateASRItemStatusBo.setItemStatus("csn"); 184 updateASRItemStatusBo.setOperatorId("dev2"); 185 String parameter; 186 if(format.equals("json")) 187 parameter=responseHandler.marshalObjectToJson(updateASRItemStatusBo); 188 else 189 parameter=responseHandler.marshalObjectToXml(updateASRItemStatusBo); 190 /*String parameter = "<updateASRItemStatus>\n" + 191 "<itemBarcode>0103</itemBarcode>\n" + 192 "<itemStatus>csp</itemStatus>\n" + 193 "<operatorId>dev2</operatorId>\n" + 194 "</updateASRItemStatus>";*/ 195 sendPostRequest(url, parameter,format); 196 } 197 198 public void sendGetRequest(String request,String format) throws Exception { 199 String url = request; 200 HttpClient client = new HttpClient(); 201 // Create a method instance. 202 GetMethod method = new GetMethod(url); 203 method.addRequestHeader("Accept", "application/"+format); 204 205 // Provide custom retry handler is necessary 206 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 207 new DefaultHttpMethodRetryHandler(3, false)); 208 209 try { 210 // Execute the method. 211 int statusCode = 0; 212 try { 213 statusCode = client.executeMethod(method); 214 } catch (IOException e) { 215 e.printStackTrace(); 216 } 217 218 if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) { 219 System.err.println("Method failed: " + method.getStatusLine()); 220 } 221 222 // Read the response body. 223 byte[] responseBody = method.getResponseBody(); 224 225 System.out.println(new String(responseBody)); 226 227 } catch (HttpException e) { 228 System.err.println("Fatal protocol violation: " + e.getMessage()); 229 e.printStackTrace(); 230 } catch (IOException e) { 231 System.err.println("Fatal transport error: " + e.getMessage()); 232 e.printStackTrace(); 233 } finally { 234 // Release the connection. 235 method.releaseConnection(); 236 } 237 } 238 239 240 private void sendPostRequest(String url, String urlParameters,String format) throws Exception { 241 URL obj = new URL(url); 242 HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 243 244 //add reuqest header 245 con.setRequestMethod("POST"); 246 con.setRequestProperty("User-Agent", USER_AGENT); 247 con.setRequestProperty("Accept", "application/" + format); 248 con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 249 con.setRequestProperty("Content-Type", "application/"+format); 250 251 // Send post request 252 con.setDoOutput(true); 253 DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 254 wr.writeBytes(urlParameters); 255 wr.flush(); 256 wr.close(); 257 258 int responseCode = con.getResponseCode(); 259 System.out.println("\nSending 'POST' request to URL : " + url); 260 System.out.println("Post parameters : " + urlParameters); 261 System.out.println("Response Code : " + responseCode); 262 263 BufferedReader in = new BufferedReader( 264 new InputStreamReader(con.getInputStream())); 265 String inputLine; 266 StringBuffer response = new StringBuffer(); 267 268 while ((inputLine = in.readLine()) != null) { 269 response.append(inputLine); 270 } 271 in.close(); 272 System.out.println(response.toString()); 273 274 } 275 276 public void sendDeleteRequest(String request,String format) throws Exception { 277 String url = request; 278 HttpClient client = new HttpClient(); 279 // Create a method instance. 280 DeleteMethod method = new DeleteMethod(url); 281 method.addRequestHeader("Accept", "application/"+format); 282 283 // Provide custom retry handler is necessary 284 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 285 new DefaultHttpMethodRetryHandler(3, false)); 286 287 try { 288 // Execute the method. 289 int statusCode = 0; 290 try { 291 statusCode = client.executeMethod(method); 292 } catch (IOException e) { 293 e.printStackTrace(); 294 } 295 296 if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) { 297 System.err.println("Method failed: " + method.getStatusLine()); 298 } 299 300 // Read the response body. 301 byte[] responseBody = method.getResponseBody(); 302 303 System.out.println(new String(responseBody)); 304 305 } catch (HttpException e) { 306 System.err.println("Fatal protocol violation: " + e.getMessage()); 307 e.printStackTrace(); 308 } catch (IOException e) { 309 System.err.println("Fatal transport error: " + e.getMessage()); 310 e.printStackTrace(); 311 } finally { 312 // Release the connection. 313 method.releaseConnection(); 314 } 315 } 316 317 318}