OpenApi add POST request to device root
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / Netconf799Test.java
index 68ae8052774b6470f39326ba295013fb597ef564..c5149112f443989d5fd1bd1da8c3b115e67dc629 100644 (file)
@@ -28,19 +28,24 @@ import org.opendaylight.mdsal.dom.api.DOMActionService;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
+import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.restconf.api.ApiPath;
+import org.opendaylight.restconf.api.query.PrettyPrintParam;
 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
-import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
+import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
+import org.opendaylight.restconf.nb.rfc8040.ErrorTagMapping;
+import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
 @ExtendWith(MockitoExtension.class)
 class Netconf799Test extends AbstractInstanceIdentifierTest {
     private static final QName OUTPUT_QNAME = QName.create(CONT_QNAME, "output");
+    private static final Absolute RESET_PATH = Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME);
 
     @Mock
     private UriInfo uriInfo;
@@ -59,12 +64,15 @@ class Netconf799Test extends AbstractInstanceIdentifierTest {
 
     @Test
     void testInvokeAction() throws Exception {
-        doReturn(Futures.immediateFuture(new SimpleDOMActionResult(
-            Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME)).build())))
-            .when(actionService).invokeAction(eq(Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME)), any(), any());
+        doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(ImmutableNodes.newContainerBuilder()
+            .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
+            .build())))
+            .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
 
-        final var restconf = new JaxRsRestconf(new MdsalRestconfServer(
-            () -> DatabindContext.ofModel(IID_SCHEMA), dataBroker, rpcService, actionService, mountPointService));
+        final var restconf = new JaxRsRestconf(
+            new MdsalRestconfServer(new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)),
+                dataBroker, rpcService, actionService, mountPointService),
+            ErrorTagMapping.RFC8040, PrettyPrintParam.FALSE);
         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
         doReturn(true).when(asyncResponse).resume(captor.capture());
         restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
@@ -78,4 +86,36 @@ class Netconf799Test extends AbstractInstanceIdentifierTest {
         assertEquals(204, response.getStatus());
         assertNull(response.getEntity());
     }
+
+    @Test
+    void testInvokeActionOutput() throws Exception {
+        doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(ImmutableNodes.newContainerBuilder()
+            .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
+            .withChild(ImmutableNodes.leafNode(QName.create(OUTPUT_QNAME, "timestamp"), "somevalue"))
+            .build())))
+            .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
+
+        final var restconf = new JaxRsRestconf(
+            new MdsalRestconfServer(new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)),
+                dataBroker, rpcService, actionService, mountPointService),
+            ErrorTagMapping.RFC8040, PrettyPrintParam.FALSE);
+        doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
+
+        final var apiPath = ApiPath.parse("instance-identifier-module:cont/cont1/reset");
+        final var body = AbstractRestconfTest.assertFormattableBody(200, ar -> {
+            restconf.postDataJSON(apiPath,
+                stringInputStream("""
+                    {
+                      "instance-identifier-module:input": {
+                        "delay": 600
+                      }
+                    }"""), uriInfo, ar);
+        });
+
+        AbstractJukeboxTest.assertFormat("""
+            {"instance-identifier-module:output":{"timestamp":"somevalue"}}""", body::formatToJSON, false);
+        AbstractJukeboxTest.assertFormat("""
+            <output xmlns="instance:identifier:module"><timestamp>somevalue</timestamp></output>""", body::formatToXML,
+            false);
+    }
 }