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.test;
11 import java.io.BufferedReader;
13 import java.io.FilenameFilter;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.util.ArrayList;
17 import java.util.Iterator;
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.verigraph.client.VerifyClient;
34 import it.polito.verigraph.client.VerifyClientException;
35 import it.polito.verigraph.model.Graph;
36 import it.polito.verigraph.model.Verification;
37 import it.polito.verigraph.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 List<String> results = runTestCase(tc);
108 Iterator<String> iter = tc.getResults().iterator();
109 for(String result : results){
111 if( !result.equals(iter.next()))
112 throw new TestExecutionException("Error running test given in file '"+ this.testFiles.get(counter).getName()
113 + "'. Test returned '" + result + "' instead of '" + tc.getResults() + "'.");
115 System.out.println("Test given in file '"+ this.testFiles.get(counter).getName() + "' returned '"
116 + result + "' as expected");
117 } else throw new TestExecutionException("Error running test given in file '"+ this.testFiles.get(counter).getName()
118 + "'. Test returned '" + result + "' instead of '" + tc.getResults() + "'.");
122 System.out.println("All tests PASSED");
125 private List<String> runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
126 Client client = ClientBuilder.newClient();
128 List<String> results = new ArrayList<String>();
130 Graph graph = tc.getGraph();
131 Response response = null;
133 response = this.verifyClient.createGraph(graph);
135 catch(ResponseProcessingException e){
136 throw new TestExecutionException("Response processing has failed: " + e.getResponse().readEntity(String.class));
138 catch(javax.ws.rs.ProcessingException e){
139 throw new TestExecutionException("HTTP request failed");
141 Graph createdGraph = response.readEntity(Graph.class);
142 for (String urlParams : tc.getPolicyUrlParameters()){
143 WebTarget target = client.target(this.target + "/graphs/" + createdGraph.getId() + "/policy" + urlParams);
145 response = target.request().get();
146 Verification verification = response.readEntity(Verification.class);
147 results.add(verification.getResult());
152 public void validateTestFile(File testFile) throws Exception {
153 JsonSchema schemaNode = null;
155 schemaNode = ValidationUtils.getSchemaNode(schema);
157 catch (IOException e) {
158 throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
160 catch (ProcessingException e) {
161 throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
166 jsonNode = ValidationUtils.getJsonNode(testFile);
168 catch (IOException e) {
169 throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
173 ValidationUtils.validateJson(schemaNode, jsonNode);
175 catch (ProcessingException e) {
176 throw new Exception("There were errors in the validation of file '"+ testFile.getAbsolutePath()
177 + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage());
182 public static void main(String[] args)throws JsonParseException, JsonMappingException, IOException,
183 VerifyClientException, Exception {
184 String folderName = System.getProperty("user.dir") + "/tester/testcases";
185 File folder = new File(folderName);
186 if (!folder.exists()) {
187 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
190 System.out.println("Please enter the testcases folder path: ");
193 folder = new File(s);
196 }while (s != null && s.length() != 0);
200 String schemaName = System.getProperty("user.dir") + "/tester/testcase_schema.json";
201 File schema = new File(schemaName);
202 if (!schema.exists()) {
203 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
206 System.out.println("Please enter the full path of 'testcase_schema.json': ");
209 folder = new File(s);
212 }while (s != null && s.length() != 0);
217 Tester tester = new Tester("http://localhost:8080/verigraph/api", schema, folder);
219 tester.runTestCases();
223 private static boolean isValidpath(String s) {
226 File file = new File(s);
227 return file.exists();