Bump upstreams for Silicon
[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     // copy/pasted from org.opendaylight.neutron.northbound.api.AbstractNeutronNorthbound
28     public static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158
29
30     private HttpUtils() {
31
32     }
33
34     // TODO: Java 11+ has HttpClient, we should be using that instead
35     static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
36         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
37         httpConn.setRequestMethod("GET");
38         httpConn.setRequestProperty("Content-Type", "application/json");
39         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
40         return httpConn;
41     }
42
43     static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
44         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
45         httpConn.setRequestMethod("DELETE");
46         httpConn.setRequestProperty("Content-Type", "application/json");
47         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
48         return httpConn;
49     }
50
51     static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
52         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
53         httpConn.setRequestMethod("POST");
54         httpConn.setRequestProperty("Content-Type", "application/json");
55         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
56         httpConn.setDoOutput(true);
57         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
58         out.write(content);
59         out.close();
60         return httpConn;
61     }
62
63     static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
64         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
65         httpConn.setRequestMethod("PUT");
66         httpConn.setRequestProperty("Content-Type", "application/json");
67         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
68         httpConn.setDoOutput(true);
69         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
70         out.write(content);
71         out.close();
72         return httpConn;
73     }
74
75     static void test_create(String urlStr, String content, String context) {
76         test_create(urlStr, 201, content, context);
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         test_modify(urlStr, 200, content, context);
91     }
92
93     static void test_modify(String urlStr, int responseCode, String content, String context) {
94         try {
95             URL url = new URL(urlStr);
96             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
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, int responseCode, String context) {
104         try {
105             URL url = new URL(urlStr);
106             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
107             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
108         } catch (IOException e) {
109             throw new UncheckedIOException(e);
110         }
111     }
112
113     static void test_fetch(String urlStr, String context) {
114         test_fetch(urlStr, 200, context);
115     }
116
117     static void test_fetch(String urlStr, boolean positiveTest, String context) {
118         int responseCode = positiveTest ? 200 : 404;
119         test_fetch(urlStr, responseCode, context);
120     }
121
122     static void test_delete(String urlStr, int responseCode, String context) {
123         try {
124             URL url = new URL(urlStr);
125             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
126             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
127         } catch (IOException e) {
128             throw new UncheckedIOException(e);
129         }
130     }
131
132     static void test_delete(String urlStr, String context) {
133         test_delete(urlStr, 204, context);
134     }
135
136     static void test_delete_404(String urlStr, String context) {
137         test_delete(urlStr, 404, context);
138     }
139
140     static JsonObject test_fetch_gson(String urlStr, String context) {
141         String response = fetchResponse(urlStr, context);
142         Gson gson = new Gson();
143         return gson.fromJson(response, JsonObject.class);
144     }
145
146     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
147         String response = fetchResponse(urlStr, context);
148
149         //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
150         Gson gson = new Gson();
151         JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
152         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
153         Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
154         JsonElement jsonElementValue = entrySet.iterator().next().getValue();
155         String key = entrySet.iterator().next().getKey();
156         Assert.assertEquals(context + "; jsonObjectOutput=" + jsonObjectOutput.toString(), collectionName, key);
157         Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
158                 + "; full response: " + response, jsonElementValue.isJsonArray());
159         JsonArray jsonArray = jsonElementValue.getAsJsonArray();
160         Assert.assertNotEquals(context, jsonArray.size(), 0);
161     }
162
163     // Helper function - content is json used during create. Format - {"Name": {...}}
164     static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
165         Gson gson = new Gson();
166         JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
167         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
168         JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
169         for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
170             String key = element.getKey();
171             JsonElement jsonElementValue = element.getValue();
172             // Query only values that are non null Primitives - Integer,Strings,character and boolean
173             if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
174                 String valueStr = jsonElementValue.getAsString();
175                 valueStr = valueStr.replaceAll("\\s+", "+");
176                 String queryUrl = urlStr + "?" + key + "=" + valueStr;
177                 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
178                 test_fetch_collection_response(queryUrl, collectionName, context);
179             }
180         }
181     }
182
183     private static String fetchResponse(String urlStr, String context) {
184         StringBuilder response = new StringBuilder();
185
186         try {
187             URL url = new URL(urlStr);
188             HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
189             Assert.assertEquals(context, 200, httpConn.getResponseCode());
190             BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
191             String inputLine;
192             while ((inputLine = in.readLine()) != null) {
193                 response.append(inputLine);
194             }
195             in.close();
196         } catch (IOException e) {
197             throw new UncheckedIOException(e);
198         }
199         return response.toString();
200     }
201
202 }