c3b5b6334d9dbce8cdac5edb252e28c91971d352
[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         test_create(urlStr, 201, content, context);
71     }
72
73     static void test_create(String urlStr, int responseCode, String content, String context) {
74         try {
75             URL url = new URL(urlStr);
76             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
77             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
78         } catch (IOException e) {
79             throw new UncheckedIOException(e);
80         }
81     }
82
83     static void test_modify(String urlStr, String content, String context) {
84         try {
85             URL url = new URL(urlStr);
86             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
87             Assert.assertEquals(context, 200, httpConn.getResponseCode());
88         } catch (IOException e) {
89             throw new UncheckedIOException(e);
90         }
91     }
92
93     static void test_fetch(String urlStr, int responseCode, String context) {
94         try {
95             URL url = new URL(urlStr);
96             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
97             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
98         } catch (IOException e) {
99             throw new UncheckedIOException(e);
100         }
101     }
102
103     static void test_fetch(String urlStr, String context) {
104         test_fetch(urlStr, 200, context);
105     }
106
107     static void test_fetch(String urlStr, boolean positiveTest, String context) {
108         int responseCode = positiveTest ? 200 : 404;
109         test_fetch(urlStr, responseCode, context);
110     }
111
112     static void test_delete(String urlStr, int responseCode, String context) {
113         try {
114             URL url = new URL(urlStr);
115             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
116             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
117         } catch (IOException e) {
118             throw new UncheckedIOException(e);
119         }
120     }
121
122     static void test_delete(String urlStr, String context) {
123         test_delete(urlStr, 204, context);
124     }
125
126     static void test_delete_404(String urlStr, String context) {
127         test_delete(urlStr, 404, context);
128     }
129
130     static JsonObject test_fetch_gson(String urlStr, String context) {
131         String response = fetchResponse(urlStr, context);
132         Gson gson = new Gson();
133         return gson.fromJson(response, JsonObject.class);
134     }
135
136     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
137         String response = fetchResponse(urlStr, context);
138
139         //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
140         Gson gson = new Gson();
141         JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
142         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
143         Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
144         JsonElement jsonElementValue = entrySet.iterator().next().getValue();
145         String key = entrySet.iterator().next().getKey();
146         Assert.assertEquals(context, collectionName, key);
147         Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
148                 + "; full response: " + response, jsonElementValue.isJsonArray());
149         JsonArray jsonArray = jsonElementValue.getAsJsonArray();
150         Assert.assertNotEquals(context, jsonArray.size(), 0);
151     }
152
153     // Helper function - content is json used during create. Format - {"Name": {...}}
154     static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
155         Gson gson = new Gson();
156         JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
157         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
158         JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
159         for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
160             String key = element.getKey();
161             JsonElement jsonElementValue = element.getValue();
162             // Query only values that are non null Primitives - Integer,Strings,character and boolean
163             if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
164                 String valueStr = jsonElementValue.getAsString();
165                 valueStr = valueStr.replaceAll("\\s+", "+");
166                 String queryUrl = urlStr + "?" + key + "=" + valueStr;
167                 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
168                 test_fetch_collection_response(queryUrl, collectionName, context);
169             }
170         }
171     }
172
173     private static String fetchResponse(String urlStr, String context) {
174         StringBuffer response = new StringBuffer();
175
176         try {
177             URL url = new URL(urlStr);
178             HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
179             Assert.assertEquals(context, 200, httpConn.getResponseCode());
180             BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
181             String inputLine;
182             while ((inputLine = in.readLine()) != null) {
183                 response.append(inputLine);
184             }
185             in.close();
186         } catch (IOException e) {
187             throw new UncheckedIOException(e);
188         }
189         return response.toString();
190     }
191
192 }