1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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 package it.polito.verigraph.grpc.test;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
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;
26 import static org.junit.Assert.assertEquals;
28 import java.io.BufferedReader;
29 import java.io.FilenameFilter;
30 import java.io.InputStreamReader;
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;
39 import it.polito.verigraph.grpc.GraphGrpc;
40 import it.polito.verigraph.grpc.NewGraph;
41 import it.polito.verigraph.grpc.Policy;
42 import it.polito.verigraph.grpc.VerificationGrpc;
43 import it.polito.verigraph.client.VerifyClientException;
44 import it.polito.verigraph.grpc.client.Client;
45 import it.polito.verigraph.grpc.server.GrpcUtils;
46 import it.polito.verigraph.grpc.server.Service;
47 import it.polito.verigraph.service.ValidationUtils;
48 import it.polito.verigraph.test.TestCase;
49 import it.polito.verigraph.test.TestExecutionException;
51 @RunWith(JUnit4.class)
52 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
53 public class ReachabilityTest {
55 private List<File>testFiles= new ArrayList<File>();
56 private List<TestCase>testCases= new ArrayList<TestCase>();
57 private Client client;
58 private Service server;
61 public void setUpBeforeClass() throws Exception {
62 client = new Client("localhost" , 50051);
63 server = new Service(50051);
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));
72 System.out.println("Please enter the testcases folder path: ");
78 }while (s != null && s.length() != 0);
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));
88 System.out.println("Please enter the full path of 'testcase_schema.json': ");
94 }while (s != null && s.length() != 0);
100 this.testFiles = getTests(folder);
101 this.testCases = getTestCases(this.testFiles);
105 public void tearDown() throws Exception {
111 public final void wrongReachability() {
112 System.out.println("[DEBUG] wrongReachability starts");
113 System.out.println("DEBUG: starting testWrongReachability");
115 VerificationGrpc nullVer = VerificationGrpc.newBuilder()
116 .setErrorMessage("There is no Graph whose Id is '52'").build();
117 //verification on uncreated graph
118 if(client.getGraph(52) != null){
119 client.deleteGraph(52);
121 Policy policyToVerify = Client.createPolicy("Node1", "Node4", "reachability", null, 52);
122 VerificationGrpc ver = client.verify(policyToVerify);
123 assertEquals(ver, nullVer);
125 //verification on uncreated nodes
126 GraphGrpc graph = GraphGrpc.newBuilder().build();
127 graph = client.createGraph(graph).getGraph();
128 nullVer = VerificationGrpc.newBuilder()
129 .setErrorMessage("The \'source\' parameter \'Node5\' is not valid, please insert the name of an existing node").build();
130 policyToVerify = Client.createPolicy("Node5", "Node4", "reachability", null, graph.getId());
131 ver = client.verify(policyToVerify);
132 assertEquals(ver, nullVer);
134 //verification on uncreated nodes
135 nullVer = VerificationGrpc.newBuilder()
136 .setErrorMessage("The \'source\' parameter \'Node1\' is not valid, please insert the name of an existing node").build();
138 policyToVerify = Client.createPolicy("Node1", "Node10", "reachability", null, graph.getId());
139 ver = client.verify(policyToVerify);
140 assertEquals(ver, nullVer);
144 public List<File> getTests(File folder) {
145 List<File> filesList = new ArrayList<File>();
147 System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'");
149 File[] files = folder.listFiles(new FilenameFilter() {
152 public boolean accept(File dir, String name) {
153 return name.endsWith(".json");
157 for (File f : files) {
159 System.out.println("File '" + f.getName() + "' added to test files");
165 public List<TestCase> getTestCases(List<File> files)throws JsonParseException, JsonMappingException, IOException,
167 List<TestCase> testCases = new ArrayList<TestCase>();
169 for (File file : files) {
170 validateTestFile(file);
172 TestCase tc = new ObjectMapper().readValue(file, TestCase.class);
175 catch (Exception e) {
184 public void runTestCases() throws VerifyClientException, TestExecutionException {
185 System.out.println("[DEBUG] runTestCases starts");
187 for (TestCase tc : this.testCases) {
188 List<String> results = runTestCase(tc);
189 Iterator<String> iter = tc.getResults().iterator();
191 if(results.isEmpty()){
192 throw new TestExecutionException("Error running test given in file '"+ this.testFiles.get(counter).getName()+ "'.");
195 for(String result : results){
197 if( !result.equals(iter.next()))
198 throw new TestExecutionException("Error running test given in file '"+ this.testFiles.get(counter).getName()
199 + "'. Test returned '" + result + "' instead of '" + tc.getResults() + "'.");
201 System.out.println("Test given in file '"+ this.testFiles.get(counter).getName() + "' returned '"
202 + result + "' as expected");
203 } else throw new TestExecutionException("Error running test given in file '"+ this.testFiles.get(counter).getName()
204 + "'. Test returned '" + result + "' instead of '" + tc.getResults() + "'.");
209 System.out.println("All tests PASSED");
212 private List<String> runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
213 GraphGrpc graph = GrpcUtils.obtainGraph(tc.getGraph());
214 ArrayList<String> results = new ArrayList<String>();
216 NewGraph newGraph = this.client.createGraph(graph);
217 if(newGraph.getSuccess() == false)
218 throw new VerifyClientException("gRPC request failed");
219 GraphGrpc createdGraph = newGraph.getGraph();
221 GraphGrpc addedgraph = client.getGraph(createdGraph.getId());
222 System.out.println(addedgraph);
224 for(String url : tc.getPolicyUrlParameters()){
225 final Map<String, String> map = GrpcUtils.getParamGivenString(url);
226 Policy policy = Client.createPolicy(map.get("source"),
227 map.get("destination"),
229 map.get("middlebox"),
230 createdGraph.getId());
231 VerificationGrpc verification = this.client.verify(policy);
232 results.add(verification.getResult());
237 public void validateTestFile(File testFile) throws Exception {
238 JsonSchema schemaNode = null;
240 schemaNode = ValidationUtils.getSchemaNode(schema);
242 catch (IOException e) {
243 throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
245 catch (ProcessingException e) {
246 throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
251 jsonNode = ValidationUtils.getJsonNode(testFile);
253 catch (IOException e) {
254 throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
258 ValidationUtils.validateJson(schemaNode, jsonNode);
260 catch (ProcessingException e) {
261 throw new Exception("There were errors in the validation of file '"+ testFile.getAbsolutePath()
262 + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage());
267 private static boolean isValidpath(String s) {
270 File file = new File(s);
271 return file.exists();