Peel bierman02 local RPC test invocations
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / InvokeRpcMethodTest.java
index 588689424f4715d1bfccb8d45f02fbe53a57fa69..4aa1c8ade8da5ec93c32794e523e7c4af3ffce41 100644 (file)
@@ -39,15 +39,16 @@ import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.restconf.common.ErrorTags;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -63,6 +64,7 @@ import org.opendaylight.yangtools.yang.model.api.ContainerLike;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
@@ -103,7 +105,6 @@ public class InvokeRpcMethodTest {
      * string - first argument).
      */
     @Test
-    @Ignore
     public void invokeRpcMethodTest() {
         controllerContext.findModuleNameByNamespace(XMLNamespace.of("invoke:rpc:module"));
 
@@ -121,15 +122,15 @@ public class InvokeRpcMethodTest {
         final Module rpcModule = schema.findModules("invoke-rpc-module").iterator().next();
         assertNotNull(rpcModule);
         final QName rpcQName = QName.create(rpcModule.getQNameModule(), "rpc-test");
-        ContainerLike rpcInputSchemaNode = null;
+        RpcDefinition rpcSchemaNode = null;
         for (final RpcDefinition rpc : rpcModule.getRpcs()) {
             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
-                rpcInputSchemaNode = rpc.getInput();
+                rpcSchemaNode = rpc;
                 break;
             }
         }
-        assertNotNull(rpcInputSchemaNode);
-
+        assertNotNull(rpcSchemaNode);
+        final InputSchemaNode rpcInputSchemaNode = rpcSchemaNode.getInput();
         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
                 SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
 
@@ -147,8 +148,8 @@ public class InvokeRpcMethodTest {
         contNode.withChild(lfNode);
         container.withChild(contNode.build());
 
-        return new NormalizedNodeContext(
-                new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
+        return new NormalizedNodeContext(InstanceIdentifierContext.ofLocalRpc(schema, rpcSchemaNode),
+            container.build());
     }
 
     @Test
@@ -159,7 +160,7 @@ public class InvokeRpcMethodTest {
             .when(brokerFacade).invokeRpc(eq(qname), any());
 
         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> this.restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
+            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
         verifyRestconfDocumentedException(ex, 0, ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED,
             Optional.empty(), Optional.empty());
     }
@@ -198,9 +199,14 @@ public class InvokeRpcMethodTest {
         doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(path), any());
 
         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> this.restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
-        verifyRestconfDocumentedException(ex, 0, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, Optional.of("foo"),
-            Optional.empty());
+            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
+
+        // We are performing pass-through here of error-tag, hence the tag remains as specified, but we want to make
+        // sure the HTTP status remains the same as
+        final ErrorTag bogus = new ErrorTag("bogusTag");
+        verifyRestconfDocumentedException(ex, 0, ErrorType.TRANSPORT, bogus, Optional.of("foo"), Optional.empty());
+        assertEquals(ErrorTags.statusOf(ErrorTag.OPERATION_FAILED), ErrorTags.statusOf(bogus));
+
         verifyRestconfDocumentedException(ex, 1, ErrorType.RPC, ErrorTag.IN_USE, Optional.of("bar"),
             Optional.of("app-tag"));
     }
@@ -214,7 +220,7 @@ public class InvokeRpcMethodTest {
 
         doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(qname), any());
 
-        final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo);
+        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo);
         assertNotNull(output);
         assertEquals(null, output.getData());
         // additional validation in the fact that the restconfImpl does not
@@ -232,14 +238,14 @@ public class InvokeRpcMethodTest {
         doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(qname), any());
 
         WebApplicationException exceptionToBeThrown = assertThrows(WebApplicationException.class,
-            () -> this.restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
+            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), exceptionToBeThrown.getResponse().getStatus());
     }
 
     @Test
     public void testInvokeRpcMethodWithBadMethodName() {
         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> this.restconfImpl.invokeRpc("toaster:bad-method", null, uriInfo));
+            () -> restconfImpl.invokeRpc("toaster:bad-method", null, uriInfo));
         verifyRestconfDocumentedException(ex, 0, ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT,
             Optional.empty(), Optional.empty());
     }
@@ -255,27 +261,22 @@ public class InvokeRpcMethodTest {
         final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");
 
         RpcDefinition rpcDef = null;
-        ContainerLike rpcInputSchemaNode = null;
         for (final RpcDefinition rpc : rpcModule.getRpcs()) {
             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
-                rpcInputSchemaNode = rpc.getInput();
                 rpcDef = rpc;
                 break;
             }
         }
 
         assertNotNull(rpcDef);
-        assertNotNull(rpcInputSchemaNode);
-        final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder =
-                SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
 
-        final NormalizedNodeContext payload =
-                new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode,
-                null, schemaContext), containerBuilder.build());
+        final NormalizedNodeContext payload = new NormalizedNodeContext(
+                InstanceIdentifierContext.ofLocalRpcInput(schemaContext, rpcDef),
+                SchemaAwareBuilders.containerBuilder(rpcDef.getInput()).build());
 
         doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));
 
-        final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
+        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
         assertNotNull(output);
         assertEquals(null, output.getData());
         // additional validation in the fact that the restconfImpl does not
@@ -285,7 +286,7 @@ public class InvokeRpcMethodTest {
     @Test
     public void testThrowExceptionWhenSlashInModuleName() {
         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> this.restconfImpl.invokeRpc("toaster/slash", null, uriInfo));
+            () -> restconfImpl.invokeRpc("toaster/slash", null, uriInfo));
         verifyRestconfDocumentedException(ex, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
             Optional.empty(), Optional.empty());
     }
@@ -324,7 +325,7 @@ public class InvokeRpcMethodTest {
 
         doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(rpcDef.getQName()), any());
 
-        final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:testOutput", null, uriInfo);
+        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:testOutput", null, uriInfo);
         assertNotNull(output);
         assertNotNull(output.getData());
         assertSame(container, output.getData());