Refactor OpenApiTestUtils methods
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocGeneratorRFC8040Test.java
index e54ea64a63b66eb465ebc27d52d93e67ded294ec..c9a7ab179d84c9f903279ac8960476cf7863a576 100644 (file)
@@ -12,11 +12,21 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.CONTENT_KEY;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.REF_KEY;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.REQUEST_BODY_KEY;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.RESPONSES_KEY;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.SCHEMA_KEY;
+import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.getAppropriateModelPrefix;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -41,7 +51,7 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
     private static final String CONFIG_MANDATORY_LIST = "mandatory-test_root-container_config_mandatory-list";
     private static final String CONFIG_MANDATORY_LIST_POST = "mandatory-test_root-container_config_mandatory-list_post";
     private static final String MANDATORY_LIST = "mandatory-test_root-container_mandatory-list";
-    private static final String MANDATORY_TEST_MODULE = "mandatory-test_module";
+    private static final String MANDATORY_TEST_MODULE = "mandatory-test_config_module";
     private static final String CHOICE_TEST_MODULE = "choice-test";
     private static final String PROPERTIES = "properties";
     private static final String CONTAINER = "container";
@@ -216,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<String> expected,
@@ -261,6 +271,24 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
         assertEquals(expected, actualContainerArray);
     }
 
+    /**
+     * Test that request parameters are correctly typed.
+     */
+    @Test
+    public void testParametersTypes() {
+        final var doc = (OpenApiObject) generator.getApiDeclaration("typed-params", "2023-10-24", URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        final var pathToContainer = "/rests/data/typed-params:typed/";
+        final var integerTypes = List.of("uint64", "uint32", "uint16", "uint8", "int64", "int32", "int16", "int8");
+        for (final var type: integerTypes) {
+            final var typeKey = type + "-key";
+            final var path = pathToContainer + type + "={" + typeKey + "}";
+            assertTrue(doc.getPaths().has(path));
+            assertEquals("integer", doc.getPaths().get(path).get("get").get("parameters").get(0).get("schema")
+                .get("type").textValue());
+        }
+    }
+
     /**
      * Test that request for actions is correct and has parameters.
      */
@@ -271,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
@@ -318,7 +346,7 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
         assertTrue(definitions.has("my-yang_config_data_TOP"));
         assertTrue(definitions.has("my-yang_data"));
         assertTrue(definitions.has("my-yang_data_TOP"));
-        assertTrue(definitions.has("my-yang_module"));
+        assertTrue(definitions.has("my-yang_config_module"));
     }
 
     @Test
@@ -391,6 +419,125 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
         assertEquals(44, definitions.size());
     }
 
+    /**
+     * Test that reference to schema in each path is valid (all referenced schemas exist).
+     */
+    @Test
+    public void testRootPostSchemaReference() {
+        final var document = (OpenApiObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        assertNotNull(document);
+        final var expectedSchema = "toaster2_config_module";
+        // verify schema reference itself
+        verifyRequestRef(document.getPaths().path("/rests/data").path("post"),
+                getAppropriateModelPrefix(ApiDocServiceImpl.OAversion.V3_0) + expectedSchema,
+                getAppropriateModelPrefix(ApiDocServiceImpl.OAversion.V3_0) + expectedSchema);
+        // verify existence of the schemas being referenced
+        assertTrue("The expected referenced schema (" + expectedSchema + ") is not created",
+                document.getComponents().getSchemas().has(expectedSchema));
+    }
+
+    /**
+     * Test that reference to schema in each path is valid (all referenced schemas exist).
+     */
+    @Test
+    public void testSchemasExistenceSingleModule() {
+        final var document = (OpenApiObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        assertNotNull(document);
+        final var referencedSchemas = new HashSet<String>();
+        for (final var elements = document.getPaths().elements(); elements.hasNext(); ) {
+            final var path = elements.next();
+            referencedSchemas.addAll(extractSchemaRefFromPath(path, ApiDocServiceImpl.OAversion.V3_0));
+        }
+        final var schemaNamesIterator = document.getComponents().getSchemas().fieldNames();
+        final var schemaNames = Sets.newHashSet(schemaNamesIterator);
+        for (final var ref : referencedSchemas) {
+            assertTrue("Referenced schema " + ref + " does not exist", schemaNames.contains(ref));
+        }
+    }
+
+    /**
+     * Test that checks if namespace for rpc is present.
+     */
+    @Test
+    public void testRpcNamespace() {
+        final var doc = (OpenApiObject) generator.getApiDeclaration(NAME_2, REVISION_DATE, URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        assertNotNull(doc);
+        final var path = doc.getPaths().get("/rests/operations/toaster:cancel-toast");
+        assertNotNull(path);
+        final var post = path.get("post");
+        assertNotNull(post);
+        final var requestBody = post.get("requestBody");
+        assertNotNull(requestBody);
+        final var content = requestBody.get("content");
+        assertNotNull(content);
+        final var application = content.get("application/xml");
+        assertNotNull(application);
+        final var schema = application.get("schema");
+        assertNotNull(schema);
+        final var xml = schema.get("xml");
+        assertNotNull(xml);
+        final var namespace = xml.get("namespace");
+        assertNotNull(namespace);
+        assertEquals("http://netconfcentral.org/ns/toaster", namespace.asText());
+    }
+
+    /**
+     * Test that checks if namespace for actions is present.
+     */
+    @Test
+    public void testActionsNamespace() {
+        final var doc = (OpenApiObject) generator.getApiDeclaration("action-types", null, URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        assertNotNull(doc);
+        final var path = doc.getPaths().get("/rests/operations/action-types:multi-container/inner-container/action");
+        assertNotNull(path);
+        final var post = path.get("post");
+        assertNotNull(post);
+        final var requestBody = post.get("requestBody");
+        assertNotNull(requestBody);
+        final var content = requestBody.get("content");
+        assertNotNull(content);
+        final var application = content.get("application/xml");
+        assertNotNull(application);
+        final var schema = application.get("schema");
+        assertNotNull(schema);
+        final var xml = schema.get("xml");
+        assertNotNull(xml);
+        final var namespace = xml.get("namespace");
+        assertNotNull(namespace);
+        assertEquals("urn:ietf:params:xml:ns:yang:test:action:types", namespace.asText());
+    }
+
+    /**
+     * Test that number of elements in payload is correct.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testLeafListWithMinElementsPayload() {
+        final var doc = (OpenApiObject) generator.getApiDeclaration(MANDATORY_TEST, null, URI_INFO,
+            ApiDocServiceImpl.OAversion.V3_0);
+        assertNotNull(doc);
+        final var paths = doc.getPaths();
+        final var path = paths.path("/rests/data/mandatory-test:root-container/mandatory-container");
+        final var requestBody = path.path("post").path("requestBody").path("content");
+        final var jsonRef = requestBody.path("application/json").path("schema").path("$ref");
+        final var xmlRef = requestBody.path("application/xml").path("schema").path("$ref");
+        final var schema = doc.getComponents().getSchemas().path("mandatory-test_root-container_mandatory-container");
+        final var minItems = schema.path("properties").path("leaf-list-with-min-elements").path("minItems");
+        final var listOfExamples = ((ArrayNode) schema.path("properties").path("leaf-list-with-min-elements")
+            .path("example"));
+        final var expectedListOfExamples = JsonNodeFactory.instance.arrayNode()
+            .add("Some leaf-list-with-min-elements")
+            .add("Some leaf-list-with-min-elements");
+        assertFalse(listOfExamples.isMissingNode());
+        assertEquals(xmlRef, jsonRef);
+        assertEquals(2, minItems.intValue());
+        assertEquals(expectedListOfExamples, listOfExamples);
+    }
+
     /**
      *  Test JSON and XML references for request operation.
      */
@@ -433,4 +580,42 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
         assertNotNull(postXmlRef);
         assertEquals(expectedXmlRef, postXmlRef.textValue());
     }
+
+    private static Set<String> extractSchemaRefFromPath(final JsonNode path,
+            final ApiDocServiceImpl.OAversion oaversion) {
+        if (path == null || path.isMissingNode()) {
+            return Set.of();
+        }
+        final var references = new HashSet<String>();
+        final var get = path.path("get");
+        if (!get.isMissingNode()) {
+            references.addAll(
+                    schemaRefFromContent(get.path(RESPONSES_KEY).path("200").path(CONTENT_KEY), oaversion));
+        }
+        final var post = path.path("post");
+        if (!post.isMissingNode()) {
+            references.addAll(schemaRefFromContent(post.path(REQUEST_BODY_KEY).path(CONTENT_KEY), oaversion));
+        }
+        final var put = path.path("put");
+        if (!put.isMissingNode()) {
+            references.addAll(schemaRefFromContent(put.path(REQUEST_BODY_KEY).path(CONTENT_KEY), oaversion));
+        }
+        final var patch = path.path("patch");
+        if (!patch.isMissingNode()) {
+            references.addAll(schemaRefFromContent(patch.path(REQUEST_BODY_KEY).path(CONTENT_KEY), oaversion));
+        }
+        return references;
+    }
+
+    private static Set<String> schemaRefFromContent(final JsonNode content,
+            final ApiDocServiceImpl.OAversion oaversion) {
+        final HashSet<String> refs = new HashSet<>();
+        content.fieldNames().forEachRemaining(mediaType -> {
+            final JsonNode ref = content.path(mediaType).path(SCHEMA_KEY).path(REF_KEY);
+            if (ref != null && !ref.isMissingNode()) {
+                refs.add(ref.asText().replaceFirst(getAppropriateModelPrefix(oaversion), ""));
+            }
+        });
+        return refs;
+    }
 }