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 com.google.common.collect.ImmutableList;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
21 import org.opendaylight.yangtools.yang.common.Revision;
23 public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
24 private static final String NAME = "toaster2";
25 private static final String REVISION_DATE = "2009-11-20";
26 private static final String NAME_2 = "toaster";
27 private static final String REVISION_DATE_2 = "2009-11-20";
29 private final ApiDocGeneratorRFC8040 generator = new ApiDocGeneratorRFC8040(SCHEMA_SERVICE);
32 * Test that paths are generated according to the model.
35 public void testPaths() {
36 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
37 final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
38 ApiDocServiceImpl.OAversion.V2_0);
40 assertEquals(List.of("/rests/data",
41 "/rests/data/toaster2:toaster",
42 "/rests/data/toaster2:toaster/toasterSlot={slotId}",
43 "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo",
44 "/rests/data/toaster2:lst",
45 "/rests/data/toaster2:lst/cont1",
46 "/rests/data/toaster2:lst/cont1/cont11",
47 "/rests/data/toaster2:lst/cont1/lst11",
48 "/rests/data/toaster2:lst/lst1={key1},{key2}",
49 "/rests/operations/toaster2:make-toast",
50 "/rests/operations/toaster2:cancel-toast",
51 "/rests/operations/toaster2:restock-toaster"),
52 ImmutableList.copyOf(doc.getPaths().fieldNames()));
56 * Test that generated configuration paths allow to use operations: get, put, delete and post.
59 public void testConfigPaths() {
60 final List<String> configPaths = List.of("/rests/data/toaster2:lst",
61 "/rests/data/toaster2:lst/cont1",
62 "/rests/data/toaster2:lst/cont1/cont11",
63 "/rests/data/toaster2:lst/cont1/lst11",
64 "/rests/data/toaster2:lst/lst1={key1},{key2}");
66 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
67 final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
68 ApiDocServiceImpl.OAversion.V2_0);
70 for (final String path : configPaths) {
71 final JsonNode node = doc.getPaths().get(path);
72 assertFalse(node.path("get").isMissingNode());
73 assertFalse(node.path("put").isMissingNode());
74 assertFalse(node.path("delete").isMissingNode());
75 assertFalse(node.path("post").isMissingNode());
80 * Test that generated document contains the following definitions.
83 public void testDefinitions() {
84 final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
85 final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
86 ApiDocServiceImpl.OAversion.V2_0);
88 final ObjectNode definitions = doc.getDefinitions();
89 assertNotNull(definitions);
91 final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
92 assertNotNull(configLstTop);
93 DocGenTestHelper.containsReferences(configLstTop, "lst", "#/definitions/toaster2_config_lst");
95 final JsonNode configLst = definitions.get("toaster2_config_lst");
96 assertNotNull(configLst);
97 DocGenTestHelper.containsReferences(configLst, "lst1", "#/definitions/toaster2_lst_config_lst1");
98 DocGenTestHelper.containsReferences(configLst, "cont1", "#/definitions/toaster2_lst_config_cont1");
100 final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
101 assertNotNull(configLst1Top);
102 DocGenTestHelper.containsReferences(configLst1Top, "lst1", "#/definitions/toaster2_lst_config_lst1");
104 final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
105 assertNotNull(configLst1);
107 final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
108 assertNotNull(configCont1Top);
109 DocGenTestHelper.containsReferences(configCont1Top, "cont1", "#/definitions/toaster2_lst_config_cont1");
111 final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
112 assertNotNull(configCont1);
113 DocGenTestHelper.containsReferences(configCont1, "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
114 DocGenTestHelper.containsReferences(configCont1, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
116 final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
117 assertNotNull(configCont11Top);
118 DocGenTestHelper.containsReferences(configCont11Top,
119 "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
121 final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
122 assertNotNull(configCont11);
124 final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
125 assertNotNull(configLst11Top);
126 DocGenTestHelper.containsReferences(configLst11Top, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
128 final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
129 assertNotNull(configLst11);
133 * Test that generated document contains RPC definition for "make-toast" with correct input.
136 public void testRPC() {
137 final var module = CONTEXT.findModule(NAME_2, Revision.of(REVISION_DATE_2)).orElseThrow();
138 final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
139 ApiDocServiceImpl.OAversion.V2_0);
142 final ObjectNode definitions = doc.getDefinitions();
143 final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
144 assertNotNull(inputTop);
145 final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
146 assertEquals(testString, inputTop.get("properties").toString());
147 final JsonNode input = definitions.get("toaster_make-toast_input");
148 final JsonNode properties = input.get("properties");
149 assertTrue(properties.has("toasterDoneness"));
150 assertTrue(properties.has("toasterToastType"));