2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.rest.doc.impl;
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.assertTrue;
15 import com.fasterxml.jackson.databind.JsonNode;
16 import com.fasterxml.jackson.databind.node.ObjectNode;
17 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.netconf.sal.rest.doc.AbstractOpenApiTest;
21 import org.opendaylight.netconf.sal.rest.doc.DocGenTestHelper;
22 import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
23 import org.opendaylight.netconf.sal.rest.doc.openapi.Path;
24 import org.opendaylight.yangtools.yang.common.Revision;
26 public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
27 private static final String NAME = "toaster2";
28 private static final String REVISION_DATE = "2009-11-20";
29 private static final String NAME_2 = "toaster";
30 private static final String REVISION_DATE_2 = "2009-11-20";
31 private static final String CHOICE_TEST_MODULE = "choice-test";
32 private static final String PROPERTIES = "properties";
33 private final OpenApiGeneratorRFC8040 generator = new OpenApiGeneratorRFC8040(SCHEMA_SERVICE);
36 * Test that paths are generated according to the model.
39 public void testPaths() {
40 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
41 final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
43 assertEquals(Set.of("/rests/data",
44 "/rests/data/toaster2:toaster",
45 "/rests/data/toaster2:toaster/toasterSlot={slotId}",
46 "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo",
47 "/rests/data/toaster2:lst",
48 "/rests/data/toaster2:lst/cont1",
49 "/rests/data/toaster2:lst/cont1/cont11",
50 "/rests/data/toaster2:lst/cont1/lst11",
51 "/rests/data/toaster2:lst/lst1={key1},{key2}",
52 "/rests/operations/toaster2:make-toast",
53 "/rests/operations/toaster2:cancel-toast",
54 "/rests/operations/toaster2:restock-toaster"),
55 doc.getPaths().keySet());
59 * Test that generated configuration paths allow to use operations: get, put, patch, delete and post.
62 public void testConfigPaths() {
63 final List<String> configPaths = List.of("/rests/data/toaster2:lst",
64 "/rests/data/toaster2:lst/cont1",
65 "/rests/data/toaster2:lst/cont1/cont11",
66 "/rests/data/toaster2:lst/cont1/lst11",
67 "/rests/data/toaster2:lst/lst1={key1},{key2}");
69 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
70 final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
72 for (final String path : configPaths) {
73 final Path node = doc.getPaths().get(path);
74 assertNotNull(node.getGet());
75 assertNotNull(node.getPut());
76 assertNotNull(node.getDelete());
77 assertNotNull(node.getPost());
78 assertNotNull(node.getPatch());
83 * Test that generated document contains the following schemas.
86 public void testSchemas() {
87 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
88 final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
90 final ObjectNode schemas = doc.getComponents().getSchemas();
91 assertNotNull(schemas);
93 final JsonNode configLstTop = schemas.get("toaster2_config_lst_TOP");
94 assertNotNull(configLstTop);
95 DocGenTestHelper.containsReferences(configLstTop, "lst", "#/components/schemas/toaster2_config_lst");
97 final JsonNode configLst = schemas.get("toaster2_config_lst");
98 assertNotNull(configLst);
99 DocGenTestHelper.containsReferences(configLst, "lst1", "#/components/schemas/toaster2_lst_config_lst1");
100 DocGenTestHelper.containsReferences(configLst, "cont1", "#/components/schemas/toaster2_lst_config_cont1");
102 final JsonNode configLst1Top = schemas.get("toaster2_lst_config_lst1_TOP");
103 assertNotNull(configLst1Top);
104 DocGenTestHelper.containsReferences(configLst1Top, "lst1", "#/components/schemas/toaster2_lst_config_lst1");
106 final JsonNode configLst1 = schemas.get("toaster2_lst_config_lst1");
107 assertNotNull(configLst1);
109 final JsonNode configCont1Top = schemas.get("toaster2_lst_config_cont1_TOP");
110 assertNotNull(configCont1Top);
111 DocGenTestHelper.containsReferences(configCont1Top, "cont1", "#/components/schemas/toaster2_lst_config_cont1");
113 final JsonNode configCont1 = schemas.get("toaster2_lst_config_cont1");
114 assertNotNull(configCont1);
115 DocGenTestHelper.containsReferences(configCont1, "cont11",
116 "#/components/schemas/toaster2_lst_cont1_config_cont11");
117 DocGenTestHelper.containsReferences(configCont1, "lst11",
118 "#/components/schemas/toaster2_lst_cont1_config_lst11");
120 final JsonNode configCont11Top = schemas.get("toaster2_lst_cont1_config_cont11_TOP");
121 assertNotNull(configCont11Top);
122 DocGenTestHelper.containsReferences(configCont11Top,
123 "cont11", "#/components/schemas/toaster2_lst_cont1_config_cont11");
125 final JsonNode configCont11 = schemas.get("toaster2_lst_cont1_config_cont11");
126 assertNotNull(configCont11);
128 final JsonNode configLst11Top = schemas.get("toaster2_lst_cont1_config_lst11_TOP");
129 assertNotNull(configLst11Top);
130 DocGenTestHelper.containsReferences(configLst11Top, "lst11",
131 "#/components/schemas/toaster2_lst_cont1_config_lst11");
133 final JsonNode configLst11 = schemas.get("toaster2_lst_cont1_config_lst11");
134 assertNotNull(configLst11);
138 * Test that generated document contains RPC schemas for "make-toast" with correct input.
141 public void testRPC() {
142 final var module = CONTEXT.findModule(NAME_2, Revision.of(REVISION_DATE_2)).orElseThrow();
143 final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
146 final ObjectNode schemas = doc.getComponents().getSchemas();
147 final JsonNode inputTop = schemas.get("toaster_make-toast_input_TOP");
148 assertNotNull(inputTop);
149 final String testString = "{\"input\":{\"$ref\":\"#/components/schemas/toaster_make-toast_input\"}}";
150 assertEquals(testString, inputTop.get("properties").toString());
151 final JsonNode input = schemas.get("toaster_make-toast_input");
152 final JsonNode properties = input.get("properties");
153 assertTrue(properties.has("toasterDoneness"));
154 assertTrue(properties.has("toasterToastType"));
158 public void testChoice() {
159 final var module = CONTEXT.findModule(CHOICE_TEST_MODULE).orElseThrow();
160 final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
163 final var schemas = doc.getComponents().getSchemas();
164 JsonNode firstContainer = schemas.get("choice-test_first-container");
165 assertEquals("default-value",
166 firstContainer.get(PROPERTIES).get("leaf-default").get("default").asText());
167 assertFalse(firstContainer.get(PROPERTIES).has("leaf-non-default"));
169 JsonNode secondContainer = schemas.get("choice-test_second-container");
170 assertTrue(secondContainer.get(PROPERTIES).has("leaf-first-case"));
171 assertFalse(secondContainer.get(PROPERTIES).has("leaf-second-case"));