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.controller.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.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Optional;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040;
24 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
25 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public final class ApiDocGeneratorRFC8040Test {
33 private static final String NAME = "toaster2";
34 private static final String REVISION_DATE = "2009-11-20";
35 private static final String NAME_2 = "toaster";
36 private static final String REVISION_DATE_2 = "2009-11-20";
38 private EffectiveModelContext context;
39 private ApiDocGeneratorRFC8040 generator;
43 context = YangParserTestUtils.parseYangResourceDirectory("/yang");
44 generator = new ApiDocGeneratorRFC8040(DocGenTestHelper.createMockSchemaService(context));
48 * Test that paths are generated according to the model.
51 public void testPaths() {
52 final List<String> expectedPaths = Arrays.asList("/rests/data",
53 "/rests/data/toaster2:toaster",
54 "/rests/data/toaster2:toaster/toasterSlot={slotId}",
55 "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo",
56 "/rests/data/toaster2:lst",
57 "/rests/data/toaster2:lst/cont1",
58 "/rests/data/toaster2:lst/cont1/cont11",
59 "/rests/data/toaster2:lst/cont1/lst11",
60 "/rests/data/toaster2:lst/lst1={key1},{key2}",
61 "/rests/operations/toaster2:make-toast",
62 "/rests/operations/toaster2:cancel-toast",
63 "/rests/operations/toaster2:restock-toaster");
65 final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
66 assertTrue("Desired module not found", module.isPresent());
67 final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
68 "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
69 final List<String> actualPaths = new ArrayList<>();
70 doc.getPaths().fieldNames().forEachRemaining(actualPaths::add);
72 assertEquals(expectedPaths, actualPaths);
76 * Test that generated configuration paths allow to use operations: get, put, delete and post.
79 public void testConfigPaths() {
80 final List<String> configPaths = Arrays.asList("/rests/data/toaster2:lst",
81 "/rests/data/toaster2:lst/cont1",
82 "/rests/data/toaster2:lst/cont1/cont11",
83 "/rests/data/toaster2:lst/cont1/lst11",
84 "/rests/data/toaster2:lst/lst1={key1},{key2}");
86 final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
87 assertTrue("Desired module not found", module.isPresent());
88 final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
89 "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
91 for (final String path : configPaths) {
92 final JsonNode node = doc.getPaths().get(path);
93 assertFalse(node.path("get").isMissingNode());
94 assertFalse(node.path("put").isMissingNode());
95 assertFalse(node.path("delete").isMissingNode());
96 assertFalse(node.path("post").isMissingNode());
101 * Test that generated document contains the following definitions.
104 public void testDefinitions() {
105 final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
106 assertTrue("Desired module not found", module.isPresent());
107 final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
108 "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
110 final ObjectNode definitions = doc.getDefinitions();
111 assertNotNull(definitions);
113 final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
114 assertNotNull(configLstTop);
115 DocGenTestHelper.containsReferences(configLstTop, "lst",
116 "#/definitions/toaster2_config_lst");
118 final JsonNode configLst = definitions.get("toaster2_config_lst");
119 assertNotNull(configLst);
120 DocGenTestHelper.containsReferences(configLst,
121 "lst1", "#/definitions/toaster2_lst_config_lst1");
122 DocGenTestHelper.containsReferences(configLst,
123 "cont1", "#/definitions/toaster2_lst_config_cont1");
125 final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
126 assertNotNull(configLst1Top);
127 DocGenTestHelper.containsReferences(configLst1Top,
128 "lst1", "#/definitions/toaster2_lst_config_lst1");
130 final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
131 assertNotNull(configLst1);
133 final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
134 assertNotNull(configCont1Top);
135 DocGenTestHelper.containsReferences(configCont1Top,
136 "cont1", "#/definitions/toaster2_lst_config_cont1");
138 final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
139 assertNotNull(configCont1);
140 DocGenTestHelper.containsReferences(configCont1,
141 "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
142 DocGenTestHelper.containsReferences(configCont1,
143 "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
145 final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
146 assertNotNull(configCont11Top);
147 DocGenTestHelper.containsReferences(configCont11Top,
148 "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
150 final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
151 assertNotNull(configCont11);
153 final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
154 assertNotNull(configLst11Top);
155 DocGenTestHelper.containsReferences(configLst11Top,
156 "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
158 final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
159 assertNotNull(configLst11);
163 * Test that generated document contains RPC definition for "make-toast" with correct input.
166 public void testRPC() {
167 final Optional<? extends Module> module = context.findModule(NAME_2, Revision.of(REVISION_DATE_2));
168 assertTrue("Desired module not found", module.isPresent());
169 final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
170 "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
173 final ObjectNode definitions = doc.getDefinitions();
174 final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
175 assertNotNull(inputTop);
176 final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
177 assertEquals(testString, inputTop.get("properties").toString());
178 final JsonNode input = definitions.get("toaster_make-toast_input");
179 final JsonNode properties = input.get("properties");
180 assertTrue(properties.has("toasterDoneness"));
181 assertTrue(properties.has("toasterToastType"));