1d677c4b8bfed8eb7e39d0606631ed541cccf258
[bgpcep.git] / bmp / 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 package org.opendaylight.protocol.bmp.impl.session;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.timeout;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
16 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import java.net.InetSocketAddress;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
30 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
31 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
32 import org.opendaylight.protocol.bmp.api.BmpSession;
33 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
34 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
35 import org.opendaylight.protocol.bmp.parser.BmpActivator;
36 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
37 import org.opendaylight.protocol.concepts.KeyMapping;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class BmpDispatcherImplTest {
41     private static final int PORT = 45678;
42     private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
43     private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
44
45     private BmpDispatcherImpl dispatcher;
46     private BGPActivator bgpActivator;
47     private BmpActivator bmpActivator;
48
49     @Mock
50     private BmpSession mockedSession;
51     @Mock
52     private BmpSessionListenerFactory mockedListenerFactory;
53
54     @Before
55     public void setUp() throws Exception {
56         doNothing().when(mockedSession).handlerRemoved(any(ChannelHandlerContext.class));
57         doNothing().when(mockedSession).handlerAdded(any(ChannelHandlerContext.class));
58         doNothing().when(mockedSession).channelRegistered(any(ChannelHandlerContext.class));
59         doNothing().when(mockedSession).channelActive(any(ChannelHandlerContext.class));
60         doNothing().when(mockedSession).channelInactive(any(ChannelHandlerContext.class));
61         doNothing().when(mockedSession).channelUnregistered(any(ChannelHandlerContext.class));
62
63         bgpActivator = new BGPActivator();
64         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
65         bgpActivator.start(context);
66         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
67         bmpActivator = new BmpActivator(context);
68         bmpActivator.start(ctx);
69
70         dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx,
71             (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
72     }
73
74     @After
75     public void tearDown() throws Exception {
76         dispatcher.close();
77     }
78
79     @Test
80     public void testCreateServer() throws Exception {
81         final ChannelFuture futureServer = dispatcher.createServer(SERVER, mockedListenerFactory, KeyMapping.of());
82         waitFutureSuccess(futureServer);
83         final Channel serverChannel = futureServer.channel();
84         checkEquals(() -> assertTrue(serverChannel.isActive()));
85
86         final ChannelFuture futureClient = dispatcher.createClient(CLIENT_REMOTE, mockedListenerFactory,
87             KeyMapping.of());
88         waitFutureSuccess(futureClient);
89
90         final Channel clientChannel = futureClient.channel();
91         checkEquals(() -> assertTrue(clientChannel.isActive()));
92         verify(mockedSession, timeout(500).times(2)).handlerAdded(any(ChannelHandlerContext.class));
93         verify(mockedSession, timeout(500).times(2)).channelRegistered(any(ChannelHandlerContext.class));
94         verify(mockedSession, timeout(500).times(2)).channelActive(any(ChannelHandlerContext.class));
95         waitFutureSuccess(clientChannel.close());
96         waitFutureSuccess(serverChannel.close());
97     }
98 }