0472334d0eb192adc8a2098b8b42fdaeb2a0f7eb
[neutron.git] / integration / test-standalone / src / main / java / org / opendaylight / neutron / e2etest / HttpUtils.java
1 /*
2  * Copyright (C) 2018 IBM, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.neutron.e2etest;
9
10 import com.google.gson.Gson;
11 import com.google.gson.JsonArray;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonObject;
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter;
18 import java.io.UncheckedIOException;
19 import java.net.HttpURLConnection;
20 import java.net.URL;
21 import java.util.Map;
22 import java.util.Set;
23 import org.junit.Assert;
24
25 public final class HttpUtils {
26
27     private HttpUtils() { }
28
29     static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
30         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
31         httpConn.setRequestMethod("GET");
32         httpConn.setRequestProperty("Content-Type", "application/json");
33         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
34         return httpConn;
35     }
36
37     static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
38         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
39         httpConn.setRequestMethod("DELETE");
40         httpConn.setRequestProperty("Content-Type", "application/json");
41         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
42         return httpConn;
43     }
44
45     static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
46         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
47         httpConn.setRequestMethod("POST");
48         httpConn.setRequestProperty("Content-Type", "application/json");
49         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
50         httpConn.setDoOutput(true);
51         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
52         out.write(content);
53         out.close();
54         return httpConn;
55     }
56
57     static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
58         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
59         httpConn.setRequestMethod("PUT");
60         httpConn.setRequestProperty("Content-Type", "application/json");
61         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
62         httpConn.setDoOutput(true);
63         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
64         out.write(content);
65         out.close();
66         return httpConn;
67     }
68
69     static void test_create(String urlStr, String content, String context) {
70         try {
71             URL url = new URL(urlStr);
72             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
73             Assert.assertEquals(context, 201, httpConn.getResponseCode());
74         } catch (IOException e) {
75             throw new UncheckedIOException(e);
76         }
77     }
78
79     static void test_create(String urlStr, int responseCode, String content, String context) {
80         try {
81             URL url = new URL(urlStr);
82             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
83             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
84         } catch (IOException e) {
85             throw new UncheckedIOException(e);
86         }
87     }
88
89     static void test_modify(String urlStr, String content, String context) {
90         try {
91             URL url = new URL(urlStr);
92             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
93             Assert.assertEquals(context, 200, httpConn.getResponseCode());
94         } catch (IOException e) {
95             throw new UncheckedIOException(e);
96         }
97     }
98
99     static void test_fetch(String urlStr, int responseCode, String context) {
100         try {
101             URL url = new URL(urlStr);
102             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
103             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
104         } catch (IOException e) {
105             throw new UncheckedIOException(e);
106         }
107     }
108
109     static void test_fetch(String urlStr, String context) {
110         test_fetch(urlStr, 200, context);
111     }
112
113     static void test_fetch(String urlStr, boolean positiveTest, String context) {
114         int responseCode = positiveTest ? 200 : 404;
115         test_fetch(urlStr, responseCode, context);
116     }
117
118     static void test_delete(String urlStr, int responseCode, String context) {
119         try {
120             URL url = new URL(urlStr);
121             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
122             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
123         } catch (IOException e) {
124             throw new UncheckedIOException(e);
125         }
126     }
127
128     static void test_delete(String urlStr, String context) {
129         test_delete(urlStr, 204, context);
130     }
131
132     static void test_delete_404(String urlStr, String context) {
133         test_delete(urlStr, 404, context);
134     }
135
136     static JsonObject test_fetch_gson(String urlStr, String context) {
137         String response = fetchResponse(urlStr, context);
138         Gson gson = new Gson();
139         return gson.fromJson(response, JsonObject.class);
140     }
141
142     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
143         String response = fetchResponse(urlStr, context);
144
145         //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
146         Gson gson = new Gson();
147         JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
148         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
149         Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
150         JsonElement jsonElementValue = entrySet.iterator().next().getValue();
151         String key = entrySet.iterator().next().getKey();
152         Assert.assertEquals(context, collectionName, key);
153         Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
154                 + "; full response: " + response, jsonElementValue.isJsonArray());
155         JsonArray jsonArray = jsonElementValue.getAsJsonArray();
156         Assert.assertNotEquals(context, jsonArray.size(), 0);
157     }
158
159     // Helper function - content is json used during create. Format - {"Name": {...}}
160     static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
161         Gson gson = new Gson();
162         JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
163         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
164         JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
165         for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
166             String key = element.getKey();
167             JsonElement jsonElementValue = element.getValue();
168             // Query only values that are non null Primitives - Integer,Strings,character and boolean
169             if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
170                 String valueStr = jsonElementValue.getAsString();
171                 valueStr = valueStr.replaceAll("\\s+", "+");
172                 String queryUrl = urlStr + "?" + key + "=" + valueStr;
173                 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
174                 test_fetch_collection_response(queryUrl, collectionName, context);
175             }
176         }
177     }
178
179     private static String fetchResponse(String urlStr, String context) {
180         StringBuffer response = new StringBuffer();
181
182         try {
183             URL url = new URL(urlStr);
184             HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
185             Assert.assertEquals(context, 200, httpConn.getResponseCode());
186             BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
187             String inputLine;
188             while ((inputLine = in.readLine()) != null) {
189                 response.append(inputLine);
190             }
191             in.close();
192         } catch (IOException e) {
193             throw new UncheckedIOException(e);
194         }
195         return response.toString();
196     }
197
198 }