Migrate bmp-impl tests 71/97571/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Sep 2021 09:33:21 +0000 (11:33 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Sep 2021 09:33:21 +0000 (11:33 +0200)
Do not use MockitoAnnotations.initmocks(), but rather
MockitoJUnitRunner, which has further benefits.

Change-Id: I85589b5264f62a07bbe87f13daf899fa3e947aed
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bmp/bmp-impl/src/test/java/org/opendaylight/protocol/bmp/impl/app/BmpMonitorImplTest.java
bmp/bmp-impl/src/test/java/org/opendaylight/protocol/bmp/impl/session/BmpDispatcherImplTest.java
bmp/bmp-impl/src/test/java/org/opendaylight/protocol/bmp/impl/session/BmpSessionImplTest.java

index 101dd78d737c768d880917fb45f2cebb968ff65b..376d038c8e5bf2a6942f85fcd7c36cebee1ea3c1 100644 (file)
@@ -38,8 +38,9 @@ import java.util.concurrent.ExecutionException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTestCustomizer;
@@ -98,6 +99,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
     // the local port and address where the monitor (ODL) will listen for incoming BMP request
     private static final int MONITOR_LOCAL_PORT = 12345;
@@ -135,36 +137,34 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
     public void setUp() throws Exception {
         super.setup();
 
-        MockitoAnnotations.initMocks(this);
-
         doAnswer(invocationOnMock -> {
             BmpMonitorImplTest.this.singletonService = (ClusterSingletonService) invocationOnMock.getArguments()[0];
-            this.singletonService.instantiateServiceInstance();
+            singletonService.instantiateServiceInstance();
             return BmpMonitorImplTest.this.singletonServiceRegistration;
-        }).when(this.clusterSSProv).registerClusterSingletonService(any(ClusterSingletonService.class));
+        }).when(clusterSSProv).registerClusterSingletonService(any(ClusterSingletonService.class));
 
         doAnswer(invocationOnMock -> BmpMonitorImplTest.this.singletonService.closeServiceInstance())
-            .when(this.singletonServiceRegistration).close();
+            .when(singletonServiceRegistration).close();
 
         doAnswer(invocationOnMock -> {
-            this.singletonService2 = (ClusterSingletonService) invocationOnMock.getArguments()[0];
-            this.singletonService2.instantiateServiceInstance();
+            singletonService2 = (ClusterSingletonService) invocationOnMock.getArguments()[0];
+            singletonService2.instantiateServiceInstance();
             return BmpMonitorImplTest.this.singletonServiceRegistration2;
-        }).when(this.clusterSSProv2).registerClusterSingletonService(any(ClusterSingletonService.class));
+        }).when(clusterSSProv2).registerClusterSingletonService(any(ClusterSingletonService.class));
 
         doAnswer(invocationOnMock -> BmpMonitorImplTest.this.singletonService2.closeServiceInstance())
-            .when(this.singletonServiceRegistration2).close();
+            .when(singletonServiceRegistration2).close();
 
-        this.ribActivator.startRIBExtensionProvider(this.ribExtension, this.mappingService.currentSerializer());
+        ribActivator.startRIBExtensionProvider(ribExtension, mappingService.currentSerializer());
 
         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
-        this.bgpActivator.start(context);
+        bgpActivator.start(context);
         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
-        this.bmpActivator = new BmpActivator(context);
-        this.bmpActivator.start(ctx);
-        this.msgRegistry = ctx.getBmpMessageRegistry();
+        bmpActivator = new BmpActivator(context);
+        bmpActivator.start(ctx);
+        msgRegistry = ctx.getBmpMessageRegistry();
 
-        this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx,
+        dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx,
             new DefaultBmpSessionFactory());
 
         final InetSocketAddress inetAddress = new InetSocketAddress(InetAddresses.forString(MONITOR_LOCAL_ADDRESS),
@@ -177,8 +177,8 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
         wTx.merge(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of(BmpMonitor.QNAME), parentNode);
         wTx.commit().get();
 
-        this.bmpApp = new BmpMonitoringStationImpl(getDomBroker(), this.dispatcher, this.ribExtension,
-            this.mappingService.currentSerializer(), this.clusterSSProv, MONITOR_ID, inetAddress, null);
+        bmpApp = new BmpMonitoringStationImpl(getDomBroker(), dispatcher, ribExtension,
+            mappingService.currentSerializer(), clusterSSProv, MONITOR_ID, inetAddress, null);
         readDataOperational(getDataBroker(), BMP_II, monitor -> {
             assertEquals(1, monitor.nonnullMonitor().size());
             final Monitor bmpMonitor = monitor.nonnullMonitor().values().iterator().next();
@@ -193,14 +193,14 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
     @Override
     protected final AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer() {
         final AbstractDataBrokerTestCustomizer customizer = super.createDataBrokerTestCustomizer();
-        this.mappingService = customizer.getAdapterContext();
+        mappingService = customizer.getAdapterContext();
         return customizer;
     }
 
     @After
     public void tearDown() throws Exception {
-        this.dispatcher.close();
-        this.bmpApp.close();
+        dispatcher.close();
+        bmpApp.close();
 
         checkNotPresentOperational(getDataBroker(), BMP_II);
     }
@@ -222,7 +222,7 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
 
         // initiate another BMP request from router 1, create a redundant connection
         // we expect the connection to be closed
-        final Channel channel3 = connectTestClient(REMOTE_ROUTER_ADDRESS_1, this.msgRegistry);
+        final Channel channel3 = connectTestClient(REMOTE_ROUTER_ADDRESS_1, msgRegistry);
 
 
         // channel 1 should still be open, while channel3 should be closed
@@ -263,7 +263,7 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
 
     private Channel testMonitoringStation(final String remoteRouterIpAddr) throws InterruptedException,
             ExecutionException {
-        final Channel channel = connectTestClient(remoteRouterIpAddr, this.msgRegistry);
+        final Channel channel = connectTestClient(remoteRouterIpAddr, msgRegistry);
         final RouterId routerId = getRouterId(remoteRouterIpAddr);
 
         readDataOperational(getDataBroker(), MONITOR_IID, monitor -> {
@@ -422,8 +422,8 @@ public class BmpMonitorImplTest extends AbstractConcurrentDataBrokerTest {
 
     @Test
     public void deploySecondInstance() throws Exception {
-        final BmpMonitoringStation monitoringStation2 = new BmpMonitoringStationImpl(getDomBroker(), this.dispatcher,
-            this.ribExtension, this.mappingService.currentSerializer(), this.clusterSSProv2, new MonitorId("monitor2"),
+        final BmpMonitoringStation monitoringStation2 = new BmpMonitoringStationImpl(getDomBroker(), dispatcher,
+            ribExtension, mappingService.currentSerializer(), clusterSSProv2, new MonitorId("monitor2"),
             new InetSocketAddress(InetAddresses.forString(MONITOR_LOCAL_ADDRESS_2), MONITOR_LOCAL_PORT), null);
 
         readDataOperational(getDataBroker(), BMP_II, monitor -> {
index 06144a49a83dfb23efdbf0bf869f3dbb5ced26cf..ecb5e27195b8f8073921f8654e9782e60a0d9cc1 100644 (file)
@@ -23,8 +23,9 @@ import java.net.InetSocketAddress;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
@@ -35,8 +36,8 @@ import org.opendaylight.protocol.bmp.parser.BmpActivator;
 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
 import org.opendaylight.protocol.concepts.KeyMapping;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class BmpDispatcherImplTest {
-
     private static final int PORT = 45678;
     private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
     private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
@@ -52,49 +53,48 @@ public class BmpDispatcherImplTest {
 
     @Before
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-        doNothing().when(this.mockedSession).handlerRemoved(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).handlerAdded(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).channelRegistered(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).channelActive(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).channelInactive(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).channelUnregistered(any(ChannelHandlerContext.class));
-        doNothing().when(this.mockedSession).channelReadComplete(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).handlerRemoved(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).handlerAdded(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).channelRegistered(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).channelActive(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).channelInactive(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).channelUnregistered(any(ChannelHandlerContext.class));
+        doNothing().when(mockedSession).channelReadComplete(any(ChannelHandlerContext.class));
 
-        this.bgpActivator = new BGPActivator();
+        bgpActivator = new BGPActivator();
         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
-        this.bgpActivator.start(context);
+        bgpActivator.start(context);
         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
-        this.bmpActivator = new BmpActivator(context);
-        this.bmpActivator.start(ctx);
+        bmpActivator = new BmpActivator(context);
+        bmpActivator.start(ctx);
 
-        this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
-                ctx, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
+        dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx,
+            (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
     }
 
     @After
     public void tearDown() throws Exception {
-        this.dispatcher.close();
+        dispatcher.close();
     }
 
     @Test
     public void testCreateServer() throws Exception {
-        final ChannelFuture futureServer = this.dispatcher.createServer(SERVER,
-                this.mockedListenerFactory, KeyMapping.getKeyMapping());
+        final ChannelFuture futureServer = dispatcher.createServer(SERVER, mockedListenerFactory,
+            KeyMapping.getKeyMapping());
         waitFutureSuccess(futureServer);
         final Channel serverChannel = futureServer.channel();
         checkEquals(() -> assertTrue(serverChannel.isActive()));
 
 
-        final ChannelFuture futureClient = this.dispatcher.createClient(CLIENT_REMOTE,
-                this.mockedListenerFactory, KeyMapping.getKeyMapping());
+        final ChannelFuture futureClient = dispatcher.createClient(CLIENT_REMOTE, mockedListenerFactory,
+            KeyMapping.getKeyMapping());
         waitFutureSuccess(futureClient);
 
         final Channel clientChannel = futureClient.channel();
         checkEquals(() -> assertTrue(clientChannel.isActive()));
-        verify(this.mockedSession, timeout(500).times(2)).handlerAdded(any(ChannelHandlerContext.class));
-        verify(this.mockedSession, timeout(500).times(2)).channelRegistered(any(ChannelHandlerContext.class));
-        verify(this.mockedSession, timeout(500).times(2)).channelActive(any(ChannelHandlerContext.class));
+        verify(mockedSession, timeout(500).times(2)).handlerAdded(any(ChannelHandlerContext.class));
+        verify(mockedSession, timeout(500).times(2)).channelRegistered(any(ChannelHandlerContext.class));
+        verify(mockedSession, timeout(500).times(2)).channelActive(any(ChannelHandlerContext.class));
         waitFutureSuccess(clientChannel.close());
         waitFutureSuccess(serverChannel.close());
     }
index a43e1d5101311da3bb4372db4d2c35f0a0686a44..693716b6d57aa41196ba63b930d059ea28c59050 100644 (file)
@@ -5,25 +5,25 @@
  * 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.protocol.bmp.impl.session;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
 
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.embedded.EmbeddedChannel;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.protocol.bmp.api.BmpSession;
 import org.opendaylight.protocol.bmp.parser.message.TestUtil;
 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.InitiationMessage;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class BmpSessionImplTest {
 
     private BmpSession session;
@@ -38,51 +38,47 @@ public class BmpSessionImplTest {
 
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
-        doReturn(null).when(this.mockedHandlerContext).channel();
-        doReturn(null).when(this.mockedHandlerContext).fireChannelInactive();
-        this.listener = new BmpTestSessionListener();
-        this.session = new BmpSessionImpl(this.listener);
-        this.channel = new EmbeddedChannel(this.session);
-        assertTrue(this.listener.isUp());
+        listener = new BmpTestSessionListener();
+        session = new BmpSessionImpl(listener);
+        channel = new EmbeddedChannel(session);
+        assertTrue(listener.isUp());
     }
 
     @Test
     public void testOnInitiate() {
-        this.channel.writeInbound(createInitMsg());
-        assertEquals(this.listener.getListMsg().size(), 1);
-        this.channel.writeInbound(createInitMsg());
-        assertEquals(this.listener.getListMsg().size(), 2);
+        channel.writeInbound(createInitMsg());
+        assertEquals(listener.getListMsg().size(), 1);
+        channel.writeInbound(createInitMsg());
+        assertEquals(listener.getListMsg().size(), 2);
     }
 
     @Test
     public void testOnTerminationAfterInitiated() {
-        this.channel.writeInbound(createInitMsg());
-        assertEquals(this.listener.getListMsg().size(), 1);
-        this.channel.writeInbound(TestUtil.createTerminationMsg());
-        assertEquals(this.listener.getListMsg().size(), 1);
-        assertFalse(this.listener.isUp());
+        channel.writeInbound(createInitMsg());
+        assertEquals(listener.getListMsg().size(), 1);
+        channel.writeInbound(TestUtil.createTerminationMsg());
+        assertEquals(listener.getListMsg().size(), 1);
+        assertFalse(listener.isUp());
     }
 
     @Test
     public void testOnTermination() {
-        this.channel.writeInbound(TestUtil.createTerminationMsg());
-        assertEquals(this.listener.getListMsg().size(), 0);
-        assertFalse(this.listener.isUp());
+        channel.writeInbound(TestUtil.createTerminationMsg());
+        assertEquals(listener.getListMsg().size(), 0);
+        assertFalse(listener.isUp());
     }
 
     @Test
     public void testOnUnexpectedMessage() {
-        this.channel.writeInbound(TestUtil.createPeerDownFSM());
-        assertEquals(this.listener.getListMsg().size(), 0);
-        assertFalse(this.listener.isUp());
+        channel.writeInbound(TestUtil.createPeerDownFSM());
+        assertEquals(listener.getListMsg().size(), 0);
+        assertFalse(listener.isUp());
     }
 
     @Test
     public void testExceptionCaught() throws Exception {
-        this.session.exceptionCaught(this.mockedHandlerContext, new BmpDeserializationException(""));
-        assertEquals(this.listener.getListMsg().size(), 0);
-        assertFalse(this.listener.isUp());
+        session.exceptionCaught(mockedHandlerContext, new BmpDeserializationException(""));
+        assertEquals(listener.getListMsg().size(), 0);
+        assertFalse(listener.isUp());
     }
-
 }