Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 import io.netty.channel.ChannelFuture;
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.PeerRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.RouteRefreshRequestInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.RouteRefreshRequestInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29
30 public class BgpPeerRpcTest {
31
32     private final Set<TablesKey> supportedFamilies = new HashSet<TablesKey>();
33     private final BGPSessionImpl session = Mockito.mock(BGPSessionImpl.class);
34     private final BgpPeerRpc rpc = new BgpPeerRpc(this.session, this.supportedFamilies);
35     private final PeerRef peer = Mockito.mock(PeerRef.class);
36
37     private final ChannelOutputLimiter limiter = new ChannelOutputLimiter(this.session);
38     private final ChannelFuture future = Mockito.mock(ChannelFuture.class);
39
40     @Before
41     public void setUp() throws InterruptedException, ExecutionException {
42         this.supportedFamilies.add(new TablesKey(Ipv4AddressFamily.class, SubsequentAddressFamily.class));
43
44         Mockito.doReturn(this.limiter).when(this.session).getLimiter();
45         Mockito.doReturn(this.future).when(this.session).writeAndFlush(Mockito.any(Notification.class));
46         Mockito.doReturn(true).when(this.future).isDone();
47         Mockito.doReturn(null).when(this.future).get();
48         Mockito.doReturn(true).when(this.future).isSuccess();
49     }
50
51     @Test
52     public void testSuccessRequest() throws InterruptedException, ExecutionException {
53         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
54             .setAfi(Ipv4AddressFamily.class)
55             .setSafi(SubsequentAddressFamily.class)
56             .setPeerRef(this.peer).build();
57         final Future<RpcResult<Void>> result = this.rpc.routeRefreshRequest(input);
58         assertTrue(result.get().getErrors().isEmpty());
59     }
60
61     @Test
62     public void testFailedRequest() throws InterruptedException, ExecutionException {
63         final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
64             .setAfi(Ipv6AddressFamily.class)
65             .setSafi(SubsequentAddressFamily.class)
66             .setPeerRef(this.peer).build();
67         final Future<RpcResult<Void>> result = this.rpc.routeRefreshRequest(input);
68         assertEquals(1, result.get().getErrors().size());
69         assertEquals("Failed to send Route Refresh message due to unsupported address families.", result.get().getErrors().iterator().next().getMessage());
70     }
71 }