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 *******************************************************************************/
10 package it.polito.escape.verify.test;
12 import java.io.BufferedReader;
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;
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;
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;
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;
43 private List<File> testFiles = new ArrayList<File>();
45 private List<TestCase> testCases = new ArrayList<TestCase>();
47 private String target;
49 private VerifyClient verifyClient;
51 public Tester(String target, File schema, File folder) throws JsonParseException, JsonMappingException, IOException,
53 init(target, schema, folder);
56 private void init(String target, File schema, File folder) throws JsonParseException, JsonMappingException,
57 IOException, Exception {
59 this.verifyClient = new VerifyClient(this.target);
61 this.testFiles = getTests(folder);
62 this.testCases = getTestCases(this.testFiles);
65 public List<File> getTests(File folder) {
66 List<File> filesList = new ArrayList<File>();
68 System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'");
70 File[] files = folder.listFiles(new FilenameFilter() {
73 public boolean accept(File dir, String name) {
74 return name.endsWith(".json");
78 for (File f : files) {
80 System.out.println("File '" + f.getName() + "' added to test files");
86 public List<TestCase> getTestCases(List<File> files) throws JsonParseException, JsonMappingException, IOException,
88 List<TestCase> testCases = new ArrayList<TestCase>();
90 for (File file : files) {
91 validateTestFile(file);
93 TestCase tc = new ObjectMapper().readValue(file, TestCase.class);
104 private void runTestCases() throws VerifyClientException, TestExecutionException {
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() + "'.");
112 System.out.println("Test given in file '" + this.testFiles.get(counter).getName() + "' returned '"
113 + result + "' as expected");
116 System.out.println("All tests PASSED");
119 private String runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
120 Client client = ClientBuilder.newClient();
122 Graph graph = tc.getGraph();
123 Response response = null;
125 response = this.verifyClient.createGraph(graph);
127 catch(ResponseProcessingException e){
128 throw new TestExecutionException("Response processing has failed: " + e.getResponse().readEntity(String.class));
130 catch(javax.ws.rs.ProcessingException e){
131 throw new TestExecutionException("HTTP request failed");
133 Graph createdGraph = response.readEntity(Graph.class);
134 String urlParams = tc.getPolicyUrlParameters();
135 WebTarget target = client.target(this.target + "/graphs/" + createdGraph.getId() + "/policy" + urlParams);
137 response = target.request().get();
138 Verification verification = response.readEntity(Verification.class);
139 return verification.getResult();
142 public void validateTestFile(File testFile) throws Exception {
143 JsonSchema schemaNode = null;
145 schemaNode = ValidationUtils.getSchemaNode(schema);
147 catch (IOException e) {
148 throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
150 catch (ProcessingException e) {
151 throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
156 jsonNode = ValidationUtils.getJsonNode(testFile);
158 catch (IOException e) {
159 throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
163 ValidationUtils.validateJson(schemaNode, jsonNode);
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());
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));
180 System.out.println("Please enter the testcases folder path: ");
183 folder = new File(s);
186 }while (s != null && s.length() != 0);
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));
196 System.out.println("Please enter the full path of 'testcase_schema.json': ");
199 folder = new File(s);
202 }while (s != null && s.length() != 0);
207 Tester tester = new Tester("http://localhost:8080/verify/api", schema, folder);
209 tester.runTestCases();
213 private static boolean isValidpath(String s) {
216 File file = new File(s);
217 return file.exists();