View Javadoc
1   package org.kuali.asr;
2   
3   import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
4   import org.apache.commons.httpclient.HttpClient;
5   import org.apache.commons.httpclient.HttpException;
6   import org.apache.commons.httpclient.HttpStatus;
7   import org.apache.commons.httpclient.methods.DeleteMethod;
8   import org.apache.commons.httpclient.methods.GetMethod;
9   import org.apache.commons.httpclient.params.HttpMethodParams;
10  import org.apache.log4j.Logger;
11  import org.junit.Test;
12  import org.kuali.asr.bo.*;
13  import org.kuali.asr.handler.ResponseHandler;
14  
15  import java.io.*;
16  import java.net.HttpURLConnection;
17  import java.net.URL;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: sheiksalahudeenm
22   * Date: 2/13/14
23   * Time: 3:05 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class ASRRequestProcessor_UT {
27      private final String USER_AGENT = "Mozilla/5.0";
28  
29      private Logger LOG = org.apache.log4j.Logger.getLogger(ASRRequestProcessor_UT.class);
30      private String baseUrl="http://localhost:8080/olefs/asrService/asr";
31      ResponseHandler responseHandler =  new ResponseHandler();
32  
33      @Test
34      public void lookupNewASRItem() throws Exception {
35          String format="json";
36          String url =baseUrl+"/lookupNewAsrItems/dev2";
37          sendGetRequest(url,format);
38      }
39  
40      @Test
41      public void lookupNewASRTypeRequest() throws Exception {
42          String format="json";
43          String url =baseUrl+"/lookupAsrRequests/dev2,ASR";
44          sendGetRequest(url,format);
45      }
46  
47      @Test
48      public void lookupASRRequests() throws Exception {
49          String format="json";
50          String url =baseUrl+"/lookupAsrItemHolds/dev2,010";
51          sendGetRequest(url,format);
52      }
53  
54      @Test
55      public void placeRequest() throws Exception {
56          String format="json";
57          String url =baseUrl+"/placeASRTypeRequest";
58          PlaceASRItemRequestBo placeASRItemRequestBo = new PlaceASRItemRequestBo();
59          placeASRItemRequestBo.setItemBarcode("0101");
60          placeASRItemRequestBo.setOperatorId("dev2");
61          placeASRItemRequestBo.setPatronId("00100055U");
62          placeASRItemRequestBo.setPickUpLocation("BL_EDUC");
63          String parameter;
64          if(format.equals("json"))
65              parameter=responseHandler.marshalObjectToJson(placeASRItemRequestBo);
66          else
67              parameter=responseHandler.marshalObjectToXml(placeASRItemRequestBo);
68  
69          /*String parameter = "<placeASRRequest>\n" +
70                  "<itemBarcode>0101</itemBarcode>\n" +
71                  "<patronId>00100055U</patronId>\n" +
72                  "<operatorId>dev2</operatorId>\n" +
73                  "<pickUpLocation>BL_EDUC</pickUpLocation>\n" +
74                  "</placeASRRequest>";*/
75          sendPostRequest(url, parameter,format);
76      }
77  
78      @Test
79      public void cancelRequest() throws Exception {
80          String format="json";
81          String url =baseUrl+"/cancelHold/2,dev2";
82          sendDeleteRequest(url,format);
83      }
84  
85      @Test
86      public void removeItem() throws Exception {
87          String format="json";
88          String url =baseUrl+"/removeASRItem/0103";
89          sendDeleteRequest(url,format);
90      }
91  
92      @Test
93      public void updateRequestStatus() throws Exception {
94          String format="json";
95          String url =baseUrl+"/updateASRTypeRequest";
96          UpdateASRRequestStatusBo updateASRRequestStatusBo = new UpdateASRRequestStatusBo();
97          updateASRRequestStatusBo.setHoldId("1");
98          updateASRRequestStatusBo.setOperatorId("dev2");
99          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 }