Bug-730: More tests for testing tools. 97/10597/4
authorMilos Fabian <milfabia@cisco.com>
Mon, 1 Sep 2014 14:03:52 +0000 (16:03 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 1 Sep 2014 19:14:34 +0000 (19:14 +0000)
Change-Id: I0e467f5cf0c43e4ecbf54d853438a432381ab917
Signed-off-by: Milos Fabian <milfabia@cisco.com>
data-change-counter/pom.xml
data-change-counter/src/test/java/org/opendaylight/controller/config/yang/bgpcep/data/change/counter/DataChangeCounterImplModuleTest.java [new file with mode: 0644]
pcep/pcc-mock/pom.xml
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java [new file with mode: 0644]
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/SimpleSessionListenerTest.java
pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/PCEPTestingToolTest.java
pcep/testtool/src/test/java/org/opendaylight/protocol/pcep/testtool/SimpleSessionListener.java [moved from pcep/testtool/src/main/java/org/opendaylight/protocol/pcep/testtool/SimpleSessionListener.java with 100% similarity]

index ea0a37ceb3010692e476587e1d6689481f8b837a..45277d9e503310bc21ec7cfc69dfc75099e60d31 100644 (file)
       <scope>test</scope>
       <type>test-jar</type>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>mockito-configuration</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>config-manager</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>config-util</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>mockito-configuration</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>config-manager</artifactId>
+      <type>test-jar</type>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/data-change-counter/src/test/java/org/opendaylight/controller/config/yang/bgpcep/data/change/counter/DataChangeCounterImplModuleTest.java b/data-change-counter/src/test/java/org/opendaylight/controller/config/yang/bgpcep/data/change/counter/DataChangeCounterImplModuleTest.java
new file mode 100644 (file)
index 0000000..d126f3e
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2014 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.bgpcep.data.change.counter;
+
+import java.util.Collections;
+import java.util.Set;
+import javax.management.ObjectName;
+import junit.framework.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.DependencyResolverFactory;
+import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
+import org.opendaylight.controller.config.api.jmx.CommitStatus;
+import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
+import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
+import org.opendaylight.controller.config.spi.Module;
+import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev140815.DataChangeCounter;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.osgi.framework.BundleContext;
+
+public class DataChangeCounterImplModuleTest extends AbstractConfigTest {
+
+    private static final String FACTORY_NAME = DataChangeCounterImplModuleFactory.NAME;
+    private static final String INSTANCE_NAME = DataChangeCounterImplModuleFactory.SINGLETON_NAME;
+    private static final String DATA_BROKER_INSTANCE_NAME = "data-broker-instance";
+
+    private static final String TOPOLOGY_NAME = "test";
+    private static final String NEW_TOPOLOGY_NAME = "new-test";
+
+    @Mock
+    private CloseableDataBroker dataBorker;
+    @Mock
+    private WriteTransaction wTx;
+    @Mock
+    private ListenerRegistration<DataChangeListener> registration;
+
+    @Before
+    public void setUp() throws Exception {
+        Mockito.doNothing().when(this.registration).close();
+        Mockito.doReturn(null).when(this.wTx).submit();
+        Mockito.doNothing().when(this.wTx).put(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<DataChangeCounter>>any(), Mockito.any(DataChangeCounter.class));
+        Mockito.doReturn(registration).when(this.dataBorker).registerDataChangeListener(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<?>>any(), Mockito.any(DataChangeListener.class), Mockito.any(DataBroker.DataChangeScope.class));
+        Mockito.doNothing().when(this.wTx).delete(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<?>>any());
+        Mockito.doReturn(this.wTx).when(this.dataBorker).newWriteOnlyTransaction();
+        Mockito.doNothing().when(this.dataBorker).close();
+        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, new DataChangeCounterImplModuleFactory(), new MockDataBrokerModuleFct()));
+    }
+
+    @Test
+    public void testCreateBean() throws Exception {
+        final CommitStatus status = createInstance(TOPOLOGY_NAME);
+        assertBeanCount(1, FACTORY_NAME);
+        assertStatus(status, 2, 0, 0);
+    }
+
+    @Test
+    public void testReusingOldInstance() throws Exception {
+        createInstance(TOPOLOGY_NAME);
+        final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+        assertBeanCount(1, FACTORY_NAME);
+        final CommitStatus status = transaction.commit();
+        assertBeanCount(1, FACTORY_NAME);
+        assertStatus(status, 0, 0, 2);
+    }
+
+    @Test
+    public void testReconfigureBean() throws Exception {
+        createInstance(TOPOLOGY_NAME);
+        final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+        final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
+                DataChangeCounterImplModuleMXBean.class);
+        mxBean.setTopologyName(NEW_TOPOLOGY_NAME);
+        final CommitStatus status = transaction.commit();
+        assertBeanCount(1, FACTORY_NAME);
+        assertStatus(status, 0, 1, 1);
+
+        final ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
+        final DataChangeCounterImplModuleMXBean mxBean2 = transaction2.newMXBeanProxy(transaction2.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
+                DataChangeCounterImplModuleMXBean.class);
+        Assert.assertEquals(NEW_TOPOLOGY_NAME, mxBean2.getTopologyName());
+    }
+
+    private CommitStatus createInstance(final String topologyName) throws Exception {
+        final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+        final ObjectName on = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
+        final ObjectName dbOn = transaction.createModule(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
+        final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, DataChangeCounterImplModuleMXBean.class);
+        mxBean.setTopologyName(topologyName);
+        mxBean.setDataProvider(dbOn);
+        return transaction.commit();
+    }
+
+    private final class MockDataBrokerModuleFct implements org.opendaylight.controller.config.spi.ModuleFactory {
+
+        private static final String INSTANCE_NAME = "data-broker-fct";
+
+        @Override
+        public String getImplementationName() {
+            return INSTANCE_NAME;
+        }
+
+        @Override
+        public Module createModule(String instanceName, DependencyResolver dependencyResolver,
+                BundleContext bundleContext) {
+            return new MockDataBrokerModule();
+        }
+
+        @Override
+        public Module createModule(String instanceName, DependencyResolver dependencyResolver,
+                DynamicMBeanWithInstance old, BundleContext bundleContext) throws Exception {
+            return new MockDataBrokerModule();
+        }
+
+        @Override
+        public boolean isModuleImplementingServiceInterface(Class<? extends AbstractServiceInterface> serviceInterface) {
+            return true;
+        }
+
+        @Override
+        public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
+            java.util.Set<Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>> serviceIfcs2 = new java.util.HashSet<Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>>();
+            return java.util.Collections.unmodifiableSet(serviceIfcs2);
+        }
+
+        @Override
+        public Set<? extends Module> getDefaultModules(DependencyResolverFactory dependencyResolverFactory,
+                BundleContext bundleContext) {
+            return Collections.emptySet();
+        }
+
+    }
+
+    private final class MockDataBrokerModule implements org.opendaylight.controller.config.spi.Module,org.opendaylight.controller.config.yang.md.sal.binding.impl.BindingAsyncDataBrokerImplModuleMXBean,org.opendaylight.controller.config.yang.md.sal.binding.DataBrokerServiceInterface {
+
+        @Override
+        public ModuleIdentifier getIdentifier() {
+            return new ModuleIdentifier(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
+        }
+
+        @Override
+        public ObjectName getBindingMappingService() {
+            return null;
+        }
+
+        @Override
+        public void setBindingMappingService(ObjectName bindingMappingService) {
+            return;
+        }
+
+        @Override
+        public ObjectName getDomAsyncBroker() {
+            return null;
+        }
+
+        @Override
+        public void setDomAsyncBroker(ObjectName domAsyncBroker) {
+            return;
+        }
+
+        @Override
+        public void validate() {
+            return;
+        }
+
+        @Override
+        public AutoCloseable getInstance() {
+            return (AutoCloseable) DataChangeCounterImplModuleTest.this.dataBorker;
+        }
+
+    }
+
+    private interface CloseableDataBroker extends DataBroker, AutoCloseable {
+    }
+
+}
index 74ad42868313d300036b2ccf3ea252d9e8474fc0..d358ed760d9678e57fa4a3f3670a05173e579e18 100644 (file)
       <artifactId>logback-classic</artifactId>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>pcep-testtool</artifactId>
+      <scope>test</scope>
+    </dependency>
 
     <!-- Testing dependencies -->
     <dependency>
diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java
new file mode 100644 (file)
index 0000000..7937654
--- /dev/null
@@ -0,0 +1,41 @@
+package org.opendaylight.protocol.pcep.pcc.mock;
+
+import io.netty.buffer.Unpooled;
+import junit.framework.Assert;
+import org.junit.Test;
+import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
+import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
+
+
+
+public class PCCMockTest {
+
+    @Test
+    public void testSessionEstablishment() {
+        try {
+            org.opendaylight.protocol.pcep.testtool.Main.main(new String[]{"-a", "127.0.1.0:4189", "-ka", "10", "-d", "0", "--stateful", "--active"});
+            Main.main(new String[] {"--address", "127.0.1.0", "--pcc", "1", "--lsp", "1"});
+        } catch (Exception e) {
+            Assert.fail();
+        }
+    }
+
+    @Test
+    public void testSrpObjectParser() throws PCEPDeserializerException {
+        final byte[] bytes = {
+            0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00,
+        };
+        final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
+        final PCCSrpObjectParser parser = new PCCSrpObjectParser(ctx.getTlvHandlerRegistry(), ctx.getVendorInformationTlvRegistry());
+        final Srp srp = new SrpBuilder().setIgnore(true).setProcessingRule(true).setOperationId(new SrpIdNumber(0L)).setTlvs(new TlvsBuilder().build()).build();
+        Assert.assertEquals(srp, parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(bytes)));
+    }
+
+}
index a94f85426fc037c0eb95ca103324710a1afd8fdc..057cc70306edddc3ab3720efd32b3919a1f4da9f 100644 (file)
@@ -24,6 +24,8 @@ import org.mockito.MockitoAnnotations;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
+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.pcep.ietf.stateful.rev131222.Pcrpt;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcupdBuilder;
@@ -37,6 +39,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.iet
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
 
 public class SimpleSessionListenerTest {
 
@@ -81,6 +86,9 @@ public class SimpleSessionListenerTest {
         Mockito.verify(this.mockedSession, Mockito.times(3)).sendMessage(Mockito.any(Message.class));
         assertEquals(3, this.sendMessages.size());
         assertTrue(this.sendMessages.get(2) instanceof Pcrpt);
+
+        sessionListser.onSessionDown(mockedSession, new Exception());
+        Mockito.verify(this.mockedSession, Mockito.times(1)).close();
     }
 
     @Test
@@ -100,7 +108,10 @@ public class SimpleSessionListenerTest {
         final UpdatesBuilder updsBuilder = new UpdatesBuilder();
         updsBuilder.setLsp(new LspBuilder().setPlspId(new PlspId(1L)).build());
         final PathBuilder pathBuilder = new PathBuilder();
-        pathBuilder.setEro(new EroBuilder().build());
+        pathBuilder.setEro(
+                new EroBuilder()
+                    .setSubobject(Lists.newArrayList(new SubobjectBuilder().setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
+                        new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("127.0.0.2/32"))).build()).build()).build())).build());
         updsBuilder.setPath(pathBuilder.build());
         updsBuilder.setSrp(new SrpBuilder().setOperationId(new SrpIdNumber(0L)).build());
         msgBuilder.setUpdates(Lists.newArrayList(updsBuilder.build()));
index 6adf0da8787ff7e10db4e003100d94661dc71344..b7627c4f20feae6496eb9b4f4844018f1926aab2 100644 (file)
@@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import junit.framework.Assert;
 import org.junit.Test;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.KeepaliveMessage;
@@ -18,6 +19,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 
 public class PCEPTestingToolTest {
 
+    @Test
+    public void testSessionEstablishment() {
+        try {
+            Main.main(new String[]{"-a", "127.0.0.3:12345", "-ka", "10", "-d", "0", "--stateful", "--active", "--instant"});
+            PCCMock.main(new String[0]);
+        } catch (Exception e) {
+            Assert.fail();
+        }
+    }
+
     @Test
     public void testSimpleSessionListener() {
         final TestingSessionListener ssl = new TestingSessionListener();