Bug 5030: BGP Benchmark app - unit tests 21/36721/1
authorMilos Fabian <milfabia@cisco.com>
Fri, 18 Mar 2016 13:19:49 +0000 (14:19 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 24 Mar 2016 09:40:14 +0000 (09:40 +0000)
Added unit tests for implementation.

Change-Id: I69f76aee8c1d1b2364796cc47501230f65d94ac2
Signed-off-by: Milos Fabian <milfabia@cisco.com>
bgp/benchmark-app/pom.xml
bgp/benchmark-app/src/main/java/org/opendaylight/controller/config/yang/odl_bgp_benchmark_cfg/AppPeerBenchmarkModule.java
bgp/benchmark-app/src/main/java/org/opendaylight/protocol/bgp/benchmark/app/AppPeerBenchmark.java
bgp/benchmark-app/src/test/java/org/opendaylight/controller/config/yang/odl_bgp_benchmark_cfg/AppPeerBenchmarkModuleTest.java [new file with mode: 0644]
bgp/benchmark-app/src/test/java/org/opendaylight/protocol/bgp/benchmark/app/AppPeerBenchmarkTest.java [new file with mode: 0644]

index 0b1d529f85796afb2d59f300b7cfe536aa2dc908..aaed264bf6b53c08cb355537a278462f1ae66f2b 100644 (file)
             <groupId>org.opendaylight.controller</groupId>
             <artifactId>sal-binding-config</artifactId>
         </dependency>
+        <!-- test dependencies -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.controller</groupId>
+            <artifactId>sal-binding-broker-impl</artifactId>
+            <scope>test</scope>
+            <type>test-jar</type>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
index 385205997c62467ab5ede7537ce6c727428fb68d..00892fdb582a5071ccef7148382651b76ab88b46 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.config.yang.odl_bgp_benchmark_cfg;
 
 import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.JmxAttributeValidationException;
 import org.opendaylight.controller.config.api.ModuleIdentifier;
 import org.opendaylight.protocol.bgp.benchmark.app.AppPeerBenchmark;
 
@@ -23,7 +24,7 @@ public class AppPeerBenchmarkModule extends AbstractAppPeerBenchmarkModule {
 
     @Override
     public void customValidation() {
-        // add custom validation form module attributes here.
+        JmxAttributeValidationException.checkNotNull(getAppRibId(), "value is not set.", appRibIdJmxAttribute);
     }
 
     @Override
index f580b3d814425d0631537189731af8754e0a4d64..1fb5546b276ee9a69a8e03353ddbc50c046f14c8 100644 (file)
@@ -102,6 +102,7 @@ public class AppPeerBenchmark implements OdlBgpAppPeerBenchmarkService, Transact
             final Throwable cause) {
         LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}", chain,
                 transaction.getIdentifier(), cause);
+        close();
     }
 
     @Override
@@ -166,10 +167,10 @@ public class AppPeerBenchmark implements OdlBgpAppPeerBenchmarkService, Transact
         LOG.info("BGP Application Peer Benchmark Application closed.");
     }
 
-    private long addRoute(final Ipv4Prefix ipv4Prefix, final Ipv4Address ipv4Address, final long count, final long batch) {
+    private long addRoute(final Ipv4Prefix ipv4Prefix, final Ipv4Address nextHop, final long count, final long batch) {
         final AttributesBuilder attributesBuilder = new AttributesBuilder();
         attributesBuilder.setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(
-                new Ipv4NextHopBuilder().setGlobal(new Ipv4Address(ipv4Address)).build()).build());
+                new Ipv4NextHopBuilder().setGlobal(new Ipv4Address(nextHop)).build()).build());
         attributesBuilder.setMultiExitDisc(MED);
         attributesBuilder.setLocalPref(LOC_PREF);
         attributesBuilder.setOrigin(ORIGIN);
diff --git a/bgp/benchmark-app/src/test/java/org/opendaylight/controller/config/yang/odl_bgp_benchmark_cfg/AppPeerBenchmarkModuleTest.java b/bgp/benchmark-app/src/test/java/org/opendaylight/controller/config/yang/odl_bgp_benchmark_cfg/AppPeerBenchmarkModuleTest.java
new file mode 100644 (file)
index 0000000..988d707
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.config.yang.odl_bgp_benchmark_cfg;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.JmxAttribute;
+import org.opendaylight.controller.config.api.JmxAttributeValidationException;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.protocol.bgp.benchmark.app.AppPeerBenchmark;
+import org.osgi.framework.BundleContext;
+
+public class AppPeerBenchmarkModuleTest extends AbstractDataBrokerTest {
+
+    private static final String INSTANCE_NAME = "instance";
+
+    @Mock
+    private DependencyResolver dependencyResolver;
+    @Mock
+    private BundleContext bundleContextON;
+    @Mock
+    private ObjectName bindingDataBrokerON;
+    @Mock
+    private ObjectName rpcRegistryON;
+    @Mock
+    private RpcProviderRegistry rpcRegistry;
+
+    @Before
+    public void setUp() throws MalformedObjectNameException {
+        MockitoAnnotations.initMocks(this);
+        Mockito.doReturn(this.rpcRegistry).when(this.dependencyResolver).resolveInstance(Matchers.eq(RpcProviderRegistry.class), Mockito.any(ObjectName.class), Mockito.any(JmxAttribute.class));
+        Mockito.doReturn(getDataBroker()).when(this.dependencyResolver).resolveInstance(Matchers.eq(DataBroker.class), Mockito.any(ObjectName.class), Mockito.any(JmxAttribute.class));
+    }
+
+    @Test
+    public void testCreateInstance() {
+        final AppPeerBenchmarkModule module = (AppPeerBenchmarkModule) new AppPeerBenchmarkModuleFactory().createModule(INSTANCE_NAME,
+                this.dependencyResolver, this.bundleContextON);
+        module.setAppRibId("appRib");
+        module.setBindingDataBroker(this.bindingDataBrokerON);
+        module.setRpcRegistry(this.rpcRegistryON);
+        module.resolveDependencies();
+        module.customValidation();
+        final AutoCloseable instance = module.createInstance();
+        Assert.assertTrue(instance instanceof AppPeerBenchmark);
+    }
+
+    @Test(expected=JmxAttributeValidationException.class)
+    public void testCustomValidation() {
+        final AppPeerBenchmarkModule module = (AppPeerBenchmarkModule) new AppPeerBenchmarkModuleFactory().createModule(INSTANCE_NAME,
+                this.dependencyResolver, this.bundleContextON);
+        module.setAppRibId(null);
+        module.customValidation();
+    }
+
+}
diff --git a/bgp/benchmark-app/src/test/java/org/opendaylight/protocol/bgp/benchmark/app/AppPeerBenchmarkTest.java b/bgp/benchmark-app/src/test/java/org/opendaylight/protocol/bgp/benchmark/app/AppPeerBenchmarkTest.java
new file mode 100644 (file)
index 0000000..d0ba3e8
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.benchmark.app;
+
+import java.util.concurrent.ExecutionException;
+import javax.management.MalformedObjectNameException;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRib;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.AddPrefixInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.AddPrefixOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.DeletePrefixInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.DeletePrefixOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.OdlBgpAppPeerBenchmarkService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.app.peer.benchmark.rev160309.output.Result;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+public class AppPeerBenchmarkTest extends AbstractDataBrokerTest {
+
+    private static final String PREFIX = "1.1.1.1/32";
+    private static final String NH = "127.0.0.1";
+    private static final String PEER_RIB_ID = "app-peer";
+
+    private static final InstanceIdentifier<ApplicationRib> BASE_IID = KeyedInstanceIdentifier.builder(ApplicationRib.class,
+            new ApplicationRibKey(new ApplicationRibId(PEER_RIB_ID))).build();
+    private static final InstanceIdentifier tablesIId = BASE_IID
+            .child(Tables.class, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
+    private static final InstanceIdentifier<Ipv4Routes> ROUTES = tablesIId.child(Ipv4Routes.class);
+
+    @Mock
+    private RpcProviderRegistry rpcRegistry;
+    @Mock
+    private RoutedRpcRegistration<OdlBgpAppPeerBenchmarkService> registration;
+
+    private AppPeerBenchmark appPeerBenchmark;
+
+    @Before
+    public void setUp() throws MalformedObjectNameException {
+        MockitoAnnotations.initMocks(this);
+        Mockito.doReturn(this.registration).when(this.rpcRegistry).addRpcImplementation(Mockito.<Class<OdlBgpAppPeerBenchmarkService>>any(),
+                Mockito.any(OdlBgpAppPeerBenchmarkService.class));
+        Mockito.doNothing().when(this.registration).close();
+        this.appPeerBenchmark = new AppPeerBenchmark(getDataBroker(), this.rpcRegistry, PEER_RIB_ID);
+    }
+
+    @Test
+    public void testRpcs() throws InterruptedException, ExecutionException, ReadFailedException {
+        final RpcResult<AddPrefixOutput> addRpcResult = this.appPeerBenchmark.addPrefix(new AddPrefixInputBuilder()
+            .setBatchsize(1L)
+            .setCount(1L)
+            .setNexthop(new Ipv4Address(NH))
+            .setPrefix(new Ipv4Prefix(PREFIX)).build()).get();
+        final Result addResult = addRpcResult.getResult().getResult();
+        Assert.assertEquals(1, addResult.getCount().intValue());
+        Assert.assertEquals(1, addResult.getRate().intValue());
+        final Ipv4Routes routesAfterAdd = getRoutes();
+        Assert.assertEquals(1, routesAfterAdd.getIpv4Route().size());
+
+        final RpcResult<DeletePrefixOutput> deleteRpcResult = this.appPeerBenchmark.deletePrefix(new DeletePrefixInputBuilder()
+            .setBatchsize(1L)
+            .setCount(1L)
+            .setPrefix(new Ipv4Prefix(PREFIX)).build()).get();
+        final Result deleteResult = deleteRpcResult.getResult().getResult();
+        Assert.assertEquals(1, deleteResult.getCount().intValue());
+        Assert.assertEquals(1, deleteResult.getRate().intValue());
+        final Ipv4Routes routesAfterDelete = getRoutes();
+        Assert.assertTrue(routesAfterDelete.getIpv4Route().isEmpty());
+    }
+
+    @After
+    public void tearDown() {
+        this.appPeerBenchmark.close();
+    }
+
+    private Ipv4Routes getRoutes() throws ReadFailedException {
+        final ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction();
+        return rTx.read(LogicalDatastoreType.CONFIGURATION, ROUTES).checkedGet().get();
+    }
+
+}