Remove reliance on org.opendaylight.controller.sal.core.api.Broker
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / AbstractRpcTest.java
index 46406fd4feebad58c7546ec5df12e5bb1f2f8137..32b54c4a8cf5470e2fa11060f17e48f88f7cff41 100644 (file)
@@ -8,37 +8,38 @@
 
 package org.opendaylight.controller.remote.rpc;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.testkit.JavaTestKit;
-import com.google.common.collect.ImmutableList;
+import java.net.URI;
+import java.util.Collection;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
-import org.mockito.Mockito;
-import org.opendaylight.controller.sal.core.api.Broker;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
-import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
-
-import java.io.File;
-import java.net.URI;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 /**
  * Base class for RPC tests.
@@ -55,20 +56,34 @@ public class AbstractRpcTest {
     static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
     static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
 
+
+    static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
+    static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(
+            new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
+    public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
+
     static ActorSystem node1;
     static ActorSystem node2;
+    static RemoteRpcProviderConfig config1;
+    static RemoteRpcProviderConfig config2;
 
-    protected ActorRef rpcBroker1;
-    protected JavaTestKit probeReg1;
-    protected ActorRef rpcBroker2;
-    protected JavaTestKit probeReg2;
-    protected Broker.ProviderSession brokerSession;
+    protected ActorRef rpcInvoker1;
+    protected JavaTestKit rpcRegistry1Probe;
+    protected ActorRef rpcInvoker2;
+    protected JavaTestKit rpcRegistry2Probe;
     protected SchemaContext schemaContext;
+    protected RemoteRpcImplementation remoteRpcImpl1;
+    protected RemoteRpcImplementation remoteRpcImpl2;
+
+    @Mock
+    protected DOMRpcService domRpcService1;
+    @Mock
+    protected DOMRpcService domRpcService2;
 
     @BeforeClass
     public static void setup() throws InterruptedException {
-        RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
-        RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
+        config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
+        config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
     }
@@ -82,21 +97,22 @@ public class AbstractRpcTest {
     }
 
     @Before
-    public void setUp() {
-        schemaContext = new YangParserImpl().parseFiles(Arrays.asList(
-                new File(RpcBrokerTest.class.getResource("/test-rpc.yang").getPath())));
+    public void setUp() throws ReactorException {
+        schemaContext = YangParserTestUtils.parseYangResources(AbstractRpcTest.class, "/test-rpc.yang");
 
-        brokerSession = Mockito.mock(Broker.ProviderSession.class);
-        probeReg1 = new JavaTestKit(node1);
-        rpcBroker1 = node1.actorOf(RpcBroker.props(brokerSession, probeReg1.getRef(), schemaContext));
-        probeReg2 = new JavaTestKit(node2);
-        rpcBroker2 = node2.actorOf(RpcBroker.props(brokerSession, probeReg2.getRef(), schemaContext));
+        MockitoAnnotations.initMocks(this);
 
+        rpcRegistry1Probe = new JavaTestKit(node1);
+        rpcInvoker1 = node1.actorOf(RpcInvoker.props(domRpcService1));
+        rpcRegistry2Probe = new JavaTestKit(node2);
+        rpcInvoker2 = node2.actorOf(RpcInvoker.props(domRpcService2));
+        remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
+        remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
     }
 
-    static void assertRpcErrorEquals(RpcError rpcError, ErrorSeverity severity,
-            ErrorType errorType, String tag, String message, String applicationTag, String info,
-            String causeMsg) {
+    static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
+            final ErrorType errorType, final String tag, final String message, final String applicationTag,
+            final String info, final String causeMsg) {
         assertEquals("getSeverity", severity, rpcError.getSeverity());
         assertEquals("getErrorType", errorType, rpcError.getErrorType());
         assertEquals("getTag", tag, rpcError.getTag());
@@ -104,61 +120,41 @@ public class AbstractRpcTest {
         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
         assertEquals("getInfo", info, rpcError.getInfo());
 
-        if(causeMsg == null) {
+        if (causeMsg == null) {
             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
         } else {
             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
         }
     }
 
-    static void assertCompositeNodeEquals(CompositeNode exp, CompositeNode actual) {
-        assertEquals("NodeType getNamespace", exp.getNodeType().getNamespace(),
-                actual.getNodeType().getNamespace());
-        assertEquals("NodeType getLocalName", exp.getNodeType().getLocalName(),
-                actual.getNodeType().getLocalName());
-        for(Node<?> child: exp.getValue()) {
-            List<Node<?>> c = actual.get(child.getNodeType());
-            assertNotNull("Missing expected child " + child.getNodeType(), c);
-            if(child instanceof CompositeNode) {
-                assertCompositeNodeEquals((CompositeNode) child, (CompositeNode)c.get(0));
-            } else {
-                assertEquals("Value for Node " + child.getNodeType(), child.getValue(),
-                        c.get(0).getValue());
-            }
-        }
+    static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
+        assertEquals(exp, actual);
     }
 
-    static CompositeNode makeRPCInput(String data) {
-        CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder()
-                .setQName(TEST_RPC_INPUT).addLeaf(TEST_RPC_INPUT_DATA, data);
-        return ImmutableCompositeNode.create(
-                TEST_RPC, ImmutableList.<Node<?>>of(builder.toInstance()));
-    }
+    public static ContainerNode makeRPCInput(final String data) {
+        return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
+            .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
 
-    static CompositeNode makeRPCOutput(String data) {
-        CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder()
-                .setQName(TEST_RPC_OUTPUT).addLeaf(TEST_RPC_OUTPUT_DATA, data);
-        return ImmutableCompositeNode.create(
-                TEST_RPC, ImmutableList.<Node<?>>of(builder.toInstance()));
     }
 
-    static void assertFailedRpcResult(RpcResult<CompositeNode> rpcResult, ErrorSeverity severity,
-            ErrorType errorType, String tag, String message, String applicationTag, String info,
-            String causeMsg) {
+    public static ContainerNode makeRPCOutput(final String data) {
+        return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
+                .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
+    }
 
+    static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
+            final ErrorType errorType, final String tag, final String message, final String applicationTag,
+            final String info, final String causeMsg) {
         assertNotNull("RpcResult was null", rpcResult);
-        assertEquals("isSuccessful", false, rpcResult.isSuccessful());
-        Collection<RpcError> rpcErrors = rpcResult.getErrors();
+        final Collection<RpcError> rpcErrors = rpcResult.getErrors();
         assertEquals("RpcErrors count", 1, rpcErrors.size());
         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
                 applicationTag, info, causeMsg);
     }
 
-    static void assertSuccessfulRpcResult(RpcResult<CompositeNode> rpcResult,
-            CompositeNode expOutput) {
-
+    static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
+            final NormalizedNode<? , ?> expOutput) {
         assertNotNull("RpcResult was null", rpcResult);
-        assertEquals("isSuccessful", true, rpcResult.isSuccessful());
         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
     }