Refactor OpenApiTestUtils methods
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocGeneratorRFC8040Test.java
index c982de432b0c6951686e730ea9c1db98cd42cc13..c9a7ab179d84c9f903279ac8960476cf7863a576 100644 (file)
@@ -20,6 +20,8 @@ import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuild
 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;
@@ -224,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,
@@ -269,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.
      */
@@ -279,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
@@ -491,6 +511,33 @@ public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
         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.
      */