17c801c15da422ef5d607ea5f1eaf19f62fba59d
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImplTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10
11 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Sets;
15 import io.netty.channel.Channel;
16 import io.netty.util.concurrent.Future;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.rib.spi.State;
22 import org.opendaylight.protocol.util.InetSocketAddressUtil;
23
24 public class BGPDispatcherImplTest extends AbstractBGPDispatcherTest {
25     @Test
26     public void testCreateClient() throws InterruptedException, ExecutionException {
27         final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
28         final Channel serverChannel = createServer(serverAddress);
29         final Future<BGPSessionImpl> futureClient = this.clientDispatcher.createClient(this.clientAddress, serverAddress, this.registry, 2, true);
30         waitFutureSuccess(futureClient);
31         final BGPSessionImpl session = futureClient.get();
32         Assert.assertEquals(State.UP, this.clientListener.getState());
33         Assert.assertEquals(State.UP, this.serverListener.getState());
34         Assert.assertEquals(AS_NUMBER, session.getAsNumber());
35         Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
36         Assert.assertTrue(serverChannel.isWritable());
37         session.close();
38         this.serverListener.releaseConnection();
39         checkIdleState(this.clientListener);
40         checkIdleState(this.serverListener);
41     }
42
43     @Test
44     public void testCreateReconnectingClient() throws Exception {
45         final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
46         final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent(),
47             this.clientAddress, true);
48         waitFutureSuccess(future);
49         final Channel serverChannel = createServer(serverAddress);
50         Assert.assertEquals(State.UP, this.serverListener.getState());
51         Assert.assertTrue(serverChannel.isWritable());
52         future.cancel(true);
53         this.serverListener.releaseConnection();
54         checkIdleState(this.serverListener);
55     }
56
57 }