Fix sonar complains
[bgpcep.git] / bgp / 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
9 package org.opendaylight.protocol.bmp.mock;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.Lists;
16 import io.netty.channel.Channel;
17 import io.netty.channel.ChannelFuture;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.ChannelPipeline;
20 import java.net.InetSocketAddress;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.protocol.util.InetSocketAddressUtil;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.InitiationMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.RouteMonitoringMessage;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31
32 public class BmpMockSessionTest {
33
34     private static final InetSocketAddress REMOTE_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
35     private static final InetSocketAddress LOCAL_ADDRESS = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
36
37     private ChannelHandlerContext context;
38     private Channel channel;
39     private BmpMockSession session;
40     private List<Notification> messages;
41
42     @Before
43     public void setUp() {
44         MockitoAnnotations.initMocks(this);
45         this.messages = Lists.newArrayList();
46         this.session = new BmpMockSession(1, 1, 1);
47         this.channel = Mockito.mock(Channel.class);
48         Mockito.doReturn(REMOTE_ADDRESS).when(this.channel).remoteAddress();
49         Mockito.doReturn(LOCAL_ADDRESS).when(this.channel).localAddress();
50         final ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
51         Mockito.doReturn(pipeline).when(this.channel).pipeline();
52         Mockito.doAnswer(invocation -> {
53             messages.add((Notification) invocation.getArguments()[0]);
54             return null;
55         }).when(this.channel).writeAndFlush(Mockito.any());
56         final ChannelFuture channelFuture = Mockito.mock(ChannelFuture.class);
57         Mockito.doReturn(null).when(channelFuture).addListener(Mockito.any());
58         Mockito.doReturn(channelFuture).when(this.channel).closeFuture();
59         Mockito.doReturn(channelFuture).when(this.channel).close();
60         this.context = Mockito.mock(ChannelHandlerContext.class);
61         Mockito.doReturn(this.channel).when(this.context).channel();
62     }
63
64     @Test
65     public void testBmpMockSession() throws Exception {
66         this.session.channelActive(this.context);
67
68         assertEquals(REMOTE_ADDRESS.getAddress(), this.session.getRemoteAddress());
69         assertTrue(this.messages.get(0) instanceof InitiationMessage);
70         assertTrue(this.messages.get(1) instanceof PeerUp);
71         assertTrue(this.messages.get(2) instanceof RouteMonitoringMessage);
72         assertTrue(this.messages.get(3) instanceof RouteMonitoringMessage);
73
74         this.session.close();
75         assertFalse(this.channel.isWritable());
76     }
77
78 }