Code Clean Up
[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
36 public class BmpDispatcherImplTest {
37
38     private static final int PORT = 45678;
39     private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
40     private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
41
42     private BmpDispatcher dispatcher;
43     private BGPActivator bgpActivator;
44     private BmpActivator bmpActivator;
45
46     @Mock
47     private BmpSession mockedSession;
48     @Mock
49     private BmpSessionListenerFactory mockedListenerFactory;
50     @Mock
51     private ChannelHandler mockedChannelHandler;
52
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         Mockito.doNothing().when(this.mockedSession).handlerRemoved(Mockito.any(ChannelHandlerContext.class));
57         Mockito.doNothing().when(this.mockedSession).handlerAdded(Mockito.any(ChannelHandlerContext.class));
58         Mockito.doNothing().when(this.mockedSession).channelRegistered(Mockito.any(ChannelHandlerContext.class));
59         Mockito.doNothing().when(this.mockedSession).channelActive(Mockito.any(ChannelHandlerContext.class));
60         Mockito.doNothing().when(this.mockedSession).channelInactive(Mockito.any(ChannelHandlerContext.class));
61         Mockito.doNothing().when(this.mockedSession).channelUnregistered(Mockito.any(ChannelHandlerContext.class));
62         Mockito.doNothing().when(this.mockedSession).channelReadComplete(Mockito.any(ChannelHandlerContext.class));
63
64         this.bgpActivator = new BGPActivator();
65         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
66         this.bgpActivator.start(context);
67         final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
68         this.bmpActivator = new BmpActivator(context);
69         this.bmpActivator.start(ctx);
70         final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
71
72         this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), messageRegistry, (channel, sessionListenerFactory) -> BmpDispatcherImplTest.this.mockedSession);
73     }
74
75     @After
76     public void tearDown() throws Exception {
77         this.bgpActivator.close();
78         this.bmpActivator.close();
79         this.dispatcher.close();
80     }
81
82     @Test
83     public void testCreateServer() throws Exception {
84         final Channel serverChannel = this.dispatcher.createServer(SERVER, this.mockedListenerFactory, Optional.absent()).await().channel();
85         Assert.assertTrue(serverChannel.isActive());
86         final Channel clientChannel = this.dispatcher.createClient(CLIENT_REMOTE, this.mockedListenerFactory, Optional.absent()).await().channel();
87         Assert.assertTrue(clientChannel.isActive());
88         Thread.sleep(500);
89         Mockito.verify(this.mockedSession, Mockito.times(2)).handlerAdded(Mockito.any(ChannelHandlerContext.class));
90         Mockito.verify(this.mockedSession, Mockito.times(2)).channelRegistered(Mockito.any(ChannelHandlerContext.class));
91         Mockito.verify(this.mockedSession, Mockito.times(2)).channelActive(Mockito.any(ChannelHandlerContext.class));
92         clientChannel.close().get();
93         serverChannel.close().get();
94     }
95 }