82463e25a31b66ca15ae4c00735d614148df75bd
[bgpcep.git] / bgp / bmp-impl / src / test / java / org / opendaylight / protocol / bmp / impl / session / BmpDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 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.impl.session;
10
11 import com.google.common.base.Optional;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelHandler;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.net.InetSocketAddress;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
25 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
27 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
28 import org.opendaylight.protocol.bmp.api.BmpSession;
29 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
30 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
31 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
32 import org.opendaylight.protocol.bmp.parser.BmpActivator;
33 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
34 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
35 import org.opendaylight.protocol.concepts.KeyMapping;
36
37 public class BmpDispatcherImplTest {
38
39     private static final int PORT = 45678;
40     private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
41     private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
42
43     private BmpDispatcher dispatcher;
44     private BGPActivator bgpActivator;
45     private BmpActivator bmpActivator;
46
47     @Mock
48     private BmpSession mockedSession;
49     @Mock
50     private BmpSessionListenerFactory mockedListenerFactory;
51     @Mock
52     private ChannelHandler mockedChannelHandler;
53
54     @Before
55     public void setUp() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         Mockito.doNothing().when(this.mockedSession).handlerRemoved(Mockito.any(ChannelHandlerContext.class));
58         Mockito.doNothing().when(this.mockedSession).handlerAdded(Mockito.any(ChannelHandlerContext.class));
59         Mockito.doNothing().when(this.mockedSession).channelRegistered(Mockito.any(ChannelHandlerContext.class));
60         Mockito.doNothing().when(this.mockedSession).channelActive(Mockito.any(ChannelHandlerContext.class));
61         Mockito.doNothing().when(this.mockedSession).channelInactive(Mockito.any(ChannelHandlerContext.class));
62         Mockito.doNothing().when(this.mockedSession).channelUnregistered(Mockito.any(ChannelHandlerContext.class));
63         Mockito.doNothing().when(this.mockedSession).channelReadComplete(Mockito.any(ChannelHandlerContext.class));
64
65         this.bgpActivator = new BGPActivator();
66         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
67         this.bgpActivator.start(context);
68         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
69         this.bmpActivator = new BmpActivator(context);
70         this.bmpActivator.start(ctx);
71         final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
72
73         this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), messageRegistry, new BmpSessionFactory() {
74             @Override
75             public BmpSession getSession(final Channel channel,
76                     final BmpSessionListenerFactory sessionListenerFactory) {
77                 return BmpDispatcherImplTest.this.mockedSession;
78             }
79         });
80     }
81
82     @After
83     public void tearDown() throws Exception {
84         this.bgpActivator.close();
85         this.bmpActivator.close();
86         this.dispatcher.close();
87     }
88
89     @Test
90     public void testCreateServer() throws Exception {
91         final Channel serverChannel = this.dispatcher.createServer(SERVER, this.mockedListenerFactory, Optional.<KeyMapping>absent()).await().channel();
92         Assert.assertTrue(serverChannel.isActive());
93         final Channel clientChannel = this.dispatcher.createClient(CLIENT_REMOTE, this.mockedListenerFactory, Optional.<KeyMapping>absent()).await().channel();
94         Assert.assertTrue(clientChannel.isActive());
95         Thread.sleep(500);
96         Mockito.verify(this.mockedSession, Mockito.times(2)).handlerAdded(Mockito.any(ChannelHandlerContext.class));
97         Mockito.verify(this.mockedSession, Mockito.times(2)).channelRegistered(Mockito.any(ChannelHandlerContext.class));
98         Mockito.verify(this.mockedSession, Mockito.times(2)).channelActive(Mockito.any(ChannelHandlerContext.class));
99         clientChannel.close().get();
100         serverChannel.close().get();
101     }
102 }