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.grpc.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14 import java.util.ArrayList;
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
20 import java.util.TreeSet;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.FixMethodOrder;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 import org.junit.runners.MethodSorters;
28 import it.polito.verigraph.grpc.ConfigurationGrpc;
29 import it.polito.verigraph.grpc.GraphGrpc;
30 import it.polito.verigraph.grpc.NeighbourGrpc;
31 import it.polito.verigraph.grpc.NewGraph;
32 import it.polito.verigraph.grpc.NewNeighbour;
33 import it.polito.verigraph.grpc.NewNode;
34 import it.polito.verigraph.grpc.NodeGrpc;
35 import it.polito.verigraph.grpc.client.Client;
36 import it.polito.verigraph.grpc.server.Service;
38 @RunWith(JUnit4.class)
39 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
40 public class GrpcTest {
41 private Service server;
42 private Client client;
45 public void setUpBeforeClass() throws Exception {
46 client = new Client("localhost" , 50051);
47 server = new Service(50051);
53 public void tearDown() throws Exception {
58 // method for comparing two non-null strings
59 private void compareString(String rs, String ts, String meaning) {
60 assertNotNull("NULL "+meaning, ts);
61 assertEquals("Wrong "+meaning, rs, ts);
64 public void deleteGraphs() {
65 for(GraphGrpc graph : client.getGraphs()){
66 client.deleteGraph(graph.getId());
72 public final void test1Load() throws Exception{
73 System.out.println("[DEBUG] test1Load starts");
75 String funcType1 = "vpnaccess";
76 String funcType2 = "vpnexit";
78 // load an existing graph with 2 nodes
79 Map<String,String> map = new HashMap<String,String>();
80 map.put("vpnexit", "Node2");
81 ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
82 NodeGrpc node1 = Client.createNodeGrpc("Node1", funcType1, null, conf);
83 List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
84 NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
87 map.put("vpnaccess", "Node1");
88 conf = Client.createConfigurationGrpc(map, null, null, null);
89 NodeGrpc node2 = Client.createNodeGrpc("Node2", funcType2, neighbours, conf);
90 List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
93 GraphGrpc graph = Client.createGraphGrpc(nodes);
95 graph = client.createGraph(graph).getGraph();
98 GraphGrpc retreivedGraph = client.getGraph(graph.getId());
100 assertNotNull("NULL Graph ",retreivedGraph);
101 assertEquals(graph.getId(), retreivedGraph.getId());
103 // check the name of first node and the id
104 compareString(retreivedGraph.getNodeList().get(0).getName(), graph.getNodeList().get(0).getName(), "node name");
105 assertEquals(retreivedGraph.getNodeList().get(0).getId(), graph.getNodeList().get(0).getId());
107 // check the name of second node and the id
108 compareString(retreivedGraph.getNodeList().get(1).getName(), graph.getNodeList().get(1).getName(), "node name");
109 assertEquals(retreivedGraph.getNodeList().get(1).getId(), graph.getNodeList().get(1).getId());
112 GraphGrpc updatedGraph = GraphGrpc.newBuilder().build();
113 NewGraph response = client.updateGraph(graph.getId(),updatedGraph);
115 assertEquals(response.getSuccess(),true);
119 public final void test2LoadWithError() throws Exception{
120 System.out.println("[DEBUG] test2Load starts");
122 // try to load a graph with node without functionalType
123 NodeGrpc node = null;
125 node = Client.createNodeGrpc("Node1", null, null, null);
126 fail( "createNodeGrpc didn't throw when I expected it to" );
130 List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
134 GraphGrpc graph = Client.createGraphGrpc(nodes);
135 GraphGrpc g = client.createGraph(graph).getGraph();
137 GraphGrpc get_graph= client.getGraph(g.getId());
138 assertEquals(g.getErrorMessage(), get_graph.getErrorMessage());
141 List<GraphGrpc> graph_list = client.getGraphs();
142 TreeSet<GraphGrpc> pts = new TreeSet<GraphGrpc>(new GraphGrpcComparator());
143 pts.addAll(graph_list);
144 Iterator<GraphGrpc> graphs = pts.iterator();
146 //assertEquals(graphs.next().getId(), g.getId());
148 assertEquals(graphs.next(), g);
151 boolean resp= client.deleteGraph(g.getId());
152 assertEquals(resp, true);
154 List<GraphGrpc> listGraphs = client.getGraphs();
156 assertEquals(listGraphs.size(), 0);
157 //assertEquals(listGraphs.get(0).getId(), 1);
161 public void test3Node() throws Exception {
162 System.out.println("[DEBUG] test3Load starts");
165 NodeGrpc ufoundedGraph = NodeGrpc.newBuilder()
166 .setErrorMessage("There is no Graph whose Id is '1'").build();
168 // graph not found in the server
169 NodeGrpc node = client.getNode(1, 1);//id not present
171 assertEquals(ufoundedGraph, node);
173 // graph found in the server, but first add it
174 NodeGrpc addedNode = Client.createNodeGrpc("Node4", "firewall", null, null);
175 GraphGrpc addedgraph = Client.createGraphGrpc(null);
176 NewGraph response_graph = client.createGraph(addedgraph);
177 NewNode response = client.createNode(addedNode, response_graph.getGraph().getId());
178 addedNode = response.getNode();
179 node = client.getNode(response_graph.getGraph().getId(), addedNode.getId());
181 assertEquals(addedNode.getId(), node.getId());
184 NodeGrpc updatedNode = Client.createNodeGrpc("Node9", "endhost", null, null);
185 response = client.updateNode(response_graph.getGraph().getId(), addedNode.getId(), updatedNode);
187 assertEquals(response.getSuccess(),true);
190 //this configuration is valid only on endhost!
191 Map<String,String> params = new HashMap<String,String>();
192 params.put("url", "www.facebook.com");
193 params.put("body", "word");
194 params.put("destination","server");
195 params.put("protocol", "HTTP_REQUEST");
196 ConfigurationGrpc configuration = Client.createConfigurationGrpc(params, null, null, null);
198 boolean status = client.configureNode(response_graph.getGraph().getId(), addedNode.getId(), configuration);
200 assertEquals(status,true);
204 public void test4Nodes() throws Exception {
205 System.out.println("[DEBUG] test4Load starts");
207 GraphGrpc graph = Client.createGraphGrpc(null);
209 graph = client.createGraph(graph).getGraph();
211 List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
212 NeighbourGrpc nb = Client.createNeighbourGrpc("Node6");
214 NodeGrpc n1 = Client.createNodeGrpc("Node6", "mailserver", null, null);
215 NodeGrpc n2 = Client.createNodeGrpc("Node9", "endhost", neighbours, null);
216 Map<String,String> map = new HashMap<String,String>();
217 map.put("mailserver", "Node6");
218 ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
219 NodeGrpc n3 = Client.createNodeGrpc("Node10", "mailclient", null, conf);
220 NodeGrpc n4 = Client.createNodeGrpc("Node11", "nat", null, null);
222 NewNode nw1 = client.createNode(n1, graph.getId());
223 NewNode nw2 = client.createNode(n2, graph.getId());
224 NewNode nw3 = client.createNode(n3, graph.getId());
225 NewNode nw4 = client.createNode(n4, graph.getId());
226 assertEquals(nw1.getSuccess(),true);
227 assertEquals(nw2.getSuccess(),true);
228 assertEquals(nw3.getSuccess(),true);
229 assertEquals(nw4.getSuccess(),true);
230 n1 = NodeGrpc.newBuilder(n1).setId(nw1.getNode().getId()).build();
231 n2 = NodeGrpc.newBuilder(n2).setId(nw2.getNode().getId()).build();
232 n3 = NodeGrpc.newBuilder(n3).setId(nw3.getNode().getId()).build();
233 n4 = NodeGrpc.newBuilder(n4).setId(nw4.getNode().getId()).build();
236 List<NodeGrpc> node_list = client.getNodes(graph.getId());
237 TreeSet<NodeGrpc> pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
238 pts.addAll(node_list);
239 Iterator<NodeGrpc> nodes = pts.iterator();
242 assertEquals(nodes.next().getName(), n3.getName());
243 assertEquals(nodes.next().getName(), n4.getName());
244 assertEquals(nodes.next().getName(), n1.getName());
245 assertEquals(nodes.next().getName(), n2.getName());
249 client.deleteNode(graph.getId(), n1.getId());
251 node_list = client.getNodes(graph.getId());
252 pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
253 pts.addAll(node_list);
254 nodes = pts.iterator();
256 assertEquals(nodes.next().getName(), n3.getName());
257 assertEquals(nodes.next().getName(), n4.getName());
258 assertEquals(nodes.next().getName(), n2.getName());
262 public void test5Neighbours() throws Exception {
263 System.out.println("[DEBUG] test5Load starts");
264 NeighbourGrpc ufoundedNeighbour = NeighbourGrpc.newBuilder()
265 .setErrorMessage("There is no Graph whose Id is '1'").build();;
267 // Neighbour not found in the server
268 NeighbourGrpc neighbour = client.getNeighbour(1, 1, 1);//id not present
270 assertEquals(ufoundedNeighbour, neighbour);
272 GraphGrpc graph = Client.createGraphGrpc(null);
273 graph = client.createGraph(graph).getGraph();
275 List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
276 NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
278 NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
279 NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", neighbours, null);
280 NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
281 NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
282 NodeGrpc n5 = Client.createNodeGrpc("Node5", "webserver", null, null);
283 Map<String,String> map = new HashMap<String,String>();
284 map.put("webserver", "Node5");
285 ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
286 NodeGrpc n6 = Client.createNodeGrpc("Node6", "webclient", null, conf);
287 NodeGrpc n7 = Client.createNodeGrpc("Node7", "cache", null, null);
288 NodeGrpc n8 = Client.createNodeGrpc("Node8", "firewall", null, null);
289 NodeGrpc n9 = Client.createNodeGrpc("Node9", "fieldmodifier", null, null);
290 NodeGrpc n10 = Client.createNodeGrpc("Node10", "dpi", null, null);
291 NewNode nw1 = client.createNode(n1, graph.getId());
292 NewNode nw2 = client.createNode(n2, graph.getId());
293 NewNode nw3 = client.createNode(n3, graph.getId());
294 NewNode nw4 = client.createNode(n4, graph.getId());
295 NewNode nw5 = client.createNode(n5, graph.getId());
296 NewNode nw6 = client.createNode(n6, graph.getId());
297 NewNode nw7 = client.createNode(n7, graph.getId());
298 NewNode nw8 = client.createNode(n8, graph.getId());
299 NewNode nw9 = client.createNode(n9, graph.getId());
300 NewNode nw10 = client.createNode(n10, graph.getId());
301 assertEquals(nw1.getSuccess(),true);
302 assertEquals(nw2.getSuccess(),true);
303 assertEquals(nw3.getSuccess(),true);
304 assertEquals(nw4.getSuccess(),true);
305 assertEquals(nw5.getSuccess(),true);
306 assertEquals(nw6.getSuccess(),true);
307 assertEquals(nw7.getSuccess(),true);
308 assertEquals(nw8.getSuccess(),true);
309 assertEquals(nw9.getSuccess(),true);
310 assertEquals(nw10.getSuccess(),true);
312 // getNeighbour, but first add it
313 NeighbourGrpc addedNeighbour = Client.createNeighbourGrpc("Node9");
314 NewNeighbour response = client.createNeighbour(addedNeighbour, graph.getId(), nw1.getNode().getId());
315 addedNeighbour = response.getNeighbour();
316 neighbour = client.getNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour.getId());
318 assertEquals(addedNeighbour.getId(), neighbour.getId());
321 NeighbourGrpc updatedNeighbour = Client.createNeighbourGrpc("Node10");
323 response = client.updateNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour.getId(),updatedNeighbour);
325 assertEquals(response.getSuccess(),true);
326 assertEquals(response.getNeighbour().getName(),"Node10");
330 public void test6Neighbours() throws Exception {
331 System.out.println("[DEBUG] test6Load starts");
333 GraphGrpc graph = Client.createGraphGrpc(null);
335 graph = client.createGraph(graph).getGraph();
337 NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
338 NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", null, null);
339 NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
340 NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
341 NewNode nw1 = client.createNode(n1, graph.getId());
342 NewNode nw2 = client.createNode(n2, graph.getId());
343 NewNode nw3 = client.createNode(n3, graph.getId());
344 NewNode nw4 = client.createNode(n4, graph.getId());
345 assertEquals(nw1.getSuccess(),true);
346 assertEquals(nw2.getSuccess(),true);
347 assertEquals(nw3.getSuccess(),true);
348 assertEquals(nw4.getSuccess(),true);
351 NeighbourGrpc nn1 = Client.createNeighbourGrpc("Node2");
352 NewNeighbour addedNeighbour1 = client.createNeighbour(nn1, graph.getId(), nw1.getNode().getId());
353 assertEquals(addedNeighbour1.getSuccess(),true);
354 NeighbourGrpc nn2 = Client.createNeighbourGrpc("Node3");
355 NewNeighbour addedNeighbour2 = client.createNeighbour(nn2, graph.getId(), nw1.getNode().getId());
356 assertEquals(addedNeighbour2.getSuccess(),true);
357 NeighbourGrpc nn3 = Client.createNeighbourGrpc("Node4");
358 NewNeighbour addedNeighbour3 = client.createNeighbour(nn3, graph.getId(), nw1.getNode().getId());
359 assertEquals(addedNeighbour3.getSuccess(),true);
361 nn1 = NeighbourGrpc.newBuilder(nn1).setId(1).build();
362 nn2 = NeighbourGrpc.newBuilder(nn2).setId(2).build();
363 nn3 = NeighbourGrpc.newBuilder(nn3).setId(3).build();
365 List<NeighbourGrpc> node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
366 TreeSet<NeighbourGrpc> pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
367 pts.addAll(node_list);
368 Iterator<NeighbourGrpc> neighbours = pts.iterator();
370 while(neighbours.hasNext()){
374 if(neighbours.hasNext()){
375 assertEquals(neighbours.next(), addedNeighbour1.getNeighbour());
376 assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
377 assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
381 boolean succ = client.deleteNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour1.getNeighbour().getId());
382 assertEquals(succ, true);
384 node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
385 pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
386 pts.addAll(node_list);
387 neighbours = pts.iterator();
389 while(neighbours.hasNext()){
394 if(neighbours.hasNext()){
395 assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
396 assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
402 class NodeGrpcComparator implements Comparator<NodeGrpc> {
403 public int compare(NodeGrpc n0, NodeGrpc n1) {
404 return n0.getName().compareTo(n1.getName());
408 class NeighbourGrpcComparator implements Comparator<NeighbourGrpc> {
409 public int compare(NeighbourGrpc n0, NeighbourGrpc n1) {
410 return n0.getName().compareTo(n1.getName());
414 class GraphGrpcComparator implements Comparator<GraphGrpc> {
415 public int compare(GraphGrpc n0, GraphGrpc n1) {
416 if(n0.getId() == n1.getId())
418 else if (n0.getId() > n1.getId())