Do not use RpcService in bgp-benchmark-app
[bgpcep.git] / bgp / benchmark-app / src / test / java / org / opendaylight / protocol / bgp / benchmark / app / AppPeerBenchmarkTest.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.benchmark.app;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
16 import static org.opendaylight.protocol.util.CheckTestUtil.checkNotPresentConfiguration;
17 import static org.opendaylight.protocol.util.CheckTestUtil.readDataConfiguration;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.AddPrefixInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.AddPrefixInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.AddPrefixOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.DeletePrefixInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.DeletePrefixInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.DeletePrefixOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev200120.output.Result;
36 import org.opendaylight.yangtools.concepts.Registration;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40
41 @RunWith(MockitoJUnitRunner.StrictStubs.class)
42 public class AppPeerBenchmarkTest extends AbstractConcurrentDataBrokerTest {
43
44     private static final String PREFIX = "1.1.1.1/32";
45     private static final String NH = "127.0.0.1";
46     private static final String PEER_RIB_ID = "app-peer";
47
48     @Mock
49     private RpcProviderService rpcRegistry;
50     @Mock
51     private Registration registration;
52
53
54     @Before
55     public void setUp() {
56         doReturn(this.registration).when(this.rpcRegistry).registerRpcImplementations(any());
57         doNothing().when(this.registration).close();
58     }
59
60     @Test
61     public void testRpcs() throws Exception {
62         final AppPeerBenchmark appPeerBenchmark = new AppPeerBenchmark(getDataBroker(), this.rpcRegistry, PEER_RIB_ID);
63         appPeerBenchmark.start();
64         final InstanceIdentifier<Ipv4Routes> routesIID = appPeerBenchmark.getIpv4RoutesIID();
65
66         final AddPrefixInput addPrefix = new AddPrefixInputBuilder().setBatchsize(Uint32.ONE).setCount(Uint32.ONE)
67                 .setNexthop(new Ipv4AddressNoZone(NH)).setPrefix(new Ipv4Prefix(PREFIX)).build();
68
69         final RpcResult<AddPrefixOutput> addRpcResult = appPeerBenchmark.addPrefix(addPrefix).get();
70         final Result addResult = addRpcResult.getResult().getResult();
71         checkEquals(() -> assertEquals(1, addResult.getCount().intValue()));
72         checkEquals(() -> assertEquals(1, addResult.getRate().intValue()));
73
74         readDataConfiguration(getDataBroker(), routesIID, routes -> {
75             assertNotNull(routes.getIpv4Route());
76             assertEquals(1, routes.getIpv4Route().size());
77             return routes;
78         });
79
80         final DeletePrefixInput deletePrefix = new DeletePrefixInputBuilder().setBatchsize(Uint32.ONE)
81                 .setCount(Uint32.ONE).setPrefix(new Ipv4Prefix(PREFIX)).build();
82         final RpcResult<DeletePrefixOutput> deleteRpcResult = appPeerBenchmark
83                 .deletePrefix(deletePrefix).get();
84         final Result deleteResult = deleteRpcResult.getResult().getResult();
85         checkEquals(() -> assertEquals(1, deleteResult.getCount().intValue()));
86         checkEquals(() -> assertEquals(1, deleteResult.getRate().intValue()));
87
88         checkNotPresentConfiguration(getDataBroker(), appPeerBenchmark.getIpv4RoutesIID());
89
90         appPeerBenchmark.close();
91     }
92 }