Specialize RestconfInvokeOperationsServiceImpl.invokeRpc()
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfInvokeOperationsServiceImplTest.java
index 1bc05d629ea756be516442c54de54041728e649d..6ab73b33d40904aa2363d4189ef787aa3c395b57 100644 (file)
@@ -14,12 +14,16 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
@@ -36,15 +40,16 @@ import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMRpcException;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
-import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
+import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
+import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -53,8 +58,6 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 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.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
@@ -69,32 +72,38 @@ public class RestconfInvokeOperationsServiceImplTest {
         .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "operation result"))
         .build();
 
-    private static EffectiveModelContext CONTEXT;
+    private static DatabindContext CONTEXT;
 
     @Mock
     private DOMRpcService rpcService;
     @Mock
     private DOMMountPoint mountPoint;
+    @Mock
+    private DOMMountPointService mountPointService;
     private RestconfInvokeOperationsServiceImpl invokeOperationsService;
 
     @BeforeClass
-    public static void beforeClass() throws Exception {
-        CONTEXT = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/invoke-rpc"));
+    public static void beforeClass() {
+        CONTEXT = DatabindContext.ofModel(YangParserTestUtils.parseYangResourceDirectory("/invoke-rpc"));
     }
 
     @Before
     public void setup() {
-        invokeOperationsService = new RestconfInvokeOperationsServiceImpl(rpcService);
+        invokeOperationsService = new RestconfInvokeOperationsServiceImpl(() -> CONTEXT, rpcService, mountPointService,
+            new StreamsConfiguration(0, 1, 0, false));
     }
 
     @Test
     public void testInvokeRpcWithNonEmptyOutput() {
-        final ContainerNode result = mock(ContainerNode.class);
+        final var result = mock(ContainerNode.class);
         doReturn(false).when(result).isEmpty();
 
-        final AsyncResponse ar = mock(AsyncResponse.class);
-        final ArgumentCaptor<NormalizedNodePayload> response = ArgumentCaptor.forClass(NormalizedNodePayload.class);
-        invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
+        prepNNC(result);
+        final var ar = mock(AsyncResponse.class);
+        final var response = ArgumentCaptor.forClass(NormalizedNodePayload.class);
+        invokeOperationsService.invokeRpcXML("invoke-rpc-module:rpc-test", new ByteArrayInputStream("""
+            <input xmlns="invoke:rpc:module"/>
+            """.getBytes(StandardCharsets.UTF_8)), mock(UriInfo.class), ar);
         verify(ar).resume(response.capture());
 
         assertSame(result, response.getValue().getData());
@@ -102,12 +111,18 @@ public class RestconfInvokeOperationsServiceImplTest {
 
     @Test
     public void testInvokeRpcWithEmptyOutput() {
-        final ContainerNode result = mock(ContainerNode.class);
+        final var result = mock(ContainerNode.class);
         doReturn(true).when(result).isEmpty();
 
-        final AsyncResponse ar = mock(AsyncResponse.class);
-        final ArgumentCaptor<Throwable> response = ArgumentCaptor.forClass(Throwable.class);
-        invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
+        prepNNC(result);
+        final var ar = mock(AsyncResponse.class);
+        final var response = ArgumentCaptor.forClass(Throwable.class);
+        invokeOperationsService.invokeRpcJSON("invoke-rpc-module:rpc-test", new ByteArrayInputStream("""
+            {
+              "invoke-rpc-module:input" : {
+              }
+            }
+            """.getBytes(StandardCharsets.UTF_8)), mock(UriInfo.class), ar);
         verify(ar).resume(response.capture());
 
         final Throwable failure = response.getValue();
@@ -120,8 +135,8 @@ public class RestconfInvokeOperationsServiceImplTest {
         final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
         doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
-        assertTrue(rpcResult.getErrors().isEmpty());
-        assertEquals(OUTPUT, rpcResult.getResult());
+        assertTrue(rpcResult.errors().isEmpty());
+        assertEquals(OUTPUT, rpcResult.value());
     }
 
     @Test
@@ -131,8 +146,8 @@ public class RestconfInvokeOperationsServiceImplTest {
                 "No implementation of RPC " + errorRpc + " available.");
         doReturn(immediateFailedFluentFuture(exception)).when(rpcService).invokeRpc(errorRpc, INPUT);
         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, errorRpc, rpcService).get();
-        assertNull(rpcResult.getResult());
-        final Collection<? extends RpcError> errorList = rpcResult.getErrors();
+        assertNull(rpcResult.value());
+        final Collection<? extends RpcError> errorList = rpcResult.errors();
         assertEquals(1, errorList.size());
         final RpcError actual = errorList.iterator().next();
         assertEquals("No implementation of RPC " + errorRpc + " available.", actual.getMessage());
@@ -146,8 +161,8 @@ public class RestconfInvokeOperationsServiceImplTest {
         final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
         doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint).get();
-        assertTrue(rpcResult.getErrors().isEmpty());
-        assertEquals(OUTPUT, rpcResult.getResult());
+        assertTrue(rpcResult.errors().isEmpty());
+        assertEquals(OUTPUT, rpcResult.value());
     }
 
     @Test
@@ -162,21 +177,14 @@ public class RestconfInvokeOperationsServiceImplTest {
         doReturn(immediateFluentFuture(new DefaultDOMRpcResult(OUTPUT, List.of())))
             .when(rpcService).invokeRpc(RPC, INPUT);
         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
-        assertTrue(rpcResult.getErrors().isEmpty());
-        assertEquals(OUTPUT, rpcResult.getResult());
+        assertTrue(rpcResult.errors().isEmpty());
+        assertEquals(OUTPUT, rpcResult.value());
     }
 
-    private NormalizedNodePayload prepNNC(final ContainerNode result) {
+    private void prepNNC(final ContainerNode result) {
         final QName qname = QName.create("invoke:rpc:module", "2013-12-03", "rpc-test");
-        final RpcDefinition schemaNode = CONTEXT.getOperations().stream().filter(rpc -> rpc.getQName().equals(qname))
-            .findFirst()
-            .orElseThrow();
-
-        final ContainerNode data = mock(ContainerNode.class);
         final DOMRpcResult domRpcResult = mock(DOMRpcResult.class);
-        doReturn(immediateFluentFuture(domRpcResult)).when(rpcService).invokeRpc(qname, data);
-        doReturn(result).when(domRpcResult).getResult();
-        return NormalizedNodePayload.of(
-            InstanceIdentifierContext.ofRpcInput(CONTEXT, schemaNode, null), data);
+        doReturn(immediateFluentFuture(domRpcResult)).when(rpcService).invokeRpc(eq(qname), any(ContainerNode.class));
+        doReturn(result).when(domRpcResult).value();
     }
 }