Use base64 encoding for netconf device passwords
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / NetconfDeviceTopologyAdapterTest.java
index c86bb3c8e62b26a935eb2766b2b5a746133d9f33..2b00def16726531616fd71d727f4d6247b634ab9 100644 (file)
@@ -7,13 +7,19 @@
  */
 package org.opendaylight.netconf.topology.spi;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
+import com.google.common.util.concurrent.Futures;
 import java.net.InetSocketAddress;
+import java.util.concurrent.ExecutionException;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -27,8 +33,10 @@ import org.opendaylight.mdsal.binding.api.TransactionChainListener;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
-import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
+import org.opendaylight.netconf.client.mdsal.NetconfDeviceCapabilities;
+import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
 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;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
@@ -36,6 +44,8 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 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;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Empty;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class NetconfDeviceTopologyAdapterTest {
@@ -59,7 +69,8 @@ public class NetconfDeviceTopologyAdapterTest {
     public void before() {
         doReturn(mockTx).when(mockChain).newWriteOnlyTransaction();
         // FIXME: exact match
-        doNothing().when(mockTx).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
+        doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
+            any(Node.class));
         doReturn("test transaction").when(mockTx).getIdentifier();
         doReturn(CommitInfo.emptyFluentFuture()).when(mockTx).commit();
 
@@ -69,12 +80,18 @@ public class NetconfDeviceTopologyAdapterTest {
 
     @Test
     public void replaceChainIfFailed() {
+        doNothing().when(mockChain).close();
+        doReturn("mockChain").when(mockChain).toString();
         adapter.onTransactionChainFailed(mockChain, mockTx, new Exception("chain failed"));
         verify(mockBroker, times(2)).createMergingTransactionChain(any());
     }
 
     @Test
     public void testFailedDevice() {
+        // FIXME: exact match
+        doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
+            any(NetconfNode.class));
+
         adapter.setDeviceAsFailed(null);
 
         verify(mockChain, times(2)).newWriteOnlyTransaction();
@@ -85,7 +102,10 @@ public class NetconfDeviceTopologyAdapterTest {
 
     @Test
     public void testDeviceUpdate() throws Exception {
-        adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty());
+        // FIXME: exact match
+        doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
+            any(NetconfNode.class));
+        adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty(), new SessionIdType(Uint32.ONE));
 
         verify(mockChain, times(2)).newWriteOnlyTransaction();
         verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
@@ -94,12 +114,49 @@ public class NetconfDeviceTopologyAdapterTest {
 
     @Test
     public void testRemoveDeviceConfiguration() throws Exception {
+        // FIXME: exact match
+        doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
+        doNothing().when(mockChain).close();
+
+        final var future = adapter.shutdown();
+        verify(mockChain, times(2)).newWriteOnlyTransaction();
+        verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
+            TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
+        verify(mockTx, times(2)).commit();
+        verify(mockChain).close();
+
+        assertFalse(future.isDone());
+
+        // Idempotent
+        assertSame(future, adapter.shutdown());
+
+        // future completes
         listeners.getValue().onTransactionChainSuccessful(mockChain);
-        adapter.close();
+        assertSame(Empty.value(), Futures.getDone(future));
+    }
+
+    @Test
+    public void testShutdownCompletion() throws Exception {
+        // FIXME: exact match
+        doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
+        doNothing().when(mockChain).close();
 
+        final var future = adapter.shutdown();
         verify(mockChain, times(2)).newWriteOnlyTransaction();
         verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
             TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
         verify(mockTx, times(2)).commit();
+        verify(mockChain).close();
+
+        assertFalse(future.isDone());
+
+        // Idempotent
+        assertSame(future, adapter.shutdown());
+
+        // future completes
+        final var cause = new Throwable();
+        listeners.getValue().onTransactionChainFailed(mockChain, mockTx, cause);
+        final var ex = assertThrows(ExecutionException.class, () -> Futures.getDone(future));
+        assertSame(cause, ex.getCause());
     }
 }