Merge "Migrate netconf to mockito ArgumentMatchers"
[netconf.git] / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / impl / NetconfTopologyImplTest.java
index caff132e58dce945f7448e28e72fc2793264e7a3..e5d79fb3b06bfbaee8e4bcdbb786f0c182adfdae 100644 (file)
@@ -5,18 +5,18 @@
  * 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.netconf.topology.impl;
 
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
 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 static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
 
-import akka.actor.ActorContext;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -26,10 +26,12 @@ import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.ImmediateEventExecutor;
 import io.netty.util.concurrent.SucceededFuture;
 import java.util.Collection;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.opendaylight.aaa.encrypt.AAAEncryptionService;
 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
 import org.opendaylight.controller.config.threadpool.ThreadPool;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -37,22 +39,21 @@ import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.core.api.Broker;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.netconf.client.NetconfClientDispatcher;
+import org.opendaylight.netconf.client.NetconfClientSessionListener;
+import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
-import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
-import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
-import org.opendaylight.netconf.topology.SchemaRepositoryProvider;
-import org.opendaylight.netconf.topology.util.TopologyUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
+import org.opendaylight.netconf.topology.api.SchemaRepositoryProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 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.netconf.node.connection.parameters.Protocol.Name;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.ProtocolBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
@@ -73,15 +74,9 @@ public class NetconfTopologyImplTest {
     private static final NodeId NODE_ID = new NodeId("testing-node");
     private static final String TOPOLOGY_ID = "testing-topology";
 
-    @Mock
-    private Broker mockedDataBroker;
-
     @Mock
     private NetconfClientDispatcher mockedClientDispatcher;
 
-    @Mock
-    private BindingAwareBroker mockedBindingAwareBroker;
-
     @Mock
     private EventExecutor mockedEventExecutor;
 
@@ -94,6 +89,14 @@ public class NetconfTopologyImplTest {
     @Mock
     private SchemaRepositoryProvider mockedSchemaRepositoryProvider;
 
+    @Mock
+    private DataBroker dataBroker;
+
+    @Mock
+    private DOMMountPointService mountPointService;
+
+    @Mock
+    private AAAEncryptionService encryptionService;
 
     private TestingNetconfTopologyImpl topology;
     private TestingNetconfTopologyImpl spyTopology;
@@ -102,71 +105,60 @@ public class NetconfTopologyImplTest {
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
-        when(mockedSchemaRepositoryProvider.getSharedSchemaRepository()).thenReturn(new SharedSchemaRepository("testingSharedSchemaRepo"));
+        when(mockedSchemaRepositoryProvider.getSharedSchemaRepository())
+                .thenReturn(new SharedSchemaRepository("testingSharedSchemaRepo"));
         when(mockedProcessingExecutor.getExecutor()).thenReturn(MoreExecutors.newDirectExecutorService());
-        Future future = new SucceededFuture(ImmediateEventExecutor.INSTANCE, new NetconfDeviceCapabilities());
-        when(mockedClientDispatcher.createReconnectingClient(any(NetconfReconnectingClientConfiguration.class))).thenReturn(future);
+        final Future future = new SucceededFuture(ImmediateEventExecutor.INSTANCE, new NetconfDeviceCapabilities());
+        when(mockedClientDispatcher.createReconnectingClient(any(NetconfReconnectingClientConfiguration.class)))
+                .thenReturn(future);
 
-        topology = new TestingNetconfTopologyImpl(TOPOLOGY_ID, mockedClientDispatcher, mockedBindingAwareBroker,
-                mockedDataBroker, mockedEventExecutor, mockedKeepaliveExecutor, mockedProcessingExecutor, mockedSchemaRepositoryProvider);
+        topology = new TestingNetconfTopologyImpl(TOPOLOGY_ID, mockedClientDispatcher,
+                mockedEventExecutor, mockedKeepaliveExecutor, mockedProcessingExecutor, mockedSchemaRepositoryProvider,
+                dataBroker, mountPointService, encryptionService);
 
         spyTopology = spy(topology);
     }
 
-    @Test(expected = UnsupportedOperationException.class)
-    public void testRegisterMountPointNotSupported() {
-        ActorContext context = mock(ActorContext.class);
-        topology.registerMountPoint(context, NODE_ID);
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testUnregisterMountPointNotSupported() {
-        topology.unregisterMountPoint(NODE_ID);
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testRegisterConnectionStatusListener() {
-        RemoteDeviceHandler<NetconfSessionPreferences> listener = mock(RemoteDeviceHandler.class);
-        topology.registerConnectionStatusListener(NODE_ID, listener);
-    }
-
     @Test
-    public void testOnSessionInitiated() {
-        BindingAwareBroker.ProviderContext session = mock(BindingAwareBroker.ProviderContext.class);
-        DataBroker dataBroker = mock(DataBroker.class);
-        when(session.getSALService(DataBroker.class)).thenReturn(dataBroker);
-        WriteTransaction wtx = mock(WriteTransaction.class);
+    public void testInit() {
+        final WriteTransaction wtx = mock(WriteTransaction.class);
         when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
-        doNothing().when(wtx).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
-        when(wtx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
-        topology.onSessionInitiated(session);
+        doNothing().when(wtx)
+                .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
+        doReturn(emptyFluentFuture()).when(wtx).commit();
+        topology.init();
 
         //verify initialization of topology
-        final InstanceIdentifier<NetworkTopology> networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build();
+        final InstanceIdentifier<NetworkTopology> networkTopologyId =
+                InstanceIdentifier.builder(NetworkTopology.class).build();
         final Topology topo = new TopologyBuilder().setTopologyId(new TopologyId(TOPOLOGY_ID)).build();
         final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
         verify(wtx).merge(LogicalDatastoreType.CONFIGURATION, networkTopologyId, networkTopology);
         verify(wtx).merge(LogicalDatastoreType.OPERATIONAL, networkTopologyId, networkTopology);
-        verify(wtx).merge(LogicalDatastoreType.CONFIGURATION, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
-        verify(wtx).merge(LogicalDatastoreType.OPERATIONAL, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
+        verify(wtx).merge(LogicalDatastoreType.CONFIGURATION,
+                networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
+        verify(wtx).merge(LogicalDatastoreType.OPERATIONAL,
+                networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
     }
 
     @Test
     public void testOnDataTreeChange() {
 
-        DataObjectModification<Node> newNode = mock(DataObjectModification.class);
+        final DataObjectModification<Node> newNode = mock(DataObjectModification.class);
         when(newNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
 
         InstanceIdentifier.PathArgument pa = null;
 
-        for (InstanceIdentifier.PathArgument p : TopologyUtil.createTopologyListPath(TOPOLOGY_ID).child(Node.class, new NodeKey(NODE_ID)).getPathArguments()) {
+        for (final InstanceIdentifier.PathArgument p
+                : TopologyUtil.createTopologyListPath(TOPOLOGY_ID)
+                    .child(Node.class, new NodeKey(NODE_ID)).getPathArguments()) {
             pa = p;
         }
 
         when(newNode.getIdentifier()).thenReturn(pa);
 
 
-        NetconfNode testingNode = new NetconfNodeBuilder()
+        final NetconfNode testingNode = new NetconfNodeBuilder()
                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
                 .setPort(new PortNumber(9999))
                 .setReconnectOnChangedSchema(true)
@@ -174,17 +166,17 @@ public class NetconfTopologyImplTest {
                 .setBetweenAttemptsTimeoutMillis(100)
                 .setKeepaliveDelay(1000L)
                 .setTcpOnly(true)
-                .setCredentials(new LoginPasswordBuilder().setUsername("testuser").setPassword("testpassword").build())
+                .setCredentials(new LoginPasswordBuilder()
+                        .setUsername("testuser").setPassword("testpassword").build())
                 .build();
 
-        NodeBuilder nn = new NodeBuilder().addAugmentation(NetconfNode.class, testingNode);
+        final NodeBuilder nn = new NodeBuilder().addAugmentation(NetconfNode.class, testingNode);
 
         when(newNode.getDataAfter()).thenReturn(nn.build());
 
 
-
-        Collection<DataTreeModification<Node>> changes = Sets.newHashSet();
-        DataTreeModification<Node> ch = mock(DataTreeModification.class);
+        final Collection<DataTreeModification<Node>> changes = Sets.newHashSet();
+        final DataTreeModification<Node> ch = mock(DataTreeModification.class);
         when(ch.getRootNode()).thenReturn(newNode);
         changes.add(ch);
         spyTopology.onDataTreeChanged(changes);
@@ -204,21 +196,106 @@ public class NetconfTopologyImplTest {
 
     }
 
+    @Test
+    public void testGetClientConfig() {
+        final NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
+
+        final NetconfNode testingNode = new NetconfNodeBuilder()
+                .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
+                .setPort(new PortNumber(9999))
+                .setReconnectOnChangedSchema(true)
+                .setDefaultRequestTimeoutMillis(1000L)
+                .setBetweenAttemptsTimeoutMillis(100)
+                .setKeepaliveDelay(1000L)
+                .setTcpOnly(true)
+                .setCredentials(new LoginPasswordBuilder()
+                        .setUsername("testuser").setPassword("testpassword").build())
+                .build();
+        final NetconfReconnectingClientConfiguration configuration =
+                spyTopology.getClientConfig(sessionListener, testingNode);
+        Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.TCP, configuration.getProtocol());
+        Assert.assertNotNull(configuration.getAuthHandler());
+        Assert.assertNull(configuration.getSslHandlerFactory());
+
+
+        final NetconfNode testingNode2 = new NetconfNodeBuilder()
+                .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
+                .setPort(new PortNumber(9999))
+                .setReconnectOnChangedSchema(true)
+                .setDefaultRequestTimeoutMillis(1000L)
+                .setBetweenAttemptsTimeoutMillis(100)
+                .setKeepaliveDelay(1000L)
+                .setTcpOnly(false)
+                .setCredentials(new LoginPasswordBuilder()
+                        .setUsername("testuser").setPassword("testpassword").build())
+                .build();
+        final NetconfReconnectingClientConfiguration configuration2 =
+                spyTopology.getClientConfig(sessionListener, testingNode2);
+        Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.SSH, configuration2.getProtocol());
+        Assert.assertNotNull(configuration2.getAuthHandler());
+        Assert.assertNull(configuration2.getSslHandlerFactory());
+
+
+        final NetconfNode testingNode3 = new NetconfNodeBuilder()
+                .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
+                .setPort(new PortNumber(9999))
+                .setReconnectOnChangedSchema(true)
+                .setDefaultRequestTimeoutMillis(1000L)
+                .setBetweenAttemptsTimeoutMillis(100)
+                .setKeepaliveDelay(1000L)
+                .setTcpOnly(false)
+                .setProtocol(new ProtocolBuilder().setName(Name.SSH).build())
+                .setCredentials(new LoginPasswordBuilder()
+                        .setUsername("testuser").setPassword("testpassword").build())
+                .build();
+        final NetconfReconnectingClientConfiguration configuration3 =
+                spyTopology.getClientConfig(sessionListener, testingNode3);
+        Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.SSH, configuration3.getProtocol());
+        Assert.assertNotNull(configuration3.getAuthHandler());
+        Assert.assertNull(configuration3.getSslHandlerFactory());
+
+
+        final NetconfNode testingNode4 = new NetconfNodeBuilder()
+                .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
+                .setPort(new PortNumber(9999))
+                .setReconnectOnChangedSchema(true)
+                .setDefaultRequestTimeoutMillis(1000L)
+                .setBetweenAttemptsTimeoutMillis(100)
+                .setKeepaliveDelay(1000L)
+                .setTcpOnly(false)
+                .setProtocol(new ProtocolBuilder().setName(Name.TLS).build())
+                .setCredentials(new LoginPasswordBuilder()
+                        .setUsername("testuser").setPassword("testpassword").build())
+                .build();
+        final NetconfReconnectingClientConfiguration configuration4 =
+                spyTopology.getClientConfig(sessionListener, testingNode4);
+        Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.TLS, configuration4.getProtocol());
+        Assert.assertNull(configuration4.getAuthHandler());
+        Assert.assertNotNull(configuration4.getSslHandlerFactory());
+    }
+
     public static class TestingNetconfTopologyImpl extends NetconfTopologyImpl {
 
-        public TestingNetconfTopologyImpl(String topologyId, NetconfClientDispatcher clientDispatcher, BindingAwareBroker bindingAwareBroker, Broker domBroker, EventExecutor eventExecutor, ScheduledThreadPool keepaliveExecutor, ThreadPool processingExecutor, SchemaRepositoryProvider schemaRepositoryProvider) {
-            super(topologyId, clientDispatcher, bindingAwareBroker, domBroker, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider);
+        public TestingNetconfTopologyImpl(final String topologyId, final NetconfClientDispatcher clientDispatcher,
+                                          final EventExecutor eventExecutor,
+                                          final ScheduledThreadPool keepaliveExecutor,
+                                          final ThreadPool processingExecutor,
+                                          final SchemaRepositoryProvider schemaRepositoryProvider,
+                                          final DataBroker dataBroker, final DOMMountPointService mountPointService,
+                                          final AAAEncryptionService encryptionService) {
+            super(topologyId, clientDispatcher, eventExecutor, keepaliveExecutor,
+                    processingExecutor, schemaRepositoryProvider, dataBroker,
+                  mountPointService, encryptionService);
         }
 
         @Override
-        public ListenableFuture<NetconfDeviceCapabilities> connectNode(NodeId nodeId, Node configNode) {
+        public ListenableFuture<NetconfDeviceCapabilities> connectNode(final NodeId nodeId, final Node configNode) {
             return Futures.immediateFuture(new NetconfDeviceCapabilities());
         }
 
         @Override
-        public ListenableFuture<Void> disconnectNode(NodeId nodeId) {
+        public ListenableFuture<Void> disconnectNode(final NodeId nodeId) {
             return Futures.immediateFuture(null);
         }
     }
-
 }