Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / RpcProviderRegistryTest.java
index 8782046eeef3c13da933bc01ab8d68476b0a0664..4535a7fff44d143405bf756491febe0ae3c45114 100644 (file)
 package org.opendaylight.controller.md.sal.binding.impl.test;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
 
-import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
+import org.opendaylight.controller.md.sal.binding.compat.HeliumRpcProviderRegistry;
 
-import org.junit.Before;
+import com.google.common.base.Throwables;
+import java.util.Arrays;
+import javassist.ClassPool;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.binding.test.AssertCollections;
-import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
-import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
+import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
+import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcServiceAdapter;
+import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
+import org.opendaylight.controller.md.sal.binding.test.AbstractSchemaAwareTest;
+import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
+import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
-import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
 import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
-import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
 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.list.rev140701.two.level.list.TopLevelList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
+import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
+import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
+import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
 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.model.api.SchemaContext;
 
-import com.google.common.util.concurrent.SettableFuture;
 
-
-public class RpcProviderRegistryTest {
+public class RpcProviderRegistryTest  extends AbstractSchemaAwareTest {
 
     private static InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
     private static InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
     private static RpcContextIdentifier ROUTING_CONTEXT = RpcContextIdentifier.contextFor(OpendaylightTestRoutedRpcService.class, TestContext.class);
 
-    private RpcProviderRegistryImpl rpcRegistry;
-
-    @Before
-    public void setup() {
-        rpcRegistry = new RpcProviderRegistryImpl("test");
-    }
+    private RpcProviderRegistry rpcRegistry;
 
-    private static class TestListener implements RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>> {
 
-        final SettableFuture<RouteChange<RpcContextIdentifier, InstanceIdentifier<?>>> event = SettableFuture.create();
-        @Override
-        public void onRouteChange(
-                final RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> change) {
-            event.set(change);
+    @Override
+    protected Iterable<YangModuleInfo> getModuleInfos() {
+        try {
+            return Arrays.asList(
+                    BindingReflections.getModuleInfo(TopLevelList.class),
+                    BindingReflections.getModuleInfo(OpendaylightTestRoutedRpcService.class),
+                    BindingReflections.getModuleInfo(OpendaylightTestRpcServiceService.class));
+        } catch (final Exception e) {
+            throw Throwables.propagate(e);
         }
     }
 
+    @Override
+    protected void setupWithSchema(final SchemaContext context) {
+        final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
+        final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
+        final GeneratedClassLoadingStrategy classLoadingStrategy = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
+        final BindingToNormalizedNodeCodec codec = new BindingToNormalizedNodeCodec(classLoadingStrategy, null, codecRegistry);
+        final DOMRpcRouter domRpcRegistry = new DOMRpcRouter();
+        domRpcRegistry.onGlobalContextUpdated(context);
+        codec.onGlobalContextUpdated(context);
+        final RpcConsumerRegistry consumer = new BindingDOMRpcServiceAdapter(domRpcRegistry, codec);
+        final BindingDOMRpcProviderServiceAdapter provider = new BindingDOMRpcProviderServiceAdapter( domRpcRegistry,codec);
+        rpcRegistry = new HeliumRpcProviderRegistry(consumer,provider);
+    }
+
     @Test
     public void testGlobalRpcRegistrations() throws Exception {
-        OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
-        OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
+        final OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
+        final OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
 
-        RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
+        final RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
         assertNotNull(regOne);
-
-        try {
-            rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
-        fail("Second call for registration of same RPC must throw IllegalStateException");
-        } catch (IllegalStateException e) {
-            assertNotNull(e.getMessage());
-        }
-
+        rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
         regOne.close();
-
-        RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
+        final RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
         assertNotNull(regTwo);
     }
 
-    @Test
-    public void routedRpcRegisteredUsingGlobalAsDefaultInstance() throws Exception {
-        OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
-        rpcRegistry.addRpcImplementation(OpendaylightTestRoutedRpcService.class, def);
-        RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
-        assertEquals(def, router.getDefaultService());
-    }
 
     @Test
+    @Ignore
     public void nonRoutedRegisteredAsRouted() {
-        OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
+        final OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
         try {
-            rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRpcServiceService.class, one);
+            final RoutedRpcRegistration<OpendaylightTestRpcServiceService> reg = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRpcServiceService.class, one);
+            reg.registerPath(null, BAR_PATH);
             fail("RpcIsNotRoutedException should be thrown");
-        } catch (RpcIsNotRoutedException e) {
+        } catch (final RpcIsNotRoutedException e) {
             assertNotNull(e.getMessage());
-        } catch (Exception e) {
+        } catch (final Exception e) {
             fail("RpcIsNotRoutedException should be thrown");
         }
 
     }
 
-    @Test
-    public void testRpcRouterInstance() throws Exception  {
-        OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
-
-        RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
-
-        assertEquals(OpendaylightTestRoutedRpcService.class, router.getServiceType());
-        assertNotNull(router.getInvocationProxy());
-        assertNull(router.getDefaultService());
-
-        AssertCollections.assertContains(router.getContexts(), TestContext.class);
-
-        RpcRegistration<OpendaylightTestRoutedRpcService> regDef = router.registerDefaultService(def);
-        assertNotNull(regDef);
-        assertEquals(OpendaylightTestRoutedRpcService.class,regDef.getServiceType());
-        assertEquals(def,regDef.getInstance());
-        assertEquals(def, router.getDefaultService());
-
-        regDef.close();
-        assertNull("Default instance should be null after closing registration",  router.getDefaultService());
-    }
-
-    @Test
-    public void testRoutedRpcPathChangeEvents() throws InterruptedException, TimeoutException, ExecutionException {
-        OpendaylightTestRoutedRpcService one = Mockito.mock(OpendaylightTestRoutedRpcService.class);
-        OpendaylightTestRoutedRpcService two = Mockito.mock(OpendaylightTestRoutedRpcService.class);
-        RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regOne = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, one);
-        RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regTwo = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, two);
-        assertNotNull(regOne);
-        assertNotNull(regTwo);
-
-        final TestListener addListener = new TestListener();
-        rpcRegistry.registerRouteChangeListener(addListener);
-        regOne.registerPath(TestContext.class, FOO_PATH);
-
-        RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooAddEvent = addListener.event.get(500, TimeUnit.MILLISECONDS);
-        Set<InstanceIdentifier<?>> announce = fooAddEvent.getAnnouncements().get(ROUTING_CONTEXT);
-        assertNotNull(announce);
-        AssertCollections.assertContains(announce, FOO_PATH);
-        AssertCollections.assertNotContains(announce, BAR_PATH);
-
-
-
-        final TestListener removeListener = new TestListener();
-        rpcRegistry.registerRouteChangeListener(removeListener);
-
-        regOne.unregisterPath(TestContext.class, FOO_PATH);
-
-        RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooRemoveEvent = removeListener.event.get(500, TimeUnit.MILLISECONDS);
-        Set<InstanceIdentifier<?>> removal = fooRemoveEvent.getRemovals().get(ROUTING_CONTEXT);
-        assertNotNull(removal);
-        AssertCollections.assertContains(removal, FOO_PATH);
-        AssertCollections.assertNotContains(removal, BAR_PATH);
-
-
-    }
-
 }