UT coverage increased in vpp-renderer
[groupbasedpolicy.git] / renderers / vpp / src / test / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / GbpVppNetconfConnectionProbeTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.spy;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import com.google.common.util.concurrent.SettableFuture;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
34
35 public class GbpVppNetconfConnectionProbeTest {
36
37     private final DataBroker dataBroker = mock(DataBroker.class);
38     @SuppressWarnings("unchecked")
39     private final DataTreeModification<Node> modification = (DataTreeModification<Node>) mock(DataTreeModification.class);
40     @SuppressWarnings("unchecked")
41     private final DataObjectModification<Node> rootNode = (DataObjectModification<Node>) mock(DataObjectModification.class);
42     private final NodeId NODE_ID = new NodeId("dummy-node");
43     private Collection<DataTreeModification<Node>> changes;
44     private SettableFuture<Boolean> future;
45     private GbpVppNetconfConnectionProbe probeSpy;
46
47     @Before
48     public void init() {
49         when(modification.getRootNode()).thenReturn(rootNode);
50         future = SettableFuture.create();
51         changes = new ArrayList<>();
52         changes.add(modification);
53         GbpVppNetconfConnectionProbe probeObject =
54                 new GbpVppNetconfConnectionProbe(new NodeKey(NODE_ID), future, dataBroker);
55         probeSpy = spy(probeObject);
56     }
57
58     @Test
59     public void testNullNode() throws Exception {
60         when(rootNode.getDataAfter()).thenReturn(null);
61         probeSpy.onDataTreeChanged(changes);
62         future = probeSpy.getFutureStatus();
63         final Boolean result = future.get();
64         assertFalse(result);
65         verify(probeSpy, times(1)).unregister();
66     }
67
68     @Test
69     public void testNodeWithoutAugmentation() throws Exception {
70         when(rootNode.getDataAfter()).thenReturn(nodeWithoutAugmentation());
71         probeSpy.onDataTreeChanged(changes);
72         future = probeSpy.getFutureStatus();
73         final Boolean result = future.get();
74         assertFalse(result);
75         verify(probeSpy, times(1)).unregister();
76     }
77
78     @Test
79     public void testConnectingNode() throws Exception {
80         when(rootNode.getDataAfter()).thenReturn(connectingNode());
81         probeSpy.onDataTreeChanged(changes);
82         verify(probeSpy, times(0)).unregister();
83     }
84
85     @Test
86     public void testConnectedNode() throws Exception {
87         when(rootNode.getDataAfter()).thenReturn(connectedNode());
88         probeSpy.onDataTreeChanged(changes);
89         future = probeSpy.getFutureStatus();
90         final Boolean result = future.get();
91         assertTrue(result);
92         verify(probeSpy, times(1)).unregister();
93     }
94
95     @Test
96     public void testFailedNode() throws Exception {
97         when(rootNode.getDataAfter()).thenReturn(failedNode());
98         probeSpy.onDataTreeChanged(changes);
99         future = probeSpy.getFutureStatus();
100         final Boolean result = future.get();
101         assertFalse(result);
102         verify(probeSpy, times(1)).unregister();
103     }
104
105     private Node nodeWithoutAugmentation() {
106         final NodeBuilder nodeBuilder = new NodeBuilder();
107         nodeBuilder.setNodeId(NODE_ID);
108         return nodeBuilder.build();
109     }
110
111     private Node connectingNode() {
112         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
113         netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.Connecting);
114         final NodeBuilder nodeBuilder = new NodeBuilder();
115         nodeBuilder.setNodeId(NODE_ID)
116                 .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
117         return nodeBuilder.build();
118     }
119
120     private Node connectedNode() {
121         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
122         netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.Connected);
123         final NodeBuilder nodeBuilder = new NodeBuilder();
124         nodeBuilder.setNodeId(NODE_ID)
125                 .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
126         return nodeBuilder.build();
127     }
128
129     private Node failedNode() {
130         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
131         netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect);
132         final NodeBuilder nodeBuilder = new NodeBuilder();
133         nodeBuilder.setNodeId(NODE_ID)
134                 .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
135         return nodeBuilder.build();
136     }
137 }