Adjust unit test to YangModuleInfo API change
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / DOMRpcServiceTestBugfix560.java
index 6a6af0687a9046f45fa82d262246af8fbbc329f1..041c20709846e5e87ab568278adae2f56bac2d4f 100644 (file)
@@ -7,48 +7,48 @@
  */
 package org.opendaylight.controller.sal.binding.test.connect.dom;
 
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
-
-import java.io.InputStream;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
-import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
+import org.opendaylight.controller.md.sal.binding.api.MountPoint;
+import org.opendaylight.controller.md.sal.binding.api.MountPointService;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
-import org.opendaylight.controller.sal.common.util.Rpcs;
-import org.opendaylight.controller.sal.core.api.RpcImplementation;
-import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
-import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
-
-import com.google.common.collect.ImmutableSet;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.MoreExecutors;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 /**
  * Test case for reported bug 560
@@ -64,15 +64,15 @@ public class DOMRpcServiceTestBugfix560 {
     private final static QName RPC_NAME = QName.create(RPC_SERVICE_NAMESPACE,
             REVISION_DATE, "rock-the-house");
 
-    private static final NodeId MOUNT_NODE = new NodeId("id");
-    private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
+    private static final String TLL_NAME = "id";
+    private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
 
-    private static final InstanceIdentifier<Node> BA_MOUNT_ID = createBANodeIdentifier(MOUNT_NODE);
-    private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_MOUNT_ID = createBINodeIdentifier(MOUNT_NODE);
+    private static final InstanceIdentifier<TopLevelList> BA_MOUNT_ID = createBATllIdentifier(TLL_NAME);
+    private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_MOUNT_ID = createBITllIdentifier(TLL_NAME);
 
     private BindingTestContext testContext;
-    private MountProvisionService domMountPointService;
-    private MountProviderService bindingMountPointService;
+    private DOMMountPointService domMountPointService;
+    private MountPointService bindingMountPointService;
     private SchemaContext schemaContext;
 
     /**
@@ -80,81 +80,73 @@ public class DOMRpcServiceTestBugfix560 {
      */
     @Before
     public void setUp() throws Exception {
-        BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
-        testFactory.setExecutor(MoreExecutors.sameThreadExecutor());
+        final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
+        testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
         testFactory.setStartWithParsedSchema(true);
         testContext = testFactory.getTestContext();
 
         testContext.start();
         domMountPointService = testContext.getDomMountProviderService();
-        bindingMountPointService = testContext.getBindingMountProviderService();
+        bindingMountPointService = testContext.getBindingMountPointService();
         assertNotNull(domMountPointService);
 
-        final YangContextParser parser = new YangParserImpl();
-        final InputStream moduleStream = BindingReflections.getModuleInfo(
-                OpendaylightTestRpcServiceService.class)
-                .getModuleSourceStream();
+        final YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(OpendaylightTestRpcServiceService.class);
+        assertNotNull(moduleInfo);
 
-        assertNotNull(moduleStream);
-        List<InputStream> rpcModels = Collections.singletonList(moduleStream);
-        Set<Module> modules = parser.parseYangModelsFromStreams(rpcModels);
-        schemaContext = parser.resolveSchemaContext(modules);
+        schemaContext = YangParserTestUtils.parseYangSources(StatementParserMode.DEFAULT_MODE, null,
+            YangTextSchemaSource.delegateForByteSource(RevisionSourceIdentifier.create(
+                    moduleInfo.getName().getLocalName(), moduleInfo.getName().getRevision()),
+                moduleInfo.getYangTextByteSource()));
     }
 
-    private static org.opendaylight.yangtools.yang.data.api.InstanceIdentifier createBINodeIdentifier(
-            NodeId mountNode) {
-        return org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-                .builder().node(Nodes.QNAME)
-                .nodeWithKey(Node.QNAME, NODE_ID_QNAME, mountNode.getValue())
-                .toInstance();
+    private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBITllIdentifier(
+            final String mount) {
+        return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
+                .builder().node(Top.QNAME)
+                .node(TopLevelList.QNAME)
+                .nodeWithKey(TopLevelList.QNAME, TLL_NAME_QNAME, mount)
+                .build();
     }
 
-    private static InstanceIdentifier<Node> createBANodeIdentifier(
-            NodeId mountNode) {
-        return InstanceIdentifier.builder(Nodes.class)
-                .child(Node.class, new NodeKey(mountNode)).toInstance();
+    private static InstanceIdentifier<TopLevelList> createBATllIdentifier(
+            final String mount) {
+        return InstanceIdentifier.builder(Top.class)
+                .child(TopLevelList.class, new TopLevelListKey(mount)).build();
     }
 
     @Test
     public void test() throws ExecutionException, InterruptedException {
-        testContext.getBindingDataBroker().readOperationalData(BA_MOUNT_ID);
-        final MountProvisionInstance mountPoint = domMountPointService
-                .createMountPoint(BI_MOUNT_ID);
-        mountPoint.setSchemaContext(schemaContext);
-        assertNotNull(mountPoint);
-
-        mountPoint.addRpcImplementation(RPC_NAME, new RpcImplementation() {
-
-            @Override
-            public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(
-                    QName rpc, CompositeNode input) {
-
-                return Futures.immediateFuture(Rpcs
-                        .<CompositeNode> getRpcResult(true));
-            }
-
-            @Override
-            public Set<QName> getSupportedRpcs() {
-                return ImmutableSet.of(RPC_NAME);
-            }
-        });
-
-        final Set<QName> biSupportedRpcs = mountPoint.getSupportedRpcs();
-        assertNotNull(biSupportedRpcs);
-        assertTrue(!biSupportedRpcs.isEmpty());
-
-        MountProviderInstance mountInstance = bindingMountPointService
-                .getMountPoint(BA_MOUNT_ID);
-        assertNotNull(mountInstance);
-        final OpendaylightTestRpcServiceService rpcService = mountInstance
+        // FIXME: This is made to only make sure instance identifier codec for path is instantiated.
+        domMountPointService
+                .createMountPoint(BI_MOUNT_ID).addService(DOMRpcService.class, new DOMRpcService() {
+
+                    @Override
+                    public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T arg0) {
+                        // TODO Auto-generated method stub
+                        return null;
+                    }
+
+                    @Override
+                    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath arg0, final NormalizedNode<?, ?> arg1) {
+                        final DOMRpcResult result = new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
+                        return Futures.immediateCheckedFuture(result);
+                    }
+                }).register();
+
+        final Optional<MountPoint> mountInstance = bindingMountPointService.getMountPoint(BA_MOUNT_ID);
+        assertTrue(mountInstance.isPresent());
+
+        final Optional<RpcConsumerRegistry> rpcRegistry = mountInstance.get().getService(RpcConsumerRegistry.class);
+        assertTrue(rpcRegistry.isPresent());
+        final OpendaylightTestRpcServiceService rpcService = rpcRegistry.get()
                 .getRpcService(OpendaylightTestRpcServiceService.class);
         assertNotNull(rpcService);
 
         try {
-            Future<RpcResult<Void>> result = rpcService
+            final Future<RpcResult<Void>> result = rpcService
                     .rockTheHouse(new RockTheHouseInputBuilder().build());
             assertTrue(result.get().isSuccessful());
-        } catch (IllegalStateException ex) {
+        } catch (final IllegalStateException ex) {
             fail("OpendaylightTestRpcServiceService class doesn't contain rockTheHouse method!");
         }
     }