From d86dd59e06109eb52e1e0d23a0909729db2f5255 Mon Sep 17 00:00:00 2001 From: Vladimir Lavor Date: Tue, 22 Nov 2016 15:46:13 +0100 Subject: [PATCH] UT coverage increased in vpp-renderer * GbpVppNetconfConnectionProbeTest.class * GbpNetconfTransaction Change-Id: I7615d2eaeb6f3a64ab42aaf534807ddcffe74ded Signed-off-by: Vladimir Lavor --- .../vpp/util/GbpNetconfTransaction.java | 11 +- .../util/GbpVppNetconfConnectionProbe.java | 13 +- .../vpp/util/GbpNetconfTransactionTest.java | 213 ++++++++++++++++++ .../GbpVppNetconfConnectionProbeTest.java | 137 +++++++++++ 4 files changed, 365 insertions(+), 9 deletions(-) create mode 100644 renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransactionTest.java create mode 100644 renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbeTest.java diff --git a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransaction.java b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransaction.java index 407e47837..f0e820daa 100644 --- a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransaction.java +++ b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransaction.java @@ -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; } diff --git a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbe.java b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbe.java index b4d078f1b..255a624c3 100644 --- a/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbe.java +++ b/renderers/vpp/src/main/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbe.java @@ -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 { final DataObjectModification 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 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 index 000000000..29e33ca43 --- /dev/null +++ b/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpNetconfTransactionTest.java @@ -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 nodeIid = mock(InstanceIdentifier.class); + @SuppressWarnings("unchecked") + private final CheckedFuture future = mock(CheckedFuture.class); + @SuppressWarnings("unchecked") + private final CheckedFuture, 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 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 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 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 index 000000000..a57f4d509 --- /dev/null +++ b/renderers/vpp/src/test/java/org/opendaylight/groupbasedpolicy/renderer/vpp/util/GbpVppNetconfConnectionProbeTest.java @@ -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 modification = (DataTreeModification) mock(DataTreeModification.class); + @SuppressWarnings("unchecked") + private final DataObjectModification rootNode = (DataObjectModification) mock(DataObjectModification.class); + private final NodeId NODE_ID = new NodeId("dummy-node"); + private Collection> changes; + private SettableFuture 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 -- 2.36.6