X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FInvokeRpcMethodTest.java;h=2f045ce381ca0572cbeff4ad43147834fb59d2cf;hp=d12bf9e44efa96be39b2dea863c2339e74e8577b;hb=51e91f6bdcc88c5aa96f956e516d31dbb5e5d5e0;hpb=484151212f586dd533984a11063c92799c414ec8 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java index d12bf9e44e..2f045ce381 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java @@ -36,9 +36,8 @@ import javax.ws.rs.core.UriInfo; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.sal.common.util.RpcErrors; -import org.opendaylight.controller.sal.common.util.Rpcs; -import org.opendaylight.controller.sal.core.api.mount.MountInstance; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry; import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode; @@ -50,8 +49,8 @@ import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; import org.opendaylight.controller.sal.restconf.impl.StructuredData; 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.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.ModifyAction; import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; @@ -95,7 +94,7 @@ public class InvokeRpcMethodTest { * string - first argument). */ @Test - public void invokeRpcMtethodTest() { + public void invokeRpcMethodTest() { ControllerContext contContext = controllerContext; try { contContext.findModuleNameByNamespace(new URI("invoke:rpc:module")); @@ -112,7 +111,7 @@ public class InvokeRpcMethodTest { CompositeNode payload = preparePayload(); when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn( - Futures.> immediateFuture(Rpcs. getRpcResult(true))); + Futures.> immediateFuture(RpcResultBuilder.success().build())); StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo); assertTrue(structData == null); @@ -132,7 +131,7 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcWithNoPayloadRpc_FailNoErrors() { - RpcResult rpcResult = Rpcs. getRpcResult(false); + RpcResult rpcResult = RpcResultBuilder.failed().build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); when( @@ -147,7 +146,7 @@ public class InvokeRpcMethodTest { restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo); fail("Expected an exception to be thrown."); } catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException(e, 0, ErrorType.RPC, ErrorTag.OPERATION_FAILED, + verifyRestconfDocumentedException(e, 0, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, Optional. absent(), Optional. absent()); } } @@ -177,11 +176,13 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcWithNoPayloadRpc_FailWithRpcError() { - List rpcErrors = Arrays.asList(RpcErrors.getRpcError(null, "bogusTag", null, ErrorSeverity.ERROR, - "foo", RpcError.ErrorType.TRANSPORT, null), RpcErrors.getRpcError("app-tag", "in-use", null, - ErrorSeverity.WARNING, "bar", RpcError.ErrorType.RPC, null)); + List rpcErrors = Arrays.asList( + RpcResultBuilder.newError( RpcError.ErrorType.TRANSPORT, "bogusTag", "foo" ), + RpcResultBuilder.newWarning( RpcError.ErrorType.RPC, "in-use", "bar", + "app-tag", null, null ) ); - RpcResult rpcResult = Rpcs. getRpcResult(false, rpcErrors); + RpcResult rpcResult = RpcResultBuilder.failed() + .withRpcErrors(rpcErrors).build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); when( @@ -205,7 +206,7 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcWithNoPayload_Success() { - RpcResult rpcResult = Rpcs. getRpcResult(true); + RpcResult rpcResult = RpcResultBuilder.success().build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); when( @@ -246,7 +247,7 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcMethodWithInput() { - RpcResult rpcResult = Rpcs. getRpcResult(true); + RpcResult rpcResult = RpcResultBuilder.success().build(); CompositeNode payload = mock(CompositeNode.class); @@ -279,8 +280,8 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcWithNoPayloadWithOutput_Success() { CompositeNode compositeNode = mock(CompositeNode.class); - RpcResult rpcResult = Rpcs. getRpcResult(true, compositeNode, - Collections. emptyList()); + RpcResult rpcResult = + RpcResultBuilder.success(compositeNode).build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); when( @@ -297,9 +298,17 @@ public class InvokeRpcMethodTest { assertNotNull(output.getSchema()); } + /** + * + * Tests calling of RestConfImpl method invokeRpc. In the method there is searched rpc in remote schema context. + * This rpc is then executed. + * + * I wasn't able to simulate calling of rpc on remote device therefore this testing method raise method when rpc is + * invoked. + */ @Test public void testMountedRpcCallNoPayload_Success() throws Exception { - RpcResult rpcResult = Rpcs. getRpcResult(true); + RpcResult rpcResult = RpcResultBuilder.success().build(); ListenableFuture> mockListener = mock(ListenableFuture.class); when(mockListener.get()).thenReturn(rpcResult); @@ -309,24 +318,36 @@ public class InvokeRpcMethodTest { RpcDefinition mockRpc = mock(RpcDefinition.class); when(mockRpc.getQName()).thenReturn(cancelToastQName); - MountInstance mockMountPoint = mock(MountInstance.class); - when(mockMountPoint.rpc(eq(cancelToastQName), any(CompositeNode.class))).thenReturn(mockListener); + DOMMountPoint mockMountPoint = mock(DOMMountPoint.class); + RpcProvisionRegistry mockedRpcProvisionRegistry = mock(RpcProvisionRegistry.class); + when(mockedRpcProvisionRegistry.invokeRpc(eq(cancelToastQName), any(CompositeNode.class))).thenReturn(mockListener); + when(mockMountPoint.getService(eq(RpcProvisionRegistry.class))).thenReturn(Optional.of(mockedRpcProvisionRegistry)); + when(mockMountPoint.getSchemaContext()).thenReturn(TestUtils.loadSchemaContext("/invoke-rpc")); InstanceIdWithSchemaNode mockedInstanceId = mock(InstanceIdWithSchemaNode.class); when(mockedInstanceId.getMountPoint()).thenReturn(mockMountPoint); ControllerContext mockedContext = mock(ControllerContext.class); - String cancelToastStr = "toaster:cancel-toast"; - when(mockedContext.urlPathArgDecode(cancelToastStr)).thenReturn(cancelToastStr); - when(mockedContext.getRpcDefinition(cancelToastStr)).thenReturn(mockRpc); + String rpcNoop = "invoke-rpc-module:rpc-noop"; + when(mockedContext.urlPathArgDecode(rpcNoop)).thenReturn(rpcNoop); + when(mockedContext.getRpcDefinition(rpcNoop)).thenReturn(mockRpc); when( - mockedContext.toMountPointIdentifier("opendaylight-inventory:nodes/node/" - + "REMOTE_HOST/yang-ext:mount/toaster:cancel-toast")).thenReturn(mockedInstanceId); + mockedContext.toMountPointIdentifier(eq("opendaylight-inventory:nodes/node/" + + "REMOTE_HOST/yang-ext:mount/invoke-rpc-module:rpc-noop"))).thenReturn(mockedInstanceId); restconfImpl.setControllerContext(mockedContext); - StructuredData output = restconfImpl.invokeRpc( - "opendaylight-inventory:nodes/node/REMOTE_HOST/yang-ext:mount/toaster:cancel-toast", "", uriInfo); - assertEquals(null, output); + try { + restconfImpl.invokeRpc( + "opendaylight-inventory:nodes/node/REMOTE_HOST/yang-ext:mount/invoke-rpc-module:rpc-noop", "", + uriInfo); + fail("RestconfDocumentedException wasn't raised"); + } catch (RestconfDocumentedException e) { + List errors = e.getErrors(); + assertNotNull(errors); + assertEquals(1, errors.size()); + assertEquals(ErrorType.APPLICATION, errors.iterator().next().getErrorType()); + assertEquals(ErrorTag.OPERATION_FAILED, errors.iterator().next().getErrorTag()); + } // additional validation in the fact that the restconfImpl does not // throw an exception.