Extend getJsonResult() to accommodate HTTP methods other than "GET". 98/398/1
authortaochang <taochang@cisco.com>
Tue, 28 May 2013 21:43:36 +0000 (14:43 -0700)
committertaochang <taochang@cisco.com>
Tue, 28 May 2013 21:43:36 +0000 (14:43 -0700)
Also pass the HTTP response code to calling program.

Change-Id: Ie5ff6af9765a82ea5dbf7d2108da081b15c65d76
Signed-off-by: taochang <taochang@cisco.com>
opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIntegrationTest.java

index f8af5c49ec45c872d9adf35dd70608608083c029..19c756a1cbe231c13fc186986ea4633e015b9333 100644 (file)
@@ -19,6 +19,7 @@ import org.ops4j.pax.exam.util.PathUtils;
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
@@ -87,40 +88,56 @@ public class NorthboundIntegrationTest {
 
     }
 
 
     }
 
+    // static variable to pass response code from getJsonResult()
+    private static Integer httpResponseCode = null;
+
     private String getJsonResult(String restUrl) {
     private String getJsonResult(String restUrl) {
-        try {
-            URL url = new URL(restUrl);
-
-            this.users.getAuthorizationList();
-            this.users.authenticate("admin", "admin");
-            String authString = "admin:admin";
-            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
-            String authStringEnc = new String(authEncBytes);
-            URLConnection connection = url.openConnection();
-            connection.setRequestProperty("Authorization", "Basic "
-                    + authStringEnc);
-
-            connection.setRequestProperty("Content-Type", "application/json");
-            connection.setRequestProperty("Accept", "application/json");
-
-            connection.connect();
-            connection.getContentType();
-            InputStream is = connection.getInputStream();
-            // InputStream is = connection.getInputStream();
-            BufferedReader rd = new BufferedReader(new InputStreamReader(is,
-                    Charset.forName("UTF-8")));
-            StringBuilder sb = new StringBuilder();
-            int cp;
-            while ((cp = rd.read()) != -1) {
-                sb.append((char) cp);
-            }
-            is.close();
-            return sb.toString();
-        } catch (Exception e) {
-            return null;
-        }
+        return getJsonResult(restUrl, "GET");
     }
 
     }
 
+    private String getJsonResult(String restUrl, String method) {
+        // initialize response code to indicate error
+        httpResponseCode = 400;
+        
+        try {
+          URL url = new URL(restUrl);
+
+          this.users.getAuthorizationList();
+          this.users.authenticate("admin", "admin");
+          String authString = "admin:admin";
+          byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
+          String authStringEnc = new String(authEncBytes);
+          
+          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
+          connection.setRequestMethod(method);
+          connection.setRequestProperty("Authorization", "Basic "
+                  + authStringEnc);
+          connection.setRequestProperty("Content-Type", "application/json");
+          connection.setRequestProperty("Accept", "application/json");
+
+          connection.connect();
+          connection.getContentType();
+          
+          // Response code for success should be 2xx
+          httpResponseCode = connection.getResponseCode();
+          
+          InputStream is = connection.getInputStream();
+          BufferedReader rd = new BufferedReader(new InputStreamReader(is,
+                  Charset.forName("UTF-8")));
+          StringBuilder sb = new StringBuilder();
+          int cp;
+          while ((cp = rd.read()) != -1) {
+              sb.append((char) cp);
+          }
+          is.close();
+          connection.disconnect();
+          return sb.toString();
+      } catch (Exception e) {
+          return null;
+      }
+      
+  }
+
     @Test
     public void testStatistics() {
 
     @Test
     public void testStatistics() {