Bug 4837 - BMP test tool
[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.ChannelHandlerContext;
18 import io.netty.channel.ChannelOutboundHandlerAdapter;
19 import io.netty.channel.ChannelPromise;
20 import io.netty.channel.embedded.EmbeddedChannel;
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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.InitiationMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.RouteMonitoringMessage;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30
31 public class BmpMockSessionTest {
32
33     private static final InetSocketAddress REMOTE_ADDRESS = new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 0);
34     private static final InetSocketAddress LOCAL_ADDRESS = new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0);
35
36     private ChannelHandlerContext context;
37     private EmbeddedChannel channel;
38     private BmpMockSession session;
39
40     @Before
41     public void setUp() {
42         this.session = new BmpMockSession(1, 1, 1);
43         this.channel = Mockito.spy(new EmbeddedChannel());
44         Mockito.doReturn(REMOTE_ADDRESS).when(this.channel).remoteAddress();
45         Mockito.doReturn(LOCAL_ADDRESS).when(this.channel).localAddress();
46         this.channel.pipeline().addLast(this.session);
47         this.context = Mockito.mock(ChannelHandlerContext.class);
48         Mockito.doReturn(this.channel).when(this.context).channel();
49     }
50
51     @Test
52     public void testBmpMockSession() throws Exception {
53         final List<Notification> messages = Lists.newArrayList();
54         this.channel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
55             @Override
56             public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
57                 messages.add((Notification) msg);
58             }
59         });
60         this.session.channelActive(this.context);
61
62         assertEquals(REMOTE_ADDRESS.getAddress(), this.session.getRemoteAddress());
63         assertTrue(messages.get(0) instanceof InitiationMessage);
64         assertTrue(messages.get(1) instanceof PeerUp);
65         assertTrue(messages.get(2) instanceof RouteMonitoringMessage);
66         assertTrue(messages.get(3) instanceof RouteMonitoringMessage);
67
68         this.session.close();
69         assertFalse(this.channel.isWritable());
70     }
71
72 }