BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCDispatcherImplTest.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.pcep.pcc.mock;
10
11 import static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
12 import static org.opendaylight.protocol.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
13
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.protocol.pcep.PCEPCapability;
28 import org.opendaylight.protocol.pcep.PCEPDispatcher;
29 import org.opendaylight.protocol.pcep.PCEPSession;
30 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
31 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
32 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
33 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
34 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
35 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
36 import org.opendaylight.protocol.util.InetSocketAddressUtil;
37
38 public class PCCDispatcherImplTest {
39
40     private static final List<PCEPCapability> CAPS = new ArrayList<>();
41     private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(30, 120, CAPS);
42     private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0);
43     private PCCDispatcherImpl dispatcher;
44     private PCEPDispatcher pcepDispatcher;
45     private InetSocketAddress serverAddress;
46     private InetSocketAddress clientAddress;
47     private EventLoopGroup workerGroup;
48     private EventLoopGroup bossGroup;
49
50     @Before
51     public void setUp() {
52         this.workerGroup = new NioEventLoopGroup();
53         this.bossGroup = new NioEventLoopGroup();
54         this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
55         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
56             this.nf, this.bossGroup, this.workerGroup);
57         this.serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
58         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
59     }
60
61     @After
62     public void tearDown() throws InterruptedException, ExecutionException {
63         this.dispatcher.close();
64         closeEventLoopGroups();
65     }
66
67     private void closeEventLoopGroups() throws ExecutionException, InterruptedException {
68         this.workerGroup.shutdownGracefully().get();
69         this.bossGroup.shutdownGracefully().get();
70     }
71
72     @Test
73     public void testClientReconnect() throws Exception {
74         final Future<PCEPSession> futureSession = this.dispatcher.createClient(this.serverAddress, 1, new TestingSessionListenerFactory(),
75             this.nf, null, this.clientAddress);
76         waitFutureSuccess(futureSession);
77         final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
78         final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.serverAddress, slf, null);
79         waitFutureSuccess(futureServer);
80         final Channel channel = futureServer.channel();
81         Assert.assertNotNull(futureSession.get());
82         checkSessionListenerNotNull(slf, this.clientAddress.getHostString());
83         final TestingSessionListener sl = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress());
84         Assert.assertNotNull(sl.getSession());
85         Assert.assertTrue(sl.isUp());
86         channel.close().get();
87         closeEventLoopGroups();
88
89         this.workerGroup = new NioEventLoopGroup();
90         this.bossGroup = new NioEventLoopGroup();
91         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
92             this.nf, this.bossGroup, this.workerGroup);
93
94         final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
95         final ChannelFuture future2 = this.pcepDispatcher.createServer(this.serverAddress, slf2, null);
96         waitFutureSuccess(future2);
97         final Channel channel2 = future2.channel();
98         final TestingSessionListener sl2 = checkSessionListenerNotNull(slf2, this.clientAddress.getAddress().getHostAddress());
99         Assert.assertNotNull(sl2.getSession());
100         Assert.assertTrue(sl2.isUp());
101     }
102 }