From 53d83244c1bf36af86e90ce5fe758a369f73563e Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Sat, 25 Feb 2017 12:00:55 +0100 Subject: Add verigraph code base JIRA: PARSER-111 Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892 Signed-off-by: serena.spinoso --- .../escape/verify/test/MultiThreadedTestCase.java | 201 ++++++++++ .../it/polito/escape/verify/test/Scalability.java | 409 +++++++++++++++++++++ .../it/polito/escape/verify/test/TestCase.java | 161 ++++++++ .../escape/verify/test/TestExecutionException.java | 24 ++ .../java/it/polito/escape/verify/test/Tester.java | 220 +++++++++++ 5 files changed, 1015 insertions(+) create mode 100644 verigraph/src/main/java/it/polito/escape/verify/test/MultiThreadedTestCase.java create mode 100644 verigraph/src/main/java/it/polito/escape/verify/test/Scalability.java create mode 100644 verigraph/src/main/java/it/polito/escape/verify/test/TestCase.java create mode 100644 verigraph/src/main/java/it/polito/escape/verify/test/TestExecutionException.java create mode 100644 verigraph/src/main/java/it/polito/escape/verify/test/Tester.java (limited to 'verigraph/src/main/java/it/polito/escape/verify/test') diff --git a/verigraph/src/main/java/it/polito/escape/verify/test/MultiThreadedTestCase.java b/verigraph/src/main/java/it/polito/escape/verify/test/MultiThreadedTestCase.java new file mode 100644 index 0000000..7989b37 --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/test/MultiThreadedTestCase.java @@ -0,0 +1,201 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import javax.ws.rs.core.Response; + +import org.junit.Assert; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import it.polito.escape.verify.client.VerifyClient; +import it.polito.escape.verify.client.VerifyClientException; +import it.polito.escape.verify.model.Configuration; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Node; + +public class MultiThreadedTestCase { + + private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException { + final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api"); + + Response retrieveGraphResponse = verifyClient.retrieveGraph(1); + String responseString = retrieveGraphResponse.readEntity(String.class); + System.out.println(responseString); + + Graph graph = new ObjectMapper().readValue(responseString, Graph.class); + + UpdateGraph task = new UpdateGraph(verifyClient, 1, graph); + + List tasks = Collections.nCopies(threadCount, task); + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); + + List> futures = executorService.invokeAll(tasks); + List resultList = new ArrayList(futures.size()); + + // Check for exceptions + for (Future future : futures) { + // Throws an exception if an exception was thrown by the task. + resultList.add(future.get().getStatus()); + } + // Validate the dimensions + Assert.assertEquals(threadCount, futures.size()); + List expectedList = new ArrayList(threadCount); + for (int i = 1; i <= threadCount; i++) { + expectedList.add(200); + } + // Validate expected results + Assert.assertEquals(expectedList, resultList); + } + + private void testUpdateGraph(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException { + final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api"); + + Response retrieveGraphResponse = verifyClient.retrieveGraph(2L); + String responseString = retrieveGraphResponse.readEntity(String.class); + System.out.println(responseString); + + Graph graph = new ObjectMapper().readValue(responseString, Graph.class); + + Node nodeToEdit = graph.getNodes().get(1L); + nodeToEdit.setFunctional_type("endpoint"); + nodeToEdit.setConfiguration(new Configuration(nodeToEdit.getName(), "", new ObjectMapper().createArrayNode())); + + String graphAsString = new ObjectMapper().writeValueAsString(graph); + System.out.println(graphAsString); + + UpdateGraph task = new UpdateGraph(verifyClient, 2, graph); + + List tasks = Collections.nCopies(threadCount, task); + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); + + List> futures = executorService.invokeAll(tasks); + List resultList = new ArrayList(futures.size()); + + // Check for exceptions + for (Future future : futures) { + // Throws an exception if an exception was thrown by the task. + resultList.add(future.get().readEntity(String.class)); + } + // Validate dimensions + Assert.assertEquals(threadCount, futures.size()); + + List expectedList = new ArrayList(threadCount); + for (int i = 1; i <= threadCount; i++) { + expectedList.add(graphAsString); + } + // Validate expected results + Assert.assertEquals(expectedList, resultList); + } + + private void testCreateGraphStatus(final int threadCount, Graph graph) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException { + final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api"); + + CreateGraph task = new CreateGraph(verifyClient, graph); + + List tasks = Collections.nCopies(threadCount, task); + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); + + List> futures = executorService.invokeAll(tasks); + List resultList = new ArrayList(futures.size()); + + // Check for exceptions + for (Future future : futures) { + // Throws an exception if an exception was thrown by the task. + resultList.add(future.get().getStatus()); + } + // Validate the IDs + Assert.assertEquals(threadCount, futures.size()); + + List expectedList = new ArrayList(threadCount); + for (int i = 1; i <= threadCount; i++) { + expectedList.add(201); + } + // Validate expected results + Assert.assertEquals(expectedList, resultList); + } + + private int randInt(int min, int max){ + Random rand = new Random(); + int randomNum = rand.nextInt((max - min) + 1) + min; + return randomNum; + } + + @Test + public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException { + testUpdateGraphStatus(64); + } + + @Test + public void updateGraphResponseCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException { + testUpdateGraph(16); + } + + @Test + public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException { + Graph graph = new Graph(); + testCreateGraphStatus(8, graph); + } + + class UpdateGraph implements Callable { + + private VerifyClient verifyClient; + + private int graphId; + + private Graph graph; + + public UpdateGraph(VerifyClient verifyClient, int graphId, Graph graph) { + this.graphId = graphId; + this.graph = graph; + this.verifyClient = verifyClient; + } + + @Override + public Response call() throws Exception { + Thread.sleep(randInt(0,2000)); + Response updateGraphResponse = this.verifyClient.updateGraph(this.graphId, this.graph); + return updateGraphResponse; + } + } + + class CreateGraph implements Callable { + + private VerifyClient verifyClient; + + private Graph graph; + + public CreateGraph(VerifyClient verifyClient, Graph graph) { + this.graph = graph; + this.verifyClient = verifyClient; + } + + @Override + public Response call() throws Exception { + Thread.sleep(randInt(0,2000)); + return this.verifyClient.createGraph(this.graph); + } + + } +} \ No newline at end of file diff --git a/verigraph/src/main/java/it/polito/escape/verify/test/Scalability.java b/verigraph/src/main/java/it/polito/escape/verify/test/Scalability.java new file mode 100644 index 0000000..3039ca0 --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/test/Scalability.java @@ -0,0 +1,409 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.test; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import it.polito.escape.verify.client.VerifyClient; +import it.polito.escape.verify.client.VerifyClientException; +import it.polito.escape.verify.model.Configuration; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Neighbour; +import it.polito.escape.verify.model.Node; +import it.polito.escape.verify.model.Verification; + +public class Scalability { + + private VerifyClient client = new VerifyClient("http://localhost:8080/verify/api"); + + public static void main(String[] args) throws VerifyClientException { + Scalability s = new Scalability(); + + reachabilityTest(s, 10); + + isolationTest(s,1500); + traversalTest(s,600); + } + + private static void reachabilityTest(Scalability s, int n) throws VerifyClientException { + System.out.printf("Reachability test with N=" + n + ": "); + printTimestamp(); + Graph graph = generateNatScenario(n); + Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class); + Verification result = s.client.getReachability(createdGraph.getId(), "client", "server"); + System.out.println("Test returned " + result.getResult()); + System.out.printf("Finished reachability test with N=" + n + ": "); + printTimestamp(); + System.out.println(); + } + + private static void isolationTest(Scalability s, int n) throws VerifyClientException { + System.out.printf("Isolation test with N=" + n + ": "); + printTimestamp(); + Graph graph = generateNatScenario(n); + Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class); + Verification result = s.client.getIsolation(createdGraph.getId(), "client", "server", "firewall"); + System.out.println("Test returned " + result.getResult()); + System.out.printf("Finished isolation test with N=" + n + ": "); + printTimestamp(); + System.out.println(); + } + + private static void traversalTest(Scalability s, int n) throws VerifyClientException { + System.out.printf("Traversal test with N=" + n + ": "); + printTimestamp(); + Graph graph = generateNatScenario(n); + Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class); + Verification result = s.client.getTraversal(createdGraph.getId(), "client", "server", "firewall"); + System.out.println("Test returned " + result.getResult()); +// System.out.println("Result explanation: " + result.getComment()); + System.out.printf("Finished traversal test with N=" + n + ": "); + printTimestamp(); + System.out.println(); + } + + private static void printTimestamp() { + java.util.Date date= new java.util.Date(); + System.out.println(new Timestamp(date.getTime())); + } + + private static Graph generateNatScenario(int n) { + List nodes = new ArrayList(); + + Node client = new Node(); + client.setName("client"); + client.setFunctional_type("endhost"); + ArrayNode clientConfigArray = new ObjectMapper().createArrayNode(); + JsonNode clientConfig = new ObjectMapper().createObjectNode(); + ((ObjectNode)clientConfig).put("url", "www.facebook.com"); + ((ObjectNode)clientConfig).put("body", "word"); + ((ObjectNode)clientConfig).put("destination","server"); + ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST"); + clientConfigArray.add(clientConfig); + client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray)); + + Map clientNeighbours = new HashMap(); + clientNeighbours.put(1L, new Neighbour(1L, "nat1")); + client.setNeighbours(clientNeighbours ); + //add client to list + nodes.add(client); + + for(int i=0; i< n;i++){ + Node nat = new Node(); + nat.setId(i+1); + nat.setName("nat" + (i+1)); + nat.setFunctional_type("nat"); + ArrayNode configArray = new ObjectMapper().createArrayNode(); + + Map natNeighbours = new HashMap(); + + //set left neighbour for each node except the first + if(nat.getId() != 1){ + natNeighbours.put(1L, new Neighbour(1L, "nat" + i)); + configArray.add("client"); + for (int j=1; j <= i; j++){ + configArray.add("nat" + j); + } + } + //first nat: set only client as neighbour and natted node + else{ + natNeighbours.put(1L, new Neighbour(1L, "client")); + configArray.add("client"); + } + //set right neighbour for each node except the last + if(nat.getId() != n){ + natNeighbours.put(2L, new Neighbour(1L, "nat" + (i+2))); + } + //last nat: set server as neighbour + else{ + natNeighbours.put(2L, new Neighbour(1L, "server")); + } + + nat.setNeighbours(natNeighbours); + nat.setConfiguration(new Configuration(nat.getName(),"", configArray)); + + //add nat to list + nodes.add(nat); + } + + Node server = new Node(); + server.setName("server"); + server.setFunctional_type("webserver"); + ArrayNode serverConfigArray = new ObjectMapper().createArrayNode(); + server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray)); + + Map serverNeighbours = new HashMap(); + serverNeighbours.put(1L, new Neighbour(1L, "nat" + (n))); + server.setNeighbours(serverNeighbours ); + + //add server to list + nodes.add(server); + + //create graph + Graph g = new Graph(); + Map graphNodes = new HashMap(); + long index = 1L; + for (Node node : nodes){ + graphNodes.put(index, node); + index++; + } + g.setNodes(graphNodes); + + return g; + } + + private static Graph generateFirewallScenario(int n) { + List nodes = new ArrayList(); + + Node client = new Node(); + client.setName("client"); + client.setFunctional_type("endhost"); + ArrayNode clientConfigArray = new ObjectMapper().createArrayNode(); + JsonNode clientConfig = new ObjectMapper().createObjectNode(); + ((ObjectNode)clientConfig).put("url", "www.facebook.com"); + ((ObjectNode)clientConfig).put("body", "word"); + ((ObjectNode)clientConfig).put("destination","server"); + ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST"); + clientConfigArray.add(clientConfig); + client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray)); + + Map clientNeighbours = new HashMap(); + clientNeighbours.put(1L, new Neighbour(1L, "firewall1")); + client.setNeighbours(clientNeighbours ); + //add client to list + nodes.add(client); + + for(int i=0; i< n;i++){ + Node firewall = new Node(); + firewall.setId(i+1); + firewall.setName("firewall" + (i+1)); + firewall.setFunctional_type("firewall"); + ArrayNode configArray = new ObjectMapper().createArrayNode(); + + Map natNeighbours = new HashMap(); + + //set left neighbour for each node except the first + if(firewall.getId() != 1){ + natNeighbours.put(1L, new Neighbour(1L, "firewall" + i)); + } + //first firewall: set only client as neighbour and natted node + else{ + natNeighbours.put(1L, new Neighbour(1L, "client")); + } + //set right neighbour for each node except the last + if(firewall.getId() != n){ + natNeighbours.put(2L, new Neighbour(1L, "firewall" + (i+2))); + } + //last firewall: set server as neighbour + else{ + natNeighbours.put(2L, new Neighbour(1L, "server")); + } + + firewall.setNeighbours(natNeighbours); + firewall.setConfiguration(new Configuration(firewall.getName(),"", configArray)); + + //add nat to list + nodes.add(firewall); + } + + Node server = new Node(); + server.setName("server"); + server.setFunctional_type("webserver"); + ArrayNode serverConfigArray = new ObjectMapper().createArrayNode(); + server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray)); + + Map serverNeighbours = new HashMap(); + serverNeighbours.put(1L, new Neighbour(1L, "firewall" + (n))); + server.setNeighbours(serverNeighbours ); + + //add server to list + nodes.add(server); + + //create graph + Graph g = new Graph(); + Map graphNodes = new HashMap(); + long index = 1L; + for (Node node : nodes){ + graphNodes.put(index, node); + index++; + } + g.setNodes(graphNodes); + + return g; + } + + private static Graph generateScenario(int n) { + List nodes = new ArrayList(); + + Node firewall = new Node(); + firewall.setName("firewall"); + firewall.setFunctional_type("firewall"); + ArrayNode firewallConfigArray = new ObjectMapper().createArrayNode(); + Map firewallNeighbours = new HashMap(); + + for (int i=0; i < n; i++){ + if(i!=0){ + JsonNode firewallEntry = new ObjectMapper().createObjectNode(); + ((ObjectNode) firewallEntry).put("server", "client" + (i+1)); + firewallConfigArray.add(firewallEntry); + } + firewallNeighbours.put(new Long(i+1), new Neighbour(new Long(i+1), "client" + (i+1))); + } + + firewallNeighbours.put(new Long(n+1), new Neighbour(new Long(n+1), "server")); + + firewall.setConfiguration(new Configuration(firewall.getName(),"", firewallConfigArray)); + + + firewall.setNeighbours(firewallNeighbours ); + //add client to list + nodes.add(firewall); + + for(int i=0; i< n;i++){ + Node client = new Node(); + client.setId(i+1); + client.setName("client" + (i+1)); + client.setFunctional_type("endhost"); + ArrayNode clientConfigArray = new ObjectMapper().createArrayNode(); + JsonNode clientConfig = new ObjectMapper().createObjectNode(); + ((ObjectNode)clientConfig).put("url", "www.facebook.com"); + ((ObjectNode)clientConfig).put("body", "word"); + ((ObjectNode)clientConfig).put("destination","server"); + ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST"); + clientConfigArray.add(clientConfig); + + Map clientNeighbours = new HashMap(); + + clientNeighbours.put(1L, new Neighbour(1L, "firewall")); + + client.setNeighbours(clientNeighbours); + client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray)); + + //add client to list + nodes.add(client); + } + + Node server = new Node(); + server.setName("server"); + server.setFunctional_type("webserver"); + ArrayNode serverConfigArray = new ObjectMapper().createArrayNode(); + server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray)); + + Map serverNeighbours = new HashMap(); + serverNeighbours.put(1L, new Neighbour(1L, "firewall")); + server.setNeighbours(serverNeighbours ); + + //add server to list + nodes.add(server); + + //create graph + Graph g = new Graph(); + Map graphNodes = new HashMap(); + long index = 1L; + for (Node node : nodes){ + graphNodes.put(index, node); + index++; + } + g.setNodes(graphNodes); + + return g; + } + + private static Graph generateDpiScenario(int n) { + List nodes = new ArrayList(); + + Node client = new Node(); + client.setName("client"); + client.setFunctional_type("endhost"); + ArrayNode clientConfigArray = new ObjectMapper().createArrayNode(); + JsonNode clientConfig = new ObjectMapper().createObjectNode(); + ((ObjectNode)clientConfig).put("url", "www.facebook.com"); + ((ObjectNode)clientConfig).put("body", "word"); + ((ObjectNode)clientConfig).put("destination","server"); + ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST"); + clientConfigArray.add(clientConfig); + client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray)); + + Map clientNeighbours = new HashMap(); + clientNeighbours.put(1L, new Neighbour(1L, "dpi1")); + client.setNeighbours(clientNeighbours ); + //add client to list + nodes.add(client); + + for(int i=0; i< n;i++){ + Node dpi = new Node(); + dpi.setId(i+1); + dpi.setName("dpi" + (i+1)); + dpi.setFunctional_type("dpi"); + ArrayNode configArray = new ObjectMapper().createArrayNode(); + configArray.add("drug"); + + Map natNeighbours = new HashMap(); + + //set left neighbour for each node except the first + if(dpi.getId() != 1){ + natNeighbours.put(1L, new Neighbour(1L, "dpi" + i)); + } + //first firewall: set only client as neighbour and natted node + else{ + natNeighbours.put(1L, new Neighbour(1L, "client")); + } + //set right neighbour for each node except the last + if(dpi.getId() != n){ + natNeighbours.put(2L, new Neighbour(1L, "dpi" + (i+2))); + } + //last firewall: set server as neighbour + else{ + natNeighbours.put(2L, new Neighbour(1L, "server")); + } + + dpi.setNeighbours(natNeighbours); + dpi.setConfiguration(new Configuration(dpi.getName(),"", configArray)); + + //add nat to list + nodes.add(dpi); + } + + Node server = new Node(); + server.setName("server"); + server.setFunctional_type("webserver"); + ArrayNode serverConfigArray = new ObjectMapper().createArrayNode(); + server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray)); + + Map serverNeighbours = new HashMap(); + serverNeighbours.put(1L, new Neighbour(1L, "dpi" + (n))); + server.setNeighbours(serverNeighbours ); + + //add server to list + nodes.add(server); + + //create graph + Graph g = new Graph(); + Map graphNodes = new HashMap(); + long index = 1L; + for (Node node : nodes){ + graphNodes.put(index, node); + index++; + } + g.setNodes(graphNodes); + + return g; + } + +} diff --git a/verigraph/src/main/java/it/polito/escape/verify/test/TestCase.java b/verigraph/src/main/java/it/polito/escape/verify/test/TestCase.java new file mode 100644 index 0000000..a4c85ab --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/test/TestCase.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.test; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import it.polito.escape.verify.model.Graph; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ "id", "name", "description", "policy_url_parameters", "result", "graph" }) +public class TestCase { + + @JsonProperty("id") + private Integer id; + + @JsonProperty("name") + private String name; + + @JsonProperty("description") + private String description; + + @JsonProperty("policy_url_parameters") + private String policyUrlParameters; + + @JsonProperty("result") + private String result; + + @JsonProperty("graph") + private Graph graph; + + @JsonIgnore + private Map additionalProperties = new HashMap(); + + /** + * @return The id + */ + @JsonProperty("id") + public Integer getId() { + return id; + } + + /** + * @param id + * The id + */ + @JsonProperty("id") + public void setId(Integer id) { + this.id = id; + } + + /** + * @return The name + */ + @JsonProperty("name") + public String getName() { + return name; + } + + /** + * @param name + * The name + */ + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + /** + * @return The description + */ + @JsonProperty("description") + public String getDescription() { + return description; + } + + /** + * @param description + * The description + */ + @JsonProperty("description") + public void setDescription(String description) { + this.description = description; + } + + /** + * @return The policyUrlParameters + */ + @JsonProperty("policy_url_parameters") + public String getPolicyUrlParameters() { + return policyUrlParameters; + } + + /** + * @param policyUrlParameters + * The policy_url_parameters + */ + @JsonProperty("policy_url_parameters") + public void setPolicyUrlParameters(String policyUrlParameters) { + this.policyUrlParameters = policyUrlParameters; + } + + /** + * @return The result + */ + @JsonProperty("result") + public String getResult() { + return result; + } + + /** + * @param result + * The result + */ + @JsonProperty("result") + public void setResult(String result) { + this.result = result; + } + + /** + * @return The graph + */ + @JsonProperty("graph") + public Graph getGraph() { + return graph; + } + + /** + * @param graph + * The graph + */ + @JsonProperty("graph") + public void setGraph(Graph graph) { + this.graph = graph; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/verigraph/src/main/java/it/polito/escape/verify/test/TestExecutionException.java b/verigraph/src/main/java/it/polito/escape/verify/test/TestExecutionException.java new file mode 100644 index 0000000..8babe7b --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/test/TestExecutionException.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.test; + + +public class TestExecutionException extends Exception{ + + /** + * + */ + private static final long serialVersionUID = 4749065055436886197L; + + public TestExecutionException(String message){ + super(message); + } + +} diff --git a/verigraph/src/main/java/it/polito/escape/verify/test/Tester.java b/verigraph/src/main/java/it/polito/escape/verify/test/Tester.java new file mode 100644 index 0000000..4b85e1e --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/test/Tester.java @@ -0,0 +1,220 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.test; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ResponseProcessingException; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.fge.jsonschema.core.exceptions.ProcessingException; +import com.github.fge.jsonschema.main.JsonSchema; + +import it.polito.escape.verify.client.VerifyClient; +import it.polito.escape.verify.client.VerifyClientException; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Verification; +import it.polito.escape.verify.service.ValidationUtils; + +public class Tester { + + private File schema; + + private List testFiles = new ArrayList(); + + private List testCases = new ArrayList(); + + private String target; + + private VerifyClient verifyClient; + + public Tester(String target, File schema, File folder) throws JsonParseException, JsonMappingException, IOException, + Exception { + init(target, schema, folder); + } + + private void init(String target, File schema, File folder) throws JsonParseException, JsonMappingException, + IOException, Exception { + this.target = target; + this.verifyClient = new VerifyClient(this.target); + this.schema = schema; + this.testFiles = getTests(folder); + this.testCases = getTestCases(this.testFiles); + } + + public List getTests(File folder) { + List filesList = new ArrayList(); + + System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'"); + + File[] files = folder.listFiles(new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".json"); + } + }); + + for (File f : files) { + filesList.add(f); + System.out.println("File '" + f.getName() + "' added to test files"); + } + + return filesList; + } + + public List getTestCases(List files) throws JsonParseException, JsonMappingException, IOException, + Exception { + List testCases = new ArrayList(); + + for (File file : files) { + validateTestFile(file); + try { + TestCase tc = new ObjectMapper().readValue(file, TestCase.class); + testCases.add(tc); + } + catch (Exception e) { + throw e; + } + } + + return testCases; + } + + private void runTestCases() throws VerifyClientException, TestExecutionException { + int counter = 0; + for (TestCase tc : this.testCases) { + String result = runTestCase(tc); + if (!result.equals(tc.getResult())) + throw new TestExecutionException("Error running test given in file '" + this.testFiles.get(counter).getName() + + "'. Test returned '" + result + "' instead of '" + tc.getResult() + "'."); + else + System.out.println("Test given in file '" + this.testFiles.get(counter).getName() + "' returned '" + + result + "' as expected"); + counter++; + } + System.out.println("All tests PASSED"); + } + + private String runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{ + Client client = ClientBuilder.newClient(); + + Graph graph = tc.getGraph(); + Response response = null; + try{ + response = this.verifyClient.createGraph(graph); + } + catch(ResponseProcessingException e){ + throw new TestExecutionException("Response processing has failed: " + e.getResponse().readEntity(String.class)); + } + catch(javax.ws.rs.ProcessingException e){ + throw new TestExecutionException("HTTP request failed"); + } + Graph createdGraph = response.readEntity(Graph.class); + String urlParams = tc.getPolicyUrlParameters(); + WebTarget target = client.target(this.target + "/graphs/" + createdGraph.getId() + "/policy" + urlParams); + + response = target.request().get(); + Verification verification = response.readEntity(Verification.class); + return verification.getResult(); + } + + public void validateTestFile(File testFile) throws Exception { + JsonSchema schemaNode = null; + try { + schemaNode = ValidationUtils.getSchemaNode(schema); + } + catch (IOException e) { + throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file"); + } + catch (ProcessingException e) { + throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node"); + } + + JsonNode jsonNode; + try { + jsonNode = ValidationUtils.getJsonNode(testFile); + } + catch (IOException e) { + throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node"); + } + + try { + ValidationUtils.validateJson(schemaNode, jsonNode); + } + catch (ProcessingException e) { + throw new Exception("There were errors in the validation of file '" + testFile.getAbsolutePath() + + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage()); + + } + } + + public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException, + VerifyClientException, Exception { + String folderName = System.getProperty("user.dir") + "/tester/testcases"; + File folder = new File(folderName); + if (!folder.exists()) { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String s; + do{ + System.out.println("Please enter the testcases folder path: "); + s = in.readLine(); + if (isValidpath(s)){ + folder = new File(s); + break; + } + }while (s != null && s.length() != 0); + if(s == null) + System.exit(0); + } + String schemaName = System.getProperty("user.dir") + "/tester/testcase_schema.json"; + File schema = new File(schemaName); + if (!schema.exists()) { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String s; + do{ + System.out.println("Please enter the full path of 'testcase_schema.json': "); + s = in.readLine(); + if (isValidpath(s)){ + folder = new File(s); + break; + } + }while (s != null && s.length() != 0); + if(s == null) + System.exit(0); + } + + Tester tester = new Tester("http://localhost:8080/verify/api", schema, folder); + + tester.runTestCases(); + + } + + private static boolean isValidpath(String s) { + if (s==null) + return false; + File file = new File(s); + return file.exists(); + } + +} -- cgit 1.2.3-korg