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 com.google.common.base.Optional;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelHandler;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.net.InetSocketAddress;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
25 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
27 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
28 import org.opendaylight.protocol.bmp.api.BmpSession;
29 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
30 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
31 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
32 import org.opendaylight.protocol.bmp.parser.BmpActivator;
33 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
34 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
36 public class BmpDispatcherImplTest {
38 private static final int PORT = 45678;
39 private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
40 private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
42 private BmpDispatcher dispatcher;
43 private BGPActivator bgpActivator;
44 private BmpActivator bmpActivator;
47 private BmpSession mockedSession;
49 private BmpSessionListenerFactory mockedListenerFactory;
51 private ChannelHandler mockedChannelHandler;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 Mockito.doNothing().when(this.mockedSession).handlerRemoved(Mockito.any(ChannelHandlerContext.class));
57 Mockito.doNothing().when(this.mockedSession).handlerAdded(Mockito.any(ChannelHandlerContext.class));
58 Mockito.doNothing().when(this.mockedSession).channelRegistered(Mockito.any(ChannelHandlerContext.class));
59 Mockito.doNothing().when(this.mockedSession).channelActive(Mockito.any(ChannelHandlerContext.class));
60 Mockito.doNothing().when(this.mockedSession).channelInactive(Mockito.any(ChannelHandlerContext.class));
61 Mockito.doNothing().when(this.mockedSession).channelUnregistered(Mockito.any(ChannelHandlerContext.class));
62 Mockito.doNothing().when(this.mockedSession).channelReadComplete(Mockito.any(ChannelHandlerContext.class));
64 this.bgpActivator = new BGPActivator();
65 final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
66 this.bgpActivator.start(context);
67 final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
68 this.bmpActivator = new BmpActivator(context);
69 this.bmpActivator.start(ctx);
70 final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
72 this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), messageRegistry, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
76 public void tearDown() throws Exception {
77 this.bgpActivator.close();
78 this.bmpActivator.close();
79 this.dispatcher.close();
83 public void testCreateServer() throws Exception {
84 final Channel serverChannel = this.dispatcher.createServer(SERVER, this.mockedListenerFactory, Optional.absent()).await().channel();
85 Assert.assertTrue(serverChannel.isActive());
86 final Channel clientChannel = this.dispatcher.createClient(CLIENT_REMOTE, this.mockedListenerFactory, Optional.absent()).await().channel();
87 Assert.assertTrue(clientChannel.isActive());
89 Mockito.verify(this.mockedSession, Mockito.times(2)).handlerAdded(Mockito.any(ChannelHandlerContext.class));
90 Mockito.verify(this.mockedSession, Mockito.times(2)).channelRegistered(Mockito.any(ChannelHandlerContext.class));
91 Mockito.verify(this.mockedSession, Mockito.times(2)).channelActive(Mockito.any(ChannelHandlerContext.class));
92 clientChannel.close().get();
93 serverChannel.close().get();