+ @Test
+ public void testInvokeRpcWithNoPayloadRpc_FailNoErrors() {
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(false);
+
+ ArgumentCaptor<CompositeNode> payload = ArgumentCaptor
+ .forClass(CompositeNode.class);
+ BrokerFacade brokerFacade = mock(BrokerFacade.class);
+ when(
+ brokerFacade.invokeRpc(
+ eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")),
+ payload.capture())).thenReturn(rpcResult);
+
+ restconfImpl.setBroker(brokerFacade);
+
+ try {
+ restconfImpl.invokeRpc("toaster:cancel-toast", "");
+ fail("Expected an exception to be thrown.");
+ } catch (ResponseException e) {
+ assertEquals(e.getMessage(),
+ Status.INTERNAL_SERVER_ERROR.getStatusCode(), e
+ .getResponse().getStatus());
+ }
+ }
+
+ @Test
+ public void testInvokeRpcWithNoPayloadRpc_FailWithRpcError() {
+ List<RpcError> rpcErrors = new LinkedList<RpcError>();
+
+ RpcError unknownError = mock(RpcError.class);
+ when( unknownError.getTag() ).thenReturn( "bogusTag" );
+ rpcErrors.add( unknownError );
+
+ RpcError knownError = mock( RpcError.class );
+ when( knownError.getTag() ).thenReturn( "in-use" );
+ rpcErrors.add( knownError );
+
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(false);
+ when(rpcResult.getErrors()).thenReturn( rpcErrors );
+
+ ArgumentCaptor<CompositeNode> payload = ArgumentCaptor
+ .forClass(CompositeNode.class);
+ BrokerFacade brokerFacade = mock(BrokerFacade.class);
+ when(
+ brokerFacade.invokeRpc(
+ eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")),
+ payload.capture())).thenReturn(rpcResult);
+
+ restconfImpl.setBroker(brokerFacade);
+
+ try {
+ restconfImpl.invokeRpc("toaster:cancel-toast", "");
+ fail("Expected an exception to be thrown.");
+ } catch (ResponseException e) {
+ //TODO: Change to a 409 in the future - waiting on additional BUG to enhance this.
+ assertEquals(e.getMessage(), 500, e.getResponse().getStatus());
+ }
+ }
+
+ @Test
+ public void testInvokeRpcWithNoPayload_Success() {
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(true);
+
+ 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(rpcResult);
+
+ restconfImpl.setBroker(brokerFacade);
+
+ StructuredData output = restconfImpl.invokeRpc("toaster:cancel-toast",
+ "");
+ assertEquals(null, output);
+ //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 ");
+ fail("Expected an exception");
+ } catch (ResponseException e) {
+ assertEquals(e.getMessage(),
+ Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode(), e
+ .getResponse().getStatus());
+ }
+ }
+
+ @Test
+ public void testInvokeRpcMethodWithBadMethodName() {
+ try {
+ restconfImpl.invokeRpc("toaster:bad-method", "");
+ fail("Expected an exception");
+ } catch (ResponseException e) {
+ assertEquals(e.getMessage(), Status.NOT_FOUND.getStatusCode(), e
+ .getResponse().getStatus());
+ }
+ }
+
+ @Test
+ public void testInvokeRpcMethodWithInput() {
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(true);
+
+ 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(rpcResult);
+
+ restconfImpl.setBroker(brokerFacade);
+
+ StructuredData output = restconfImpl.invokeRpc("toaster:make-toast",
+ payload);
+ assertEquals(null, output);
+ //additional validation in the fact that the restconfImpl does not throw an exception.
+ }
+
+ @Test
+ public void testThrowExceptionWhenSlashInModuleName() {
+ try {
+ restconfImpl.invokeRpc("toaster/slash", "");
+ fail("Expected an exception.");
+ } catch (ResponseException e) {
+ assertEquals(e.getMessage(), Status.NOT_FOUND.getStatusCode(), e
+ .getResponse().getStatus());
+ }
+ }
+
+ @Test
+ public void testInvokeRpcWithNoPayloadWithOutput_Success() {
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(true);
+
+ CompositeNode compositeNode = mock( CompositeNode.class );
+ when( rpcResult.getResult() ).thenReturn( compositeNode );
+
+ BrokerFacade brokerFacade = mock(BrokerFacade.class);
+ when( brokerFacade.invokeRpc(
+ eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)testOutput")),
+ any( CompositeNode.class ))).thenReturn(rpcResult);
+
+ restconfImpl.setBroker(brokerFacade);
+
+ StructuredData output = restconfImpl.invokeRpc("toaster:testOutput",
+ "");
+ assertNotNull( output );
+ assertSame( compositeNode, output.getData() );
+ assertNotNull( output.getSchema() );
+ }
+
+ @Test
+ public void testMountedRpcCallNoPayload_Success() throws Exception
+ {
+ RpcResult<CompositeNode> rpcResult = mock(RpcResult.class);
+ when(rpcResult.isSuccessful()).thenReturn(true);
+
+ ListenableFuture<RpcResult<CompositeNode>> mockListener = mock( ListenableFuture.class );
+ when( mockListener.get() ).thenReturn( rpcResult );
+
+ QName cancelToastQName = QName.create( "cancelToast" );
+
+ 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 );
+
+ 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 );
+ when( mockedContext.toMountPointIdentifier( "opendaylight-inventory:nodes/node/"
+ + "REMOTE_HOST/yang-ext:mount/toaster:cancel-toast" ) ).thenReturn( mockedInstanceId );
+
+ restconfImpl.setControllerContext( mockedContext );
+ StructuredData output = restconfImpl.invokeRpc(
+ "opendaylight-inventory:nodes/node/REMOTE_HOST/yang-ext:mount/toaster:cancel-toast",
+ "");
+ assertEquals(null, output);
+
+ //additional validation in the fact that the restconfImpl does not throw an exception.
+ }
+
+