X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-topology-singleton%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Ftopology%2Fsingleton%2Fimpl%2FNetconfTopologyManagerTest.java;h=b3d4be1cde2c7f9b1931acdf42ef80f541944cde;hb=d682d29c57e64070078813dd1e70883a4ba258ee;hp=ffc52a5f4663184c2d9f01e575f378a9af9b4ba2;hpb=e1bdc7d1eff9c117a0e3c4559f5e8a0595f53a3f;p=netconf.git diff --git a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManagerTest.java b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManagerTest.java index ffc52a5f46..b3d4be1cde 100644 --- a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManagerTest.java +++ b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManagerTest.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -31,6 +32,7 @@ import javax.annotation.Nonnull; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.opendaylight.aaa.encrypt.AAAEncryptionService; import org.opendaylight.controller.cluster.ActorSystemProvider; import org.opendaylight.controller.config.threadpool.ScheduledThreadPool; import org.opendaylight.controller.config.threadpool.ThreadPool; @@ -83,25 +85,93 @@ public class NetconfTopologyManagerTest { final EventExecutor eventExecutor = mock(EventExecutor.class); final NetconfClientDispatcher clientDispatcher = mock(NetconfClientDispatcher.class); final DOMMountPointService mountPointService = mock(DOMMountPointService.class); + final AAAEncryptionService encryptionService = mock(AAAEncryptionService.class); final Config config = new ConfigBuilder().setWriteTransactionIdleTimeout(0).build(); netconfTopologyManager = new NetconfTopologyManager(dataBroker, rpcProviderRegistry, clusterSingletonServiceProvider, keepaliveExecutor, processingExecutor, - actorSystemProvider, eventExecutor, clientDispatcher, topologyId, config, mountPointService); + actorSystemProvider, eventExecutor, clientDispatcher, topologyId, config, + mountPointService, encryptionService); } @Test public void testWriteConfiguration() throws Exception { + writeConfiguration(false); + } - final ClusterSingletonServiceRegistration clusterRegistration = mock(ClusterSingletonServiceRegistration.class); + @Test + public void testWriteConfigurationFail() throws Exception { + writeConfiguration(true); + } + + @Test + public void testRegisterDataTreeChangeListener() { + + final WriteTransaction wtx = mock(WriteTransaction.class); + + doReturn(wtx).when(dataBroker).newWriteOnlyTransaction(); + doNothing().when(wtx).merge(any(), any(), any()); + doReturn(Futures.immediateCheckedFuture(null)).when(wtx).submit(); + + netconfTopologyManager.init(); + + // verify if listener is called with right parameters = registered on right path + + verify(dataBroker, times(1)).registerDataTreeChangeListener( + new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils + .createTopologyListPath(topologyId).child(Node.class)), netconfTopologyManager); + + } + + @Test + public void testClose() throws Exception { final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts"); fieldContexts.setAccessible(true); + @SuppressWarnings("unchecked") final Map, NetconfTopologyContext> contexts = + (Map, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager); + + final Field fieldClusterRegistrations = + NetconfTopologyManager.class.getDeclaredField("clusterRegistrations"); + fieldClusterRegistrations.setAccessible(true); @SuppressWarnings("unchecked") - final Map, NetconfTopologyContext> contexts = + final Map, ClusterSingletonServiceRegistration> clusterRegistrations = + (Map, ClusterSingletonServiceRegistration>) + fieldClusterRegistrations.get(netconfTopologyManager); + + final InstanceIdentifier instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath( + new NodeKey(new NodeId("node-id-1")), "topology-1"); + + + final NetconfTopologyContext context = mock(NetconfTopologyContext.class); + final ClusterSingletonServiceRegistration clusterRegistration = + mock(ClusterSingletonServiceRegistration.class); + contexts.put(instanceIdentifier, context); + clusterRegistrations.put(instanceIdentifier, clusterRegistration); + + doNothing().when(context).closeFinal(); + doNothing().when(clusterRegistration).close(); + + netconfTopologyManager.close(); + verify(context, times(1)).closeFinal(); + verify(clusterRegistration, times(1)).close(); + + assertTrue(contexts.isEmpty()); + assertTrue(clusterRegistrations.isEmpty()); + + } + + private void writeConfiguration(final boolean fail) throws Exception { + + final ClusterSingletonServiceRegistration clusterRegistration = mock(ClusterSingletonServiceRegistration.class); + + final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts"); + fieldContexts.setAccessible(true); + @SuppressWarnings("unchecked") final Map, NetconfTopologyContext> contexts = (Map, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager); - final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations"); + final Field fieldClusterRegistrations = + NetconfTopologyManager.class.getDeclaredField("clusterRegistrations"); fieldClusterRegistrations.setAccessible(true); @SuppressWarnings("unchecked") final Map, ClusterSingletonServiceRegistration> clusterRegistrations = @@ -111,10 +181,10 @@ public class NetconfTopologyManagerTest { final Collection> changes = new ArrayList<>(); final InstanceIdentifier instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath( - new NodeKey(new NodeId("node-id-1")),"topology-1"); + new NodeKey(new NodeId("node-id-1")), "topology-1"); final InstanceIdentifier instanceIdentifierDiferent = NetconfTopologyUtils.createTopologyNodeListPath( - new NodeKey(new NodeId("node-id-2")),"topology-2"); + new NodeKey(new NodeId("node-id-2")), "topology-2"); final DataTreeIdentifier rootIdentifier = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifier); @@ -122,8 +192,8 @@ public class NetconfTopologyManagerTest { final DataTreeIdentifier rootIdentifierDifferent = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierDiferent); - @SuppressWarnings("unchecked") - final DataObjectModification objectModification = mock(DataObjectModification.class); + @SuppressWarnings("unchecked") final DataObjectModification objectModification = + mock(DataObjectModification.class); final NetconfNode netconfNode = new NetconfNodeBuilder() .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1")))) @@ -140,116 +210,71 @@ public class NetconfTopologyManagerTest { final Identifier key = new NodeKey(new NodeId("node-id")); - @SuppressWarnings("unchecked") - final InstanceIdentifier.IdentifiableItem pathArgument = + @SuppressWarnings("unchecked") final InstanceIdentifier.IdentifiableItem pathArgument = new InstanceIdentifier.IdentifiableItem(Node.class, key); // testing WRITE on two identical rootIdentifiers and one different - - changes.add(new CustomTreeModification(rootIdentifier, objectModification)); - changes.add(new CustomTreeModification(rootIdentifier, objectModification)); - changes.add(new CustomTreeModification(rootIdentifierDifferent, objectModification)); - + if (fail) { + changes.add(new CustomTreeModification(rootIdentifier, objectModification)); + } else { + changes.add(new CustomTreeModification(rootIdentifier, objectModification)); + changes.add(new CustomTreeModification(rootIdentifier, objectModification)); + changes.add(new CustomTreeModification(rootIdentifierDifferent, objectModification)); + } doReturn(WRITE).when(objectModification).getModificationType(); doReturn(node).when(objectModification).getDataAfter(); doReturn(pathArgument).when(objectModification).getIdentifier(); - doReturn(clusterRegistration).when(clusterSingletonServiceProvider).registerClusterSingletonService(any()); - - netconfTopologyManager.onDataTreeChanged(changes); - - verify(clusterSingletonServiceProvider, times(2)).registerClusterSingletonService(any()); - - // only two created contexts - assertEquals(2, contexts.size()); - assertTrue(contexts.containsKey(rootIdentifier.getRootIdentifier())); - assertTrue(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier())); - - // only two created cluster registrations - assertEquals(2, contexts.size()); - assertTrue(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier())); - assertTrue(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier())); - - // after delete there should be no context and clustered registrations - doReturn(DELETE).when(objectModification).getModificationType(); - - doNothing().when(clusterRegistration).close(); + if (fail) { + doThrow(new RuntimeException("error")).when(clusterSingletonServiceProvider) + .registerClusterSingletonService(any()); + } else { + doReturn(clusterRegistration).when(clusterSingletonServiceProvider).registerClusterSingletonService(any()); + } netconfTopologyManager.onDataTreeChanged(changes); - verify(clusterRegistration, times(2)).close(); - - // empty map of contexts - assertTrue(contexts.isEmpty()); - assertFalse(contexts.containsKey(rootIdentifier.getRootIdentifier())); - assertFalse(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier())); - - // empty map of clustered registrations - assertTrue(clusterRegistrations.isEmpty()); - assertFalse(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier())); - assertFalse(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier())); - - } - - @Test - public void testRegisterDataTreeChangeListener() { - - final WriteTransaction wtx = mock(WriteTransaction.class); - - doReturn(wtx).when(dataBroker).newWriteOnlyTransaction(); - doNothing().when(wtx).merge(any(), any(), any()); - doReturn(Futures.immediateCheckedFuture(null)).when(wtx).submit(); - doReturn(null).when(dataBroker).registerDataChangeListener(any(), any(), any(), any()); - - netconfTopologyManager.init(); - - // verify if listener is called with right parameters = registered on right path - - verify(dataBroker, times(1)).registerDataTreeChangeListener( - new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils - .createTopologyListPath(topologyId).child(Node.class)), netconfTopologyManager); - - } + if (fail) { + verify(clusterSingletonServiceProvider, times(3)) + .registerClusterSingletonService(any()); + assertTrue(contexts.isEmpty()); + assertTrue(clusterRegistrations.isEmpty()); + } else { + verify(clusterSingletonServiceProvider, times(2)) + .registerClusterSingletonService(any()); - @Test - public void testClose() throws Exception { + // only two created contexts + assertEquals(2, contexts.size()); + assertTrue(contexts.containsKey(rootIdentifier.getRootIdentifier())); + assertTrue(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier())); - final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts"); - fieldContexts.setAccessible(true); - @SuppressWarnings("unchecked") - final Map, NetconfTopologyContext> contexts = - (Map, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager); + // only two created cluster registrations + assertEquals(2, clusterRegistrations.size()); + assertTrue(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier())); + assertTrue(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier())); - final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations"); - fieldClusterRegistrations.setAccessible(true); - @SuppressWarnings("unchecked") - final Map, ClusterSingletonServiceRegistration> clusterRegistrations = - (Map, ClusterSingletonServiceRegistration>) - fieldClusterRegistrations.get(netconfTopologyManager); + // after delete there should be no context and clustered registrations + doReturn(DELETE).when(objectModification).getModificationType(); - final InstanceIdentifier instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath( - new NodeKey(new NodeId("node-id-1")),"topology-1"); + doNothing().when(clusterRegistration).close(); + netconfTopologyManager.onDataTreeChanged(changes); - final NetconfTopologyContext context = mock(NetconfTopologyContext.class); - final ClusterSingletonServiceRegistration clusterRegistration = - mock(ClusterSingletonServiceRegistration.class); - contexts.put(instanceIdentifier, context); - clusterRegistrations.put(instanceIdentifier, clusterRegistration); + verify(clusterRegistration, times(2)).close(); - doNothing().when(context).closeFinal(); - doNothing().when(clusterRegistration).close(); - - netconfTopologyManager.close(); - verify(context, times(1)).closeFinal(); - verify(clusterRegistration, times(1)).close(); - - assertTrue(contexts.isEmpty()); - assertTrue(clusterRegistrations.isEmpty()); + // empty map of contexts + assertTrue(contexts.isEmpty()); + assertFalse(contexts.containsKey(rootIdentifier.getRootIdentifier())); + assertFalse(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier())); + // empty map of clustered registrations + assertTrue(clusterRegistrations.isEmpty()); + assertFalse(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier())); + assertFalse(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier())); + } } - private class CustomTreeModification implements DataTreeModification { + static class CustomTreeModification implements DataTreeModification { private final DataTreeIdentifier rootPath; private final DataObjectModification rootNode;