Bump versions to 0.21.8-SNAPSHOT
[bgpcep.git] / bmp / bmp-mock / src / test / java / org / opendaylight / protocol / bmp / mock / BmpMockSessionTest.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 package org.opendaylight.protocol.bmp.mock;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelHandlerContext;
21 import java.net.InetSocketAddress;
22 import java.util.List;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.protocol.util.InetSocketAddressUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.InitiationMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.PeerUp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.RouteMonitoringMessage;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class BmpMockSessionTest {
35     private static final InetSocketAddress REMOTE_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
36     private static final InetSocketAddress LOCAL_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
37
38     private final BmpMockSession session = new BmpMockSession(1, 1, 1);
39
40     @Mock
41     private ChannelHandlerContext context;
42     @Mock
43     private Channel channel;
44
45     @Test
46     public void testBmpMockSession() throws InterruptedException {
47         doReturn(REMOTE_ADDRESS).when(channel).remoteAddress();
48         doReturn(LOCAL_ADDRESS).when(channel).localAddress();
49
50         final ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
51         doReturn(null).when(channel).writeAndFlush(captor.capture());
52
53         final ChannelFuture channelFuture = mock(ChannelFuture.class);
54         doReturn(null).when(channelFuture).addListener(any());
55         doReturn(channelFuture).when(channel).closeFuture();
56         doReturn(channelFuture).when(channel).close();
57         doReturn(channel).when(context).channel();
58
59         session.channelActive(context);
60
61         assertEquals(REMOTE_ADDRESS.getAddress(), session.getRemoteAddress());
62
63         final List<Object> messages = captor.getAllValues();
64         assertEquals(4, messages.size());
65         assertThat(messages.get(0), instanceOf(InitiationMessage.class));
66         assertThat(messages.get(1), instanceOf(PeerUp.class));
67         assertThat(messages.get(2), instanceOf(RouteMonitoringMessage.class));
68         assertThat(messages.get(3), instanceOf(RouteMonitoringMessage.class));
69
70         session.close();
71         assertFalse(channel.isWritable());
72     }
73 }