Add unit test for request parameters 96/105896/18
authorOleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Fri, 12 May 2023 09:21:41 +0000 (11:21 +0200)
committerOleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Mon, 22 May 2023 14:57:07 +0000 (16:57 +0200)
Added unit test that checks for correct amount of parameters in
requests.

JIRA: NETCONF-1023
Change-Id: I9a448173c5d066ff019cf6b3d07b720d75a73e40
Signed-off-by: OleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
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
restconf/restconf-openapi/src/test/resources/yang/recursive.yang [new file with mode: 0644]

index ce132bb913f0f3c36ea222072a0cb98ec6c49783..e4944694becf1a4e9d1b6b88463fa4a2cea09f69 100644 (file)
@@ -30,6 +30,7 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
     private static final String NAME_2 = "toaster";
     private static final String REVISION_DATE_2 = "2009-11-20";
     private static final String CHOICE_TEST_MODULE = "choice-test";
+    private static final String RECURSIVE_TEST_MODULE = "recursive";
 
     private final OpenApiGeneratorRFC8040 generator = new OpenApiGeneratorRFC8040(SCHEMA_SERVICE);
 
@@ -171,4 +172,49 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
         assertTrue(secondContainer.getProperties().has("leaf-first-case"));
         assertFalse(secondContainer.getProperties().has("leaf-second-case"));
     }
+
+    /**
+     * Test that checks for correct amount of parameters in requests.
+     */
+    @Test
+    public void testRecursiveParameters() {
+        final var configPaths = Map.of("/rests/data/recursive:container-root", 0,
+            "/rests/data/recursive:container-root/root-list={name}", 1,
+            "/rests/data/recursive:container-root/root-list={name}/nested-list={name1}", 2,
+            "/rests/data/recursive:container-root/root-list={name}/nested-list={name1}/super-nested-list={name2}", 3);
+
+        final var module = CONTEXT.findModule(RECURSIVE_TEST_MODULE, Revision.of("2023-05-22")).orElseThrow();
+        final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+        assertNotNull(doc);
+
+        final var paths = doc.getPaths();
+        assertEquals(5, paths.size());
+
+        for (final var expectedPath : configPaths.entrySet()) {
+            assertTrue(paths.containsKey(expectedPath.getKey()));
+            final int expectedSize = expectedPath.getValue();
+
+            final var path = paths.get(expectedPath.getKey());
+
+            final var get = path.getGet();
+            assertFalse(get.isMissingNode());
+            assertEquals(expectedSize + 1, get.get("parameters").size());
+
+            final var put = path.getPut();
+            assertFalse(put.isMissingNode());
+            assertEquals(expectedSize, put.get("parameters").size());
+
+            final var delete = path.getDelete();
+            assertFalse(delete.isMissingNode());
+            assertEquals(expectedSize, delete.get("parameters").size());
+
+            final var post = path.getPost();
+            assertFalse(post.isMissingNode());
+            assertEquals(expectedSize, post.get("parameters").size());
+
+            final var patch = path.getPatch();
+            assertFalse(patch.isMissingNode());
+            assertEquals(expectedSize, patch.get("parameters").size());
+        }
+    }
 }
index fa16f95a5ed6139fd2b5d187f5c18b5c1a84b0b6..2d7634664e749d3069723215ea8539ee5c178b1a 100644 (file)
@@ -8,7 +8,9 @@
 package org.opendaylight.restconf.openapi.mountpoints;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -110,7 +112,7 @@ public final class MountPointOpenApiTest extends AbstractOpenApiTest {
         final Map<String, Path> paths = mountPointApi.getPaths();
         assertNotNull(paths);
 
-        assertEquals("Unexpected api list size", 27, paths.size());
+        assertEquals("Unexpected api list size", 31, paths.size());
 
         final List<JsonNode> getOperations = new ArrayList<>();
         final List<JsonNode> postOperations = new ArrayList<>();
@@ -126,10 +128,58 @@ public final class MountPointOpenApiTest extends AbstractOpenApiTest {
             Optional.ofNullable(path.getValue().getDelete()).ifPresent(deleteOperations::add);
         }
 
-        assertEquals("Unexpected GET paths size", 19, getOperations.size());
-        assertEquals("Unexpected POST paths size", 25, postOperations.size());
-        assertEquals("Unexpected PUT paths size", 17, putOperations.size());
-        assertEquals("Unexpected PATCH paths size", 17, patchOperations.size());
-        assertEquals("Unexpected DELETE paths size", 17, deleteOperations.size());
+        assertEquals("Unexpected GET paths size", 23, getOperations.size());
+        assertEquals("Unexpected POST paths size", 29, postOperations.size());
+        assertEquals("Unexpected PUT paths size", 21, putOperations.size());
+        assertEquals("Unexpected PATCH paths size", 21, patchOperations.size());
+        assertEquals("Unexpected DELETE paths size", 21, deleteOperations.size());
+    }
+
+    /**
+     * Test that checks for correct amount of parameters in requests.
+     */
+    @Test
+    @SuppressWarnings("checkstyle:lineLength")
+    public void testMountPointRecursiveParameters() throws Exception {
+        final var configPaths = Map.of("/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root", 0,
+            "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}", 1,
+            "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}/nested-list={name1}", 2,
+            "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}/nested-list={name1}/super-nested-list={name2}", 3);
+
+        final var mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
+        openApi.onMountPointCreated(INSTANCE_ID);
+
+        final var mountPointApi = openApi.getMountPointApi(mockInfo, 1L, "recursive", "2023-05-22");
+        assertNotNull("Failed to find Datastore API", mountPointApi);
+
+        final var paths = mountPointApi.getPaths();
+        assertEquals(5, paths.size());
+
+        for (final var expectedPath : configPaths.entrySet()) {
+            assertTrue(paths.containsKey(expectedPath.getKey()));
+            final int expectedSize = expectedPath.getValue();
+
+            final var path = paths.get(expectedPath.getKey());
+
+            final var get = path.getGet();
+            assertFalse(get.isMissingNode());
+            assertEquals(expectedSize + 1, get.get("parameters").size());
+
+            final var put = path.getPut();
+            assertFalse(put.isMissingNode());
+            assertEquals(expectedSize, put.get("parameters").size());
+
+            final var delete = path.getDelete();
+            assertFalse(delete.isMissingNode());
+            assertEquals(expectedSize, delete.get("parameters").size());
+
+            final var post = path.getPost();
+            assertFalse(post.isMissingNode());
+            assertEquals(expectedSize, post.get("parameters").size());
+
+            final var patch = path.getPatch();
+            assertFalse(patch.isMissingNode());
+            assertEquals(expectedSize, patch.get("parameters").size());
+        }
     }
 }
diff --git a/restconf/restconf-openapi/src/test/resources/yang/recursive.yang b/restconf/restconf-openapi/src/test/resources/yang/recursive.yang
new file mode 100644 (file)
index 0000000..a298a93
--- /dev/null
@@ -0,0 +1,30 @@
+module recursive {
+  namespace "urn:opendaylight:test:recursive";
+  prefix "rec";
+
+  revision "2023-05-22" {
+    description
+        "Initial revision.";
+  }
+
+  container container-root {
+    list root-list {
+      key name;
+      leaf name {
+        type string;
+      }
+      list nested-list {
+        key name;
+        leaf name {
+          type string;
+        }
+        list super-nested-list {
+          key name;
+          leaf name {
+            type string;
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file