Disable syslog in heat-translator for functest integration
[parser.git] / verigraph / src / main / java / it / polito / grpc / test / ReachabilityTest.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.grpc.test;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.FixMethodOrder;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 import org.junit.runners.MethodSorters;
25
26 import static org.junit.Assert.assertEquals;
27
28 import java.io.BufferedReader;
29 import java.io.FilenameFilter;
30 import java.io.InputStreamReader;
31
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
37 import com.github.fge.jsonschema.main.JsonSchema;
38
39 import io.grpc.verigraph.GraphGrpc;
40 import io.grpc.verigraph.NewGraph;
41 import io.grpc.verigraph.Policy;
42 import io.grpc.verigraph.VerificationGrpc;
43 import it.polito.escape.verify.client.VerifyClientException;
44 import it.polito.escape.verify.service.ValidationUtils;
45 import it.polito.escape.verify.test.TestCase;
46 import it.polito.escape.verify.test.TestExecutionException;
47 import it.polito.grpc.Client;
48 import it.polito.grpc.GrpcUtils;
49 import it.polito.grpc.Service;
50
51 @RunWith(JUnit4.class)
52 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
53 public class ReachabilityTest {
54         private File                    schema;
55         private List<File>              testFiles       = new ArrayList<File>();
56         private List<TestCase>  testCases       = new ArrayList<TestCase>();
57         private Client  client;
58         private Service server;
59
60         @Before
61         public void setUpBeforeClass() throws Exception {
62                 client = new Client("localhost" , 50051);
63                 server = new Service(50051);
64                 server.start();
65
66                 String folderName = System.getProperty("user.dir") + "/tester/testcases";
67                 File folder = new File(folderName);
68                 if (!folder.exists()) {
69                         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
70                     String s;
71                     do{
72                         System.out.println("Please enter the testcases folder path: ");
73                         s = in.readLine();
74                         if (isValidpath(s)){
75                                 folder = new File(s);
76                                 break;
77                         }
78                     }while (s != null && s.length() != 0);
79                     if(s == null)
80                         System.exit(0);
81                 }
82                 String schemaName = System.getProperty("user.dir") + "/tester/testcase_schema.json";
83                 File schema = new File(schemaName);
84                 if (!schema.exists()) {
85                         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
86                     String s;
87                     do{
88                         System.out.println("Please enter the full path of 'testcase_schema.json': ");
89                         s = in.readLine();
90                         if (isValidpath(s)){
91                                 folder = new File(s);
92                                 break;
93                         }
94                     }while (s != null && s.length() != 0);
95                     if(s == null)
96                         System.exit(0);
97                 }
98
99                 this.schema = schema;
100                 this.testFiles = getTests(folder);
101                 this.testCases = getTestCases(this.testFiles);
102         }
103
104         @After
105         public void tearDown() throws Exception {
106                 server.stop();
107                 client.shutdown();
108         }
109
110         @Test
111         public final void wrongReachability() {
112                 System.out.println("DEBUG: starting testWrongReachability");
113
114                 VerificationGrpc nullVer = VerificationGrpc.newBuilder()
115                                                         .setErrorMessage("Graph with id 52 not found").build();
116                 //verification on uncreated graph
117                 Policy policyToVerify = Client.createPolicy("Node1", "Node4", "reachability", null, 52);
118                 VerificationGrpc ver = client.verify(policyToVerify);
119                 assertEquals(ver, nullVer);
120
121                 //verification on uncreated nodes
122                 nullVer = VerificationGrpc.newBuilder()
123                                 .setErrorMessage("The \'source\' parameter \'Node5\' is not valid, please insert the name of an existing node").build();
124                 policyToVerify = Client.createPolicy("Node5", "Node4", "reachability", null, 1);
125                 ver = client.verify(policyToVerify);
126                 assertEquals(ver, nullVer);
127
128                 //verification on uncreated nodes
129                 nullVer = VerificationGrpc.newBuilder()
130                                 .setErrorMessage("The \'source\' parameter \'Node1\' is not valid, please insert the name of an existing node").build();
131
132                 policyToVerify = Client.createPolicy("Node1", "Node10", "reachability", null, 1);
133                 ver = client.verify(policyToVerify);
134                 assertEquals(ver, nullVer);
135
136         }
137
138         public List<File> getTests(File folder) {
139                 List<File> filesList = new ArrayList<File>();
140
141                 System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'");
142
143                 File[] files = folder.listFiles(new FilenameFilter() {
144
145                         @Override
146                         public boolean accept(File dir, String name) {
147                                 return name.endsWith(".json");
148                         }
149                 });
150
151                 for (File f : files) {
152                         filesList.add(f);
153                         System.out.println("File '" + f.getName() + "' added to test files");
154                 }
155
156                 return filesList;
157         }
158
159         public List<TestCase> getTestCases(List<File> files)    throws JsonParseException, JsonMappingException, IOException,
160                                                                                                                         Exception {
161                 List<TestCase> testCases = new ArrayList<TestCase>();
162
163                 for (File file : files) {
164                         validateTestFile(file);
165                         try {
166                                 TestCase tc = new ObjectMapper().readValue(file, TestCase.class);
167                                 testCases.add(tc);
168                         }
169                         catch (Exception e) {
170                                 throw e;
171                         }
172                 }
173
174                 return testCases;
175         }
176
177         @Test
178         public void runTestCases() throws VerifyClientException, TestExecutionException {
179                 int counter = 0;
180                 for (TestCase tc : this.testCases) {
181                         String result = runTestCase(tc);
182                         if (!result.equals(tc.getResult()))
183                                 throw new TestExecutionException("Error running test given in file '"   + this.testFiles.get(counter).getName()
184                                                                         + "'. Test returned '" + result + "' instead of '" + tc.getResult() + "'.");
185                         else
186                                 System.out.println("Test given in file '"       + this.testFiles.get(counter).getName() + "' returned '"
187                                                                         + result + "' as expected");
188                         counter++;
189
190                 }
191                 System.out.println("All tests PASSED");
192         }
193
194         private String runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
195                 GraphGrpc graph = GrpcUtils.obtainGraph(tc.getGraph());
196
197                 NewGraph newGraph = this.client.createGraph(graph);
198                 if(newGraph.getSuccess() == false)
199                         throw new VerifyClientException("gRPC request failed");
200                 GraphGrpc createdGraph = newGraph.getGraph();
201
202                 GraphGrpc addedgraph = client.getGraph(createdGraph.getId());
203                 System.out.println(addedgraph);
204
205                 final Map<String, String> map = GrpcUtils.getParamGivenString(tc.getPolicyUrlParameters());
206
207                 Policy policy = Client.createPolicy(map.get("source"),
208                                                                                         map.get("destination"),
209                                                                                         map.get("type"),
210                                                                                         map.get("middlebox"),
211                                                                                         createdGraph.getId());
212                 VerificationGrpc verification = this.client.verify(policy);
213                 return verification.getResult();
214         }
215
216         public void validateTestFile(File testFile) throws Exception {
217                 JsonSchema schemaNode = null;
218                 try {
219                         schemaNode = ValidationUtils.getSchemaNode(schema);
220                 }
221                 catch (IOException e) {
222                         throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
223                 }
224                 catch (ProcessingException e) {
225                         throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
226                 }
227
228                 JsonNode jsonNode;
229                 try {
230                         jsonNode = ValidationUtils.getJsonNode(testFile);
231                 }
232                 catch (IOException e) {
233                         throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
234                 }
235
236                 try {
237                         ValidationUtils.validateJson(schemaNode, jsonNode);
238                 }
239                 catch (ProcessingException e) {
240                         throw new Exception("There were errors in the validation of file '"     + testFile.getAbsolutePath()
241                                                                 + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage());
242
243                 }
244         }
245
246         private static boolean isValidpath(String s) {
247                 if (s==null)
248                         return false;
249                 File file = new File(s);
250                 return file.exists();
251         }
252 }