Fix sonar complains
[bgpcep.git] / bgp / bmp-impl / src / test / java / org / opendaylight / protocol / bmp / impl / session / BmpDispatcherImplTest.java
index a3bc7a5e8166e55bc196be54fb9a540f8abd785b..d48310e4682ba86488e7ebf1d28c7c1ea57ae69d 100644 (file)
@@ -8,33 +8,35 @@
 
 package org.opendaylight.protocol.bmp.impl.session;
 
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
+import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
+
 import com.google.common.base.Optional;
 import io.netty.channel.Channel;
-import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.nio.NioEventLoopGroup;
 import java.net.InetSocketAddress;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 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;
 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
 import org.opendaylight.protocol.bmp.api.BmpSession;
-import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
-import org.opendaylight.protocol.bmp.impl.BmpActivator;
 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
+import org.opendaylight.protocol.bmp.parser.BmpActivator;
 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
-import org.opendaylight.tcpmd5.api.KeyMapping;
-import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
-import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
 
 public class BmpDispatcherImplTest {
 
@@ -50,19 +52,17 @@ public class BmpDispatcherImplTest {
     private BmpSession mockedSession;
     @Mock
     private BmpSessionListenerFactory mockedListenerFactory;
-    @Mock
-    private ChannelHandler mockedChannelHandler;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        Mockito.doNothing().when(this.mockedSession).handlerRemoved(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).handlerAdded(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).channelRegistered(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).channelActive(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).channelInactive(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).channelUnregistered(Mockito.any(ChannelHandlerContext.class));
-        Mockito.doNothing().when(this.mockedSession).channelReadComplete(Mockito.any(ChannelHandlerContext.class));
+        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));
 
         this.bgpActivator = new BGPActivator();
         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
@@ -72,13 +72,8 @@ public class BmpDispatcherImplTest {
         this.bmpActivator.start(ctx);
         final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
 
-        this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), messageRegistry, new BmpSessionFactory() {
-            @Override
-            public BmpSession getSession(final Channel channel,
-                    final BmpSessionListenerFactory sessionListenerFactory) {
-                return BmpDispatcherImplTest.this.mockedSession;
-            }
-        }, Optional.<MD5ChannelFactory<?>>absent(), Optional.<MD5ServerChannelFactory<?>>absent());
+        this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
+            messageRegistry, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
     }
 
     @After
@@ -90,15 +85,23 @@ public class BmpDispatcherImplTest {
 
     @Test
     public void testCreateServer() throws Exception {
-        final Channel serverChannel = this.dispatcher.createServer(SERVER, this.mockedListenerFactory, Optional.<KeyMapping>absent()).await().channel();
-        Assert.assertTrue(serverChannel.isActive());
-        final Channel clientChannel = this.dispatcher.createClient(CLIENT_REMOTE, this.mockedListenerFactory, Optional.<KeyMapping>absent()).await().channel();
-        Assert.assertTrue(clientChannel.isActive());
-        Thread.sleep(500);
-        Mockito.verify(this.mockedSession, Mockito.times(2)).handlerAdded(Mockito.any(ChannelHandlerContext.class));
-        Mockito.verify(this.mockedSession, Mockito.times(2)).channelRegistered(Mockito.any(ChannelHandlerContext.class));
-        Mockito.verify(this.mockedSession, Mockito.times(2)).channelActive(Mockito.any(ChannelHandlerContext.class));
-        clientChannel.close().get();
-        serverChannel.close().get();
+        final ChannelFuture futureServer = this.dispatcher.createServer(SERVER,
+            this.mockedListenerFactory, Optional.absent());
+        waitFutureSuccess(futureServer);
+        final Channel serverChannel = futureServer.channel();
+        checkEquals(()-> assertTrue(serverChannel.isActive()));
+
+
+        final ChannelFuture futureClient = this.dispatcher.createClient(CLIENT_REMOTE,
+            this.mockedListenerFactory, Optional.absent());
+        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));
+        waitFutureSuccess(clientChannel.close());
+        waitFutureSuccess(serverChannel.close());
     }
 }