Fix sonar complains
[bgpcep.git] / bgp / bmp-impl / src / test / java / org / opendaylight / protocol / bmp / impl / session / BmpDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bmp.impl.session;
10
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.timeout;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
17 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
18
19 import com.google.common.base.Optional;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.nio.NioEventLoopGroup;
24 import java.net.InetSocketAddress;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
31 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
32 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
33 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
34 import org.opendaylight.protocol.bmp.api.BmpSession;
35 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
36 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
37 import org.opendaylight.protocol.bmp.parser.BmpActivator;
38 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
39 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
40
41 public class BmpDispatcherImplTest {
42
43     private static final int PORT = 45678;
44     private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
45     private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
46
47     private BmpDispatcher dispatcher;
48     private BGPActivator bgpActivator;
49     private BmpActivator bmpActivator;
50
51     @Mock
52     private BmpSession mockedSession;
53     @Mock
54     private BmpSessionListenerFactory mockedListenerFactory;
55
56     @Before
57     public void setUp() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         doNothing().when(this.mockedSession).handlerRemoved(any(ChannelHandlerContext.class));
60         doNothing().when(this.mockedSession).handlerAdded(any(ChannelHandlerContext.class));
61         doNothing().when(this.mockedSession).channelRegistered(any(ChannelHandlerContext.class));
62         doNothing().when(this.mockedSession).channelActive(any(ChannelHandlerContext.class));
63         doNothing().when(this.mockedSession).channelInactive(any(ChannelHandlerContext.class));
64         doNothing().when(this.mockedSession).channelUnregistered(any(ChannelHandlerContext.class));
65         doNothing().when(this.mockedSession).channelReadComplete(any(ChannelHandlerContext.class));
66
67         this.bgpActivator = new BGPActivator();
68         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
69         this.bgpActivator.start(context);
70         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
71         this.bmpActivator = new BmpActivator(context);
72         this.bmpActivator.start(ctx);
73         final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
74
75         this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
76             messageRegistry, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
77     }
78
79     @After
80     public void tearDown() throws Exception {
81         this.bgpActivator.close();
82         this.bmpActivator.close();
83         this.dispatcher.close();
84     }
85
86     @Test
87     public void testCreateServer() throws Exception {
88         final ChannelFuture futureServer = this.dispatcher.createServer(SERVER,
89             this.mockedListenerFactory, Optional.absent());
90         waitFutureSuccess(futureServer);
91         final Channel serverChannel = futureServer.channel();
92         checkEquals(()-> assertTrue(serverChannel.isActive()));
93
94
95         final ChannelFuture futureClient = this.dispatcher.createClient(CLIENT_REMOTE,
96             this.mockedListenerFactory, Optional.absent());
97         waitFutureSuccess(futureClient);
98
99         final Channel clientChannel = futureClient.channel();
100         checkEquals(()-> assertTrue(clientChannel.isActive()));
101         verify(this.mockedSession, timeout(500).times(2)).handlerAdded(any(ChannelHandlerContext.class));
102         verify(this.mockedSession, timeout(500).times(2)).channelRegistered(any(ChannelHandlerContext.class));
103         verify(this.mockedSession, timeout(500).times(2)).channelActive(any(ChannelHandlerContext.class));
104         waitFutureSuccess(clientChannel.close());
105         waitFutureSuccess(serverChannel.close());
106     }
107 }