UT coverage increased in vpp-renderer 79/48579/7
authorVladimir Lavor <vlavor@cisco.com>
Tue, 22 Nov 2016 14:46:13 +0000 (15:46 +0100)
committerVladimir Lavor <vlavor@cisco.com>
Thu, 15 Dec 2016 11:56:18 +0000 (12:56 +0100)
* GbpVppNetconfConnectionProbeTest.class
* GbpNetconfTransaction

Change-Id: I7615d2eaeb6f3a64ab42aaf534807ddcffe74ded
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransaction.java
renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbe.java
renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransactionTest.java [new file with mode: 0644]
renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbeTest.java [new file with mode: 0644]

index 407e478376c9dbc8907a647b84014b6e499b853f..f0e820daaf1810f95b5b0dc955b1cbfdaff113a1 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
 
-import java.util.concurrent.ExecutionException;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
@@ -57,7 +56,7 @@ public class GbpNetconfTransaction {
                 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
                 return false;
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (Exception e) {
             LOG.warn("Exception while writing data ...", e.getMessage());
             return false;
         }
@@ -95,7 +94,7 @@ public class GbpNetconfTransaction {
                 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
                 return false;
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (Exception e) {
             LOG.warn("Exception while writing data ...", e.getMessage());
             return false;
         }
@@ -136,7 +135,7 @@ public class GbpNetconfTransaction {
                 LOG.warn("Netconf read-transaction failed. Maximal number of attempts reached", e.getMessage());
                 return Optional.absent();
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (Exception e) {
             LOG.warn("Exception while reading data ...", e.getMessage());
             return Optional.absent();
         }
@@ -170,7 +169,7 @@ public class GbpNetconfTransaction {
                 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
                 return false;
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (Exception e) {
             LOG.warn("Exception while removing data ...", e.getMessage());
             return false;
         }
@@ -206,7 +205,7 @@ public class GbpNetconfTransaction {
                 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
                 return false;
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (Exception e) {
             LOG.warn("Exception while removing data ...", e.getMessage());
             return false;
         }
index b4d078f1bb5fa889be05ca1b22579ecd7485acd6..255a624c305864fbaa595bd6aaf7df5365a2239b 100644 (file)
@@ -13,6 +13,7 @@ import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topolog
 
 import javax.annotation.Nonnull;
 import java.util.Collection;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.SettableFuture;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
@@ -64,10 +65,12 @@ class GbpVppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<No
         changes.forEach(modification -> {
             final DataObjectModification<Node> rootNode = modification.getRootNode();
             final Node node = rootNode.getDataAfter();
-            final NetconfNode netconfNode = getNodeAugmentation(node);
-            if (node == null || node.getNodeId() == null) {
+            if (node == null) {
+                futureStatus.set(false);
+                unregister();
                 return;
             }
+            final NetconfNode netconfNode = getNodeAugmentation(node);
             final NodeId nodeId = node.getNodeId();
             if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
                 LOG.warn("Node {} does not contain netconf augmentation", nodeId);
@@ -99,11 +102,15 @@ class GbpVppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<No
         return netconfNode;
     }
 
-    private void unregister() {
+    void unregister() {
         LOG.debug("Listener for path {} unregistered", path.getRootIdentifier());
         if (registeredListener != null) {
             registeredListener.close();
         }
     }
 
+    @VisibleForTesting
+    SettableFuture<Boolean> getFutureStatus() {
+        return futureStatus;
+    }
 }
diff --git a/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransactionTest.java b/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransactionTest.java
new file mode 100644 (file)
index 0000000..29e33ca
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ * 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
diff --git a/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbeTest.java b/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbeTest.java
new file mode 100644 (file)
index 0000000..a57f4d5
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * 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