BGPCEP-685: Add Peer Release session rpc
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BgpPeerRpcTest.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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import com.google.common.util.concurrent.Futures;
14 import io.netty.channel.ChannelFuture;
15 import java.util.Collections;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.protocol.bgp.rib.spi.PeerRPCs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171027.PeerRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171027.ReleaseConnectionInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171027.ReleaseConnectionInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171027.RouteRefreshRequestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171027.RouteRefreshRequestInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35
36 public final class BgpPeerRpcTest {
37     @Mock
38     private BGPSessionImpl session;
39     @Mock
40     private PeerRPCs peerRpcs;
41     @Mock
42     private PeerRef peer;
43     @Mock
44     private ChannelFuture future;
45     private BgpPeerRpc rpc;
46
47     @Before
48     public void setUp() throws InterruptedException, ExecutionException {
49         MockitoAnnotations.initMocks(this);
50         this.rpc = new BgpPeerRpc(this.peerRpcs, this.session,
51                 Collections.singleton(new TablesKey(Ipv4AddressFamily.class, SubsequentAddressFamily.class)));
52         final ChannelOutputLimiter limiter = new ChannelOutputLimiter(this.session);
53
54         Mockito.doReturn(limiter).when(this.session).getLimiter();
55         Mockito.doReturn(this.future).when(this.session).writeAndFlush(Mockito.any(Notification.class));
56         Mockito.doReturn(true).when(this.future).isDone();
57         Mockito.doReturn(null).when(this.future).get();
58         Mockito.doReturn(true).when(this.future).isSuccess();
59     }
60
61     @Test
62     public void testRouteRefreshRequestSuccessRequest() throws InterruptedException, ExecutionException {
63         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
64                 .setAfi(Ipv4AddressFamily.class)
65                 .setSafi(SubsequentAddressFamily.class)
66                 .setPeerRef(this.peer).build();
67         final Future<RpcResult<Void>> result = this.rpc.routeRefreshRequest(input);
68         assertTrue(result.get().getErrors().isEmpty());
69     }
70
71     @Test
72     public void testRouteRefreshRequestFailedRequest() throws InterruptedException, ExecutionException {
73         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
74                 .setAfi(Ipv6AddressFamily.class)
75                 .setSafi(SubsequentAddressFamily.class)
76                 .setPeerRef(this.peer).build();
77         final Future<RpcResult<Void>> result = this.rpc.routeRefreshRequest(input);
78         assertEquals(1, result.get().getErrors().size());
79         assertEquals("Failed to send Route Refresh message due to unsupported address families.",
80                 result.get().getErrors().iterator().next().getMessage());
81     }
82
83     @Test
84     public void testResetSessionRequestSuccessRequest() throws InterruptedException, ExecutionException {
85         Mockito.doReturn(Futures.immediateFuture(null)).when(this.peerRpcs).releaseConnection();
86         final ReleaseConnectionInput input = new ReleaseConnectionInputBuilder()
87                 .setPeerRef(this.peer).build();
88         final Future<RpcResult<Void>> result = this.rpc.releaseConnection(input);
89         assertTrue(result.get().getErrors().isEmpty());
90     }
91 }