2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bmp.impl.session;
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;
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;
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);
45 private BmpDispatcherImpl dispatcher;
46 private BGPActivator bgpActivator;
47 private BmpActivator bmpActivator;
50 private BmpSession mockedSession;
52 private BmpSessionListenerFactory mockedListenerFactory;
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));
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);
70 dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx,
71 (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
75 public void tearDown() throws Exception {
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()));
86 final ChannelFuture futureClient = dispatcher.createClient(CLIENT_REMOTE, mockedListenerFactory,
88 waitFutureSuccess(futureClient);
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());