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) {
70 test_create(urlStr, 201, content, context);
73 static void test_create(String urlStr, int responseCode, String content, String context) {
75 URL url = new URL(urlStr);
76 HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
77 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
78 } catch (IOException e) {
79 throw new UncheckedIOException(e);
83 static void test_modify(String urlStr, String content, String context) {
85 URL url = new URL(urlStr);
86 HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
87 Assert.assertEquals(context, 200, httpConn.getResponseCode());
88 } catch (IOException e) {
89 throw new UncheckedIOException(e);
93 static void test_fetch(String urlStr, int responseCode, String context) {
95 URL url = new URL(urlStr);
96 HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
97 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
98 } catch (IOException e) {
99 throw new UncheckedIOException(e);
103 static void test_fetch(String urlStr, String context) {
104 test_fetch(urlStr, 200, context);
107 static void test_fetch(String urlStr, boolean positiveTest, String context) {
108 int responseCode = positiveTest ? 200 : 404;
109 test_fetch(urlStr, responseCode, context);
112 static void test_delete(String urlStr, int responseCode, String context) {
114 URL url = new URL(urlStr);
115 HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
116 Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
117 } catch (IOException e) {
118 throw new UncheckedIOException(e);
122 static void test_delete(String urlStr, String context) {
123 test_delete(urlStr, 204, context);
126 static void test_delete_404(String urlStr, String context) {
127 test_delete(urlStr, 404, context);
130 static JsonObject test_fetch_gson(String urlStr, String context) {
131 String response = fetchResponse(urlStr, context);
132 Gson gson = new Gson();
133 return gson.fromJson(response, JsonObject.class);
136 static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
137 String response = fetchResponse(urlStr, context);
139 //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
140 Gson gson = new Gson();
141 JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
142 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
143 Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
144 JsonElement jsonElementValue = entrySet.iterator().next().getValue();
145 String key = entrySet.iterator().next().getKey();
146 Assert.assertEquals(context, collectionName, key);
147 Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue + "; URL: " + urlStr
148 + "; full response: " + response, jsonElementValue.isJsonArray());
149 JsonArray jsonArray = jsonElementValue.getAsJsonArray();
150 Assert.assertNotEquals(context, jsonArray.size(), 0);
153 // Helper function - content is json used during create. Format - {"Name": {...}}
154 static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
155 Gson gson = new Gson();
156 JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
157 Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
158 JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
159 for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
160 String key = element.getKey();
161 JsonElement jsonElementValue = element.getValue();
162 // Query only values that are non null Primitives - Integer,Strings,character and boolean
163 if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
164 String valueStr = jsonElementValue.getAsString();
165 valueStr = valueStr.replaceAll("\\s+", "+");
166 String queryUrl = urlStr + "?" + key + "=" + valueStr;
167 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
168 test_fetch_collection_response(queryUrl, collectionName, context);
173 private static String fetchResponse(String urlStr, String context) {
174 StringBuffer response = new StringBuffer();
177 URL url = new URL(urlStr);
178 HttpURLConnection httpConn = HttpUtils.httpURLConnectionFactoryGet(url);
179 Assert.assertEquals(context, 200, httpConn.getResponseCode());
180 BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
182 while ((inputLine = in.readLine()) != null) {
183 response.append(inputLine);
186 } catch (IOException e) {
187 throw new UncheckedIOException(e);
189 return response.toString();