--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class GbpNetconfTransactionTest {
+
+ private final DataBroker dataBroker = mock(DataBroker.class);
+ private final ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
+ private final ReadOnlyTransaction rTx = mock(ReadOnlyTransaction.class);
+ private final Node node = mock(Node.class);
+ @SuppressWarnings("unchecked")
+ private final InstanceIdentifier<Node> nodeIid = mock(InstanceIdentifier.class);
+ @SuppressWarnings("unchecked")
+ private final CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
+ @SuppressWarnings("unchecked")
+ private final CheckedFuture<Optional<Node>, ReadFailedException> futureNode = mock(CheckedFuture.class);
+ private final ConfigCommand command = mock(ConfigCommand.class);
+
+ @Test
+ public void writeConfigCommandExceptionTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new RuntimeException()).when(command).execute(rwTx);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertFalse(result);
+
+ }
+
+ @Test
+ public void writeConfigCommandReattemptTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new IllegalStateException()).when(command).execute(rwTx);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
+ verify(dataBroker, times(6)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void writeConfigCommandTest() throws Exception {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ when(rwTx.submit()).thenReturn(future);
+ doNothing().when(command).execute(rwTx);
+ when(future.get()).thenReturn(null);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertTrue(result);
+ }
+
+ @Test
+ public void writeDataExceptionTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new RuntimeException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void writeDataReattemptTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new IllegalStateException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
+ verify(dataBroker, times(6)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void writeDataTest() throws Exception {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ when(rwTx.submit()).thenReturn(future);
+ doNothing().when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
+ when(future.get()).thenReturn(null);
+
+ final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertTrue(result);
+ }
+
+ @Test
+ public void readDataExceptionTest() {
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+ doThrow(new RuntimeException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
+
+ final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
+ nodeIid, (byte)5);
+ verify(dataBroker, times(1)).newReadOnlyTransaction();
+ assertFalse(result.isPresent());
+ }
+
+ @Test
+ public void readDataReattemptTest() {
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+ doThrow(new IllegalStateException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
+
+ final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
+ nodeIid, (byte)5);
+ verify(dataBroker, times(6)).newReadOnlyTransaction();
+ assertFalse(result.isPresent());
+ }
+
+ @Test
+ public void readDataTest() throws Exception {
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+ when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
+ when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
+ .setKey(new NodeKey(new NodeId("node"))).build()));
+
+ final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
+ nodeIid, (byte)5);
+ verify(dataBroker, times(1)).newReadOnlyTransaction();
+ assertTrue(result.isPresent());
+ }
+
+ @Test
+ public void deleteConfigCommandExceptionTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new RuntimeException()).when(command).execute(rwTx);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void deleteConfigCommandReattemptTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new IllegalStateException()).when(command).execute(rwTx);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
+ verify(dataBroker, times(6)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void deleteConfigCommandTest() throws Exception {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ when(rwTx.submit()).thenReturn(future);
+ doNothing().when(command).execute(rwTx);
+ when(future.get()).thenReturn(null);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertTrue(result);
+ }
+
+ @Test
+ public void deleteDataExceptionTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new RuntimeException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void deleteDataReattemptTest() {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ doThrow(new IllegalStateException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
+ verify(dataBroker, times(6)).newReadWriteTransaction();
+ assertFalse(result);
+ }
+
+ @Test
+ public void deleteDataTest() throws Exception {
+ when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
+ when(rwTx.submit()).thenReturn(future);
+ doNothing().when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
+ when(future.get()).thenReturn(null);
+
+ final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
+ verify(dataBroker, times(1)).newReadWriteTransaction();
+ assertTrue(result);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import com.google.common.util.concurrent.SettableFuture;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+
+public class GbpVppNetconfConnectionProbeTest {
+
+ private final DataBroker dataBroker = mock(DataBroker.class);
+ @SuppressWarnings("unchecked")
+ private final DataTreeModification<Node> modification = (DataTreeModification<Node>) mock(DataTreeModification.class);
+ @SuppressWarnings("unchecked")
+ private final DataObjectModification<Node> rootNode = (DataObjectModification<Node>) mock(DataObjectModification.class);
+ private final NodeId NODE_ID = new NodeId("dummy-node");
+ private Collection<DataTreeModification<Node>> changes;
+ private SettableFuture<Boolean> future;
+ private GbpVppNetconfConnectionProbe probeSpy;
+
+ @Before
+ public void init() {
+ when(modification.getRootNode()).thenReturn(rootNode);
+ future = SettableFuture.create();
+ changes = new ArrayList<>();
+ changes.add(modification);
+ GbpVppNetconfConnectionProbe probeObject =
+ new GbpVppNetconfConnectionProbe(new NodeKey(NODE_ID), future, dataBroker);
+ probeSpy = spy(probeObject);
+ }
+
+ @Test
+ public void testNullNode() throws Exception {
+ when(rootNode.getDataAfter()).thenReturn(null);
+ probeSpy.onDataTreeChanged(changes);
+ future = probeSpy.getFutureStatus();
+ final Boolean result = future.get();
+ assertFalse(result);
+ verify(probeSpy, times(1)).unregister();
+ }
+
+ @Test
+ public void testNodeWithoutAugmentation() throws Exception {
+ when(rootNode.getDataAfter()).thenReturn(nodeWithoutAugmentation());
+ probeSpy.onDataTreeChanged(changes);
+ future = probeSpy.getFutureStatus();
+ final Boolean result = future.get();
+ assertFalse(result);
+ verify(probeSpy, times(1)).unregister();
+ }
+
+ @Test
+ public void testConnectingNode() throws Exception {
+ when(rootNode.getDataAfter()).thenReturn(connectingNode());
+ probeSpy.onDataTreeChanged(changes);
+ verify(probeSpy, times(0)).unregister();
+ }
+
+ @Test
+ public void testConnectedNode() throws Exception {
+ when(rootNode.getDataAfter()).thenReturn(connectedNode());
+ probeSpy.onDataTreeChanged(changes);
+ future = probeSpy.getFutureStatus();
+ final Boolean result = future.get();
+ assertTrue(result);
+ verify(probeSpy, times(1)).unregister();
+ }
+
+ @Test
+ public void testFailedNode() throws Exception {
+ when(rootNode.getDataAfter()).thenReturn(failedNode());
+ probeSpy.onDataTreeChanged(changes);
+ future = probeSpy.getFutureStatus();
+ final Boolean result = future.get();
+ assertFalse(result);
+ verify(probeSpy, times(1)).unregister();
+ }
+
+ private Node nodeWithoutAugmentation() {
+ final NodeBuilder nodeBuilder = new NodeBuilder();
+ nodeBuilder.setNodeId(NODE_ID);
+ return nodeBuilder.build();
+ }
+
+ private Node connectingNode() {
+ final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
+ netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.Connecting);
+ final NodeBuilder nodeBuilder = new NodeBuilder();
+ nodeBuilder.setNodeId(NODE_ID)
+ .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
+ return nodeBuilder.build();
+ }
+
+ private Node connectedNode() {
+ final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
+ netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.Connected);
+ final NodeBuilder nodeBuilder = new NodeBuilder();
+ nodeBuilder.setNodeId(NODE_ID)
+ .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
+ return nodeBuilder.build();
+ }
+
+ private Node failedNode() {
+ final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
+ netconfNodeBuilder.setConnectionStatus(NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect);
+ final NodeBuilder nodeBuilder = new NodeBuilder();
+ nodeBuilder.setNodeId(NODE_ID)
+ .addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
+ return nodeBuilder.build();
+ }
+}
\ No newline at end of file