Fix deviceName with specific module request
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocGeneratorRFC8040Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.rest.doc.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.node.ObjectNode;
18 import com.google.common.collect.ImmutableList;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.StreamSupport;
24 import org.junit.Test;
25 import org.opendaylight.netconf.sal.rest.doc.swagger.OpenApiObject;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
27
28 public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
29     private static final String NAME = "toaster2";
30     private static final String MY_YANG = "my-yang";
31     private static final String MY_YANG_REVISION = "2022-10-06";
32     private static final String REVISION_DATE = "2009-11-20";
33     private static final String NAME_2 = "toaster";
34     private static final String REVISION_DATE_2 = "2009-11-20";
35     private static final String PATH_PARAMS_TEST_MODULE = "path-params-test";
36     private static final String MANDATORY_TEST = "mandatory-test";
37     private static final String CONFIG_ROOT_CONTAINER = "mandatory-test_config_root-container";
38     private static final String CONFIG_ROOT_CONTAINER_POST = "mandatory-test_config_root-container_post";
39     private static final String ROOT_CONTAINER = "mandatory-test_root-container";
40     private static final String CONFIG_MANDATORY_CONTAINER = "mandatory-test_root-container_config_mandatory-container";
41     private static final String CONFIG_MANDATORY_CONTAINER_POST
42         = "mandatory-test_root-container_config_mandatory-container_post";
43     private static final String MANDATORY_CONTAINER = "mandatory-test_root-container_mandatory-container";
44     private static final String CONFIG_MANDATORY_LIST = "mandatory-test_root-container_config_mandatory-list";
45     private static final String CONFIG_MANDATORY_LIST_POST = "mandatory-test_root-container_config_mandatory-list_post";
46     private static final String MANDATORY_LIST = "mandatory-test_root-container_mandatory-list";
47     private static final String MANDATORY_TEST_MODULE = "mandatory-test_module";
48     private static final String CHOICE_TEST_MODULE = "choice-test";
49     private static final String PROPERTIES = "properties";
50
51     private final ApiDocGeneratorRFC8040 generator = new ApiDocGeneratorRFC8040(SCHEMA_SERVICE);
52
53     /**
54      * Test that paths are generated according to the model.
55      */
56     @Test
57     public void testPaths() {
58         final SwaggerObject doc = (SwaggerObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
59             ApiDocServiceImpl.OAversion.V2_0);
60
61         assertEquals(List.of("/rests/data",
62             "/rests/data/toaster2:toaster",
63             "/rests/data/toaster2:toaster/toasterSlot={slotId}",
64             "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo",
65             "/rests/data/toaster2:lst",
66             "/rests/data/toaster2:lst/cont1",
67             "/rests/data/toaster2:lst/cont1/cont11",
68             "/rests/data/toaster2:lst/cont1/lst11",
69             "/rests/data/toaster2:lst/lst1={key1},{key2}",
70             "/rests/operations/toaster2:make-toast",
71             "/rests/operations/toaster2:cancel-toast",
72             "/rests/operations/toaster2:restock-toaster"),
73             ImmutableList.copyOf(doc.getPaths().fieldNames()));
74     }
75
76     /**
77      * Test that generated configuration paths allow to use operations: get, put, delete and post.
78      */
79     @Test
80     public void testConfigPaths() {
81         final List<String> configPaths = List.of("/rests/data/toaster2:lst",
82                 "/rests/data/toaster2:lst/cont1",
83                 "/rests/data/toaster2:lst/cont1/cont11",
84                 "/rests/data/toaster2:lst/cont1/lst11",
85                 "/rests/data/toaster2:lst/lst1={key1},{key2}");
86
87         final SwaggerObject doc = (SwaggerObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
88             ApiDocServiceImpl.OAversion.V2_0);
89
90         for (final String path : configPaths) {
91             final JsonNode node = doc.getPaths().get(path);
92             assertFalse(node.path("get").isMissingNode());
93             assertFalse(node.path("put").isMissingNode());
94             assertFalse(node.path("delete").isMissingNode());
95             assertFalse(node.path("post").isMissingNode());
96         }
97     }
98
99     /**
100      * Test that generated document contains the following definitions.
101      */
102     @Test
103     public void testDefinitions() {
104         final SwaggerObject doc = (SwaggerObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
105             ApiDocServiceImpl.OAversion.V2_0);
106
107         final ObjectNode definitions = doc.getDefinitions();
108         assertNotNull(definitions);
109
110         final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
111         assertNotNull(configLstTop);
112         DocGenTestHelper.containsReferences(configLstTop, "lst", "#/definitions/toaster2_config_lst");
113
114         final JsonNode configLst = definitions.get("toaster2_config_lst");
115         assertNotNull(configLst);
116         DocGenTestHelper.containsReferences(configLst, "lst1", "#/definitions/toaster2_lst_config_lst1");
117         DocGenTestHelper.containsReferences(configLst, "cont1", "#/definitions/toaster2_lst_config_cont1");
118
119         final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
120         assertNotNull(configLst1Top);
121         DocGenTestHelper.containsReferences(configLst1Top, "lst1", "#/definitions/toaster2_lst_config_lst1");
122
123         final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
124         assertNotNull(configLst1);
125
126         final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
127         assertNotNull(configCont1Top);
128         DocGenTestHelper.containsReferences(configCont1Top, "cont1", "#/definitions/toaster2_lst_config_cont1");
129
130         final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
131         assertNotNull(configCont1);
132         DocGenTestHelper.containsReferences(configCont1, "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
133         DocGenTestHelper.containsReferences(configCont1, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
134
135         final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
136         assertNotNull(configCont11Top);
137         DocGenTestHelper.containsReferences(configCont11Top,
138             "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
139
140         final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
141         assertNotNull(configCont11);
142
143         final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
144         assertNotNull(configLst11Top);
145         DocGenTestHelper.containsReferences(configLst11Top, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
146
147         final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
148         assertNotNull(configLst11);
149     }
150
151     /**
152      * Test that generated document contains RPC definition for "make-toast" with correct input.
153      */
154     @Test
155     public void testRPC() {
156         final SwaggerObject doc = (SwaggerObject) generator.getApiDeclaration(NAME_2, REVISION_DATE_2, URI_INFO,
157             ApiDocServiceImpl.OAversion.V2_0);
158         assertNotNull(doc);
159
160         final ObjectNode definitions = doc.getDefinitions();
161         final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
162         assertNotNull(inputTop);
163         final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
164         assertEquals(testString, inputTop.get("properties").toString());
165         final JsonNode input = definitions.get("toaster_make-toast_input");
166         final JsonNode properties = input.get("properties");
167         assertTrue(properties.has("toasterDoneness"));
168         assertTrue(properties.has("toasterToastType"));
169     }
170
171     @Test
172     public void testMandatory() {
173         final var doc = (OpenApiObject) generator.getApiDeclaration(MANDATORY_TEST, null, URI_INFO,
174             ApiDocServiceImpl.OAversion.V3_0);
175         assertNotNull(doc);
176         final var definitions = doc.getComponents().getSchemas();
177         final var containersWithRequired = new ArrayList<String>();
178
179         final var reqRootContainerElements = Set.of("mandatory-root-leaf", "mandatory-container",
180             "mandatory-first-choice", "mandatory-list");
181         verifyRequiredField(definitions.get(CONFIG_ROOT_CONTAINER), reqRootContainerElements);
182         containersWithRequired.add(CONFIG_ROOT_CONTAINER);
183         verifyRequiredField(definitions.get(ROOT_CONTAINER), reqRootContainerElements);
184         containersWithRequired.add(ROOT_CONTAINER);
185         verifyRequiredField(definitions.get(CONFIG_ROOT_CONTAINER_POST), reqRootContainerElements);
186         containersWithRequired.add(CONFIG_ROOT_CONTAINER_POST);
187
188         final var reqMandatoryContainerElements = Set.of("mandatory-leaf", "leaf-list-with-min-elements");
189         verifyRequiredField(definitions.get(CONFIG_MANDATORY_CONTAINER), reqMandatoryContainerElements);
190         containersWithRequired.add(CONFIG_MANDATORY_CONTAINER);
191         verifyRequiredField(definitions.get(MANDATORY_CONTAINER), reqMandatoryContainerElements);
192         containersWithRequired.add(MANDATORY_CONTAINER);
193         verifyRequiredField(definitions.get(CONFIG_MANDATORY_CONTAINER_POST), reqMandatoryContainerElements);
194         containersWithRequired.add(CONFIG_MANDATORY_CONTAINER_POST);
195
196         final var reqMandatoryListElements = Set.of("mandatory-list-field");
197         verifyRequiredField(definitions.get(CONFIG_MANDATORY_LIST), reqMandatoryListElements);
198         containersWithRequired.add(CONFIG_MANDATORY_LIST);
199         verifyRequiredField(definitions.get(MANDATORY_LIST), reqMandatoryListElements);
200         containersWithRequired.add(MANDATORY_LIST);
201         verifyRequiredField(definitions.get(CONFIG_MANDATORY_LIST_POST), reqMandatoryListElements);
202         containersWithRequired.add(CONFIG_MANDATORY_LIST_POST);
203
204         final var testModuleMandatoryArray = Set.of("root-container", "root-mandatory-list");
205         verifyRequiredField(definitions.get(MANDATORY_TEST_MODULE), testModuleMandatoryArray);
206         containersWithRequired.add(MANDATORY_TEST_MODULE);
207
208         verifyThatPropertyDoesNotHaveRequired(containersWithRequired, definitions);
209     }
210
211     /**
212      * Test that request parameters are correctly numbered.
213      *
214      * <p>
215      * It means we should have name and name1, etc. when we have the same parameter in path multiple times.
216      */
217     @Test
218     public void testParametersNumbering() {
219         final var doc = (OpenApiObject) generator.getApiDeclaration(PATH_PARAMS_TEST_MODULE, null, URI_INFO,
220             ApiDocServiceImpl.OAversion.V3_0);
221
222         var pathToList1 = "/rests/data/path-params-test:cont/list1={name}";
223         assertTrue(doc.getPaths().has(pathToList1));
224         assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList1));
225
226         var pathToList2 = "/rests/data/path-params-test:cont/list1={name}/list2={name1}";
227         assertTrue(doc.getPaths().has(pathToList2));
228         assertEquals(List.of("name", "name1"), getPathParameters(doc.getPaths(), pathToList2));
229
230         var pathToList3 = "/rests/data/path-params-test:cont/list3={name}";
231         assertTrue(doc.getPaths().has(pathToList3));
232         assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList3));
233
234         var pathToList4 = "/rests/data/path-params-test:cont/list1={name}/list4={name1}";
235         assertTrue(doc.getPaths().has(pathToList4));
236         assertEquals(List.of("name", "name1"), getPathParameters(doc.getPaths(), pathToList4));
237
238         var pathToList5 = "/rests/data/path-params-test:cont/list1={name}/cont2";
239         assertTrue(doc.getPaths().has(pathToList4));
240         assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathToList5));
241     }
242
243     private static void verifyThatPropertyDoesNotHaveRequired(final List<String> expected,
244         final ObjectNode definitions) {
245         final var fields = definitions.fields();
246         while (fields.hasNext()) {
247             final var next = fields.next();
248             final var nodeName = next.getKey();
249             final var jsonNode = next.getValue();
250             if (expected.contains(nodeName)) {
251                 continue;
252             }
253             assertNull("Json node " + nodeName + " should not have 'required' field in body",
254                 jsonNode.get("required"));
255         }
256     }
257
258     private static void verifyRequiredField(final JsonNode rootContainer, final Set<String> expected) {
259         assertNotNull(rootContainer);
260         final var required = rootContainer.get("required");
261         assertNotNull(required);
262         assertTrue(required.isArray());
263         final var actualContainerArray = StreamSupport.stream(required.spliterator(), false)
264             .map(JsonNode::textValue)
265             .collect(Collectors.toSet());
266         assertEquals(expected, actualContainerArray);
267     }
268
269     /**
270      * Test that request for actions is correct and has parameters.
271      */
272     @Test
273     public void testActionPathsParams() {
274         final var doc = (OpenApiObject) generator.getApiDeclaration("action-types", null, URI_INFO,
275             ApiDocServiceImpl.OAversion.V3_0);
276
277         final var pathWithParameters = "/rests/operations/action-types:list={name}/list-action";
278         assertTrue(doc.getPaths().has(pathWithParameters));
279         assertEquals(List.of("name"), getPathParameters(doc.getPaths(), pathWithParameters));
280
281         final var pathWithoutParameters = "/rests/operations/action-types:multi-container/inner-container/action";
282         assertTrue(doc.getPaths().has(pathWithoutParameters));
283         assertEquals(List.of(), getPathParameters(doc.getPaths(), pathWithoutParameters));
284     }
285
286     @Test
287     public void testChoice() {
288         final var doc = (SwaggerObject) generator.getApiDeclaration(CHOICE_TEST_MODULE, null, URI_INFO,
289             ApiDocServiceImpl.OAversion.V2_0);
290         assertNotNull(doc);
291
292         final var definitions = doc.getDefinitions();
293         JsonNode firstContainer = definitions.get("choice-test_first-container");
294         assertEquals("default-value",
295                 firstContainer.get(PROPERTIES).get("leaf-default").get("default").asText());
296         assertFalse(firstContainer.get(PROPERTIES).has("leaf-non-default"));
297
298         JsonNode secondContainer = definitions.get("choice-test_second-container");
299         assertTrue(secondContainer.get(PROPERTIES).has("leaf-first-case"));
300         assertFalse(secondContainer.get(PROPERTIES).has("leaf-second-case"));
301     }
302
303     @Test
304     public void testSimpleOpenApiObjects() {
305         final var doc = (OpenApiObject) generator.getApiDeclaration(MY_YANG, MY_YANG_REVISION, URI_INFO,
306             ApiDocServiceImpl.OAversion.V3_0);
307         assertEquals(List.of("/rests/data", "/rests/data/my-yang:data"),
308                 ImmutableList.copyOf(doc.getPaths().fieldNames()));
309
310         final var JsonNodeMyYangData = doc.getPaths().get("/rests/data/my-yang:data");
311         verifyRequestRef(JsonNodeMyYangData.path("post"),
312                 "#/components/schemas/my-yang_config_data_post",
313                 "#/components/schemas/my-yang_config_data_post_xml");
314         verifyRequestRef(JsonNodeMyYangData.path("put"), "#/components/schemas/my-yang_config_data_TOP",
315                 "#/components/schemas/my-yang_config_data");
316         verifyRequestRef(JsonNodeMyYangData.path("get"), "#/components/schemas/my-yang_data_TOP",
317                 "#/components/schemas/my-yang_data");
318
319         // Test `components/schemas` objects
320         final var definitions = doc.getComponents().getSchemas();
321         assertEquals(7, definitions.size());
322         assertTrue(definitions.has("my-yang_config_data"));
323         assertTrue(definitions.has("my-yang_config_data_post"));
324         assertTrue(definitions.has("my-yang_config_data_post_xml"));
325         assertTrue(definitions.has("my-yang_config_data_TOP"));
326         assertTrue(definitions.has("my-yang_data"));
327         assertTrue(definitions.has("my-yang_data_TOP"));
328         assertTrue(definitions.has("my-yang_module"));
329     }
330
331     @Test
332     public void testToaster2OpenApiObjects() {
333         final var doc = (OpenApiObject) generator.getApiDeclaration(NAME, REVISION_DATE, URI_INFO,
334             ApiDocServiceImpl.OAversion.V3_0);
335         final var jsonNodeToaster = doc.getPaths().get("/rests/data/toaster2:toaster");
336         verifyRequestRef(jsonNodeToaster.path("post"), "#/components/schemas/toaster2_config_toaster_post",
337                 "#/components/schemas/toaster2_config_toaster_post_xml");
338         verifyRequestRef(jsonNodeToaster.path("put"), "#/components/schemas/toaster2_config_toaster_TOP",
339                 "#/components/schemas/toaster2_config_toaster");
340         verifyRequestRef(jsonNodeToaster.path("get"), "#/components/schemas/toaster2_toaster_TOP",
341                 "#/components/schemas/toaster2_toaster");
342
343         final var jsonNodeToasterSlot = doc.getPaths().get("/rests/data/toaster2:toaster/toasterSlot={slotId}");
344         verifyRequestRef(jsonNodeToasterSlot.path("post"),
345                 "#/components/schemas/toaster2_toaster_config_toasterSlot_post",
346                 "#/components/schemas/toaster2_toaster_config_toasterSlot_post_xml");
347         verifyRequestRef(jsonNodeToasterSlot.path("put"),
348                 "#/components/schemas/toaster2_toaster_config_toasterSlot_TOP",
349                 "#/components/schemas/toaster2_toaster_config_toasterSlot");
350         verifyRequestRef(jsonNodeToasterSlot.path("get"), "#/components/schemas/toaster2_toaster_toasterSlot_TOP",
351                 "#/components/schemas/toaster2_toaster_toasterSlot");
352
353         final var jsonNodeSlotInfo = doc.getPaths().get(
354                 "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo");
355         verifyRequestRef(jsonNodeSlotInfo.path("post"),
356                 "#/components/schemas/toaster2_toaster_toasterSlot_config_slotInfo_post",
357                 "#/components/schemas/toaster2_toaster_toasterSlot_config_slotInfo_post_xml");
358         verifyRequestRef(jsonNodeSlotInfo.path("put"),
359                 "#/components/schemas/toaster2_toaster_toasterSlot_config_slotInfo_TOP",
360                 "#/components/schemas/toaster2_toaster_toasterSlot_config_slotInfo");
361         verifyRequestRef(jsonNodeSlotInfo.path("get"), "#/components/schemas/toaster2_toaster_toasterSlot_slotInfo_TOP",
362                 "#/components/schemas/toaster2_toaster_toasterSlot_slotInfo");
363
364         final var jsonNodeLst = doc.getPaths().get("/rests/data/toaster2:lst");
365         verifyRequestRef(jsonNodeLst.path("post"), "#/components/schemas/toaster2_config_lst_post",
366                 "#/components/schemas/toaster2_config_lst_post_xml");
367         verifyRequestRef(jsonNodeLst.path("put"), "#/components/schemas/toaster2_config_lst_TOP",
368                 "#/components/schemas/toaster2_config_lst");
369         verifyRequestRef(jsonNodeLst.path("get"), "#/components/schemas/toaster2_lst_TOP",
370                 "#/components/schemas/toaster2_lst");
371
372         final var jsonNodeLst1 = doc.getPaths().get("/rests/data/toaster2:lst/lst1={key1},{key2}");
373         verifyRequestRef(jsonNodeLst1.path("post"), "#/components/schemas/toaster2_lst_config_lst1_post",
374                 "#/components/schemas/toaster2_lst_config_lst1_post_xml");
375         verifyRequestRef(jsonNodeLst1.path("put"), "#/components/schemas/toaster2_lst_config_lst1_TOP",
376                 "#/components/schemas/toaster2_lst_config_lst1");
377         verifyRequestRef(jsonNodeLst1.path("get"), "#/components/schemas/toaster2_lst_lst1_TOP",
378                 "#/components/schemas/toaster2_lst_lst1");
379
380         final var jsonNodeMakeToast = doc.getPaths().get("/rests/operations/toaster2:make-toast");
381         assertTrue(jsonNodeMakeToast.path("get").isMissingNode());
382         verifyRequestRef(jsonNodeMakeToast.path("post"), "#/components/schemas/toaster2_make-toast_input_TOP",
383                 "#/components/schemas/toaster2_make-toast_input");
384
385         final var jsonNodeCancelToast = doc.getPaths().get("/rests/operations/toaster2:cancel-toast");
386         assertTrue(jsonNodeCancelToast.path("get").isMissingNode());
387         // Test RPC with empty input
388         final var postContent = jsonNodeCancelToast.path("post").get("requestBody").get("content");
389         final var jsonSchema = postContent.get("application/json").get("schema");
390         assertNull(jsonSchema.get("$ref"));
391         assertEquals(2, jsonSchema.size());
392         final var xmlSchema = postContent.get("application/xml").get("schema");
393         assertNull(xmlSchema.get("$ref"));
394         assertEquals(2, xmlSchema.size());
395         // Test `components/schemas` objects
396         final var definitions = doc.getComponents().getSchemas();
397         assertEquals(60, definitions.size());
398     }
399
400     /**
401      *  Test JSON and XML references for request operation.
402      */
403     private static void verifyRequestRef(final JsonNode path, final String expectedJsonRef,
404             final String expectedXmlRef) {
405         final JsonNode postContent;
406         if (path.get("requestBody") != null) {
407             postContent = path.get("requestBody").get("content");
408         } else {
409             postContent = path.get("responses").get("200").get("content");
410         }
411         assertNotNull(postContent);
412         final var postJsonRef = postContent.get("application/json").get("schema").get("$ref");
413         assertNotNull(postJsonRef);
414         assertEquals(expectedJsonRef, postJsonRef.textValue());
415         final var postXmlRef = postContent.get("application/xml").get("schema").get("$ref");
416         assertNotNull(postXmlRef);
417         assertEquals(expectedXmlRef, postXmlRef.textValue());
418     }
419 }