summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/grpc/test/GrpcTest.java
blob: 41f9cad9bb3dc0a09849bd71869d50d8289af035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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);
	}
}