Neutron Model update
[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
9 package org.opendaylight.neutron.e2etest;
10
11 import static org.ops4j.pax.exam.CoreOptions.maven;
12 import static org.ops4j.pax.exam.CoreOptions.vmOption;
13 import static org.ops4j.pax.exam.CoreOptions.when;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
18
19 import com.google.gson.Gson;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.UncheckedIOException;
29 import java.net.HttpURLConnection;
30 import java.net.URL;
31 import java.util.Map;
32 import java.util.Set;
33 import javax.inject.Inject;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.ops4j.pax.exam.Configuration;
38 import org.ops4j.pax.exam.Option;
39 import org.ops4j.pax.exam.junit.PaxExam;
40 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
41 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.service.cm.ConfigurationAdmin;
44
45 @RunWith(PaxExam.class)
46 public class ITNeutronE2E {
47
48     private static final String KARAF_DEBUG_PORT = "5005";
49     private static final String KARAF_DEBUG_PROP = "karaf.debug";
50
51     @Inject
52     private BundleContext bundleContext;
53
54     @Inject
55     private ConfigurationAdmin configurationAdmin;
56
57     @Configuration
58     public Option[] config() {
59         return new Option[] {
60                 // Provision and launch a container based on a distribution of Karaf (Apache ServiceMix).
61                 // FIXME: need to *NOT* hardcode the version here - it breaks on
62                 // version bumps
63                 karafDistributionConfiguration()
64                         .frameworkUrl(maven().groupId("org.opendaylight.neutron").artifactId("neutron-karaf")
65                                 .type("zip").versionAsInProject())
66                         .karafVersion("3.0.3").name("Neutron").unpackDirectory(new File("target/pax"))
67                         .useDeployFolder(false),
68                 // It is really nice if the container sticks around after the test so you can check the contents
69                 // of the data directory when things go wrong.
70                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=jacoco-it.exec"), keepRuntimeFolder(),
71                 // Don't bother with local console output as it just ends up cluttering the logs
72                 configureConsole().ignoreLocalConsole(),
73                 // Force the log level to INFO so we have more details during the test.  It defaults to WARN.
74                 logLevel(LogLevel.INFO),
75                 // provision the needed features for this test
76                 //    features("mvn:org.opendaylight.neutron/features-test/0.5.0-SNAPSHOT/xml/features",
77                 //        "features-neutron-test"),
78                 // Remember that the test executes in another process.  If you want to debug it, you need
79                 // to tell Pax Exam to launch that process with debugging enabled.  Launching the test class itself with
80                 // debugging enabled (for example in Eclipse) will not get you the desired results.
81                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
82                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)), };
83     }
84
85     final String base = "http://127.0.0.1:8080/controller/nb/v2/neutron";
86
87     @Test
88     public void test() throws IOException, InterruptedException {
89         NeutronNetworkTests.runTests(base);
90         NeutronSubnetTests.runTests(base);
91         NeutronPortTests.runTests(base);
92         NeutronRouterTests.runTests(base);
93         NeutronFloatingIpTests.runTests(base);
94         NeutronSecurityGroupTests.runTests(base);
95         NeutronSecurityRuleTests.runTests(base);
96         NeutronFirewallTests.runTests(base);
97         NeutronFirewallPolicyTests.runTests(base);
98         NeutronFirewallRuleTests.runTests(base);
99         NeutronLoadBalancerTests.runTests(base);
100         NeutronLBListenerTests.runTests(base);
101         NeutronLBPoolTests.runTests(base);
102         NeutronLBPoolMembersTests.runTests(base);
103         NeutronLBHealthMonitorTests.runTests(base);
104         NeutronMeteringLabelTests.runTests(base);
105         NeutronMeteringRuleTests.runTests(base);
106         NeutronVpnServicesTests.runTests(base);
107         NeutronIpSecPoliciesTests.runTests(base);
108         NeutronIpSecSiteConnectionTests.runTests(base);
109         NeutronIKEPoliciesTests.runTests(base);
110         NeutronBgpvpnTests.runTests(base);
111         NeutronL2GatewayTests.runTests(base);
112         NeutronL2GatewayConnectionTests.runTests(base);
113         NeutronQosPolicyTests.runTests(base);
114         NeutronSFCPortPairTests.runTests(base);
115         NeutronSFCPortPairGroupTests.runTests(base);
116         NeutronSFCPortChainTests.runTests(base);
117         NeutronSFCFlowClassifierTests.runTests(base);
118         NeutronTrunkTests.runTests(base);
119         NeutronRevisionNumberTests.runTests(base);
120         NeutronProjectIdTests.runTests(base);
121         // tests related to bugs
122         NeutronBug3812Tests.runTests(base);
123         TempestPortsIpV6TestJSON.runTests(base);
124         NeutronBug4027Tests.runTests(base);
125     }
126
127     static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
128         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
129         httpConn.setRequestMethod("GET");
130         httpConn.setRequestProperty("Content-Type", "application/json");
131         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
132         return httpConn;
133     }
134
135     static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
136         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
137         httpConn.setRequestMethod("DELETE");
138         httpConn.setRequestProperty("Content-Type", "application/json");
139         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
140         return httpConn;
141     }
142
143     static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
144         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
145         httpConn.setRequestMethod("POST");
146         httpConn.setRequestProperty("Content-Type", "application/json");
147         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
148         httpConn.setDoOutput(true);
149         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
150         out.write(content);
151         out.close();
152         return httpConn;
153     }
154
155     static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
156         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
157         httpConn.setRequestMethod("PUT");
158         httpConn.setRequestProperty("Content-Type", "application/json");
159         httpConn.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
160         httpConn.setDoOutput(true);
161         OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
162         out.write(content);
163         out.close();
164         return httpConn;
165     }
166
167     static void test_create(String urlStr, String content, String context) {
168         try {
169             URL url = new URL(urlStr);
170             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
171             Assert.assertEquals(context, 201, httpConn.getResponseCode());
172         } catch (IOException e) {
173             throw new UncheckedIOException(e);
174         }
175     }
176
177     static void test_create(String urlStr, int responseCode, String content, String context) {
178         try {
179             URL url = new URL(urlStr);
180             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
181             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
182         } catch (IOException e) {
183             throw new UncheckedIOException(e);
184         }
185     }
186
187     static void test_modify(String urlStr, String content, String context) {
188         try {
189             URL url = new URL(urlStr);
190             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
191             Assert.assertEquals(context, 200, httpConn.getResponseCode());
192         } catch (IOException e) {
193             throw new UncheckedIOException(e);
194         }
195     }
196
197     static void test_fetch(String urlStr, int responseCode, String context) {
198         try {
199             URL url = new URL(urlStr);
200             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
201             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
202         } catch (IOException e) {
203             throw new UncheckedIOException(e);
204         }
205     }
206
207     static void test_fetch(String urlStr, String context) {
208         test_fetch(urlStr, 200, context);
209     }
210
211     static void test_fetch(String urlStr, boolean positiveTest, String context) {
212         int responseCode = positiveTest ? 200 : 404;
213         test_fetch(urlStr, responseCode, context);
214     }
215
216     static void test_delete(String urlStr, int responseCode, String context) {
217         try {
218             URL url = new URL(urlStr);
219             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
220             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
221         } catch (IOException e) {
222             throw new UncheckedIOException(e);
223         }
224     }
225
226     static void test_delete(String urlStr, String context) {
227         test_delete(urlStr, 204, context);
228     }
229
230     static void test_delete_404(String urlStr, String context) {
231         test_delete(urlStr, 404, context);
232     }
233
234     private static String fetchResponse(String urlStr, String context) {
235         StringBuffer response = new StringBuffer();
236
237         try {
238             URL url = new URL(urlStr);
239             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
240             Assert.assertEquals(context, 200, httpConn.getResponseCode());
241             BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
242             String inputLine;
243             while ((inputLine = in.readLine()) != null) {
244                 response.append(inputLine);
245             }
246             in.close();
247         } catch (IOException e) {
248             throw new UncheckedIOException(e);
249         }
250         return response.toString();
251     }
252
253     static JsonObject test_fetch_gson(String urlStr, String context) {
254         String response = fetchResponse(urlStr, context);
255         Gson gson = new Gson();
256         return gson.fromJson(response, JsonObject.class);
257     }
258
259     static void test_fetch_collection_response(String urlStr, String collectionName, String context) {
260         String response = fetchResponse(urlStr, context);
261
262         //Collection is returned in an array. Format - {"collectionName": [{...}, {....}]}
263         Gson gson = new Gson();
264         JsonObject jsonObjectOutput = gson.fromJson(response, JsonObject.class);
265         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectOutput.entrySet();
266         Assert.assertTrue("E2E Tests Failed - Json Error", entrySet.size() > 0);
267         JsonElement jsonElementValue = entrySet.iterator().next().getValue();
268         String key = entrySet.iterator().next().getKey();
269         Assert.assertEquals(context, collectionName, key);
270         Assert.assertTrue("E2E Tests Failed - Collection not Array", jsonElementValue.isJsonArray());
271         JsonArray jsonArray = jsonElementValue.getAsJsonArray();
272         Assert.assertNotEquals(context, jsonArray.size(), 0);
273     }
274
275     // Helper function - content is json used during create. Format - {"Name": {...}}
276     static void test_fetch_with_one_query_item(String urlStr, String content, String collectionName) {
277         Gson gson = new Gson();
278         JsonObject jsonObjectInput = gson.fromJson(content, JsonObject.class);
279         Set<Map.Entry<String, JsonElement>> entrySet = jsonObjectInput.entrySet();
280         JsonObject jsonObjectOutput = entrySet.iterator().next().getValue().getAsJsonObject();
281         for (Map.Entry<String, JsonElement> element : jsonObjectOutput.entrySet()) {
282             String key = element.getKey();
283             JsonElement jsonElementValue = element.getValue();
284             // Query only values that are non null Primitives - Integer,Strings,character and boolean
285             if (jsonElementValue.isJsonPrimitive() && !jsonElementValue.isJsonNull()) {
286                 String valueStr = jsonElementValue.getAsString();
287                 valueStr = valueStr.replaceAll("\\s+", "+");
288                 String queryUrl = urlStr + "?" + key + "=" + valueStr;
289                 String context = collectionName + " " + key + "=" + jsonElementValue.toString() + " Get Failed";
290                 test_fetch_collection_response(queryUrl, collectionName, context);
291             }
292         }
293     }
294 }