From: Vaclav Demcak Date: Wed, 17 Jun 2015 15:01:59 +0000 (+0200) Subject: Bug 3650 - Can not invoke zero argument RPC X-Git-Tag: release/lithium~5 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=3ba204f4a30847344ddb8c9835914bfda0eed01f Bug 3650 - Can not invoke zero argument RPC Note: RpcDefinition hasn't any direct children. It contains only INPUT and OUTPUT containters. But old functions have maped RPC result automaticly without check exist OUTPUT ContanerSchemaNode. * fix invokeRpc methods to rpcResponse OUTPUT processing. * add tests for invokeRpc methods * fix broken test from json.to.nn.test package Change-Id: I39b8980156928d2011c0151eb798195d84614429 Signed-off-by: Jan Hajnar Signed-off-by: Vaclav Demcak --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/api/RestconfService.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/api/RestconfService.java index e95d61cae0..f11ea8c9d7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/api/RestconfService.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/api/RestconfService.java @@ -100,7 +100,6 @@ public interface RestconfService { @Produces({ Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + XML, Draft02.MediaTypes.DATA + JSON, Draft02.MediaTypes.DATA + XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML }) - @Deprecated // method isn't use anywhere public NormalizedNodeContext invokeRpc(@Encoded @PathParam("identifier") String identifier, @DefaultValue("") String noPayload, @Context UriInfo uriInfo); diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index 6a7267742e..c2774b5812 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -92,7 +92,6 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.EmptyType; -import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; @@ -430,6 +429,8 @@ public class RestconfImpl implements RestconfService { @Override public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) { + Preconditions.checkArgument(payload.getInstanceIdentifierContext().getSchemaNode() != null); + Preconditions.checkArgument(payload.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); final SchemaPath type = payload.getInstanceIdentifierContext().getSchemaNode().getPath(); final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace(); final CheckedFuture response; @@ -453,19 +454,13 @@ public class RestconfImpl implements RestconfService { } final DOMRpcResult result = checkRpcResponse(response); + final RpcDefinition rpcSchemaNode = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode(); - RpcDefinition resultNodeSchema = null; - final NormalizedNode resultData = result.getResult(); - if (result != null && result.getResult() != null) { - resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode(); - } - - return new NormalizedNodeContext(new InstanceIdentifierContext(null, - resultNodeSchema, mountPoint, schemaContext), resultData, - QueryParametersParser.parseWriterParameters(uriInfo)); + return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcSchemaNode, mountPoint, + schemaContext), result.getResult(), QueryParametersParser.parseWriterParameters(uriInfo)); } - private DOMRpcResult checkRpcResponse(final CheckedFuture response) { + private static DOMRpcResult checkRpcResponse(final CheckedFuture response) { if (response == null) { return null; } @@ -489,13 +484,11 @@ public class RestconfImpl implements RestconfService { } if (cause instanceof IllegalArgumentException) { - throw new RestconfDocumentedException(cause.getMessage(), ErrorType.PROTOCOL, - ErrorTag.INVALID_VALUE); + throw new RestconfDocumentedException(cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.",cause); - } else { - throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.",e); } + throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.", e); } catch (final CancellationException e) { final String errMsg = "The operation was cancelled while executing."; LOG.debug("Cancel RpcExecution: " + errMsg, e); @@ -503,7 +496,7 @@ public class RestconfImpl implements RestconfService { } } - private void validateInput(final SchemaNode inputSchema, final NormalizedNodeContext payload) { + private static void validateInput(final SchemaNode inputSchema, final NormalizedNodeContext payload) { if (inputSchema != null && payload.getData() == null) { // expected a non null payload throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE); @@ -553,7 +546,11 @@ public class RestconfImpl implements RestconfService { throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } - final QName outputQname = QName.create(rpcQName, "output"); + final RpcDefinition rpcSchemaNode = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode(); + RestconfValidationUtils.checkDocumentedError(rpcSchemaNode != null, ErrorType.RPC, ErrorTag.INVALID_VALUE, + "Create stream RPC output can not be null!"); + + final QName outputQname = rpcSchemaNode.getOutput().getQName(); final QName streamNameQname = QName.create(rpcQName, "stream-name"); final ContainerNode output = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(outputQname)) @@ -564,7 +561,6 @@ public class RestconfImpl implements RestconfService { } final DOMRpcResult defaultDOMRpcResult = new DefaultDOMRpcResult(output); - return Futures.immediateCheckedFuture(defaultDOMRpcResult); } @@ -630,20 +626,11 @@ public class RestconfImpl implements RestconfService { final DOMRpcResult result = checkRpcResponse(response); - DataSchemaNode resultNodeSchema = null; - NormalizedNode resultData = null; - if (result != null && result.getResult() != null) { - resultData = result.getResult(); - final ContainerSchemaNode rpcDataSchemaNode = - SchemaContextUtil.getRpcDataSchema(schemaContext, rpc.getOutput().getPath()); - resultNodeSchema = rpcDataSchemaNode.getDataChildByName(result.getResult().getNodeType()); - } - - return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint, - schemaContext), resultData, QueryParametersParser.parseWriterParameters(uriInfo)); + return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpc, mountPoint, schemaContext), + result.getResult(), QueryParametersParser.parseWriterParameters(uriInfo)); } - private RpcDefinition findRpc(final SchemaContext schemaContext, final String identifierDecoded) { + private static RpcDefinition findRpc(final SchemaContext schemaContext, final String identifierDecoded) { final String[] splittedIdentifier = identifierDecoded.split(":"); if (splittedIdentifier.length != 2) { final String errMsg = identifierDecoded + " couldn't be splitted to 2 parts (module:rpc name)"; @@ -753,7 +740,7 @@ public class RestconfImpl implements RestconfService { return Response.status(Status.OK).build(); } - private void validateTopLevelNodeName(final NormalizedNodeContext node, + private static void validateTopLevelNodeName(final NormalizedNodeContext node, final YangInstanceIdentifier identifier) { final String payloadName = node.getData().getNodeType().getLocalName(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java index 35a6162c95..e1a254587f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java @@ -11,16 +11,13 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - import java.lang.reflect.Field; import java.util.Collections; - import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Request; import javax.ws.rs.core.UriInfo; - import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.controller.sal.rest.api.RestconfConstants; import org.opendaylight.controller.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider; @@ -39,42 +36,36 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; */ public abstract class AbstractBodyReaderTest { - protected final static ControllerContext controllerContext = ControllerContext - .getInstance(); + protected final static ControllerContext controllerContext = ControllerContext.getInstance(); protected final MediaType mediaType; private static Field uriField; private static Field requestField; - public AbstractBodyReaderTest() throws NoSuchFieldException, - SecurityException { - uriField = AbstractIdentifierAwareJaxRsProvider.class - .getDeclaredField("uriInfo"); + public AbstractBodyReaderTest() throws NoSuchFieldException, SecurityException { + uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo"); + requestField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("request"); uriField.setAccessible(true); - requestField = AbstractIdentifierAwareJaxRsProvider.class - .getDeclaredField("request"); requestField.setAccessible(true); mediaType = getMediaType(); } protected abstract MediaType getMediaType(); - protected static SchemaContext schemaContextLoader(final String yangPath, - final SchemaContext schemaContext) { + protected static SchemaContext schemaContextLoader(final String yangPath, final SchemaContext schemaContext) { return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext); } - protected static void mockBodyReader( - final String identifier, final T normalizedNodeProvider, - final boolean isPost) throws NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { + protected static UriInfo mockBodyReader( + final String identifier, final T normalizedNodeProvider, final boolean isPost) throws Exception { final UriInfo uriInfoMock = mock(UriInfo.class); - final MultivaluedMap pathParm = new MultivaluedHashMap<>( - 1); - pathParm.put(RestconfConstants.IDENTIFIER, - Collections.singletonList(identifier)); + final MultivaluedMap pathParm = new MultivaluedHashMap<>(1); + pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier)); + when(uriInfoMock.getQueryParameters()).thenReturn(pathParm); when(uriInfoMock.getPathParameters()).thenReturn(pathParm); when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm); + when(uriInfoMock.getQueryParameters(false)).thenReturn(pathParm); when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm); + when(uriInfoMock.getQueryParameters(true)).thenReturn(pathParm); uriField.set(normalizedNodeProvider, uriInfoMock); final Request request = mock(Request.class); if (isPost) { @@ -83,21 +74,18 @@ public abstract class AbstractBodyReaderTest { when(request.getMethod()).thenReturn("PUT"); } requestField.set(normalizedNodeProvider, request); + return uriInfoMock; } - protected static void checkMountPointNormalizedNodeContext( - final NormalizedNodeContext nnContext) { + protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) { checkNormalizedNodeContext(nnContext); assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint()); } - protected static void checkNormalizedNodeContext( - final NormalizedNodeContext nnContext) { + protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) { assertNotNull(nnContext.getData()); - assertNotNull(nnContext.getInstanceIdentifierContext() - .getInstanceIdentifier()); - assertNotNull(nnContext.getInstanceIdentifierContext() - .getSchemaContext()); + assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier()); + assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext()); assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode()); } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeMountPointRpc.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeMountPointRpc.java new file mode 100644 index 0000000000..c2b7ee057c --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeMountPointRpc.java @@ -0,0 +1,276 @@ +package org.opendaylight.controller.sal.rest.impl.test.providers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import com.google.common.base.Optional; +import com.google.common.base.Strings; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; +import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession; +import org.opendaylight.controller.sal.rest.api.RestconfService; +import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader; +import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter; +import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext; +import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; + +public class TestInvokeMountPointRpc extends AbstractBodyReaderTest { + + private final JsonNormalizedNodeBodyReader jsonBodyReader; + private final NormalizedNodeJsonBodyWriter jsonBodyWriter; + private final RestconfService restconfService; + private final BrokerFacade brokerFacade; + private static SchemaContext schemaContext; + private final DOMRpcService rpcService; + + public TestInvokeMountPointRpc() throws NoSuchFieldException, SecurityException { + super(); + jsonBodyReader = new JsonNormalizedNodeBodyReader(); + jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); + BrokerFacade.getInstance().setContext(mock(ConsumerSession.class)); + BrokerFacade.getInstance().setDomDataBroker(mock(DOMDataBroker.class)); + rpcService = mock(DOMRpcService.class); + BrokerFacade.getInstance().setRpcService(rpcService); + brokerFacade = BrokerFacade.getInstance(); + RestconfImpl.getInstance().setBroker(brokerFacade); + RestconfImpl.getInstance().setControllerContext(controllerContext); + restconfService = RestconfImpl.getInstance(); + } + + @Override + protected MediaType getMediaType() { + return new MediaType(MediaType.APPLICATION_JSON, null); + } + + /** + * SchemaContext for RPCs initialization + * + * @throws NoSuchFieldException + * @throws SecurityException + */ + @Before + public void initialization() throws NoSuchFieldException, SecurityException { + schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); + schemaContext = schemaContextLoader("/modules", schemaContext); + schemaContext = schemaContextLoader("/invoke-rpc", schemaContext); + final DOMMountPoint mountInstance = mock(DOMMountPoint.class); + when(mountInstance.getSchemaContext()).thenReturn(schemaContext); + when(mountInstance.getService(DOMRpcService.class)).thenReturn(Optional. of(rpcService)); + final DOMMountPointService mockMountService = mock(DOMMountPointService.class); + when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); + + ControllerContext.getInstance().setMountService(mockMountService); + controllerContext.setSchemas(schemaContext); + } + + /** + * Test RPC with Input and Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleInputOutputTest() throws Exception { + final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + + final NormalizedNodeContext inputCx = parseRpcInput("/invoke-rpc/json/rpc-input.json"); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC without Input but with Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputOutputTest() throws Exception { + final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-output-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC without Input but with Output schemaNode definitions post without ContentType + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputOutputWithoutContentTypeTest() throws Exception { + final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-output-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, "", uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC with Input but without Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleInputNoOutputTest() throws Exception { + final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-input-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput("/invoke-rpc/json/rpc-input.json"); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + /** + * Test RPC without Input and Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputNoOutputTest() throws Exception { + final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-noop"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + /** + * Test RPC without Input but with Output schemaNode definitions post without ContentType + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputNoOutputWithoutContentTypeTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-noop"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, "", uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + private NormalizedNodeContext parseRpcInput(final String filePath) throws WebApplicationException, IOException { + final InputStream inStrem; + if (Strings.isNullOrEmpty(filePath)) { + inStrem = new ByteArrayInputStream("".getBytes()); + } else { + inStrem = TestInvokeRpc.class.getResourceAsStream(filePath); + } + return jsonBodyReader.readFrom(null, null, null, mediaType, null, inStrem); + } + + private NormalizedNode parseRpcOutput(final String filePath) throws WebApplicationException, IOException { + final InputStream inStrem = TestInvokeRpc.class.getResourceAsStream(filePath); + return jsonBodyReader.readFrom(null, null, null, mediaType, null, inStrem).getData(); + } +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeRpc.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeRpc.java new file mode 100644 index 0000000000..a6db074bab --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestInvokeRpc.java @@ -0,0 +1,261 @@ +package org.opendaylight.controller.sal.rest.impl.test.providers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import com.google.common.base.Strings; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; +import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession; +import org.opendaylight.controller.sal.rest.api.RestconfService; +import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader; +import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter; +import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; +import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext; +import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; + +public class TestInvokeRpc extends AbstractBodyReaderTest { + + private final JsonNormalizedNodeBodyReader jsonBodyReader; + private final NormalizedNodeJsonBodyWriter jsonBodyWriter; + private final RestconfService restconfService; + private final BrokerFacade brokerFacade; + private static SchemaContext schemaContext; + private final DOMRpcService rpcService; + + public TestInvokeRpc() throws NoSuchFieldException, SecurityException { + super(); + jsonBodyReader = new JsonNormalizedNodeBodyReader(); + jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); + BrokerFacade.getInstance().setContext(mock(ConsumerSession.class)); + BrokerFacade.getInstance().setDomDataBroker(mock(DOMDataBroker.class)); + rpcService = mock(DOMRpcService.class); + BrokerFacade.getInstance().setRpcService(rpcService); + brokerFacade = BrokerFacade.getInstance(); + RestconfImpl.getInstance().setBroker(brokerFacade); + RestconfImpl.getInstance().setControllerContext(controllerContext); + restconfService = RestconfImpl.getInstance(); + } + + @Override + protected MediaType getMediaType() { + return new MediaType(MediaType.APPLICATION_JSON, null); + } + + /** + * SchemaContext for RPCs initialization + * + * @throws NoSuchFieldException + * @throws SecurityException + */ + @BeforeClass + public static void initialization() throws NoSuchFieldException, SecurityException { + schemaContext = schemaContextLoader("/invoke-rpc", schemaContext); + controllerContext.setSchemas(schemaContext); + } + + /** + * Test RPC with Input and Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleInputOutputTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + + final NormalizedNodeContext inputCx = parseRpcInput("/invoke-rpc/json/rpc-input.json"); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), + rpcResultCx.getInstanceIdentifierContext().getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC without Input but with Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputOutputTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-output-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), + rpcResultCx.getInstanceIdentifierContext().getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC without Input but with Output schemaNode definitions post without ContentType + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputOutputWithoutContentTypeTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-output-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + final NormalizedNode outNormNode = parseRpcOutput("/invoke-rpc/json/rpc-output.json"); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(outNormNode); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, "", uriInfo); + assertNotNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), + rpcResultCx.getInstanceIdentifierContext().getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(output.toString().contains("lf-test")); + } + + /** + * Test RPC with Input but without Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleInputNoOutputTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-input-only-test"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput("/invoke-rpc/json/rpc-input.json"); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), + rpcResultCx.getInstanceIdentifierContext().getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + /** + * Test RPC without Input and Output schemaNode definitions + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputNoOutputTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-noop"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, inputCx, uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + /** + * Test RPC without Input but with Output schemaNode definitions post without ContentType + * + * @throws Exception + */ + @Test + public void rpcModuleNoInputNoOutputWithoutContentTypeTest() throws Exception { + final String uri = "invoke-rpc-module:rpc-noop"; + final UriInfo uriInfo = mockBodyReader(uri, jsonBodyReader, true); + final NormalizedNodeContext inputCx = parseRpcInput(null); + final SchemaPath schemaNodePath = inputCx.getInstanceIdentifierContext().getSchemaNode().getPath(); + final NormalizedNode inNormNode = inputCx.getData(); + + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(null, Collections. emptyList()); + final CheckedFuture rpcReturn = Futures.immediateCheckedFuture(rpcResult); + when(rpcService.invokeRpc(schemaNodePath, inNormNode)).thenReturn(rpcReturn); + + final NormalizedNodeContext rpcResultCx = restconfService.invokeRpc(uri, "", uriInfo); + assertNull(rpcResultCx.getData()); + assertTrue(rpcResultCx.getInstanceIdentifierContext().getSchemaNode() instanceof RpcDefinition); + assertEquals(inputCx.getInstanceIdentifierContext().getSchemaNode(), rpcResultCx.getInstanceIdentifierContext() + .getSchemaNode()); + final OutputStream output = new ByteArrayOutputStream(); + jsonBodyWriter.writeTo(rpcResultCx, null, null, null, mediaType, null, output); + assertTrue(Strings.isNullOrEmpty(output.toString())); + } + + private NormalizedNodeContext parseRpcInput(final String filePath) throws WebApplicationException, IOException { + final InputStream inStrem; + if (Strings.isNullOrEmpty(filePath)) { + inStrem = new ByteArrayInputStream("".getBytes()); + } else { + inStrem = TestInvokeRpc.class.getResourceAsStream(filePath); + } + return jsonBodyReader.readFrom(null, null, null, mediaType, null, inStrem); + } + + private NormalizedNode parseRpcOutput(final String filePath) throws WebApplicationException, IOException { + final InputStream inStrem = TestInvokeRpc.class.getResourceAsStream(filePath); + return jsonBodyReader.readFrom(null, null, null, mediaType, null, inStrem).getData(); + } +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java index c1f463309b..8f9c1938f7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java @@ -53,7 +53,7 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { @Override protected MediaType getMediaType() { - return new MediaType(MediaType.APPLICATION_XML, null); + return new MediaType(MediaType.APPLICATION_JSON, null); } @BeforeClass @@ -81,7 +81,7 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { @Test public void moduleSubContainerDataPutTest() throws Exception { final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont"); - QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1"); + final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1"); final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName); final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName); final String uri = "instance-identifier-module:cont/cont1"; @@ -97,7 +97,7 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { @Test public void moduleSubContainerDataPostTest() throws Exception { final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont"); - QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1"); + final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1"); final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName); final String uri = "instance-identifier-module:cont"; mockBodyReader(uri, jsonBodyReader, true); @@ -113,8 +113,8 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { public void moduleSubContainerAugmentDataPostTest() throws Exception { final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont"); final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next(); - QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment"); - YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier( + final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment"); + final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier( Sets.newHashSet(contAugmentQName)); final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()) .node(augII).node(contAugmentQName); @@ -128,17 +128,16 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII); } - //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice - //@Test + @Test public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception { final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont"); final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next(); - QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1"); - QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2"); + final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1"); + final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2"); final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1"); - YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier( + final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier( Sets.newHashSet(augmentChoice1QName)); - YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier( + final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier( Sets.newHashSet(augmentChoice2QName)); final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()) .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName) diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java index e5cda6c94d..4dd059ddad 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java @@ -9,13 +9,10 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static org.junit.Assert.assertTrue; - import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; - import javax.ws.rs.core.MediaType; - import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader; @@ -46,7 +43,7 @@ public class TestJsonBodyWriter extends AbstractBodyReaderTest { @Override protected MediaType getMediaType() { - return new MediaType(MediaType.APPLICATION_XML, null); + return new MediaType(MediaType.APPLICATION_JSON, null); } @BeforeClass diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java index 6577355b4c..8ff598a2b6 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java @@ -9,13 +9,8 @@ package org.opendaylight.controller.sal.restconf.impl.json.to.nn.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; - -import java.io.IOException; import java.io.InputStream; - -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; - import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader; @@ -43,22 +38,20 @@ public class JsonIdentityrefToNnTest extends AbstractBodyReaderTest { } @Test - public void jsonIdentityrefToNn() throws NoSuchFieldException, - SecurityException, IllegalArgumentException, - IllegalAccessException, WebApplicationException, IOException { + public void jsonIdentityrefToNn() throws Exception { - String uri = "identityref-module:cont"; + final String uri = "identityref-module:cont"; mockBodyReader(uri, jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/identityref/json/data.json"); - NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( + final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( null, null, null, mediaType, null, inputStream); assertEquals("cont", normalizedNodeContext.getData().getNodeType() .getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("cont1")); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java index afeddc2960..caccfe4003 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java @@ -9,13 +9,8 @@ package org.opendaylight.controller.sal.restconf.impl.json.to.nn.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; - -import java.io.IOException; import java.io.InputStream; - -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; - import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader; @@ -42,21 +37,19 @@ public class JsonLeafrefToNnTest extends AbstractBodyReaderTest { } @Test - public void jsonIdentityrefToNormalizeNode() throws NoSuchFieldException, - SecurityException, IllegalArgumentException, - IllegalAccessException, WebApplicationException, IOException { + public void jsonIdentityrefToNormalizeNode() throws Exception { - String uri = "leafref-module:cont"; + final String uri = "leafref-module:cont"; mockBodyReader(uri, jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/leafref/json/data.json"); - NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( + final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( null, null, null, mediaType, null, inputStream); assertEquals("cont", normalizedNodeContext.getData().getNodeType() .getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("lf2 121")); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java index c11a570d34..74702ad645 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java @@ -35,35 +35,35 @@ public class JsonToNnTest extends AbstractBodyReaderTest { super(); } - public static void initialize(String path, SchemaContext schemaContext) { + public static void initialize(final String path, SchemaContext schemaContext) { schemaContext = schemaContextLoader(path, schemaContext); controllerContext.setSchemas(schemaContext); } @Test - public void simpleListTest() { + public void simpleListTest() throws Exception { simpleTest("/json-to-nn/simple-list.json", "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1"); } @Test - public void simpleContainerTest() { + public void simpleContainerTest() throws Exception { simpleTest("/json-to-nn/simple-container.json", "/json-to-nn/simple-container-yang", "cont", "simple-container-yang"); } @Test - public void multipleItemsInLeafListTest() { + public void multipleItemsInLeafListTest() throws Exception { initialize("/json-to-nn/simple-list-yang/1", schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC( + final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/multiple-leaflist-items.json", "simple-list-yang1:lst"); assertNotNull(normalizedNodeContext); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("45")); assertTrue(dataTree.contains("55")); @@ -71,10 +71,10 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void multipleItemsInListTest() { + public void multipleItemsInListTest() throws Exception { initialize("/json-to-nn/simple-list-yang/3", schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC( + final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/multiple-items-in-list.json", "multiple-items-yang:lst"); assertNotNull(normalizedNodeContext); @@ -86,26 +86,24 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void nullArrayToSimpleNodeWithNullValueTest() { + public void nullArrayToSimpleNodeWithNullValueTest() throws Exception { initialize("/json-to-nn/simple-list-yang/4", schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC( + final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/array-with-null.json", "array-with-null-yang:cont"); assertNotNull(normalizedNodeContext); assertEquals("cont", normalizedNodeContext.getData().getNodeType() .getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("lf")); assertTrue(dataTree.contains("null")); } @Test - public void incorrectTopLevelElementsTest() throws WebApplicationException, - IOException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException { + public void incorrectTopLevelElementsTest() throws Exception { jsonBodyReader = new JsonNormalizedNodeBodyReader(); initialize("/json-to-nn/simple-list-yang/1", schemaContext); @@ -120,7 +118,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { exception = e; countExceptions++; } @@ -134,7 +132,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { exception = e; countExceptions++; } @@ -148,7 +146,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { exception = e; countExceptions++; } @@ -159,20 +157,18 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void emptyDataReadTest() throws WebApplicationException, - IOException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException { + public void emptyDataReadTest() throws Exception { initialize("/json-to-nn/simple-list-yang/4", schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC( + final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/empty-data.json", "array-with-null-yang:cont"); assertNotNull(normalizedNodeContext); assertEquals("cont", normalizedNodeContext.getData().getNodeType() .getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("lflst1")); @@ -182,13 +178,13 @@ public class JsonToNnTest extends AbstractBodyReaderTest { jsonBodyReader = new JsonNormalizedNodeBodyReader(); RestconfDocumentedException exception = null; mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/empty-data.json1"); try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { exception = e; } assertNotNull(exception); @@ -197,46 +193,42 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void testJsonBlankInput() throws NoSuchFieldException, - SecurityException, IllegalArgumentException, - IllegalAccessException, WebApplicationException, IOException { + public void testJsonBlankInput() throws Exception { initialize("/json-to-nn/simple-list-yang/4", schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC("", + final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont"); assertNull(normalizedNodeContext); } @Test - public void notSupplyNamespaceIfAlreadySupplied() - throws WebApplicationException, IOException, NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { + public void notSupplyNamespaceIfAlreadySupplied() throws Exception { initialize("/json-to-nn/simple-list-yang/1", schemaContext); - String uri = "simple-list-yang1" + ":" + "lst"; + final String uri = "simple-list-yang1" + ":" + "lst"; - NormalizedNodeContext normalizedNodeContext = prepareNNC( + final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/simple-list.json", uri); assertNotNull(normalizedNodeContext); verifyNormaluizedNodeContext(normalizedNodeContext, "lst"); mockBodyReader("simple-list-yang2:lst", jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/simple-list.json"); try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); fail("NormalizedNodeContext should not be create because of different namespace"); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { } verifyNormaluizedNodeContext(normalizedNodeContext, "lst"); } @Test - public void dataAugmentedTest() { + public void dataAugmentedTest() throws Exception { initialize("/common/augment/yang", schemaContext); @@ -265,19 +257,20 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } private void simpleTest(final String jsonPath, final String yangPath, - final String topLevelElementName, final String moduleName) { + final String topLevelElementName, + final String moduleName) throws Exception { initialize(yangPath, schemaContext); - String uri = moduleName + ":" + topLevelElementName; + final String uri = moduleName + ":" + topLevelElementName; - NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri); + final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri); assertNotNull(normalizedNodeContext); verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName); } - private NormalizedNodeContext prepareNNC(String jsonPath, String uri) { + private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception { jsonBodyReader = new JsonNormalizedNodeBodyReader(); try { mockBodyReader(uri, jsonBodyReader, false); @@ -286,7 +279,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { // TODO Auto-generated catch block e.printStackTrace(); } - InputStream inputStream = this.getClass().getResourceAsStream(jsonPath); + final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath); NormalizedNodeContext normalizedNodeContext = null; @@ -302,12 +295,12 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } private void verifyNormaluizedNodeContext( - NormalizedNodeContext normalizedNodeContext, - String topLevelElementName) { + final NormalizedNodeContext normalizedNodeContext, + final String topLevelElementName) { assertEquals(topLevelElementName, normalizedNodeContext.getData() .getNodeType().getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("cont1")); assertTrue(dataTree.contains("lst1")); @@ -320,7 +313,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { private void verityMultipleItemsInList( final NormalizedNodeContext normalizedNodeContext) { - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("lf11")); assertTrue(dataTree.contains("lf11_1")); @@ -331,14 +324,12 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void unsupportedDataFormatTest() throws NoSuchFieldException, - SecurityException, IllegalArgumentException, - IllegalAccessException, WebApplicationException, IOException { + public void unsupportedDataFormatTest() throws Exception { jsonBodyReader = new JsonNormalizedNodeBodyReader(); initialize("/json-to-nn/simple-list-yang/1", schemaContext); mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/unsupported-json-format.json"); RestconfDocumentedException exception = null; @@ -346,7 +337,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { try { jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream); - } catch (RestconfDocumentedException e) { + } catch (final RestconfDocumentedException e) { exception = e; } System.out.println(exception.getErrors().get(0).getErrorMessage()); @@ -356,25 +347,23 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } @Test - public void invalidUriCharacterInValue() throws NoSuchFieldException, - SecurityException, IllegalArgumentException, - IllegalAccessException, WebApplicationException, IOException { + public void invalidUriCharacterInValue() throws Exception { jsonBodyReader = new JsonNormalizedNodeBodyReader(); initialize("/json-to-nn/simple-list-yang/4", schemaContext); mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false); - InputStream inputStream = this.getClass().getResourceAsStream( + final InputStream inputStream = this.getClass().getResourceAsStream( "/json-to-nn/invalid-uri-character-in-value.json"); - NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( + final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom( null, null, null, mediaType, null, inputStream); assertNotNull(normalizedNodeContext); assertEquals("cont", normalizedNodeContext.getData().getNodeType() .getLocalName()); - String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext + final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext .getData()); assertTrue(dataTree.contains("lf1 moduleName:value lf2")); 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 731d6a2bc8..8ed2f48ee1 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 @@ -320,6 +320,7 @@ public class InvokeRpcMethodTest { final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo); assertNotNull(output); assertEquals(null, output.getData()); + assertEquals(rpcDef, output.getInstanceIdentifierContext().getSchemaNode()); // additional validation in the fact that the restconfImpl does not // throw an exception. } @@ -379,6 +380,7 @@ public class InvokeRpcMethodTest { assertNotNull(output); assertNotNull(output.getData()); assertSame(container, output.getData()); + assertEquals(rpcDef, output.getInstanceIdentifierContext().getSchemaNode()); assertNotNull(output.getInstanceIdentifierContext()); assertNotNull(output.getInstanceIdentifierContext().getSchemaContext()); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/invoke-rpc/invoke-rpc-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/invoke-rpc/invoke-rpc-module.yang index ad4883c064..cfcbc1a471 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/invoke-rpc/invoke-rpc-module.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/invoke-rpc/invoke-rpc-module.yang @@ -2,9 +2,9 @@ module invoke-rpc-module { namespace "invoke:rpc:module"; prefix "inrpcmod"; - + revision 2013-12-3 { - + } rpc rpc-test { @@ -27,4 +27,23 @@ module invoke-rpc-module { rpc rpc-noop { } + rpc rpc-input-only-test { + input { + container cont { + leaf lf { + type string; + } + } + } + } + + rpc rpc-output-only-test { + output { + container cont-out { + leaf lf-out { + type string; + } + } + } + } } \ No newline at end of file