Create test for custom base path verification 86/106886/10
authorŠimon Ukuš <simon.ukus@pantheon.tech>
Wed, 7 Jun 2023 10:42:40 +0000 (12:42 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Thu, 3 Aug 2023 14:31:44 +0000 (14:31 +0000)
This patch introduces test that verifies the openapi paths
for modular basePath.

JIRA: NETCONF-1021
Change-Id: I6c5bf49f2c2e0b34a37636886c2848ac904ca0a3
Signed-off-by: Šimon Ukuš <simon.ukus@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
(cherry picked from commit 24aa07321be7f0f339cd65f969c299c8c6ff0c59)

restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/CustomOpenApiBasePathTest.java [new file with mode: 0644]
restconf/sal-rest-docgen/src/test/resources/yang/custom-base-path-test.yang [new file with mode: 0644]

diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/CustomOpenApiBasePathTest.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/CustomOpenApiBasePathTest.java
new file mode 100644 (file)
index 0000000..5631d52
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.sal.rest.doc.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.OAversion;
+import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
+import org.opendaylight.netconf.sal.rest.doc.swagger.OpenApiObject;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+@RunWith(Parameterized.class)
+public final class CustomOpenApiBasePathTest {
+    private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
+        .node(QName.create("", "nodes"))
+        .node(QName.create("", "node"))
+        .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
+
+    private MountPointSwagger swagger;
+
+    @Parameter
+    public static String basePath;
+
+    @Parameters(name = "{0}")
+    public static Collection<String> data() {
+        return List.of("rests", "restconf");
+    }
+
+    @Before
+    public void before() {
+        final var schemaService = mock(DOMSchemaService.class);
+        final var context = YangParserTestUtils.parseYangResource("/yang/custom-base-path-test.yang");
+
+        when(schemaService.getGlobalContext()).thenReturn(context);
+        final var mountPoint = mock(DOMMountPoint.class);
+        when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
+        final var mountPointService = mock(DOMMountPointService.class);
+        when(mountPointService.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
+        final var generator = new MountPointSwaggerGeneratorRFC8040(schemaService, mountPointService, basePath);
+        swagger = generator.getMountPointSwagger();
+        swagger.onMountPointCreated(INSTANCE_ID);
+    }
+
+    @Test
+    public void testCustomOpenApiBasePath() throws Exception {
+        final var mockInfo = DocGenTestHelper.createMockUriInfo("http://localhost/path");
+        final var mountPointApi = (OpenApiObject) swagger.getMountPointApi(mockInfo, 1L, "custom-base-path-test",
+            "2023-05-30", OAversion.V3_0);
+        assertNotNull("Failed to find Datastore API", mountPointApi);
+
+        final var containerOperationRoot = "/" + basePath + "/operations/nodes/node=123/yang-ext:mount";
+        final var containerDataRoot = "/" + basePath + "/data/nodes/node=123/yang-ext:mount";
+        final var expectedPaths = Set.of(
+            containerOperationRoot + "/custom-base-path-test:rpc-call",
+            containerOperationRoot + "/custom-base-path-test:foo/foo-action",
+            containerDataRoot + "/custom-base-path-test:foo/foo-list={fooListKey}",
+            containerDataRoot + "/custom-base-path-test:foo",
+            containerDataRoot);
+        final var actualPaths = new HashSet<String>(mountPointApi.getPaths().size());
+        mountPointApi.getPaths().fieldNames().forEachRemaining(actualPaths::add);
+        assertEquals("Unexpected paths", expectedPaths, actualPaths);
+    }
+}
diff --git a/restconf/sal-rest-docgen/src/test/resources/yang/custom-base-path-test.yang b/restconf/sal-rest-docgen/src/test/resources/yang/custom-base-path-test.yang
new file mode 100644 (file)
index 0000000..0e57a6e
--- /dev/null
@@ -0,0 +1,41 @@
+module custom-base-path-test {
+  yang-version 1.1;
+  namespace urn:opendaylight:action-path-test;
+  prefix "path-test";
+  revision "2023-05-30" {
+    description
+    "Initial revision.";
+  }
+  rpc rpc-call {
+    input {
+      leaf rpc-call-input {
+        type string;
+      }
+    }
+    output {
+      leaf rpc-call-output {
+        type string;
+      }
+    }
+  }
+  container foo {
+    list foo-list {
+      key "fooListKey";
+      leaf fooListKey {
+        type string;
+      }
+    }
+    action foo-action {
+      input {
+        leaf foo-action-input {
+          type string;
+        }
+      }
+      output {
+        leaf foo-action-output {
+          type string;
+        }
+      }
+    }
+  }
+}