implement dependency checking for security rule's group
[neutron.git] / integration / test-standalone / src / main / java / org / opendaylight / neutron / e2etest / HttpUtils.java
1 /*
2  * Copyright (C) 2018 IBM, Inc.
3  *
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
7  */
8 package org.opendaylight.neutron.e2etest;
9
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;
20 import java.net.URL;
21 import java.util.Map;
22 import java.util.Set;
23 import org.junit.Assert;
24
25 public final class HttpUtils {
26
27     // copy/pasted from org.opendaylight.neutron.northbound.api.AbstractNeutronNorthbound
28     public static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158
29
30     private HttpUtils() { }
31
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=");
37         return httpConn;
38     }
39
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=");
45         return httpConn;
46     }
47
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());
55         out.write(content);
56         out.close();
57         return httpConn;
58     }
59
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());
67         out.write(content);
68         out.close();
69         return httpConn;
70     }
71
72     static void test_create(String urlStr, String content, String context) {
73         test_create(urlStr, 201, content, context);
74     }
75
76     static void test_create(String urlStr, int responseCode, String content, String context) {
77         try {
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);
83         }
84     }
85
86     static void test_modify(String urlStr, String content, String context) {
87         try {
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);
93         }
94     }
95
96     static void test_fetch(String urlStr, int responseCode, String context) {
97         try {
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);
103         }
104     }
105
106     static void test_fetch(String urlStr, String context) {
107         test_fetch(urlStr, 200, context);
108     }
109
110     static void test_fetch(String urlStr, boolean positiveTest, String context) {
111         int responseCode = positiveTest ? 200 : 404;
112         test_fetch(urlStr, responseCode, context);
113     }
114
115     static void test_delete(String urlStr, int responseCode, String context) {
116         try {
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);
122         }
123     }
124
125     static void test_delete(String urlStr, String context) {
126         test_delete(urlStr, 204, context);
127     }
128
129     static void test_delete_404(String urlStr, String context) {
130         test_delete(urlStr, 404, context);
131     }
132
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);
137     }
138
139     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
140         String response = fetchResponse(urlStr, context);
141
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);
154     }
155
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);
172             }
173         }
174     }
175
176     private static String fetchResponse(String urlStr, String context) {
177         StringBuffer response = new StringBuffer();
178
179         try {
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()));
184             String inputLine;
185             while ((inputLine = in.readLine()) != null) {
186                 response.append(inputLine);
187             }
188             in.close();
189         } catch (IOException e) {
190             throw new UncheckedIOException(e);
191         }
192         return response.toString();
193     }
194
195 }