Modernize sal-rest-docgen tests a bit
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / 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.controller.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.assertTrue;
14
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.impl.ApiDocGeneratorRFC8040;
21 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
22 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
23 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
24 import org.opendaylight.yangtools.yang.common.Revision;
25
26 public final class ApiDocGeneratorRFC8040Test extends AbstractApiDocTest {
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
32     private final ApiDocGeneratorRFC8040 generator = new ApiDocGeneratorRFC8040(SCHEMA_SERVICE);
33
34     /**
35      * Test that paths are generated according to the model.
36      */
37     @Test
38     public void testPaths() {
39         final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
40         final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
41             URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
42
43         assertEquals(List.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             ImmutableList.copyOf(doc.getPaths().fieldNames()));
56     }
57
58     /**
59      * Test that generated configuration paths allow to use operations: get, put, delete and post.
60      */
61     @Test
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}");
68
69         final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
70         final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
71             URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
72
73         for (final String path : configPaths) {
74             final JsonNode node = doc.getPaths().get(path);
75             assertFalse(node.path("get").isMissingNode());
76             assertFalse(node.path("put").isMissingNode());
77             assertFalse(node.path("delete").isMissingNode());
78             assertFalse(node.path("post").isMissingNode());
79         }
80     }
81
82     /**
83      * Test that generated document contains the following definitions.
84      */
85     @Test
86     public void testDefinitions() {
87         final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
88         final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
89             URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
90
91         final ObjectNode definitions = doc.getDefinitions();
92         assertNotNull(definitions);
93
94         final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
95         assertNotNull(configLstTop);
96         DocGenTestHelper.containsReferences(configLstTop, "lst", "#/definitions/toaster2_config_lst");
97
98         final JsonNode configLst = definitions.get("toaster2_config_lst");
99         assertNotNull(configLst);
100         DocGenTestHelper.containsReferences(configLst, "lst1", "#/definitions/toaster2_lst_config_lst1");
101         DocGenTestHelper.containsReferences(configLst, "cont1", "#/definitions/toaster2_lst_config_cont1");
102
103         final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
104         assertNotNull(configLst1Top);
105         DocGenTestHelper.containsReferences(configLst1Top, "lst1", "#/definitions/toaster2_lst_config_lst1");
106
107         final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
108         assertNotNull(configLst1);
109
110         final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
111         assertNotNull(configCont1Top);
112         DocGenTestHelper.containsReferences(configCont1Top, "cont1", "#/definitions/toaster2_lst_config_cont1");
113
114         final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
115         assertNotNull(configCont1);
116         DocGenTestHelper.containsReferences(configCont1, "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
117         DocGenTestHelper.containsReferences(configCont1, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
118
119         final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
120         assertNotNull(configCont11Top);
121         DocGenTestHelper.containsReferences(configCont11Top,
122             "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
123
124         final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
125         assertNotNull(configCont11);
126
127         final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
128         assertNotNull(configLst11Top);
129         DocGenTestHelper.containsReferences(configLst11Top, "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
130
131         final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
132         assertNotNull(configLst11);
133     }
134
135     /**
136      * Test that generated document contains RPC definition for "make-toast" with correct input.
137      */
138     @Test
139     public void testRPC() {
140         final var module = CONTEXT.findModule(NAME_2, Revision.of(REVISION_DATE_2)).orElseThrow();
141         final SwaggerObject doc = generator.getSwaggerDocSpec(module, "http", "localhost:8181", "/", "", CONTEXT,
142             URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
143         assertNotNull(doc);
144
145         final ObjectNode definitions = doc.getDefinitions();
146         final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
147         assertNotNull(inputTop);
148         final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
149         assertEquals(testString, inputTop.get("properties").toString());
150         final JsonNode input = definitions.get("toaster_make-toast_input");
151         final JsonNode properties = input.get("properties");
152         assertTrue(properties.has("toasterDoneness"));
153         assertTrue(properties.has("toasterToastType"));
154     }
155 }