From b3e9dde5bb2810407c35a508482f260ee479ec5d Mon Sep 17 00:00:00 2001 From: Samuel Schneider Date: Tue, 24 Oct 2023 15:13:42 +0200 Subject: [PATCH] Refactor OpenApiTestUtils methods Change getPathParameters into getPathGetParameters and add getPathPostParameters. We assume that all paths present in OpenApi should have at least GET example in the documentation. Thus we can use GET operation as a base operation to get number of parameters in path. We just need to drop 'content' parameter. For actions and RPCs we can use getPathPostParameters method. JIRA: NETCONF-1101 Change-Id: I2a0a24a5a567d7dd95b7adf6ec13dddb391fab51 Signed-off-by: Ivan Hrasko Signed-off-by: Samuel Schneider (cherry picked from commit 88403faca52518bc4d9674190f17fe934e6f23d1) --- .../sal/rest/doc/impl/AbstractApiDocTest.java | 19 ++++++++++++++++++- .../doc/impl/ApiDocGeneratorRFC8040Test.java | 14 +++++++------- .../rest/doc/impl/MountPointSwaggerTest.java | 14 +++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/AbstractApiDocTest.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/AbstractApiDocTest.java index 55debd2189..05b0076d08 100644 --- a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/AbstractApiDocTest.java +++ b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/AbstractApiDocTest.java @@ -32,10 +32,27 @@ public abstract class AbstractApiDocTest { URI_INFO = DocGenTestHelper.createMockUriInfo("http://localhost/path"); } - protected static List getPathParameters(final ObjectNode paths, final String path) { + protected static List getPathPostParameters(final ObjectNode paths, final String path) { final var params = new ArrayList(); paths.get(path).get("post").get("parameters").elements() .forEachRemaining(p -> params.add(p.get("name").asText())); return params; } + + /** + * Get path parameters names for {@code path} for GET operation. + * + * @return {@link List} of parameters excluding `content` parameter + */ + public static List getPathGetParameters(final ObjectNode paths, final String path) { + final var params = new ArrayList(); + paths.get(path).get("get").get("parameters").elements() + .forEachRemaining(p -> { + final String name = p.get("name").asText(); + if (!"content".equals(name)) { + params.add(name); + } + }); + return params; + } } diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocGeneratorRFC8040Test.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocGeneratorRFC8040Test.java index 9d525ca6fe..c9a7ab179d 100644 --- a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocGeneratorRFC8040Test.java +++ b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocGeneratorRFC8040Test.java @@ -226,23 +226,23 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest { var pathToList1 = "/rests/data/path-params-test:cont/list1={name}"; assertTrue(doc.getPaths().has(pathToList1)); - assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList1)); + assertEquals(List.of("name"), getPathGetParameters(doc.getPaths(), pathToList1)); var pathToList2 = "/rests/data/path-params-test:cont/list1={name}/list2={name1}"; assertTrue(doc.getPaths().has(pathToList2)); - assertEquals(List.of("name", "name1"), getPathParameters(doc.getPaths(), pathToList2)); + assertEquals(List.of("name", "name1"), getPathGetParameters(doc.getPaths(), pathToList2)); var pathToList3 = "/rests/data/path-params-test:cont/list3={name}"; assertTrue(doc.getPaths().has(pathToList3)); - assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList3)); + assertEquals(List.of("name"), getPathGetParameters(doc.getPaths(), pathToList3)); var pathToList4 = "/rests/data/path-params-test:cont/list1={name}/list4={name1}"; assertTrue(doc.getPaths().has(pathToList4)); - assertEquals(List.of("name", "name1"), getPathParameters(doc.getPaths(), pathToList4)); + assertEquals(List.of("name", "name1"), getPathGetParameters(doc.getPaths(), pathToList4)); var pathToList5 = "/rests/data/path-params-test:cont/list1={name}/cont2"; assertTrue(doc.getPaths().has(pathToList4)); - assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList5)); + assertEquals(List.of("name"), getPathGetParameters(doc.getPaths(), pathToList5)); } private static void verifyThatPropertyDoesNotHaveRequired(final List expected, @@ -299,11 +299,11 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest { final var pathWithParameters = "/rests/operations/action-types:list={name}/list-action"; assertTrue(doc.getPaths().has(pathWithParameters)); - assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathWithParameters)); + assertEquals(List.of("name"), getPathPostParameters(doc.getPaths(), pathWithParameters)); final var pathWithoutParameters = "/rests/operations/action-types:multi-container/inner-container/action"; assertTrue(doc.getPaths().has(pathWithoutParameters)); - assertEquals(List.of(), getPathParameters(doc.getPaths(), pathWithoutParameters)); + assertEquals(List.of(), getPathPostParameters(doc.getPaths(), pathWithoutParameters)); } @Test diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/MountPointSwaggerTest.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/MountPointSwaggerTest.java index c1db115ba1..623a166acf 100644 --- a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/MountPointSwaggerTest.java +++ b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/MountPointSwaggerTest.java @@ -141,23 +141,23 @@ public final class MountPointSwaggerTest extends AbstractApiDocTest { var pathToList1 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}"; assertTrue(mountPointApi.getPaths().has(pathToList1)); - assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList1)); + assertEquals(List.of("name"), getPathGetParameters(mountPointApi.getPaths(), pathToList1)); var pathToList2 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list2={name1}"; assertTrue(mountPointApi.getPaths().has(pathToList2)); - assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.getPaths(), pathToList2)); + assertEquals(List.of("name", "name1"), getPathGetParameters(mountPointApi.getPaths(), pathToList2)); var pathToList3 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list3={name}"; assertTrue(mountPointApi.getPaths().has(pathToList3)); - assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList3)); + assertEquals(List.of("name"), getPathGetParameters(mountPointApi.getPaths(), pathToList3)); var pathToList4 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list4={name1}"; assertTrue(mountPointApi.getPaths().has(pathToList4)); - assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.getPaths(), pathToList4)); + assertEquals(List.of("name", "name1"), getPathGetParameters(mountPointApi.getPaths(), pathToList4)); var pathToList5 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/cont2"; assertTrue(mountPointApi.getPaths().has(pathToList5)); - assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList5)); + assertEquals(List.of("name"), getPathGetParameters(mountPointApi.getPaths(), pathToList5)); } /** @@ -193,12 +193,12 @@ public final class MountPointSwaggerTest extends AbstractApiDocTest { final var pathWithParameters = "/rests/operations/nodes/node=123/yang-ext:mount/action-types:list={name}/list-action"; assertTrue(mountPointApi.getPaths().has(pathWithParameters)); - assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathWithParameters)); + assertEquals(List.of("name"), getPathPostParameters(mountPointApi.getPaths(), pathWithParameters)); final var pathWithoutParameters = "/rests/operations/nodes/node=123/yang-ext:mount/action-types:multi-container/inner-container/action"; assertTrue(mountPointApi.getPaths().has(pathWithoutParameters)); - assertEquals(List.of(), getPathParameters(mountPointApi.getPaths(), pathWithoutParameters)); + assertEquals(List.of(), getPathPostParameters(mountPointApi.getPaths(), pathWithoutParameters)); } @Test -- 2.36.6