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) {
87 test_modify(urlStr, 200, content, context);
90 static void test_modify(String urlStr, int responseCode, String content, String context) {
92 URL url = new URL(urlStr);
93 HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
94 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
95 } catch (IOException e) {
96 throw new UncheckedIOException(e);
100 static void test_fetch(String urlStr, int responseCode, String context) {
102 URL url = new URL(urlStr);
103 HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
104 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
105 } catch (IOException e) {
106 throw new UncheckedIOException(e);
110 static void test_fetch(String urlStr, String context) {
111 test_fetch(urlStr, 200, context);
114 static void test_fetch(String urlStr, boolean positiveTest, String context) {
115 int responseCode = positiveTest ? 200 : 404;
116 test_fetch(urlStr, responseCode, context);
119 static void test_delete(String urlStr, int responseCode, String context) {
121 URL url = new URL(urlStr);
122 HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
123 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
124 } catch (IOException e) {
125 throw new UncheckedIOException(e);
129 static void test_delete(String urlStr, String context) {
130 test_delete(urlStr, 204, context);
133 static void test_delete_404(String urlStr, String context) {
134 test_delete(urlStr, 404, context);
137 static JsonObject test_fetch_gson(String urlStr, String context) {
138 String response = fetchResponse(urlStr, context);
139 Gson gson = new Gson();
140 return gson.fromJson(response, JsonObject.class);
143 static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
144 String response = fetchResponse(urlStr, context);
146 //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
147 Gson gson = new Gson();
148 JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
149 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
150 Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
151 JsonElement jsonElementValue = entrySet.iterator().next().getValue();
152 String key = entrySet.iterator().next().getKey();
153 Assert.assertEquals(context, collectionName, key);
154 Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
155 + "; full response: " + response, jsonElementValue.isJsonArray());
156 JsonArray jsonArray = jsonElementValue.getAsJsonArray();
157 Assert.assertNotEquals(context, jsonArray.size(), 0);
160 // Helper function - content is json used during create. Format - {"Name": {...}}
161 static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
162 Gson gson = new Gson();
163 JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
164 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
165 JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
166 for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
167 String key = element.getKey();
168 JsonElement jsonElementValue = element.getValue();
169 // Query only values that are non null Primitives - Integer,Strings,character and boolean
170 if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
171 String valueStr = jsonElementValue.getAsString();
172 valueStr = valueStr.replaceAll("\\s+", "+");
173 String queryUrl = urlStr + "?" + key + "=" + valueStr;
174 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
175 test_fetch_collection_response(queryUrl, collectionName, context);
180 private static String fetchResponse(String urlStr, String context) {
181 StringBuffer response = new StringBuffer();
184 URL url = new URL(urlStr);
185 HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
186 Assert.assertEquals(context, 200, httpConn.getResponseCode());
187 BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
189 while ((inputLine = in.readLine()) != null) {
190 response.append(inputLine);
193 } catch (IOException e) {
194 throw new UncheckedIOException(e);
196 return response.toString();