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;
28 public class BmpMockDispatcherTest {
30 private final BmpMessageRegistry registry = Mockito.mock(BmpMessageRegistry.class);
31 private final BmpSessionFactory sessionFactory = Mockito.mock(BmpSessionFactory.class);
32 private final BmpSessionListenerFactory slf = Mockito.mock(BmpSessionListenerFactory.class);
35 public void testCreateClient() throws InterruptedException {
36 final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
37 final int port = getRandomPort();
38 final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
39 this.registry, this.sessionFactory);
40 final ChannelFuture futureServer = serverDispatcher.createServer(new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port), this.slf, Optional.<KeyMapping>absent());
41 waitFutureSuccess(futureServer);
42 final ChannelFuture channelFuture = dispatcher.createClient(new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0),
43 new InetSocketAddress(InetAddresses.forString("127.0.0.3"), port));
44 waitFutureSuccess(channelFuture);
45 final Channel channel = channelFuture.sync().channel();
47 Assert.assertTrue(channel.isActive());
49 serverDispatcher.close();
53 public void testCreateServer() throws InterruptedException {
54 final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
55 final int port = getRandomPort();
56 final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(),
57 this.registry, this.sessionFactory);
58 final ChannelFuture futureServer = dispatcher.createServer(new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port));
59 waitFutureSuccess(futureServer);
60 final ChannelFuture channelFuture = serverDispatcher.createClient(new InetSocketAddress(InetAddresses.forString("127.0.0.3"), port), this.slf, Optional.<KeyMapping>absent());
61 waitFutureSuccess(channelFuture);
62 final Channel channel = channelFuture.sync().channel();
64 Assert.assertTrue(channel.isActive());
66 serverDispatcher.close();
69 protected static int getRandomPort() {
70 return (int) (Math.random() * 64000 + 1024);