BGPCEP-737: Implement BMP client reconnection
[bgpcep.git] / bmp / 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.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
15 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
16
17 import com.google.common.net.InetAddresses;
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.nio.NioEventLoopGroup;
21 import java.net.InetSocketAddress;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
27 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
28 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
29 import org.opendaylight.protocol.bmp.impl.session.DefaultBmpSessionFactory;
30 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
31 import org.opendaylight.protocol.concepts.KeyMapping;
32 import org.opendaylight.protocol.util.InetSocketAddressUtil;
33
34 public class BmpMockDispatcherTest {
35
36     private final BmpSessionFactory sessionFactory = new DefaultBmpSessionFactory();
37     private final BmpMockSessionListener sl = new BmpMockSessionListener();
38     @Mock
39     private BmpMessageRegistry registry;
40     @Mock
41     private BmpSessionListenerFactory slf;
42     private BmpMockDispatcher bmpMockDispatcher;
43
44     @Before
45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47         doReturn(this.sl).when(this.slf).getSessionListener();
48         this.bmpMockDispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
49     }
50
51     @Test
52     public void testCreateClient() throws Exception {
53         final int port = InetSocketAddressUtil.getRandomPort();
54         final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
55
56         final BmpDispatcherImpl bmpDispatcher = new BmpDispatcherImpl(
57                 new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
58         final ChannelFuture futureServer = bmpDispatcher
59                 .createServer(serverAddr, this.slf, KeyMapping.getKeyMapping());
60         waitFutureSuccess(futureServer);
61
62         final ChannelFuture channelFuture = this.bmpMockDispatcher.createClient(InetSocketAddressUtil
63                 .getRandomLoopbackInetSocketAddress(0), serverAddr);
64         waitFutureSuccess(channelFuture);
65         final Channel channel = channelFuture.sync().channel();
66
67         assertTrue(channel.isActive());
68         checkEquals(() -> assertTrue(this.sl.getStatus()));
69         channel.close();
70         bmpDispatcher.close();
71         checkEquals(() -> assertFalse(this.sl.getStatus()));
72
73         final BmpDispatcherImpl bmpDispatcher2 = new BmpDispatcherImpl(
74                 new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
75         final ChannelFuture futureServer2 = bmpDispatcher2
76                 .createServer(serverAddr, this.slf, KeyMapping.getKeyMapping());
77         waitFutureSuccess(futureServer2);
78         checkEquals(() -> assertTrue(this.sl.getStatus()));
79
80         bmpDispatcher2.close();
81         this.bmpMockDispatcher.close();
82         checkEquals(() -> assertFalse(this.sl.getStatus()));
83     }
84
85     @Test
86     public void testCreateServer() throws Exception {
87         final int port = InetSocketAddressUtil.getRandomPort();
88         final BmpDispatcherImpl bmpDispatcher = new BmpDispatcherImpl(
89                 new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
90         final ChannelFuture futureServer = this.bmpMockDispatcher.createServer(
91                 new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port));
92         waitFutureSuccess(futureServer);
93         final ChannelFuture channelFuture = bmpDispatcher.createClient(
94                 InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port), this.slf, KeyMapping.getKeyMapping());
95         waitFutureSuccess(channelFuture);
96         final Channel channel = channelFuture.sync().channel();
97
98         assertTrue(channel.isActive());
99         checkEquals(() -> assertTrue(this.sl.getStatus()));
100         assertTrue(futureServer.channel().isActive());
101         channel.close();
102
103         bmpDispatcher.close();
104         this.bmpMockDispatcher.close();
105         checkEquals(() -> assertFalse(this.sl.getStatus()));
106     }
107
108 }