improve NeutronSecurityRuleTests to cover 404 on modify of non-existant
[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         test_modify(urlStr, 200, content, context);
88     }
89
90     static void test_modify(String urlStr, int responseCode, String content, String context) {
91         try {
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);
97         }
98     }
99
100     static void test_fetch(String urlStr, int responseCode, String context) {
101         try {
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);
107         }
108     }
109
110     static void test_fetch(String urlStr, String context) {
111         test_fetch(urlStr, 200, context);
112     }
113
114     static void test_fetch(String urlStr, boolean positiveTest, String context) {
115         int responseCode = positiveTest ? 200 : 404;
116         test_fetch(urlStr, responseCode, context);
117     }
118
119     static void test_delete(String urlStr, int responseCode, String context) {
120         try {
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);
126         }
127     }
128
129     static void test_delete(String urlStr, String context) {
130         test_delete(urlStr, 204, context);
131     }
132
133     static void test_delete_404(String urlStr, String context) {
134         test_delete(urlStr, 404, context);
135     }
136
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);
141     }
142
143     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
144         String response = fetchResponse(urlStr, context);
145
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);
158     }
159
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);
176             }
177         }
178     }
179
180     private static String fetchResponse(String urlStr, String context) {
181         StringBuffer response = new StringBuffer();
182
183         try {
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()));
188             String inputLine;
189             while ((inputLine = in.readLine()) != null) {
190                 response.append(inputLine);
191             }
192             in.close();
193         } catch (IOException e) {
194             throw new UncheckedIOException(e);
195         }
196         return response.toString();
197     }
198
199 }