From 34d5cfb9d7f8b5546af613b3a2fbcbe0f771a3fe Mon Sep 17 00:00:00 2001 From: Matej Perina Date: Tue, 19 Jul 2016 12:45:58 +0200 Subject: [PATCH] unit tests for NetconfTransactionCreator Change-Id: I0a5bb351afb4422ca875853c9aad9ebda2fa3edc Signed-off-by: Matej Perina --- .../writer/NetconfTransactionCreator.java | 17 ++- .../writer/NetconfTransactionCreatorTest.java | 110 ++++++++++++++++++ 2 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreatorTest.java diff --git a/renderers/ios-xe/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreator.java b/renderers/ios-xe/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreator.java index 7b7b8b2e7..56bd6886d 100644 --- a/renderers/ios-xe/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreator.java +++ b/renderers/ios-xe/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreator.java @@ -8,6 +8,8 @@ package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer; +import java.util.Optional; + 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; @@ -16,7 +18,7 @@ import org.opendaylight.netconf.api.NetconfDocumentedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Optional; +import com.google.common.annotations.VisibleForTesting; /** * Purpose: safely create transaction @@ -25,7 +27,7 @@ import java.util.Optional; public class NetconfTransactionCreator { private final static Logger LOG = LoggerFactory.getLogger(NetconfTransactionCreator.class); - private static final long TIMEOUT = 5000L; + private static long timeout = 5000L; public static Optional netconfReadOnlyTransaction(DataBroker mountpoint) { int attempt = 0; @@ -39,7 +41,7 @@ public class NetconfTransactionCreator { attempt++; LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt); try { - Thread.sleep(TIMEOUT); + Thread.sleep(timeout); } catch (InterruptedException i) { LOG.error("Thread interrupted while waiting ... {} ", i); } @@ -65,7 +67,7 @@ public class NetconfTransactionCreator { attempt++; LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt); try { - Thread.sleep(TIMEOUT); + Thread.sleep(timeout); } catch (InterruptedException i) { LOG.error("Thread interrupted while waiting ... {} ", i); } @@ -91,7 +93,7 @@ public class NetconfTransactionCreator { attempt++; LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt); try { - Thread.sleep(TIMEOUT); + Thread.sleep(timeout); } catch (InterruptedException i) { LOG.error("Thread interrupted while waiting ... {} ", i); } @@ -104,4 +106,9 @@ public class NetconfTransactionCreator { LOG.error("Maximum number of attempts reached"); return Optional.empty(); } + + @VisibleForTesting + public static void setTimeout (long newTimeout) { + timeout = newTimeout; + } } diff --git a/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreatorTest.java b/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreatorTest.java new file mode 100644 index 000000000..c5a345c22 --- /dev/null +++ b/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreatorTest.java @@ -0,0 +1,110 @@ +/* + * 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.ios_xe_provider.impl.writer; + +import static org.junit.Assert.assertEquals; +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 java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +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.binding.api.WriteTransaction; +import org.opendaylight.netconf.api.NetconfDocumentedException; + +public class NetconfTransactionCreatorTest { + + @Mock + private DataBroker dataBroker; + + @Before + public void init() { + dataBroker = mock(DataBroker.class); + NetconfTransactionCreator.setTimeout(10L); + } + + @Test + public void testNetconfReadOnlyTransaction_success() { + ReadOnlyTransaction rtx = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx); + Optional newRtx = NetconfTransactionCreator.netconfReadOnlyTransaction(dataBroker); + assertEquals(rtx, newRtx.get()); + } + + @Test + public void testNetconfReadOnlyTransaction_NoNetconfException() { + when(dataBroker.newReadOnlyTransaction()).thenThrow(RuntimeException.class); + Optional newRtx = NetconfTransactionCreator.netconfReadOnlyTransaction(dataBroker); + assertEquals(false, newRtx.isPresent()); + verify(dataBroker, times(1)).newReadOnlyTransaction(); + } + + @Test + public void testNetconfReadOnlyTransaction_NetconfException() { + when(dataBroker.newReadOnlyTransaction()).thenThrow(new RuntimeException(new NetconfDocumentedException(""))); + Optional newRtx = NetconfTransactionCreator.netconfReadOnlyTransaction(dataBroker); + assertEquals(false, newRtx.isPresent()); + verify(dataBroker, times(6)).newReadOnlyTransaction(); + } + + @Test + public void testNetconfWriteOnlyTransaction_success() { + WriteTransaction wtx = mock(WriteTransaction.class); + when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx); + Optional newWtx = NetconfTransactionCreator.netconfWriteOnlyTransaction(dataBroker); + assertEquals(wtx, newWtx.get()); + } + + @Test + public void testNetconfWriteOnlyTransaction_NoNetconfException() { + when(dataBroker.newWriteOnlyTransaction()).thenThrow(RuntimeException.class); + Optional newWtx = NetconfTransactionCreator.netconfWriteOnlyTransaction(dataBroker); + assertEquals(false, newWtx.isPresent()); + verify(dataBroker, times(1)).newWriteOnlyTransaction(); + } + + @Test + public void testNetconfWriteOnlyTransaction_NetconfException() { + when(dataBroker.newWriteOnlyTransaction()).thenThrow(new RuntimeException(new NetconfDocumentedException(""))); + Optional newWtx = NetconfTransactionCreator.netconfWriteOnlyTransaction(dataBroker); + assertEquals(false, newWtx.isPresent()); + verify(dataBroker, times(6)).newWriteOnlyTransaction(); + } + + @Test + public void testNetconfReadWriteTransaction_success() { + ReadWriteTransaction wtx = mock(ReadWriteTransaction.class); + when(dataBroker.newReadWriteTransaction()).thenReturn(wtx); + Optional newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); + assertEquals(wtx, newRtx.get()); + } + + @Test + public void testNetconfReadWriteTransaction_NoNetconfException() { + when(dataBroker.newReadWriteTransaction()).thenThrow(RuntimeException.class); + Optional newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); + assertEquals(false, newRtx.isPresent()); + verify(dataBroker, times(1)).newReadWriteTransaction(); + } + + @Test + public void testNetconfReadWriteTransaction_NetconfException() { + when(dataBroker.newReadWriteTransaction()).thenThrow(new RuntimeException(new NetconfDocumentedException(""))); + Optional newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); + assertEquals(false, newRtx.isPresent()); + verify(dataBroker, times(6)).newReadWriteTransaction(); + } +} -- 2.36.6