unit tests for NetconfTransactionCreator 45/41145/5
authorMatej Perina <matej.perina@pantheon.sk>
Tue, 19 Jul 2016 10:45:58 +0000 (12:45 +0200)
committerMatej Perina <matej.perina@pantheon.sk>
Tue, 19 Jul 2016 11:24:12 +0000 (11:24 +0000)
Change-Id: I0a5bb351afb4422ca875853c9aad9ebda2fa3edc
Signed-off-by: Matej Perina <matej.perina@pantheon.sk>
renderers/ios-xe/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreator.java
renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/NetconfTransactionCreatorTest.java [new file with mode: 0644]

index 7b7b8b2e7a28a56c1ef3209358016bc0c1b96ca1..56bd6886d94a2ca31d0fbc19ac23403967dd089c 100644 (file)
@@ -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<ReadOnlyTransaction> 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 (file)
index 0000000..c5a345c
--- /dev/null
@@ -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<ReadOnlyTransaction> newRtx = NetconfTransactionCreator.netconfReadOnlyTransaction(dataBroker);
+        assertEquals(rtx, newRtx.get());
+    }
+
+    @Test
+    public void testNetconfReadOnlyTransaction_NoNetconfException() {
+        when(dataBroker.newReadOnlyTransaction()).thenThrow(RuntimeException.class);
+        Optional<ReadOnlyTransaction> 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<ReadOnlyTransaction> 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<WriteTransaction> newWtx = NetconfTransactionCreator.netconfWriteOnlyTransaction(dataBroker);
+        assertEquals(wtx, newWtx.get());
+    }
+
+    @Test
+    public void testNetconfWriteOnlyTransaction_NoNetconfException() {
+        when(dataBroker.newWriteOnlyTransaction()).thenThrow(RuntimeException.class);
+        Optional<WriteTransaction> 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<WriteTransaction> 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<ReadWriteTransaction> newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker);
+        assertEquals(wtx, newRtx.get());
+    }
+
+    @Test
+    public void testNetconfReadWriteTransaction_NoNetconfException() {
+        when(dataBroker.newReadWriteTransaction()).thenThrow(RuntimeException.class);
+        Optional<ReadWriteTransaction> 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<ReadWriteTransaction> newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker);
+        assertEquals(false, newRtx.isPresent());
+        verify(dataBroker, times(6)).newReadWriteTransaction();
+    }
+}