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