2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
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;
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;
35 public class GbpVppNetconfConnectionProbeTest {
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;
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);
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();
65 verify(probeSpy, times(1)).unregister();
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();
75 verify(probeSpy, times(1)).unregister();
79 public void testConnectingNode() throws Exception {
80 when(rootNode.getDataAfter()).thenReturn(connectingNode());
81 probeSpy.onDataTreeChanged(changes);
82 verify(probeSpy, times(0)).unregister();
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();
92 verify(probeSpy, times(1)).unregister();
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();
102 verify(probeSpy, times(1)).unregister();
105 private Node nodeWithoutAugmentation() {
106 final NodeBuilder nodeBuilder = new NodeBuilder();
107 nodeBuilder.setNodeId(NODE_ID);
108 return nodeBuilder.build();
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();
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();
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();