e2813fe6240028cc63306d0e2fd029f098857dc7
[bgpcep.git] / bgp / bmp-mock / src / test / java / org / opendaylight / protocol / bmp / mock / BmpMockDispatcherTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bmp.mock;
10
11 import static org.opendaylight.protocol.bmp.mock.BmpMockTest.waitFutureSuccess;
12
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
28 public class BmpMockDispatcherTest {
29
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);
33
34     @Test
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();
46
47         Assert.assertTrue(channel.isActive());
48         channel.close();
49         serverDispatcher.close();
50     }
51
52     @Test
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();
63
64         Assert.assertTrue(channel.isActive());
65         channel.close();
66         serverDispatcher.close();
67     }
68
69     protected static int getRandomPort() {
70         return (int) (Math.random() * 64000 + 1024);
71     }
72
73 }