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
9 package org.opendaylight.protocol.bmp.impl.session;
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;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import java.net.InetSocketAddress;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
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.BmpDispatcher;
33 import org.opendaylight.protocol.bmp.api.BmpSession;
34 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
35 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
36 import org.opendaylight.protocol.bmp.parser.BmpActivator;
37 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
38 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
39 import org.opendaylight.protocol.concepts.KeyMapping;
41 public class BmpDispatcherImplTest {
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);
47 private BmpDispatcher dispatcher;
48 private BGPActivator bgpActivator;
49 private BmpActivator bmpActivator;
52 private BmpSession mockedSession;
54 private BmpSessionListenerFactory mockedListenerFactory;
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));
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();
75 this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
76 messageRegistry, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
80 public void tearDown() throws Exception {
81 this.bgpActivator.close();
82 this.bmpActivator.close();
83 this.dispatcher.close();
87 public void testCreateServer() throws Exception {
88 final ChannelFuture futureServer = this.dispatcher.createServer(SERVER,
89 this.mockedListenerFactory, KeyMapping.getKeyMapping());
90 waitFutureSuccess(futureServer);
91 final Channel serverChannel = futureServer.channel();
92 checkEquals(() -> assertTrue(serverChannel.isActive()));
95 final ChannelFuture futureClient = this.dispatcher.createClient(CLIENT_REMOTE,
96 this.mockedListenerFactory, KeyMapping.getKeyMapping());
97 waitFutureSuccess(futureClient);
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());