2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
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.rev171207.PeerRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171207.ResetSessionInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171207.ResetSessionInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171207.RouteRefreshRequestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev171207.RouteRefreshRequestInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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;
36 public final class BgpPeerRpcTest {
38 private BGPSessionImpl session;
40 private PeerRPCs peerRpcs;
44 private ChannelFuture future;
45 private BgpPeerRpc rpc;
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);
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();
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());
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());
84 public void testResetSessionRequestSuccessRequest() throws InterruptedException, ExecutionException {
85 Mockito.doReturn(Futures.immediateFuture(null)).when(this.peerRpcs).releaseConnection();
86 final ResetSessionInput input = new ResetSessionInputBuilder()
87 .setPeerRef(this.peer).build();
88 final Future<RpcResult<Void>> result = this.rpc.resetSession(input);
89 assertTrue(result.get().getErrors().isEmpty());