Merge "Add verigraph code base"
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / test / Tester.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.escape.verify.test;
11
12 import java.io.BufferedReader;
13 import java.io.File;
14 import java.io.FilenameFilter;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.ws.rs.client.Client;
21 import javax.ws.rs.client.ClientBuilder;
22 import javax.ws.rs.client.ResponseProcessingException;
23 import javax.ws.rs.client.WebTarget;
24 import javax.ws.rs.core.Response;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
31 import com.github.fge.jsonschema.main.JsonSchema;
32
33 import it.polito.escape.verify.client.VerifyClient;
34 import it.polito.escape.verify.client.VerifyClientException;
35 import it.polito.escape.verify.model.Graph;
36 import it.polito.escape.verify.model.Verification;
37 import it.polito.escape.verify.service.ValidationUtils;
38
39 public class Tester {
40
41         private File                    schema;
42
43         private List<File>              testFiles       = new ArrayList<File>();
44
45         private List<TestCase>  testCases       = new ArrayList<TestCase>();
46
47         private String                  target;
48
49         private VerifyClient    verifyClient;
50
51         public Tester(String target, File schema, File folder)  throws JsonParseException, JsonMappingException, IOException,
52                                                                                                                         Exception {
53                 init(target, schema, folder);
54         }
55
56         private void init(String target, File schema, File folder)      throws JsonParseException, JsonMappingException,
57                                                                                                                                 IOException, Exception {
58                 this.target = target;
59                 this.verifyClient = new VerifyClient(this.target);
60                 this.schema = schema;
61                 this.testFiles = getTests(folder);
62                 this.testCases = getTestCases(this.testFiles);
63         }
64
65         public List<File> getTests(File folder) {
66                 List<File> filesList = new ArrayList<File>();
67
68                 System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'");
69
70                 File[] files = folder.listFiles(new FilenameFilter() {
71
72                         @Override
73                         public boolean accept(File dir, String name) {
74                                 return name.endsWith(".json");
75                         }
76                 });
77
78                 for (File f : files) {
79                         filesList.add(f);
80                         System.out.println("File '" + f.getName() + "' added to test files");
81                 }
82
83                 return filesList;
84         }
85
86         public List<TestCase> getTestCases(List<File> files)    throws JsonParseException, JsonMappingException, IOException,
87                                                                                                                         Exception {
88                 List<TestCase> testCases = new ArrayList<TestCase>();
89
90                 for (File file : files) {
91                         validateTestFile(file);
92                         try {
93                                 TestCase tc = new ObjectMapper().readValue(file, TestCase.class);
94                                 testCases.add(tc);
95                         }
96                         catch (Exception e) {
97                                 throw e;
98                         }
99                 }
100
101                 return testCases;
102         }
103
104         private void runTestCases() throws VerifyClientException, TestExecutionException {
105                 int counter = 0;
106                 for (TestCase tc : this.testCases) {
107                         String result = runTestCase(tc);
108                         if (!result.equals(tc.getResult()))
109                                 throw new TestExecutionException("Error running test given in file '"   + this.testFiles.get(counter).getName()
110                                                                         + "'. Test returned '" + result + "' instead of '" + tc.getResult() + "'.");
111                         else
112                                 System.out.println("Test given in file '"       + this.testFiles.get(counter).getName() + "' returned '"
113                                                                         + result + "' as expected");
114                         counter++;
115                 }
116                 System.out.println("All tests PASSED");
117         }
118
119         private String runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
120                 Client client = ClientBuilder.newClient();
121
122                 Graph graph = tc.getGraph();
123                 Response response = null;
124                 try{
125                         response = this.verifyClient.createGraph(graph);
126                 }
127                 catch(ResponseProcessingException e){
128                         throw new TestExecutionException("Response processing has failed: " + e.getResponse().readEntity(String.class));
129                 }
130                 catch(javax.ws.rs.ProcessingException e){
131                         throw new TestExecutionException("HTTP request failed");
132                 }
133                 Graph createdGraph = response.readEntity(Graph.class);
134                 String urlParams = tc.getPolicyUrlParameters();
135                 WebTarget target = client.target(this.target + "/graphs/" + createdGraph.getId() + "/policy" + urlParams);
136
137                 response = target.request().get();
138                 Verification verification = response.readEntity(Verification.class);
139                 return verification.getResult();
140         }
141
142         public void validateTestFile(File testFile) throws Exception {
143                 JsonSchema schemaNode = null;
144                 try {
145                         schemaNode = ValidationUtils.getSchemaNode(schema);
146                 }
147                 catch (IOException e) {
148                         throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
149                 }
150                 catch (ProcessingException e) {
151                         throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
152                 }
153
154                 JsonNode jsonNode;
155                 try {
156                         jsonNode = ValidationUtils.getJsonNode(testFile);
157                 }
158                 catch (IOException e) {
159                         throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
160                 }
161
162                 try {
163                         ValidationUtils.validateJson(schemaNode, jsonNode);
164                 }
165                 catch (ProcessingException e) {
166                         throw new Exception("There were errors in the validation of file '"     + testFile.getAbsolutePath()
167                                                                 + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage());
168
169                 }
170         }
171
172         public static void main(String[] args)  throws JsonParseException, JsonMappingException, IOException,
173                                                                                         VerifyClientException, Exception {
174                 String folderName = System.getProperty("user.dir") + "/tester/testcases";
175                 File folder = new File(folderName);
176                 if (!folder.exists()) {
177                         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
178                     String s;
179                     do{
180                         System.out.println("Please enter the testcases folder path: ");
181                         s = in.readLine();
182                         if (isValidpath(s)){
183                                 folder = new File(s);
184                                 break;
185                         }
186                     }while (s != null && s.length() != 0);
187                     if(s == null)
188                         System.exit(0);
189                 }
190                 String schemaName = System.getProperty("user.dir") + "/tester/testcase_schema.json";
191                 File schema = new File(schemaName);
192                 if (!schema.exists()) {
193                         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
194                     String s;
195                     do{
196                         System.out.println("Please enter the full path of 'testcase_schema.json': ");
197                         s = in.readLine();
198                         if (isValidpath(s)){
199                                 folder = new File(s);
200                                 break;
201                         }
202                     }while (s != null && s.length() != 0);
203                     if(s == null)
204                         System.exit(0);
205                 }
206
207                 Tester tester = new Tester("http://localhost:8080/verify/api", schema, folder);
208
209                 tester.runTestCases();
210
211         }
212
213         private static boolean isValidpath(String s) {
214                 if (s==null)
215                         return false;
216                 File file = new File(s);
217                 return file.exists();
218         }
219
220 }