Bump to yangtools-2.0.0 and odlparent-3.0.2
[neutron.git] / integration / test / src / test / java / org / opendaylight / neutron / e2etest / ITNeutronE2E.java
1 /*
2  * Copyright (C) 2015 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 static org.ops4j.pax.exam.CoreOptions.maven;
11 import static org.ops4j.pax.exam.CoreOptions.vmOption;
12 import static org.ops4j.pax.exam.CoreOptions.when;
13 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
16
17 import com.google.gson.Gson;
18 import com.google.gson.JsonArray;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonObject;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.io.UncheckedIOException;
26 import java.net.HttpURLConnection;
27 import java.net.URL;
28 import java.util.Map;
29 import java.util.Set;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
34 import org.ops4j.pax.exam.Configuration;
35 import org.ops4j.pax.exam.Option;
36 import org.ops4j.pax.exam.junit.PaxExam;
37 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
38 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
39 import org.ops4j.pax.exam.options.MavenUrlReference;
40 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
41 import org.ops4j.pax.exam.spi.reactors.PerClass;
42
43 @RunWith(PaxExam.class)
44 @ExamReactorStrategy(PerClass.class)
45 public class ITNeutronE2E  extends AbstractMdsalTestBase {
46
47     private static final String KARAF_DEBUG_PORT = "5005";
48     private static final String KARAF_DEBUG_PROP = "karaf.debug";
49
50     final String base = "http://127.0.0.1:8181/controller/nb/v2/neutron";
51
52     @Override
53     public MavenUrlReference getFeatureRepo() {
54         return maven()
55             .groupId("org.opendaylight.neutron")
56             .artifactId("features-neutron-test")
57             .classifier("features")
58             .type("xml")
59             .versionAsInProject();
60     }
61
62     @Override
63     public String getFeatureName() {
64         return "odl-neutron-logger-test";
65     }
66
67     @Override
68     @Configuration
69     public Option[] config() {
70         Option[] options = super.config();
71         Option[] otherOptions = getOtherOptions();
72         Option[] combinedOptions = new Option[options.length + otherOptions.length];
73         System.arraycopy(options, 0, combinedOptions, 0, options.length);
74         System.arraycopy(otherOptions, 0, combinedOptions, options.length,
75             otherOptions.length);
76         return combinedOptions;
77     }
78
79     private Option[] getOtherOptions() {
80         return new Option[] {
81             vmOption("-javaagent:../../pax/jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
82             keepRuntimeFolder(), configureConsole().ignoreLocalConsole(),
83             logLevel(LogLevel.INFO),
84             when(Boolean.getBoolean(KARAF_DEBUG_PROP)).useOptions(
85                 KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),};
86     }
87
88     @Test
89     public void test() throws IOException, InterruptedException {
90         NeutronNetworkTests.runTests(base);
91         NeutronSubnetTests.runTests(base);
92         NeutronPortTests.runTests(base);
93         NeutronRouterTests.runTests(base);
94         NeutronFloatingIpTests.runTests(base);
95         NeutronSecurityGroupTests.runTests(base);
96         NeutronSecurityRuleTests.runTests(base);
97         NeutronFirewallTests.runTests(base);
98         NeutronFirewallPolicyTests.runTests(base);
99         NeutronFirewallRuleTests.runTests(base);
100         NeutronLoadBalancerTests.runTests(base);
101         NeutronLBListenerTests.runTests(base);
102         NeutronLBPoolTests.runTests(base);
103         NeutronLBPoolMembersTests.runTests(base);
104         NeutronLBHealthMonitorTests.runTests(base);
105         NeutronMeteringLabelTests.runTests(base);
106         NeutronMeteringRuleTests.runTests(base);
107         NeutronVpnServicesTests.runTests(base);
108         NeutronIpSecPoliciesTests.runTests(base);
109         NeutronIpSecSiteConnectionTests.runTests(base);
110         NeutronIKEPoliciesTests.runTests(base);
111         NeutronBgpvpnTests.runTests(base);
112         NeutronL2GatewayTests.runTests(base);
113         NeutronL2GatewayConnectionTests.runTests(base);
114         NeutronQosPolicyTests.runTests(base);
115         NeutronSFCPortPairTests.runTests(base);
116         NeutronSFCPortPairGroupTests.runTests(base);
117         NeutronSFCPortChainTests.runTests(base);
118         NeutronSFCFlowClassifierTests.runTests(base);
119         NeutronTrunkTests.runTests(base);
120         NeutronRevisionNumberTests.runTests(base);
121         NeutronProjectIdTests.runTests(base);
122         // tests related to bugs
123         NeutronBug3812Tests.runTests(base);
124         TempestPortsIpV6TestJSON.runTests(base);
125         NeutronBug4027Tests.runTests(base);
126     }
127
128     static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
129         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
130         httpConn.setRequestMethod("GET");
131         httpConn.setRequestProperty("Content-Type", "application/json");
132         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
133         return httpConn;
134     }
135
136     static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
137         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
138         httpConn.setRequestMethod("DELETE");
139         httpConn.setRequestProperty("Content-Type", "application/json");
140         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
141         return httpConn;
142     }
143
144     static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
145         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
146         httpConn.setRequestMethod("POST");
147         httpConn.setRequestProperty("Content-Type", "application/json");
148         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
149         httpConn.setDoOutput(true);
150         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
151         out.write(content);
152         out.close();
153         return httpConn;
154     }
155
156     static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
157         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
158         httpConn.setRequestMethod("PUT");
159         httpConn.setRequestProperty("Content-Type", "application/json");
160         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
161         httpConn.setDoOutput(true);
162         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
163         out.write(content);
164         out.close();
165         return httpConn;
166     }
167
168     static void test_create(String urlStr, String content, String context) {
169         try {
170             URL url = new URL(urlStr);
171             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
172             Assert.assertEquals(context, 201, httpConn.getResponseCode());
173         } catch (IOException e) {
174             throw new UncheckedIOException(e);
175         }
176     }
177
178     static void test_create(String urlStr, int responseCode, String content, String context) {
179         try {
180             URL url = new URL(urlStr);
181             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
182             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
183         } catch (IOException e) {
184             throw new UncheckedIOException(e);
185         }
186     }
187
188     static void test_modify(String urlStr, String content, String context) {
189         try {
190             URL url = new URL(urlStr);
191             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
192             Assert.assertEquals(context, 200, httpConn.getResponseCode());
193         } catch (IOException e) {
194             throw new UncheckedIOException(e);
195         }
196     }
197
198     static void test_fetch(String urlStr, int responseCode, String context) {
199         try {
200             URL url = new URL(urlStr);
201             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
202             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
203         } catch (IOException e) {
204             throw new UncheckedIOException(e);
205         }
206     }
207
208     static void test_fetch(String urlStr, String context) {
209         test_fetch(urlStr, 200, context);
210     }
211
212     static void test_fetch(String urlStr, boolean positiveTest, String context) {
213         int responseCode = positiveTest ? 200 : 404;
214         test_fetch(urlStr, responseCode, context);
215     }
216
217     static void test_delete(String urlStr, int responseCode, String context) {
218         try {
219             URL url = new URL(urlStr);
220             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
221             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
222         } catch (IOException e) {
223             throw new UncheckedIOException(e);
224         }
225     }
226
227     static void test_delete(String urlStr, String context) {
228         test_delete(urlStr, 204, context);
229     }
230
231     static void test_delete_404(String urlStr, String context) {
232         test_delete(urlStr, 404, context);
233     }
234
235     private static String fetchResponse(String urlStr, String context) {
236         StringBuffer response = new StringBuffer();
237
238         try {
239             URL url = new URL(urlStr);
240             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
241             Assert.assertEquals(context, 200, httpConn.getResponseCode());
242             BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
243             String inputLine;
244             while ((inputLine = in.readLine()) != null) {
245                 response.append(inputLine);
246             }
247             in.close();
248         } catch (IOException e) {
249             throw new UncheckedIOException(e);
250         }
251         return response.toString();
252     }
253
254     static JsonObject test_fetch_gson(String urlStr, String context) {
255         String response = fetchResponse(urlStr, context);
256         Gson gson = new Gson();
257         return gson.fromJson(response, JsonObject.class);
258     }
259
260     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
261         String response = fetchResponse(urlStr, context);
262
263         //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
264         Gson gson = new Gson();
265         JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
266         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
267         Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
268         JsonElement jsonElementValue = entrySet.iterator().next().getValue();
269         String key = entrySet.iterator().next().getKey();
270         Assert.assertEquals(context, collectionName, key);
271         Assert.assertTrue("E2E Tests Failed - Collection not Array: " + jsonElementValue,
272                 jsonElementValue.isJsonArray());
273         JsonArray jsonArray = jsonElementValue.getAsJsonArray();
274         Assert.assertNotEquals(context, jsonArray.size(), 0);
275     }
276
277     // Helper function - content is json used during create. Format - {"Name": {...}}
278     static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
279         Gson gson = new Gson();
280         JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
281         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
282         JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
283         for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
284             String key = element.getKey();
285             JsonElement jsonElementValue = element.getValue();
286             // Query only values that are non null Primitives - Integer,Strings,character and boolean
287             if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
288                 String valueStr = jsonElementValue.getAsString();
289                 valueStr = valueStr.replaceAll("\\s+", "+");
290                 String queryUrl = urlStr + "?" + key + "=" + valueStr;
291                 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
292                 test_fetch_collection_response(queryUrl, collectionName, context);
293             }
294         }
295     }
296 }