Refactor OpenApiTestUtils methods 35/107835/3
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 12 Sep 2023 13:22:15 +0000 (15:22 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Wed, 13 Sep 2023 06:46:26 +0000 (06:46 +0000)
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 <ivan.hrasko@pantheon.tech>
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/OpenApiTestUtils.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/KeysMappingTest.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/OpenApiGeneratorRFC8040Test.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/mountpoints/MountPointOpenApiTest.java

index 3091f9f0110918a1c02540c03f5a6ac147a07b85..03b7fbb859b8abb7e2b2d84f4aef11ca06d480c7 100644 (file)
@@ -19,11 +19,24 @@ public final class OpenApiTestUtils {
     }
 
     /**
-     * Get path parameters name for {@code path}.
+     * Get path parameters names for {@code path} for GET operation.
+     *
+     * @return {@link List} of parameters excluding `content` parameter
+     */
+    public static List<String> getPathGetParameters(final Map<String, Path> paths, final String path) {
+        return paths.get(path).get().parameters()
+            .stream()
+            .map(Parameter::name)
+            .filter(p -> !"content".equals(p))
+            .toList();
+    }
+
+    /**
+     * Get path parameters names for {@code path} for POST operation.
      *
      * @return {@link List} of parameters
      */
-    public static List<String> getPathParameters(final Map<String, Path> paths, final String path) {
+    public static List<String> getPathPostParameters(final Map<String, Path> paths, final String path) {
         return paths.get(path).post().parameters()
             .stream()
             .map(Parameter::name)
index 4154851991ecdf32e795c55223e1b9b1feb7c67b..90c5c142ae611817a2ea998635d62c2f77a9060a 100644 (file)
@@ -12,7 +12,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathParameters;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathGetParameters;
 
 import java.util.List;
 import org.junit.BeforeClass;
@@ -86,6 +86,6 @@ public class KeysMappingTest {
             + "/multiple-key-list2={name1},{name3}/multiple-key-list3={name31},{name4}/multiple-key-list4={name5}";
         assertTrue(doc.paths().containsKey(pathToMultipleKeyList4));
         assertEquals(List.of("name","name2", "name1", "name3", "name31", "name4", "name5"),
-            getPathParameters(doc.paths(), pathToMultipleKeyList4));
+            getPathGetParameters(doc.paths(), pathToMultipleKeyList4));
     }
 }
index d7557c2be48e0d9dd698be9fb31bf137fe808588..1359a1e194866fb5f766b4d6e8be68dbde891529 100644 (file)
@@ -14,7 +14,8 @@ 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 static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathParameters;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathGetParameters;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathPostParameters;
 import static org.opendaylight.restconf.openapi.impl.BaseYangOpenApiGenerator.BASIC_AUTH_NAME;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -249,23 +250,23 @@ public final class OpenApiGeneratorRFC8040Test {
 
         var pathToList1 = "/rests/data/path-params-test:cont/list1={name}";
         assertTrue(doc.paths().containsKey(pathToList1));
-        assertEquals(List.of("name"), getPathParameters(doc.paths(), pathToList1));
+        assertEquals(List.of("name"), getPathGetParameters(doc.paths(), pathToList1));
 
         var pathToList2 = "/rests/data/path-params-test:cont/list1={name}/list2={name1}";
         assertTrue(doc.paths().containsKey(pathToList2));
-        assertEquals(List.of("name", "name1"), getPathParameters(doc.paths(), pathToList2));
+        assertEquals(List.of("name", "name1"), getPathGetParameters(doc.paths(), pathToList2));
 
         var pathToList3 = "/rests/data/path-params-test:cont/list3={name}";
         assertTrue(doc.paths().containsKey(pathToList3));
-        assertEquals(List.of("name"), getPathParameters(doc.paths(), pathToList3));
+        assertEquals(List.of("name"), getPathGetParameters(doc.paths(), pathToList3));
 
         var pathToList4 = "/rests/data/path-params-test:cont/list1={name}/list4={name1}";
         assertTrue(doc.paths().containsKey(pathToList4));
-        assertEquals(List.of("name", "name1"), getPathParameters(doc.paths(), pathToList4));
+        assertEquals(List.of("name", "name1"), getPathGetParameters(doc.paths(), pathToList4));
 
         var pathToList5 = "/rests/data/path-params-test:cont/list1={name}/cont2";
         assertTrue(doc.paths().containsKey(pathToList4));
-        assertEquals(List.of("name"), getPathParameters(doc.paths(), pathToList5));
+        assertEquals(List.of("name"), getPathGetParameters(doc.paths(), pathToList5));
     }
 
     /**
@@ -277,11 +278,11 @@ public final class OpenApiGeneratorRFC8040Test {
 
         final var pathWithParameters = "/rests/operations/action-types:list={name}/list-action";
         assertTrue(doc.paths().containsKey(pathWithParameters));
-        assertEquals(List.of("name"), getPathParameters(doc.paths(), pathWithParameters));
+        assertEquals(List.of("name"), getPathPostParameters(doc.paths(), pathWithParameters));
 
         final var pathWithoutParameters = "/rests/operations/action-types:multi-container/inner-container/action";
         assertTrue(doc.paths().containsKey(pathWithoutParameters));
-        assertEquals(List.of(), getPathParameters(doc.paths(), pathWithoutParameters));
+        assertEquals(List.of(), getPathPostParameters(doc.paths(), pathWithoutParameters));
     }
 
     @Test
index 54eea0f52d2d89f8dc35ad94df737ea91d97920b..7b7d106e7a33865b5c280edd6809b75aafe98e95 100644 (file)
@@ -12,7 +12,8 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathParameters;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathGetParameters;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathPostParameters;
 import static org.opendaylight.restconf.openapi.impl.BaseYangOpenApiGenerator.BASIC_AUTH_NAME;
 
 import java.util.ArrayList;
@@ -230,23 +231,23 @@ public final class MountPointOpenApiTest {
 
         var pathToList1 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}";
         assertTrue(mountPointApi.paths().containsKey(pathToList1));
-        assertEquals(List.of("name"), getPathParameters(mountPointApi.paths(), pathToList1));
+        assertEquals(List.of("name"), getPathGetParameters(mountPointApi.paths(), pathToList1));
 
         var pathToList2 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list2={name1}";
         assertTrue(mountPointApi.paths().containsKey(pathToList2));
-        assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.paths(), pathToList2));
+        assertEquals(List.of("name", "name1"), getPathGetParameters(mountPointApi.paths(), pathToList2));
 
         var pathToList3 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list3={name}";
         assertTrue(mountPointApi.paths().containsKey(pathToList3));
-        assertEquals(List.of("name"), getPathParameters(mountPointApi.paths(), pathToList3));
+        assertEquals(List.of("name"), getPathGetParameters(mountPointApi.paths(), pathToList3));
 
         var pathToList4 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list4={name1}";
         assertTrue(mountPointApi.paths().containsKey(pathToList4));
-        assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.paths(), pathToList4));
+        assertEquals(List.of("name", "name1"), getPathGetParameters(mountPointApi.paths(), pathToList4));
 
         var pathToList5 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/cont2";
         assertTrue(mountPointApi.paths().containsKey(pathToList5));
-        assertEquals(List.of("name"), getPathParameters(mountPointApi.paths(), pathToList5));
+        assertEquals(List.of("name"), getPathGetParameters(mountPointApi.paths(), pathToList5));
     }
 
     /**
@@ -263,12 +264,12 @@ public final class MountPointOpenApiTest {
         final var pathWithParameters =
             "/rests/operations/nodes/node=123/yang-ext:mount/action-types:list={name}/list-action";
         assertTrue(mountPointApi.paths().containsKey(pathWithParameters));
-        assertEquals(List.of("name"), getPathParameters(mountPointApi.paths(), pathWithParameters));
+        assertEquals(List.of("name"), getPathPostParameters(mountPointApi.paths(), pathWithParameters));
 
         final var pathWithoutParameters =
             "/rests/operations/nodes/node=123/yang-ext:mount/action-types:multi-container/inner-container/action";
         assertTrue(mountPointApi.paths().containsKey(pathWithoutParameters));
-        assertEquals(List.of(), getPathParameters(mountPointApi.paths(), pathWithoutParameters));
+        assertEquals(List.of(), getPathPostParameters(mountPointApi.paths(), pathWithoutParameters));
     }
 
     /**