2 * Copyright (C) 2018 IBM, Inc.
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
8 package org.opendaylight.neutron.e2etest;
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;
23 import org.junit.Assert;
25 public final class HttpUtils {
27 // copy/pasted from org.opendaylight.neutron.northbound.api.AbstractNeutronNorthbound
28 public static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158
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=");
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=");
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());
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());
75 static void test_create(String urlStr, String content, String context) {
76 test_create(urlStr, 201, content, context);
79 static void test_create(String urlStr, int responseCode, String content, String context) {
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);
89 static void test_modify(String urlStr, String content, String context) {
90 test_modify(urlStr, 200, content, context);
93 static void test_modify(String urlStr, int responseCode, String content, String context) {
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);
103 static void test_fetch(String urlStr, int responseCode, String context) {
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);
113 static void test_fetch(String urlStr, String context) {
114 test_fetch(urlStr, 200, context);
117 static void test_fetch(String urlStr, boolean positiveTest, String context) {
118 int responseCode = positiveTest ? 200 : 404;
119 test_fetch(urlStr, responseCode, context);
122 static void test_delete(String urlStr, int responseCode, String context) {
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);
132 static void test_delete(String urlStr, String context) {
133 test_delete(urlStr, 204, context);
136 static void test_delete_404(String urlStr, String context) {
137 test_delete(urlStr, 404, context);
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);
146 static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
147 String response = fetchResponse(urlStr, context);
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);
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);
183 private static String fetchResponse(String urlStr, String context) {
184 StringBuilder response = new StringBuilder();
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()));
192 while ((inputLine = in.readLine()) != null) {
193 response.append(inputLine);
196 } catch (IOException e) {
197 throw new UncheckedIOException(e);
199 return response.toString();