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
30 private HttpUtils() { }
32 static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
33 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
34 httpConn.setRequestMethod("GET");
35 httpConn.setRequestProperty("Content-Type", "application/json");
36 httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
40 static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
41 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
42 httpConn.setRequestMethod("DELETE");
43 httpConn.setRequestProperty("Content-Type", "application/json");
44 httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
48 static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
49 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
50 httpConn.setRequestMethod("POST");
51 httpConn.setRequestProperty("Content-Type", "application/json");
52 httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
53 httpConn.setDoOutput(true);
54 OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
60 static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
61 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
62 httpConn.setRequestMethod("PUT");
63 httpConn.setRequestProperty("Content-Type", "application/json");
64 httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
65 httpConn.setDoOutput(true);
66 OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
72 static void test_create(String urlStr, String content, String context) {
73 test_create(urlStr, 201, content, context);
76 static void test_create(String urlStr, int responseCode, String content, String context) {
78 URL url = new URL(urlStr);
79 HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
80 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
81 } catch (IOException e) {
82 throw new UncheckedIOException(e);
86 static void test_modify(String urlStr, String content, String context) {
88 URL url = new URL(urlStr);
89 HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
90 Assert.assertEquals(context, 200, httpConn.getResponseCode());
91 } catch (IOException e) {
92 throw new UncheckedIOException(e);
96 static void test_fetch(String urlStr, int responseCode, String context) {
98 URL url = new URL(urlStr);
99 HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
100 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
101 } catch (IOException e) {
102 throw new UncheckedIOException(e);
106 static void test_fetch(String urlStr, String context) {
107 test_fetch(urlStr, 200, context);
110 static void test_fetch(String urlStr, boolean positiveTest, String context) {
111 int responseCode = positiveTest ? 200 : 404;
112 test_fetch(urlStr, responseCode, context);
115 static void test_delete(String urlStr, int responseCode, String context) {
117 URL url = new URL(urlStr);
118 HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
119 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
120 } catch (IOException e) {
121 throw new UncheckedIOException(e);
125 static void test_delete(String urlStr, String context) {
126 test_delete(urlStr, 204, context);
129 static void test_delete_404(String urlStr, String context) {
130 test_delete(urlStr, 404, context);
133 static JsonObject test_fetch_gson(String urlStr, String context) {
134 String response = fetchResponse(urlStr, context);
135 Gson gson = new Gson();
136 return gson.fromJson(response, JsonObject.class);
139 static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
140 String response = fetchResponse(urlStr, context);
142 //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
143 Gson gson = new Gson();
144 JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
145 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
146 Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
147 JsonElement jsonElementValue = entrySet.iterator().next().getValue();
148 String key = entrySet.iterator().next().getKey();
149 Assert.assertEquals(context, collectionName, key);
150 Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
151 + "; full response: " + response, jsonElementValue.isJsonArray());
152 JsonArray jsonArray = jsonElementValue.getAsJsonArray();
153 Assert.assertNotEquals(context, jsonArray.size(), 0);
156 // Helper function - content is json used during create. Format - {"Name": {...}}
157 static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
158 Gson gson = new Gson();
159 JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
160 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
161 JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
162 for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
163 String key = element.getKey();
164 JsonElement jsonElementValue = element.getValue();
165 // Query only values that are non null Primitives - Integer,Strings,character and boolean
166 if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
167 String valueStr = jsonElementValue.getAsString();
168 valueStr = valueStr.replaceAll("\\s+", "+");
169 String queryUrl = urlStr + "?" + key + "=" + valueStr;
170 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
171 test_fetch_collection_response(queryUrl, collectionName, context);
176 private static String fetchResponse(String urlStr, String context) {
177 StringBuffer response = new StringBuffer();
180 URL url = new URL(urlStr);
181 HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
182 Assert.assertEquals(context, 200, httpConn.getResponseCode());
183 BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
185 while ((inputLine = in.readLine()) != null) {
186 response.append(inputLine);
189 } catch (IOException e) {
190 throw new UncheckedIOException(e);
192 return response.toString();