Reduce number of paramaters for PCEP Dispatcher
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImplTest.java
index c5e5a009334cbe987c47082d140916206181aec6..64512d4719d2807e000c366de3dd71d8b8b41889 100755 (executable)
@@ -8,11 +8,12 @@
 
 package org.opendaylight.protocol.pcep.impl;
 
+import static java.util.Objects.requireNonNull;
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
 
-import com.google.common.base.Preconditions;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelInitializer;
@@ -40,6 +41,7 @@ import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.protocol.concepts.KeyMapping;
 import org.opendaylight.protocol.pcep.PCEPCapability;
+import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
 import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
@@ -59,6 +61,10 @@ public class PCEPDispatcherImplTest {
 
     @Mock
     private Channel mockChannel;
+    @Mock
+    private PCEPDispatcherDependencies dispatcherDependencies;
+    @Mock
+    private PCEPSessionListenerFactory listenerFactory;
 
     private PCCMock pccMock;
 
@@ -67,7 +73,7 @@ public class PCEPDispatcherImplTest {
         MockitoAnnotations.initMocks(this);
         final List<PCEPCapability> capList = new ArrayList<>();
         final PCEPSessionProposalFactory sessionProposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
-            capList);
+                capList);
         final EventLoopGroup eventLoopGroup;
         if (Epoll.isAvailable()) {
             eventLoopGroup = new EpollEventLoopGroup();
@@ -75,18 +81,22 @@ public class PCEPDispatcherImplTest {
             eventLoopGroup = new NioEventLoopGroup();
         }
         final MessageRegistry msgReg = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
-            .getMessageHandlerRegistry();
-        this.dispatcher = new PCEPDispatcherImpl(msgReg, new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
-            eventLoopGroup, eventLoopGroup);
+                .getMessageHandlerRegistry();
+        this.dispatcher = new PCEPDispatcherImpl(msgReg,
+                new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
+                eventLoopGroup, eventLoopGroup);
+
+        doReturn(KeyMapping.getKeyMapping()).when(this.dispatcherDependencies).getKeys();
+        doReturn(null).when(this.dispatcherDependencies).getPeerProposal();
 
-        Mockito.doReturn("mockChannel").when(this.mockChannel).toString();
+        doReturn("mockChannel").when(this.mockChannel).toString();
         final PCEPDispatcherImpl dispatcher2 = new PCEPDispatcherImpl(msgReg,
-            new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
-            eventLoopGroup, eventLoopGroup);
+                new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
+                eventLoopGroup, eventLoopGroup);
         this.disp2Spy = Mockito.spy(dispatcher2);
 
         this.pccMock = new PCCMock(new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
-            new PCEPHandlerFactory(msgReg));
+                new PCEPHandlerFactory(msgReg));
     }
 
     @Test
@@ -95,14 +105,17 @@ public class PCEPDispatcherImplTest {
         final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port);
         final InetSocketAddress clientAddr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
         final InetSocketAddress clientAddr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
-        final ChannelFuture futureChannel = this.dispatcher.createServer(serverAddr,
-            SimpleSessionListener::new, null);
+
+        doReturn(serverAddr).when(this.dispatcherDependencies).getAddress();
+        doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+        doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+        final ChannelFuture futureChannel = this.dispatcher.createServer(this.dispatcherDependencies);
         waitFutureSuccess(futureChannel);
-        final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr1,
-            RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+        final PCEPSessionImpl session1 = this.pccMock.createClient(clientAddr1,
+                RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
 
-        final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr2,
-            RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+        final PCEPSessionImpl session2 = this.pccMock.createClient(clientAddr2,
+                RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
 
         Assert.assertTrue(futureChannel.channel().isActive());
         assertEquals(clientAddr1.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress());
@@ -123,15 +136,20 @@ public class PCEPDispatcherImplTest {
         final int port = InetSocketAddressUtil.getRandomPort();
         final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port);
         final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
-        waitFutureSuccess(this.dispatcher.createServer(serverAddr, SimpleSessionListener::new, null));
-        final Future<PCEPSession> futureClient = this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
-            SimpleSessionListener::new);
+
+        doReturn(serverAddr).when(this.dispatcherDependencies).getAddress();
+        doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+        doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+
+        waitFutureSuccess(this.dispatcher.createServer(this.dispatcherDependencies));
+        final Future<PCEPSessionImpl> futureClient = this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
+                SimpleSessionListener::new);
         waitFutureSuccess(futureClient);
-        final PCEPSessionImpl session1 = (PCEPSessionImpl) futureClient.get();
+        final PCEPSessionImpl session1 = futureClient.get();
 
         try {
             this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
-                SimpleSessionListener::new).get();
+                    SimpleSessionListener::new).get();
             Assert.fail();
         } catch (final ExecutionException e) {
             Assert.assertTrue(e.getMessage().contains("A conflicting session for address"));
@@ -144,18 +162,21 @@ public class PCEPDispatcherImplTest {
     public void testReconectClient() throws InterruptedException, ExecutionException {
         final int port = InetSocketAddressUtil.getRandomPort();
         final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
-        waitFutureSuccess(this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", port),
-            SimpleSessionListener::new, null));
-        final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr,
-            RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+
+        doReturn(new InetSocketAddress("0.0.0.0", port)).when(this.dispatcherDependencies).getAddress();
+        doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+        doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+        waitFutureSuccess(this.dispatcher.createServer(this.dispatcherDependencies));
+        final PCEPSessionImpl session1 = this.pccMock.createClient(clientAddr,
+                RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
 
         assertEquals(clientAddr.getAddress(), session1.getRemoteAddress());
         assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
         assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
         waitFutureSuccess(session1.closeChannel());
 
-        final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr,
-            RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+        final PCEPSessionImpl session2 = this.pccMock.createClient(clientAddr,
+                RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
 
         assertEquals(clientAddr.getAddress(), session1.getRemoteAddress());
         assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
@@ -172,8 +193,11 @@ public class PCEPDispatcherImplTest {
         final KeyMapping keys = KeyMapping.getKeyMapping(clientAddr1.getAddress(), "CLIENT1_ADDRESS");
         keys.put(clientAddr2.getAddress(), "CLIENT2_ADDRESS".getBytes());
 
-        final ChannelFuture futureChannel = this.disp2Spy.createServer(new InetSocketAddress("0.0.0.0", port),
-            SimpleSessionListener::new, null);
+        doReturn(new InetSocketAddress("0.0.0.0", port)).when(this.dispatcherDependencies).getAddress();
+        doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+        doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+
+        final ChannelFuture futureChannel = this.disp2Spy.createServer(this.dispatcherDependencies);
         waitFutureSuccess(futureChannel);
         Mockito.verify(this.disp2Spy).createServerBootstrap(any(PCEPDispatcherImpl.ChannelPipelineInitializer.class));
     }
@@ -185,34 +209,37 @@ public class PCEPDispatcherImplTest {
     }
 
     private static class PCCMock {
-        private final PCEPSessionNegotiatorFactory negotiatorFactory;
+        private final PCEPSessionNegotiatorFactory<PCEPSessionImpl> negotiatorFactory;
         private final PCEPHandlerFactory factory;
         private final EventExecutor executor;
         private final EventLoopGroup workerGroup;
 
-        PCCMock(final PCEPSessionNegotiatorFactory negotiatorFactory, final PCEPHandlerFactory factory) {
-            this.workerGroup = Preconditions.checkNotNull(new NioEventLoopGroup());
-            this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
-            this.factory = Preconditions.checkNotNull(factory);
-            this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
+        PCCMock(final PCEPSessionNegotiatorFactory<PCEPSessionImpl> negotiatorFactory,
+                final PCEPHandlerFactory factory) {
+            this.workerGroup = new NioEventLoopGroup();
+            this.negotiatorFactory = requireNonNull(negotiatorFactory);
+            this.factory = requireNonNull(factory);
+            this.executor = requireNonNull(GlobalEventExecutor.INSTANCE);
         }
 
-        Future<PCEPSession> createClient(final InetSocketAddress address, final int retryTimer,
-            final int connectTimeout, final PCEPSessionListenerFactory listenerFactory) {
+        Future<PCEPSessionImpl> createClient(final InetSocketAddress address, final int retryTimer,
+                final int connectTimeout, final PCEPSessionListenerFactory listenerFactory) {
             return createClient(address, retryTimer, connectTimeout, (ch, promise) -> {
                 ch.pipeline().addLast(this.factory.getDecoders());
-                ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch,
-                    promise, null));
+                ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(
+                        () -> listenerFactory,
+                        ch,
+                        promise));
                 ch.pipeline().addLast(this.factory.getEncoders());
             });
         }
 
-        Future<PCEPSession> createClient(final InetSocketAddress address, final int retryTimer,
-            final int connectTimeout, final PCEPDispatcherImpl.ChannelPipelineInitializer initializer) {
+        Future<PCEPSessionImpl> createClient(final InetSocketAddress address, final int retryTimer,
+                final int connectTimeout, final PCEPDispatcherImpl.ChannelPipelineInitializer initializer) {
             final Bootstrap b = new Bootstrap();
-            final PCEPProtocolSessionPromise p = new PCEPProtocolSessionPromise(this.executor, address, retryTimer,
-                connectTimeout, b);
-            (b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)).handler(new ChannelInitializer<SocketChannel>() {
+            final PCEPProtocolSessionPromise<PCEPSessionImpl> p = new PCEPProtocolSessionPromise<>(this.executor,
+                    address, retryTimer, connectTimeout, b);
+            b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(final SocketChannel ch) {
                     initializer.initializeChannel(ch, p);
@@ -225,16 +252,15 @@ public class PCEPDispatcherImplTest {
             return p;
         }
 
-        private void setChannelFactory(final Bootstrap b) {
+        private static void setChannelFactory(final Bootstrap b) {
             try {
                 b.channel(NioSocketChannel.class);
             } catch (final IllegalStateException ignored) {
             }
-
         }
 
         private void setWorkerGroup(final Bootstrap b) {
-            if (b.group() == null) {
+            if (b.config().group() == null) {
                 b.group(this.workerGroup);
             }
         }