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=9aab841546016a5cb09f669c168307b2aaf0f725;hp=b90097bc94a0e7888b589f33960343cc94d36c12;hb=79db355d656708bcaf66940627c4adc935ecb36d;hpb=56978581354ce79ffadaa4544302e71c5169f37c 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 b90097bc94..9aab841546 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,8 +36,6 @@ 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.sal.restconf.impl.BrokerFacade; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; @@ -50,8 +48,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; @@ -67,17 +65,15 @@ public class InvokeRpcMethodTest { private static ControllerContext controllerContext = null; private static UriInfo uriInfo; - @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = new HashSet( TestUtils - .loadModulesFrom("/full-versions/yangs") ); - allModules.addAll( TestUtils.loadModulesFrom("/invoke-rpc") ); + Set allModules = new HashSet(TestUtils.loadModulesFrom("/full-versions/yangs")); + allModules.addAll(TestUtils.loadModulesFrom("/invoke-rpc")); assertNotNull(allModules); Module module = TestUtils.resolveModule("invoke-rpc-module", allModules); assertNotNull(module); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); - controllerContext = spy( ControllerContext.getInstance() ); + controllerContext = spy(ControllerContext.getInstance()); controllerContext.setSchemas(schemaContext); uriInfo = mock(UriInfo.class); MultivaluedMap map = new MultivaluedHashMap<>(); @@ -86,20 +82,18 @@ public class InvokeRpcMethodTest { } @Before - public void initMethod() - { + public void initMethod() { restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setControllerContext( controllerContext ); + restconfImpl.setControllerContext(controllerContext); } /** - * Test method invokeRpc in RestconfImpl class tests if composite node as - * input parameter of method invokeRpc (second argument) is wrapped to - * parent composite node which has QName equals to QName of rpc (resolved - * from string - first argument). + * Test method invokeRpc in RestconfImpl class tests if composite node as input parameter of method invokeRpc + * (second argument) is wrapped to parent composite node which has QName equals to QName of rpc (resolved from + * string - first argument). */ @Test - public void invokeRpcMtethodTest() { + public void invokeRpcMethodTest() { ControllerContext contContext = controllerContext; try { contContext.findModuleNameByNamespace(new URI("invoke:rpc:module")); @@ -115,11 +109,10 @@ public class InvokeRpcMethodTest { CompositeNode payload = preparePayload(); - when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))) - .thenReturn( Futures.>immediateFuture( - Rpcs.getRpcResult( true ) ) ); + when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(RpcResultBuilder.success().build())); - StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", payload,uriInfo); + StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo); assertTrue(structData == null); } @@ -137,207 +130,224 @@ public class InvokeRpcMethodTest { @Test public void testInvokeRpcWithNoPayloadRpc_FailNoErrors() { - RpcResult rpcResult = Rpcs.getRpcResult( false ); + RpcResult rpcResult = RpcResultBuilder.failed().build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); - when( brokerFacade.invokeRpc( - eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), - any(CompositeNode.class))) - .thenReturn( Futures.>immediateFuture( rpcResult ) ); + when( + brokerFacade.invokeRpc( + eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), + any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); restconfImpl.setBroker(brokerFacade); try { - restconfImpl.invokeRpc("toaster:cancel-toast", "",uriInfo); + restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo); fail("Expected an exception to be thrown."); - } - catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException( e, 0, ErrorType.RPC, ErrorTag.OPERATION_FAILED, - Optional.absent(), Optional.absent() ); + } catch (RestconfDocumentedException e) { + verifyRestconfDocumentedException(e, 0, ErrorType.RPC, ErrorTag.OPERATION_FAILED, + Optional. absent(), Optional. absent()); } } - void verifyRestconfDocumentedException( final RestconfDocumentedException e, final int index, - final ErrorType expErrorType, final ErrorTag expErrorTag, - final Optional expErrorMsg, - final Optional expAppTag ) { + void verifyRestconfDocumentedException(final RestconfDocumentedException e, final int index, + final ErrorType expErrorType, final ErrorTag expErrorTag, final Optional expErrorMsg, + final Optional expAppTag) { RestconfError actual = null; try { - actual = e.getErrors().get( index ); - } - catch( ArrayIndexOutOfBoundsException ex ) { - fail( "RestconfError not found at index " + index ); + actual = e.getErrors().get(index); + } catch (ArrayIndexOutOfBoundsException ex) { + fail("RestconfError not found at index " + index); } - assertEquals( "getErrorType", expErrorType, actual.getErrorType() ); - assertEquals( "getErrorTag", expErrorTag, actual.getErrorTag() ); - assertNotNull( "getErrorMessage is null", actual.getErrorMessage() ); + assertEquals("getErrorType", expErrorType, actual.getErrorType()); + assertEquals("getErrorTag", expErrorTag, actual.getErrorTag()); + assertNotNull("getErrorMessage is null", actual.getErrorMessage()); - if( expErrorMsg.isPresent() ) { - assertEquals( "getErrorMessage", expErrorMsg.get(), actual.getErrorMessage() ); + if (expErrorMsg.isPresent()) { + assertEquals("getErrorMessage", expErrorMsg.get(), actual.getErrorMessage()); } - if( expAppTag.isPresent() ) { - assertEquals( "getErrorAppTag", expAppTag.get(), actual.getErrorAppTag() ); + if (expAppTag.isPresent()) { + assertEquals("getErrorAppTag", expAppTag.get(), actual.getErrorAppTag()); } } @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 )); + 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( brokerFacade.invokeRpc( - eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), - any(CompositeNode.class))) - .thenReturn( Futures.>immediateFuture( rpcResult ) ); + when( + brokerFacade.invokeRpc( + eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), + any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); restconfImpl.setBroker(brokerFacade); try { - restconfImpl.invokeRpc("toaster:cancel-toast", "",uriInfo); + restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo); fail("Expected an exception to be thrown."); - } - catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException( e, 0, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, - Optional.of( "foo" ), Optional.absent() ); - verifyRestconfDocumentedException( e, 1, ErrorType.RPC, ErrorTag.IN_USE, - Optional.of( "bar" ), Optional.of( "app-tag" ) ); + } catch (RestconfDocumentedException e) { + verifyRestconfDocumentedException(e, 0, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, Optional.of("foo"), + Optional. absent()); + verifyRestconfDocumentedException(e, 1, ErrorType.RPC, ErrorTag.IN_USE, Optional.of("bar"), + Optional.of("app-tag")); } } @Test public void testInvokeRpcWithNoPayload_Success() { - RpcResult rpcResult = Rpcs.getRpcResult( true ); + RpcResult rpcResult = RpcResultBuilder.success().build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); - when( brokerFacade.invokeRpc( - eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), - any( CompositeNode.class ))) - .thenReturn( Futures.>immediateFuture( rpcResult ) ); + when( + brokerFacade.invokeRpc( + eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")), + any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); restconfImpl.setBroker(brokerFacade); - StructuredData output = restconfImpl.invokeRpc("toaster:cancel-toast", - "",uriInfo); + StructuredData output = restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo); assertEquals(null, output); - //additional validation in the fact that the restconfImpl does not throw an exception. + // additional validation in the fact that the restconfImpl does not + // throw an exception. } @Test public void testInvokeRpcMethodExpectingNoPayloadButProvidePayload() { try { - restconfImpl.invokeRpc("toaster:cancel-toast", " a payload ",uriInfo); + restconfImpl.invokeRpc("toaster:cancel-toast", " a payload ", uriInfo); fail("Expected an exception"); } catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException( e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, - Optional.absent(), Optional.absent() ); + verifyRestconfDocumentedException(e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, + Optional. absent(), Optional. absent()); } } @Test public void testInvokeRpcMethodWithBadMethodName() { try { - restconfImpl.invokeRpc("toaster:bad-method", "",uriInfo); + restconfImpl.invokeRpc("toaster:bad-method", "", uriInfo); fail("Expected an exception"); - } - catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException( e, 0, ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT, - Optional.absent(), Optional.absent() ); + } catch (RestconfDocumentedException e) { + verifyRestconfDocumentedException(e, 0, ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT, + Optional. absent(), Optional. absent()); } } @Test public void testInvokeRpcMethodWithInput() { - RpcResult rpcResult = Rpcs.getRpcResult( true ); + RpcResult rpcResult = RpcResultBuilder.success().build(); CompositeNode payload = mock(CompositeNode.class); BrokerFacade brokerFacade = mock(BrokerFacade.class); - when( brokerFacade.invokeRpc( - eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast")), - any(CompositeNode.class))) - .thenReturn( Futures.>immediateFuture( rpcResult ) ); + when( + brokerFacade.invokeRpc( + eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast")), + any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); restconfImpl.setBroker(brokerFacade); - StructuredData output = restconfImpl.invokeRpc("toaster:make-toast", - payload,uriInfo); + StructuredData output = restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo); assertEquals(null, output); - //additional validation in the fact that the restconfImpl does not throw an exception. + // additional validation in the fact that the restconfImpl does not + // throw an exception. } @Test public void testThrowExceptionWhenSlashInModuleName() { try { - restconfImpl.invokeRpc("toaster/slash", "",uriInfo); + restconfImpl.invokeRpc("toaster/slash", "", uriInfo); fail("Expected an exception."); - } - catch (RestconfDocumentedException e) { - verifyRestconfDocumentedException( e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, - Optional.absent(), Optional.absent() ); + } catch (RestconfDocumentedException e) { + verifyRestconfDocumentedException(e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, + Optional. absent(), Optional. absent()); } } @Test public void testInvokeRpcWithNoPayloadWithOutput_Success() { - CompositeNode compositeNode = mock( CompositeNode.class ); - RpcResult rpcResult = Rpcs.getRpcResult( true, compositeNode, - Collections.emptyList() ); + CompositeNode compositeNode = mock(CompositeNode.class); + RpcResult rpcResult = + RpcResultBuilder.success(compositeNode).build(); BrokerFacade brokerFacade = mock(BrokerFacade.class); - when( brokerFacade.invokeRpc( + when( + brokerFacade.invokeRpc( eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)testOutput")), - any( CompositeNode.class ))) - .thenReturn( Futures.>immediateFuture( rpcResult ) ); + any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); restconfImpl.setBroker(brokerFacade); - StructuredData output = restconfImpl.invokeRpc("toaster:testOutput", "",uriInfo); - assertNotNull( output ); - assertSame( compositeNode, output.getData() ); - assertNotNull( output.getSchema() ); + StructuredData output = restconfImpl.invokeRpc("toaster:testOutput", "", uriInfo); + assertNotNull(output); + assertSame(compositeNode, output.getData()); + 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 ); + public void testMountedRpcCallNoPayload_Success() throws Exception { + RpcResult rpcResult = RpcResultBuilder.success().build(); - ListenableFuture> mockListener = mock( ListenableFuture.class ); - when( mockListener.get() ).thenReturn( rpcResult ); + ListenableFuture> mockListener = mock(ListenableFuture.class); + when(mockListener.get()).thenReturn(rpcResult); - QName cancelToastQName = QName.create( "namespace", "2014-05-28", "cancelToast" ); + QName cancelToastQName = QName.create("namespace", "2014-05-28", "cancelToast"); - RpcDefinition mockRpc = mock( RpcDefinition.class ); - when( mockRpc.getQName() ).thenReturn( cancelToastQName ); + 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 ); + MountInstance mockMountPoint = mock(MountInstance.class); + when(mockMountPoint.rpc(eq(cancelToastQName), any(CompositeNode.class))).thenReturn(mockListener); - InstanceIdWithSchemaNode mockedInstanceId = mock( InstanceIdWithSchemaNode.class ); - when( mockedInstanceId.getMountPoint() ).thenReturn( mockMountPoint ); + when(mockMountPoint.getSchemaContext()).thenReturn(TestUtils.loadSchemaContext("/invoke-rpc")); - ControllerContext mockedContext = mock( ControllerContext.class ); - String cancelToastStr = "toaster:cancel-toast"; - when( mockedContext.urlPathArgDecode( cancelToastStr ) ).thenReturn( cancelToastStr ); - when( mockedContext.getRpcDefinition( cancelToastStr ) ).thenReturn( mockRpc ); - when( mockedContext.toMountPointIdentifier( "opendaylight-inventory:nodes/node/" - + "REMOTE_HOST/yang-ext:mount/toaster:cancel-toast" ) ).thenReturn( mockedInstanceId ); + InstanceIdWithSchemaNode mockedInstanceId = mock(InstanceIdWithSchemaNode.class); + when(mockedInstanceId.getMountPoint()).thenReturn(mockMountPoint); - restconfImpl.setControllerContext( mockedContext ); - StructuredData output = restconfImpl.invokeRpc( - "opendaylight-inventory:nodes/node/REMOTE_HOST/yang-ext:mount/toaster:cancel-toast", - "",uriInfo); - assertEquals(null, output); + ControllerContext mockedContext = mock(ControllerContext.class); + String rpcNoop = "invoke-rpc-module:rpc-noop"; + when(mockedContext.urlPathArgDecode(rpcNoop)).thenReturn(rpcNoop); + when(mockedContext.getRpcDefinition(rpcNoop)).thenReturn(mockRpc); + when( + mockedContext.toMountPointIdentifier(eq("opendaylight-inventory:nodes/node/" + + "REMOTE_HOST/yang-ext:mount/invoke-rpc-module:rpc-noop"))).thenReturn(mockedInstanceId); + + restconfImpl.setControllerContext(mockedContext); + 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. + // additional validation in the fact that the restconfImpl does not + // throw an exception. } }