Adjust to RPC method signature update
[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.rev180329.PeerRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
35 import org.opendaylight.yangtools.yang.binding.Notification;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37
38 public final class BgpPeerRpcTest {
39     @Mock
40     private BGPSessionImpl session;
41     @Mock
42     private PeerRPCs peerRpcs;
43     @Mock
44     private PeerRef peer;
45     @Mock
46     private ChannelFuture future;
47     private BgpPeerRpc rpc;
48
49     @Before
50     public void setUp() throws InterruptedException, ExecutionException {
51         MockitoAnnotations.initMocks(this);
52         this.rpc = new BgpPeerRpc(this.peerRpcs, this.session,
53                 Collections.singleton(new TablesKey(Ipv4AddressFamily.class, SubsequentAddressFamily.class)));
54         final ChannelOutputLimiter limiter = new ChannelOutputLimiter(this.session);
55
56         Mockito.doReturn(limiter).when(this.session).getLimiter();
57         Mockito.doReturn(this.future).when(this.session).writeAndFlush(Mockito.any(Notification.class));
58         Mockito.doReturn(true).when(this.future).isDone();
59         Mockito.doReturn(null).when(this.future).get();
60         Mockito.doReturn(true).when(this.future).isSuccess();
61     }
62
63     @Test
64     public void testRouteRefreshRequestSuccessRequest() throws InterruptedException, ExecutionException {
65         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
66                 .setAfi(Ipv4AddressFamily.class)
67                 .setSafi(SubsequentAddressFamily.class)
68                 .setPeerRef(this.peer).build();
69         final Future<RpcResult<RouteRefreshRequestOutput>> result = this.rpc.routeRefreshRequest(input);
70         assertTrue(result.get().getErrors().isEmpty());
71     }
72
73     @Test
74     public void testRouteRefreshRequestFailedRequest() throws InterruptedException, ExecutionException {
75         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
76                 .setAfi(Ipv6AddressFamily.class)
77                 .setSafi(SubsequentAddressFamily.class)
78                 .setPeerRef(this.peer).build();
79         final Future<RpcResult<RouteRefreshRequestOutput>> result = this.rpc.routeRefreshRequest(input);
80         assertEquals(1, result.get().getErrors().size());
81         assertEquals("Failed to send Route Refresh message due to unsupported address families.",
82                 result.get().getErrors().iterator().next().getMessage());
83     }
84
85     @Test
86     public void testResetSessionRequestSuccessRequest() throws InterruptedException, ExecutionException {
87         Mockito.doReturn(Futures.immediateFuture(null)).when(this.peerRpcs).releaseConnection();
88         final ResetSessionInput input = new ResetSessionInputBuilder()
89                 .setPeerRef(this.peer).build();
90         final Future<RpcResult<ResetSessionOutput>> result = this.rpc.resetSession(input);
91         assertTrue(result.get().getErrors().isEmpty());
92     }
93 }