summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/grpc/test
diff options
context:
space:
mode:
authorserena.spinoso <serena.spinoso@polito.it>2017-02-25 12:00:55 +0100
committerserena.spinoso <serena.spinoso@polito.it>2017-02-27 13:39:18 +0100
commit53d83244c1bf36af86e90ce5fe758a369f73563e (patch)
treef5780b3033763d6b3cd62c40f5d4a63dfac35f7a /verigraph/src/main/java/it/polito/grpc/test
parentf0108f26fb1f62fefbbc4d5484b87241d563737b (diff)
Add verigraph code base
JIRA: PARSER-111 Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892 Signed-off-by: serena.spinoso <serena.spinoso@polito.it>
Diffstat (limited to 'verigraph/src/main/java/it/polito/grpc/test')
-rw-r--r--verigraph/src/main/java/it/polito/grpc/test/GrpcServerTest.java292
-rw-r--r--verigraph/src/main/java/it/polito/grpc/test/GrpcTest.java349
-rw-r--r--verigraph/src/main/java/it/polito/grpc/test/MultiThreadTest.java220
-rw-r--r--verigraph/src/main/java/it/polito/grpc/test/ReachabilityTest.java252
4 files changed, 1113 insertions, 0 deletions
diff --git a/verigraph/src/main/java/it/polito/grpc/test/GrpcServerTest.java b/verigraph/src/main/java/it/polito/grpc/test/GrpcServerTest.java
new file mode 100644
index 0000000..7344447
--- /dev/null
+++ b/verigraph/src/main/java/it/polito/grpc/test/GrpcServerTest.java
@@ -0,0 +1,292 @@
+/*******************************************************************************
+ * 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.grpc.test;
+
+import static org.junit.Assert.assertEquals;
+
+import io.grpc.ManagedChannel;
+import io.grpc.inprocess.InProcessChannelBuilder;
+import io.grpc.inprocess.InProcessServerBuilder;
+import io.grpc.verigraph.ConfigurationGrpc;
+import io.grpc.verigraph.GetRequest;
+import io.grpc.verigraph.GraphGrpc;
+import io.grpc.verigraph.NeighbourGrpc;
+import io.grpc.verigraph.NewGraph;
+import io.grpc.verigraph.NewNeighbour;
+import io.grpc.verigraph.NewNode;
+import io.grpc.verigraph.NodeGrpc;
+import io.grpc.verigraph.NodeGrpc.FunctionalType;
+import io.grpc.verigraph.RequestID;
+import io.grpc.verigraph.Status;
+import io.grpc.verigraph.VerigraphGrpc;
+import it.polito.grpc.Client;
+import it.polito.grpc.Service;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.FixMethodOrder;
+import org.junit.runners.MethodSorters;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Unit tests for {@link Service}.
+ * For testing basic gRPC unit test only.
+ * Not intended to provide a high code coverage or to test every major usecase.
+ */
+@RunWith(JUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class GrpcServerTest {
+ private Service server;
+ private ManagedChannel inProcessChannel;
+
+ @Before
+ public void setUp() throws Exception {
+ String uniqueServerName = "in-process server for " + getClass();
+ // use directExecutor for both InProcessServerBuilder and InProcessChannelBuilder can reduce the
+ // usage timeouts and latches in test. But we still add timeout and latches where they would be
+ // needed if no directExecutor were used, just for demo purpose.
+ server = new Service(InProcessServerBuilder.forName(uniqueServerName).directExecutor(),0);
+ server.start();
+ inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ inProcessChannel.shutdownNow();
+ server.stop();
+ }
+
+ @Test
+ public void test1Graph() throws Exception {
+ RequestID request = RequestID.newBuilder().setIdGraph(1).build() ;//id not present
+ GraphGrpc ufoundedGraph = GraphGrpc.newBuilder()
+ .setErrorMessage("Graph with id 1 not found").build();
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ // graph not found in the server
+ GraphGrpc graph = stub.getGraph(request);
+
+ assertEquals(ufoundedGraph, graph);
+
+ // getGraph in the server, but first add it
+ GraphGrpc addedGraph = GraphGrpc.newBuilder().build();
+ NewGraph response = stub.createGraph(addedGraph);
+ addedGraph = response.getGraph();
+ request = RequestID.newBuilder().setIdGraph(1).build() ;
+ graph = stub.getGraph(request);
+
+ assertEquals(addedGraph.getId(), graph.getId());
+
+ //updateGraph
+ GraphGrpc updatedGraph = GraphGrpc.newBuilder().setId(response.getGraph().getId()).build();
+ response = stub.updateGraph(updatedGraph);
+
+ assertEquals(response.getSuccess(),true);
+ }
+
+ @Test
+ public void test2Graphs() throws Exception {
+ // setup
+ GetRequest request = GetRequest.newBuilder().build();
+ GraphGrpc g1 = GraphGrpc.newBuilder().build();
+ GraphGrpc g2 = GraphGrpc.newBuilder().build();
+ GraphGrpc g3 = GraphGrpc.newBuilder().build();
+ GraphGrpc g4 = GraphGrpc.newBuilder().build();
+
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ stub.createGraph(g1);
+ stub.createGraph(g2);
+ stub.createGraph(g3);
+ stub.createGraph(g4);
+ g1 = GraphGrpc.newBuilder(g1).setId(1).build();
+ g2 = GraphGrpc.newBuilder(g2).setId(2).build();
+ g3 = GraphGrpc.newBuilder(g3).setId(3).build();
+ g4 = GraphGrpc.newBuilder(g4).setId(4).build();
+ // run
+ Iterator<GraphGrpc> graphs = stub.getGraphs(request);
+
+ assertEquals(graphs.next(), g1);
+ assertEquals(graphs.next(), g2);
+ assertEquals(graphs.next(), g3);
+ assertEquals(graphs.next(), g4);
+
+ //deleteGraph
+ RequestID req = RequestID.newBuilder().setIdGraph(g1.getId()).build();
+ stub.deleteGraph(req);
+ // run
+ graphs = stub.getGraphs(request);
+
+ assertEquals(graphs.next(), g2);
+ assertEquals(graphs.next(), g3);
+ assertEquals(graphs.next(), g4);
+ }
+
+ @Test
+ public void test3Node() throws Exception {
+ RequestID request = RequestID.newBuilder().setIdGraph(2).setIdNode(1).build() ;//id not present
+ NodeGrpc ufoundedGraph = NodeGrpc.newBuilder()
+ .setErrorMessage("Node with id 1 not found in graph with id 2").build();
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ // graph not found in the server
+ NodeGrpc node = stub.getNode(request);
+
+ assertEquals(ufoundedGraph, node);
+
+ // graph found in the server, but first add it
+ NodeGrpc addedNode = NodeGrpc.newBuilder().setName("client").setIdGraph(2)
+ .setFunctionalType(FunctionalType.endhost).build();
+ NewNode response = stub.createNode(addedNode);
+ addedNode = response.getNode();
+ request = RequestID.newBuilder().setIdGraph(2).setIdNode(1).build() ;
+ node = stub.getNode(request);
+
+ assertEquals(addedNode.getId(), node.getId());
+ assertEquals(addedNode.getName(),"client");
+
+ //updateNode
+ NodeGrpc updatedNode = NodeGrpc.newBuilder().setName("Nodo2").setIdGraph(2).setId(1)
+ .setFunctionalType(FunctionalType.endhost).build();
+ response = stub.updateNode(updatedNode);
+
+ assertEquals(response.getSuccess(),true);
+ assertEquals(response.getNode().getName(),"Nodo2");
+
+ //configureNode
+ Map<String,String> params = new HashMap<String,String>();
+ params.put("url", "www.facebook.com");
+ params.put("body", "word");
+ params.put("destination","server");
+ params.put("protocol", "HTTP_REQUEST");
+ ConfigurationGrpc configuration = Client.createConfigurationGrpc(params, null, null, null);
+ ConfigurationGrpc config = ConfigurationGrpc.newBuilder(configuration).setIdGraph(2)
+ .setIdNode(1).build();
+
+ Status status = stub.configureNode(config);
+
+ assertEquals(status.getSuccess(),true);
+ }
+
+ @Test
+ public void test4Nodes() throws Exception {
+ // setup
+ RequestID request = RequestID.newBuilder().setIdGraph(2).build();
+ NodeGrpc n1 = NodeGrpc.newBuilder(Client.createNodeGrpc("Node5", "endhost", null, null))
+ .setIdGraph(2).build();
+ NodeGrpc n2 = NodeGrpc.newBuilder(Client.createNodeGrpc("Node3", "endhost", null, null))
+ .setIdGraph(2).build();
+ NodeGrpc n3 = NodeGrpc.newBuilder(Client.createNodeGrpc("Node4", "endhost", null, null))
+ .setIdGraph(2).build();
+ NodeGrpc n4 = NodeGrpc.newBuilder(Client.createNodeGrpc("client", "endhost", null, null))
+ .setIdGraph(2).build();
+
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ stub.createNode(n1);
+ stub.createNode(n2);
+ stub.createNode(n3);
+ stub.createNode(n4);
+ n1 = NodeGrpc.newBuilder(n1).setId(2).setIdGraph(0).build();
+ n2 = NodeGrpc.newBuilder(n2).setId(3).setIdGraph(0).build();
+ n3 = NodeGrpc.newBuilder(n3).setId(4).setIdGraph(0).build();
+ n4 = NodeGrpc.newBuilder(n4).setId(5).setIdGraph(0).build();
+ // run
+ Iterator<NodeGrpc> nodes = stub.getNodes(request);
+
+ nodes.next();
+ assertEquals(nodes.next(), n1);
+ assertEquals(nodes.next(), n2);
+ assertEquals(nodes.next(), n3);
+ assertEquals(nodes.next(), n4);
+
+ //deleteNode
+ RequestID req = RequestID.newBuilder().setIdGraph(2).setIdNode(1).build();
+ stub.deleteNode(req);
+ // run
+ nodes = stub.getNodes(request);
+
+ assertEquals(nodes.next(), n1);
+ assertEquals(nodes.next(), n2);
+ assertEquals(nodes.next(), n3);
+ assertEquals(nodes.next(), n4);
+ }
+
+ @Test
+ public void test5Neighbours() throws Exception {
+ RequestID request = RequestID.newBuilder().setIdGraph(2).setIdNode(2).setIdNeighbour(1).build() ;//id not present
+ NeighbourGrpc ufoundedNeighbour = NeighbourGrpc.newBuilder()
+ .setErrorMessage("Neighbour with id 1 not found for node with id 2 in graph with id 2").build();;
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ // Neighbour not found in the server
+ NeighbourGrpc neighbour = stub.getNeighbour(request);
+
+ assertEquals(ufoundedNeighbour, neighbour);
+
+ // getNeighbour, but first add it
+ NeighbourGrpc addedNeighbour = NeighbourGrpc.newBuilder().setIdGraph(2).setIdNode(2)
+ .setName("client").build();
+ NewNeighbour response = stub.createNeighbour(addedNeighbour);
+ addedNeighbour = response.getNeighbour();
+ request = RequestID.newBuilder().setIdGraph(2).setIdNode(2)
+ .setIdNeighbour(addedNeighbour.getId()).build();
+ neighbour = stub.getNeighbour(request);
+
+ assertEquals(addedNeighbour.getId(), neighbour.getId());
+
+ //updateNeighbour
+ NeighbourGrpc updatedNeighbour = Client.createNeighbourGrpc("Node4");
+ NeighbourGrpc nu = NeighbourGrpc.newBuilder(updatedNeighbour)
+ .setId(response.getNeighbour().getId()).setIdGraph(2).setIdNode(2).build();
+ response = stub.updateNeighbour(nu);
+
+ assertEquals(response.getSuccess(),true);
+ assertEquals(response.getNeighbour().getName(),"Node4");
+ }
+
+ @Test
+ public void test6Neighbours() throws Exception {
+ // setup
+ RequestID request = RequestID.newBuilder().setIdGraph(2).setIdNode(2).build();
+ NeighbourGrpc n1 = NeighbourGrpc.newBuilder().setIdGraph(2).setIdNode(2)
+ .setName("Node3").build();
+ NeighbourGrpc n2 = NeighbourGrpc.newBuilder().setIdGraph(2).setIdNode(2)
+ .setName("client").build();
+
+ VerigraphGrpc.VerigraphBlockingStub stub = VerigraphGrpc.newBlockingStub(inProcessChannel);
+
+ stub.createNeighbour(n1);
+ stub.createNeighbour(n2);
+ n1 = NeighbourGrpc.newBuilder(n1).setId(2).setIdGraph(0).setIdNode(0).build();
+ n2 = NeighbourGrpc.newBuilder(n2).setId(3).setIdGraph(0).setIdNode(0).build();
+ // run
+ Iterator<NeighbourGrpc> neighbours = stub.getNeighbours(request);
+
+ neighbours.next();
+ assertEquals(neighbours.next(), n1);
+ assertEquals(neighbours.next(), n2);
+
+ //deleteNeighbour
+ RequestID req = RequestID.newBuilder().setIdGraph(2).setIdNode(2).setIdNeighbour(1).build();
+ stub.deleteNeighbour(req);
+ // run
+ neighbours = stub.getNeighbours(request);
+
+ assertEquals(neighbours.next(), n1);
+ assertEquals(neighbours.next(), n2);
+ }
+}
diff --git a/verigraph/src/main/java/it/polito/grpc/test/GrpcTest.java b/verigraph/src/main/java/it/polito/grpc/test/GrpcTest.java
new file mode 100644
index 0000000..41f9cad
--- /dev/null
+++ b/verigraph/src/main/java/it/polito/grpc/test/GrpcTest.java
@@ -0,0 +1,349 @@
+/*******************************************************************************
+ * 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.grpc.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.junit.runners.MethodSorters;
+
+import io.grpc.verigraph.ConfigurationGrpc;
+import io.grpc.verigraph.GraphGrpc;
+import io.grpc.verigraph.NeighbourGrpc;
+import io.grpc.verigraph.NewGraph;
+import io.grpc.verigraph.NewNeighbour;
+import io.grpc.verigraph.NewNode;
+import io.grpc.verigraph.NodeGrpc;
+import it.polito.grpc.Client;
+import it.polito.grpc.Service;
+
+@RunWith(JUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class GrpcTest {
+ private Service server;
+ private Client client;
+
+ @Before
+ public void setUpBeforeClass() throws Exception {
+ client = new Client("localhost" , 50051);
+ server = new Service(50051);
+ server.start();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ server.stop();
+ client.shutdown();
+ }
+
+ // method for comparing two non-null strings
+ private void compareString(String rs, String ts, String meaning) {
+ assertNotNull("NULL "+meaning, ts);
+ assertEquals("Wrong "+meaning, rs, ts);
+ }
+
+ @Test
+ public final void test1Load() throws Exception{
+ String funcType1 = "vpnaccess";
+ String funcType2 = "vpnexit";
+
+ // load an existing graph with 2 nodes
+ Map<String,String> map = new HashMap<String,String>();
+ map.put("vpnexit", "Node2");
+ ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
+ NodeGrpc node1 = Client.createNodeGrpc("Node1", funcType1, null, conf);
+ List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
+ NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
+ neighbours.add(nb);
+ map.clear();
+ map.put("vpnaccess", "Node1");
+ conf = Client.createConfigurationGrpc(map, null, null, null);
+ NodeGrpc node2 = Client.createNodeGrpc("Node2", funcType2, neighbours, conf);
+ List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
+ nodes.add(node1);
+ nodes.add(node2);
+ GraphGrpc graph = Client.createGraphGrpc(nodes);
+ //createGraph
+ graph = client.createGraph(graph).getGraph();
+
+ //getGraph
+ GraphGrpc retreivedGraph = client.getGraph(graph.getId());
+
+ assertNotNull("NULL Graph ",retreivedGraph);
+ assertEquals(graph.getId(), retreivedGraph.getId());
+
+ // check the name of first node and the id
+ compareString(retreivedGraph.getNodeList().get(0).getName(), graph.getNodeList().get(0).getName(), "node name");
+ assertEquals(retreivedGraph.getNodeList().get(0).getId(), graph.getNodeList().get(0).getId());
+
+ // check the name of second node and the id
+ compareString(retreivedGraph.getNodeList().get(1).getName(), graph.getNodeList().get(1).getName(), "node name");
+ assertEquals(retreivedGraph.getNodeList().get(1).getId(), graph.getNodeList().get(1).getId());
+
+ //updateGraph
+ GraphGrpc updatedGraph = GraphGrpc.newBuilder().build();
+ NewGraph response = client.updateGraph(graph.getId(),updatedGraph);
+
+ assertEquals(response.getSuccess(),true);
+ }
+
+ @Test
+ public final void test2LoadWithError() throws Exception{
+ // try to load a graph with node without functionalType
+ NodeGrpc node = null;
+ try{
+ node = Client.createNodeGrpc("Node1", null, null, null);
+ fail( "createNodeGrpc didn't throw when I expected it to" );
+ }
+ catch(Exception ex){
+ }
+ List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
+ if(node != null)
+ nodes.add(node);
+
+ GraphGrpc graph = Client.createGraphGrpc(nodes);
+ graph = client.createGraph(graph).getGraph();
+
+ assertEquals(graph.getId(), 2);
+
+ //getGraphs
+ Iterator<GraphGrpc> graphs = client.getGraphs().iterator();
+
+ assertEquals(graphs.next().getId(), 1);
+ assertEquals(graphs.next(), graph);
+
+ //deleteGraph
+ boolean resp= client.deleteGraph(graph.getId());
+ assertEquals(resp, true);
+
+ List<GraphGrpc> listGraphs = client.getGraphs();
+
+ assertEquals(listGraphs.size(), 1);
+ assertEquals(listGraphs.get(0).getId(), 1);
+ }
+
+ @Test
+ public void test3Node() throws Exception {
+ NodeGrpc ufoundedGraph = NodeGrpc.newBuilder()
+ .setErrorMessage("Node with id 1 not found in graph with id 1").build();
+
+ // graph not found in the server
+ NodeGrpc node = client.getNode(1, 1);//id not present
+
+ assertEquals(ufoundedGraph, node);
+
+ // graph found in the server, but first add it
+ NodeGrpc addedNode = Client.createNodeGrpc("Node4", "firewall", null, null);
+ NewNode response = client.createNode(addedNode, 1);
+ addedNode = response.getNode();
+ node = client.getNode(1, 1);
+
+ assertEquals(addedNode.getId(), node.getId());
+
+ //updateNode
+ NodeGrpc updatedNode = Client.createNodeGrpc("Node9", "endhost", null, null);
+ response = client.updateNode(1, addedNode.getId(), updatedNode);
+
+ assertEquals(response.getSuccess(),true);
+
+ //configureNode
+ //this configuration is valid only on endhost!
+ Map<String,String> params = new HashMap<String,String>();
+ params.put("url", "www.facebook.com");
+ params.put("body", "word");
+ params.put("destination","server");
+ params.put("protocol", "HTTP_REQUEST");
+ ConfigurationGrpc configuration = Client.createConfigurationGrpc(params, null, null, null);
+
+ boolean status = client.configureNode(1, 1, configuration);
+
+ assertEquals(status,true);
+ }
+
+ @Test
+ public void test4Nodes() throws Exception {
+ // setup
+ GraphGrpc graph = Client.createGraphGrpc(null);
+ //createGraph
+ graph = client.createGraph(graph).getGraph();
+
+ List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
+ NeighbourGrpc nb = Client.createNeighbourGrpc("Node6");
+ neighbours.add(nb);
+ NodeGrpc n1 = Client.createNodeGrpc("Node6", "mailserver", null, null);
+ NodeGrpc n2 = Client.createNodeGrpc("Node9", "endhost", neighbours, null);
+ Map<String,String> map = new HashMap<String,String>();
+ map.put("mailserver", "Node6");
+ ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
+ NodeGrpc n3 = Client.createNodeGrpc("Node10", "mailclient", null, conf);
+ NodeGrpc n4 = Client.createNodeGrpc("Node11", "nat", null, null);
+
+ NewNode nw1 = client.createNode(n1, graph.getId());
+ NewNode nw2 = client.createNode(n2, graph.getId());
+ NewNode nw3 = client.createNode(n3, graph.getId());
+ NewNode nw4 = client.createNode(n4, graph.getId());
+ assertEquals(nw1.getSuccess(),true);
+ assertEquals(nw2.getSuccess(),true);
+ assertEquals(nw3.getSuccess(),true);
+ assertEquals(nw4.getSuccess(),true);
+ n1 = NodeGrpc.newBuilder(n1).setId(nw1.getNode().getId()).build();
+ n2 = NodeGrpc.newBuilder(n2).setId(nw2.getNode().getId()).build();
+ n3 = NodeGrpc.newBuilder(n3).setId(nw3.getNode().getId()).build();
+ n4 = NodeGrpc.newBuilder(n4).setId(nw4.getNode().getId()).build();
+
+ // getNodes
+ Iterator<NodeGrpc> nodes = client.getNodes(graph.getId()).iterator();
+
+ assertEquals(nodes.next().getName(), n1.getName());
+ assertEquals(nodes.next().getName(), n2.getName());
+ assertEquals(nodes.next().getName(), n3.getName());
+ assertEquals(nodes.next().getName(), n4.getName());
+
+ //deleteNode
+ client.deleteNode(graph.getId(), 1);
+ // run
+ nodes = client.getNodes(graph.getId()).iterator();
+
+ assertEquals(nodes.next().getName(), n2.getName());
+ assertEquals(nodes.next().getName(), n3.getName());
+ assertEquals(nodes.next().getName(), n4.getName());
+ }
+
+ @Test
+ public void test5Neighbours() throws Exception {
+ NeighbourGrpc ufoundedNeighbour = NeighbourGrpc.newBuilder()
+ .setErrorMessage("Neighbour with id 1 not found for node with id 1 in graph with id 1").build();;
+
+ // Neighbour not found in the server
+ NeighbourGrpc neighbour = client.getNeighbour(1, 1, 1);//id not present
+
+ assertEquals(ufoundedNeighbour, neighbour);
+
+ GraphGrpc graph = Client.createGraphGrpc(null);
+ graph = client.createGraph(graph).getGraph();
+
+ List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
+ NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
+ neighbours.add(nb);
+ NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
+ NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", neighbours, null);
+ NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
+ NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
+ NodeGrpc n5 = Client.createNodeGrpc("Node5", "webserver", null, null);
+ Map<String,String> map = new HashMap<String,String>();
+ map.put("webserver", "Node5");
+ ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
+ NodeGrpc n6 = Client.createNodeGrpc("Node6", "webclient", null, conf);
+ NodeGrpc n7 = Client.createNodeGrpc("Node7", "cache", null, null);
+ NodeGrpc n8 = Client.createNodeGrpc("Node8", "firewall", null, null);
+ NodeGrpc n9 = Client.createNodeGrpc("Node9", "fieldmodifier", null, null);
+ NodeGrpc n10 = Client.createNodeGrpc("Node10", "dpi", null, null);
+ NewNode nw1 = client.createNode(n1, graph.getId());
+ NewNode nw2 = client.createNode(n2, graph.getId());
+ NewNode nw3 = client.createNode(n3, graph.getId());
+ NewNode nw4 = client.createNode(n4, graph.getId());
+ NewNode nw5 = client.createNode(n5, graph.getId());
+ NewNode nw6 = client.createNode(n6, graph.getId());
+ NewNode nw7 = client.createNode(n7, graph.getId());
+ NewNode nw8 = client.createNode(n8, graph.getId());
+ NewNode nw9 = client.createNode(n9, graph.getId());
+ NewNode nw10 = client.createNode(n10, graph.getId());
+ assertEquals(nw1.getSuccess(),true);
+ assertEquals(nw2.getSuccess(),true);
+ assertEquals(nw3.getSuccess(),true);
+ assertEquals(nw4.getSuccess(),true);
+ assertEquals(nw5.getSuccess(),true);
+ assertEquals(nw6.getSuccess(),true);
+ assertEquals(nw7.getSuccess(),true);
+ assertEquals(nw8.getSuccess(),true);
+ assertEquals(nw9.getSuccess(),true);
+ assertEquals(nw10.getSuccess(),true);
+
+ // getNeighbour, but first add it
+ NeighbourGrpc addedNeighbour = Client.createNeighbourGrpc("Node9");
+ NewNeighbour response = client.createNeighbour(addedNeighbour, graph.getId(), nw1.getNode().getId());
+ addedNeighbour = response.getNeighbour();
+ neighbour = client.getNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour.getId());
+
+ assertEquals(addedNeighbour.getId(), neighbour.getId());
+
+ //updateNeighbour
+ NeighbourGrpc updatedNeighbour = Client.createNeighbourGrpc("Node10");
+
+ response = client.updateNeighbour(graph.getId(), 1, addedNeighbour.getId(),updatedNeighbour);
+
+ assertEquals(response.getSuccess(),true);
+ assertEquals(response.getNeighbour().getName(),"Node10");
+ }
+
+ @Test
+ public void test6Neighbours() throws Exception {
+ // setup
+ GraphGrpc graph = Client.createGraphGrpc(null);
+ //createGraph
+ graph = client.createGraph(graph).getGraph();
+
+ NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
+ NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", null, null);
+ NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
+ NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
+ NewNode nw1 = client.createNode(n1, graph.getId());
+ NewNode nw2 = client.createNode(n2, graph.getId());
+ NewNode nw3 = client.createNode(n3, graph.getId());
+ NewNode nw4 = client.createNode(n4, graph.getId());
+ assertEquals(nw1.getSuccess(),true);
+ assertEquals(nw2.getSuccess(),true);
+ assertEquals(nw3.getSuccess(),true);
+ assertEquals(nw4.getSuccess(),true);
+
+ //createNeighbour
+ NeighbourGrpc nn1 = Client.createNeighbourGrpc("Node2");
+ NewNeighbour addedNeighbour1 = client.createNeighbour(nn1, graph.getId(), nw1.getNode().getId());
+ assertEquals(addedNeighbour1.getSuccess(),true);
+ NeighbourGrpc nn2 = Client.createNeighbourGrpc("Node3");
+ NewNeighbour addedNeighbour2 = client.createNeighbour(nn2, graph.getId(), nw1.getNode().getId());
+ assertEquals(addedNeighbour2.getSuccess(),true);
+ NeighbourGrpc nn3 = Client.createNeighbourGrpc("Node4");
+ NewNeighbour addedNeighbour3 = client.createNeighbour(nn3, graph.getId(), nw1.getNode().getId());
+ assertEquals(addedNeighbour3.getSuccess(),true);
+
+ nn1 = NeighbourGrpc.newBuilder(nn1).setId(1).build();
+ nn2 = NeighbourGrpc.newBuilder(nn2).setId(2).build();
+ nn3 = NeighbourGrpc.newBuilder(nn3).setId(3).build();
+ // run
+ Iterator<NeighbourGrpc> neighbours = client.getNeighbours(graph.getId(), nw1.getNode().getId()).iterator();
+
+ assertEquals(neighbours.next(), nn1);
+ assertEquals(neighbours.next(), nn2);
+ assertEquals(neighbours.next(), nn3);
+
+ //deleteNeighbour
+ boolean succ = client.deleteNeighbour(graph.getId(), nw1.getNode().getId(), 1);
+ assertEquals(succ, true);
+ // run
+ neighbours = client.getNeighbours(graph.getId(), nw1.getNode().getId()).iterator();
+
+ assertEquals(neighbours.next(), nn2);
+ assertEquals(neighbours.next(), nn3);
+ }
+}
diff --git a/verigraph/src/main/java/it/polito/grpc/test/MultiThreadTest.java b/verigraph/src/main/java/it/polito/grpc/test/MultiThreadTest.java
new file mode 100644
index 0000000..5b76bea
--- /dev/null
+++ b/verigraph/src/main/java/it/polito/grpc/test/MultiThreadTest.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.grpc.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 org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.junit.runners.MethodSorters;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+
+import io.grpc.verigraph.GraphGrpc;
+import io.grpc.verigraph.NewGraph;
+import io.grpc.verigraph.NodeGrpc;
+import it.polito.escape.verify.client.VerifyClientException;
+import it.polito.grpc.Client;
+import it.polito.grpc.Service;
+
+/**
+ * Unit tests for gRPC project.
+ * For testing concurrency on server side.
+ */
+@RunWith(JUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class MultiThreadTest {
+ private Service server;
+ private Client client;
+
+ @Before
+ public void setUp() throws Exception {
+ client = new Client("localhost" , 50051);
+ server = new Service(50051);
+
+ server.start();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ server.stop();
+ client.shutdown();
+ }
+
+ private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
+ GraphGrpc retrieveGraphResponse =client.getGraph(1);
+
+ UpdateGraph task = new UpdateGraph(client, 1, retrieveGraphResponse);
+
+ List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
+ ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
+
+ List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
+ List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
+
+ // Check for exceptions
+ for (Future<NewGraph> future : futures) {
+ // Throws an exception if an exception was thrown by the task.
+ resultList.add(future.get().getSuccess());
+ }
+ // Validate the dimensions
+ Assert.assertEquals(threadCount, futures.size());
+
+ List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
+ for (int i = 1; i <= threadCount; i++) {
+ expectedList.add(true);
+ }
+ // Validate expected results
+ Assert.assertEquals(expectedList, resultList);
+ }
+
+ private void testUpdateGraph(final int threadCount) throws Exception {
+ GraphGrpc retrieveGraph = client.getGraph(2L);
+ NodeGrpc nodeToEdit = Client.createNodeGrpc("client",
+ "endpoint",
+ null,
+ Client.createConfigurationGrpc(null, null, "client", ""));
+
+ GraphGrpc graphToUpdate = GraphGrpc.newBuilder(retrieveGraph).addNode(nodeToEdit).build();
+
+ String graphAsString = graphToUpdate.toString();
+
+ UpdateGraph task = new UpdateGraph(client, 2, graphToUpdate);
+
+ List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
+ ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
+
+ List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
+ List<String> resultList = new ArrayList<String>(futures.size());
+
+ // Check for exceptions
+ for (Future<NewGraph> future : futures) {
+ // Throws an exception if an exception was thrown by the task.
+ GraphGrpc graphReceived = future.get().getGraph();
+ NodeGrpc node = NodeGrpc.newBuilder(graphReceived.getNode(0)).setId(0).build();
+ GraphGrpc graph = GraphGrpc.newBuilder(graphReceived).setNode(0, node).build();
+ resultList.add(graph.toString());
+ }
+ // Validate dimensions
+ Assert.assertEquals(threadCount, futures.size());
+
+ List<String> expectedList = new ArrayList<String>(threadCount);
+ for (int i = 1; i <= threadCount; i++) {
+ expectedList.add(graphAsString);
+ }
+ // Validate expected results
+ Assert.assertEquals(expectedList, resultList);
+ }
+
+ private void testCreateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
+
+ GraphGrpc graph = GraphGrpc.newBuilder().build();
+
+ CreateGraph task = new CreateGraph(client, graph);
+
+ List<MultiThreadTest.CreateGraph> tasks = Collections.nCopies(threadCount, task);
+ ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
+
+ List<Future<Boolean>> futures = executorService.invokeAll(tasks);
+ List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
+
+ // Check for exceptions
+ for (Future<Boolean> future : futures) {
+ // Throws an exception if an exception was thrown by the task.
+ resultList.add(future.get());
+ }
+ // Validate the IDs
+ Assert.assertEquals(threadCount, futures.size());
+
+ List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
+ for (int i = 1; i <= threadCount; i++) {
+ expectedList.add(true);
+ }
+ // 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 Exception {
+ testUpdateGraph(16);
+ }
+
+ @Test
+ public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
+ testCreateGraphStatus(8);
+ }
+
+ class UpdateGraph implements Callable<NewGraph> {
+
+ private Client verifyClient;
+
+ private int graphId;
+
+ private GraphGrpc graph;
+
+ public UpdateGraph(Client verifyClient, int graphId, GraphGrpc graph) {
+ this.graphId = graphId;
+ this.graph = graph;
+ this.verifyClient = verifyClient;
+ }
+
+ @Override
+ public NewGraph call() throws Exception {
+ Thread.sleep(randInt(0,2000));
+ return this.verifyClient.updateGraph(this.graphId, this.graph);
+ }
+ }
+
+ class CreateGraph implements Callable<Boolean> {
+
+ private Client verifyClient;
+
+ private GraphGrpc graph;
+
+ public CreateGraph(Client verifyClient, GraphGrpc graph) {
+ this.graph = graph;
+ this.verifyClient = verifyClient;
+ }
+
+ @Override
+ public Boolean call() throws Exception {
+ Thread.sleep(randInt(0,2000));
+ return this.verifyClient.createGraph(this.graph).getSuccess();
+ }
+
+ }
+
+}
diff --git a/verigraph/src/main/java/it/polito/grpc/test/ReachabilityTest.java b/verigraph/src/main/java/it/polito/grpc/test/ReachabilityTest.java
new file mode 100644
index 0000000..a120aff
--- /dev/null
+++ b/verigraph/src/main/java/it/polito/grpc/test/ReachabilityTest.java
@@ -0,0 +1,252 @@
+/*******************************************************************************
+ * 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.grpc.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.junit.runners.MethodSorters;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.BufferedReader;
+import java.io.FilenameFilter;
+import java.io.InputStreamReader;
+
+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 io.grpc.verigraph.GraphGrpc;
+import io.grpc.verigraph.NewGraph;
+import io.grpc.verigraph.Policy;
+import io.grpc.verigraph.VerificationGrpc;
+import it.polito.escape.verify.client.VerifyClientException;
+import it.polito.escape.verify.service.ValidationUtils;
+import it.polito.escape.verify.test.TestCase;
+import it.polito.escape.verify.test.TestExecutionException;
+import it.polito.grpc.Client;
+import it.polito.grpc.GrpcUtils;
+import it.polito.grpc.Service;
+
+@RunWith(JUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class ReachabilityTest {
+ private File schema;
+ private List<File> testFiles = new ArrayList<File>();
+ private List<TestCase> testCases = new ArrayList<TestCase>();
+ private Client client;
+ private Service server;
+
+ @Before
+ public void setUpBeforeClass() throws Exception {
+ client = new Client("localhost" , 50051);
+ server = new Service(50051);
+ server.start();
+
+ 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);
+ }
+
+ this.schema = schema;
+ this.testFiles = getTests(folder);
+ this.testCases = getTestCases(this.testFiles);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ server.stop();
+ client.shutdown();
+ }
+
+ @Test
+ public final void wrongReachability() {
+ System.out.println("DEBUG: starting testWrongReachability");
+
+ VerificationGrpc nullVer = VerificationGrpc.newBuilder()
+ .setErrorMessage("Graph with id 52 not found").build();
+ //verification on uncreated graph
+ Policy policyToVerify = Client.createPolicy("Node1", "Node4", "reachability", null, 52);
+ VerificationGrpc ver = client.verify(policyToVerify);
+ assertEquals(ver, nullVer);
+
+ //verification on uncreated nodes
+ nullVer = VerificationGrpc.newBuilder()
+ .setErrorMessage("The \'source\' parameter \'Node5\' is not valid, please insert the name of an existing node").build();
+ policyToVerify = Client.createPolicy("Node5", "Node4", "reachability", null, 1);
+ ver = client.verify(policyToVerify);
+ assertEquals(ver, nullVer);
+
+ //verification on uncreated nodes
+ nullVer = VerificationGrpc.newBuilder()
+ .setErrorMessage("The \'source\' parameter \'Node1\' is not valid, please insert the name of an existing node").build();
+
+ policyToVerify = Client.createPolicy("Node1", "Node10", "reachability", null, 1);
+ ver = client.verify(policyToVerify);
+ assertEquals(ver, nullVer);
+
+ }
+
+ public List<File> getTests(File folder) {
+ List<File> filesList = new ArrayList<File>();
+
+ 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<TestCase> getTestCases(List<File> files) throws JsonParseException, JsonMappingException, IOException,
+ Exception {
+ List<TestCase> testCases = new ArrayList<TestCase>();
+
+ 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;
+ }
+
+ @Test
+ public 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{
+ GraphGrpc graph = GrpcUtils.obtainGraph(tc.getGraph());
+
+ NewGraph newGraph = this.client.createGraph(graph);
+ if(newGraph.getSuccess() == false)
+ throw new VerifyClientException("gRPC request failed");
+ GraphGrpc createdGraph = newGraph.getGraph();
+
+ GraphGrpc addedgraph = client.getGraph(createdGraph.getId());
+ System.out.println(addedgraph);
+
+ final Map<String, String> map = GrpcUtils.getParamGivenString(tc.getPolicyUrlParameters());
+
+ Policy policy = Client.createPolicy(map.get("source"),
+ map.get("destination"),
+ map.get("type"),
+ map.get("middlebox"),
+ createdGraph.getId());
+ VerificationGrpc verification = this.client.verify(policy);
+ 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());
+
+ }
+ }
+
+ private static boolean isValidpath(String s) {
+ if (s==null)
+ return false;
+ File file = new File(s);
+ return file.exists();
+ }
+}