2 * Copyright (c) 2016 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.mock;
11 import static org.opendaylight.protocol.bmp.mock.BmpMockTest.waitFutureSuccess;
13 import com.google.common.base.Optional;
14 import com.google.common.net.InetAddresses;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import java.net.InetSocketAddress;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
23 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
24 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
25 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
26 import org.opendaylight.protocol.concepts.KeyMapping;
27 import org.opendaylight.protocol.util.InetSocketAddressUtil;
29 public class BmpMockDispatcherTest {
31 private final BmpMessageRegistry registry = Mockito.mock(BmpMessageRegistry.class);
32 private final BmpSessionFactory sessionFactory = Mockito.mock(BmpSessionFactory.class);
33 private final BmpSessionListenerFactory slf = Mockito.mock(BmpSessionListenerFactory.class);
36 public void testCreateClient() throws InterruptedException {
37 final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
38 final int port = InetSocketAddressUtil.getRandomPort();
39 final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
40 final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
41 this.registry, this.sessionFactory);
42 final ChannelFuture futureServer = serverDispatcher.createServer(serverAddr, this.slf, Optional.<KeyMapping>absent());
43 waitFutureSuccess(futureServer);
44 final ChannelFuture channelFuture = dispatcher.createClient(InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0),
46 waitFutureSuccess(channelFuture);
47 final Channel channel = channelFuture.sync().channel();
49 Assert.assertTrue(channel.isActive());
51 serverDispatcher.close();
55 public void testCreateServer() throws InterruptedException {
56 final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
57 final int port = InetSocketAddressUtil.getRandomPort();
58 final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
59 this.registry, this.sessionFactory);
60 final ChannelFuture futureServer = dispatcher.createServer(new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port));
61 waitFutureSuccess(futureServer);
62 final ChannelFuture channelFuture = serverDispatcher.createClient(InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port), this.slf, Optional.<KeyMapping>absent());
63 waitFutureSuccess(channelFuture);
64 final Channel channel = channelFuture.sync().channel();
66 Assert.assertTrue(channel.isActive());
68 serverDispatcher.close();