summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/grpc/test/GrpcTest.java
blob: a1830cfb9ffb4f8c29e2808851023306970ea0a9 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
 * 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.verigraph.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.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
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 it.polito.verigraph.grpc.ConfigurationGrpc;
import it.polito.verigraph.grpc.GraphGrpc;
import it.polito.verigraph.grpc.NeighbourGrpc;
import it.polito.verigraph.grpc.NewGraph;
import it.polito.verigraph.grpc.NewNeighbour;
import it.polito.verigraph.grpc.NewNode;
import it.polito.verigraph.grpc.NodeGrpc;
import it.polito.verigraph.grpc.client.Client;
import it.polito.verigraph.grpc.server.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);
    }

    public void deleteGraphs() {
        for(GraphGrpc graph : client.getGraphs()){
            client.deleteGraph(graph.getId());
        }

    }

    @Test
    public final void test1Load() throws Exception{
        System.out.println("[DEBUG] test1Load starts");
        deleteGraphs();
        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{
        System.out.println("[DEBUG] test2Load starts");
        deleteGraphs();
        // 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);
        GraphGrpc g = client.createGraph(graph).getGraph();

        GraphGrpc get_graph= client.getGraph(g.getId());
        assertEquals(g.getErrorMessage(), get_graph.getErrorMessage());

        //getGraphs
        List<GraphGrpc> graph_list = client.getGraphs();
        TreeSet<GraphGrpc> pts = new TreeSet<GraphGrpc>(new GraphGrpcComparator());
        pts.addAll(graph_list);
        Iterator<GraphGrpc> graphs = pts.iterator();

        //assertEquals(graphs.next().getId(), g.getId());
        if(graphs.hasNext())
            assertEquals(graphs.next(), g);

        //deleteGraph
        boolean resp= client.deleteGraph(g.getId());
        assertEquals(resp, true);

        List<GraphGrpc> listGraphs = client.getGraphs();

        assertEquals(listGraphs.size(), 0);
        //assertEquals(listGraphs.get(0).getId(), 1);
    }

    @Test
    public void test3Node() throws Exception {
        System.out.println("[DEBUG] test3Load starts");
        deleteGraphs();

        NodeGrpc ufoundedGraph = NodeGrpc.newBuilder()
                .setErrorMessage("There is no Graph whose Id is '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);
        GraphGrpc addedgraph = Client.createGraphGrpc(null);
        NewGraph response_graph = client.createGraph(addedgraph);
        NewNode response = client.createNode(addedNode, response_graph.getGraph().getId());
        addedNode = response.getNode();
        node = client.getNode(response_graph.getGraph().getId(), addedNode.getId());

        assertEquals(addedNode.getId(), node.getId());

        //updateNode
        NodeGrpc updatedNode = Client.createNodeGrpc("Node9", "endhost", null, null);
        response = client.updateNode(response_graph.getGraph().getId(), 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(response_graph.getGraph().getId(), addedNode.getId(), configuration);

        assertEquals(status,true);
    }

    @Test
    public void test4Nodes() throws Exception {
        System.out.println("[DEBUG] test4Load starts");
        // 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
        List<NodeGrpc> node_list = client.getNodes(graph.getId());
        TreeSet<NodeGrpc> pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
        pts.addAll(node_list);
        Iterator<NodeGrpc> nodes = pts.iterator();
        //sorted by name
        if(nodes.hasNext()){
            assertEquals(nodes.next().getName(), n3.getName());
            assertEquals(nodes.next().getName(), n4.getName());
            assertEquals(nodes.next().getName(), n1.getName());
            assertEquals(nodes.next().getName(), n2.getName());
        }

        //deleteNode
        client.deleteNode(graph.getId(), n1.getId());
        // run
        node_list = client.getNodes(graph.getId());
        pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
        pts.addAll(node_list);
        nodes = pts.iterator();

        assertEquals(nodes.next().getName(), n3.getName());
        assertEquals(nodes.next().getName(), n4.getName());
        assertEquals(nodes.next().getName(), n2.getName());
    }

    @Test
    public void test5Neighbours() throws Exception {
        System.out.println("[DEBUG] test5Load starts");
        NeighbourGrpc ufoundedNeighbour = NeighbourGrpc.newBuilder()
                .setErrorMessage("There is no Graph whose Id is '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(), nw1.getNode().getId(), addedNeighbour.getId(),updatedNeighbour);

                assertEquals(response.getSuccess(),true);
                assertEquals(response.getNeighbour().getName(),"Node10");
    }

    @Test
    public void test6Neighbours() throws Exception {
        System.out.println("[DEBUG] test6Load starts");
        // 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
        List<NeighbourGrpc> node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
        TreeSet<NeighbourGrpc> pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
        pts.addAll(node_list);
        Iterator<NeighbourGrpc> neighbours = pts.iterator();

        while(neighbours.hasNext()){
            neighbours.next();
        }

        if(neighbours.hasNext()){
            assertEquals(neighbours.next(), addedNeighbour1.getNeighbour());
            assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
            assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
        }

        //deleteNeighbour
        boolean succ = client.deleteNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour1.getNeighbour().getId());
        assertEquals(succ, true);
        // run
        node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
        pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
        pts.addAll(node_list);
        neighbours = pts.iterator();

        while(neighbours.hasNext()){
            neighbours.next();
        }


        if(neighbours.hasNext()){
            assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
            assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
        }
    }
}


class NodeGrpcComparator implements Comparator<NodeGrpc> {
    public int compare(NodeGrpc n0, NodeGrpc n1) {
        return n0.getName().compareTo(n1.getName());
    }
}

class NeighbourGrpcComparator implements Comparator<NeighbourGrpc> {
    public int compare(NeighbourGrpc n0, NeighbourGrpc n1) {
        return n0.getName().compareTo(n1.getName());
    }
}

class GraphGrpcComparator implements Comparator<GraphGrpc> {
    public int compare(GraphGrpc n0, GraphGrpc n1) {
        if(n0.getId() == n1.getId())
            return 0;
        else if (n0.getId() > n1.getId())
            return 1;
        else return -1;
    }
}