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