75f2ad92e4247fbdec38535e422ee5bb7ff99216
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / ApiDocGeneratorDraftO2Test.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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 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.ApiDocGeneratorDraftO2;
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;
31
32 public final class ApiDocGeneratorDraftO2Test {
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";
37
38     private EffectiveModelContext context;
39     private ApiDocGeneratorDraftO2 generator;
40
41     @Before
42     public void setUp() {
43         context = YangParserTestUtils.parseYangResourceDirectory("/yang");
44         generator = new ApiDocGeneratorDraftO2(DocGenTestHelper.createMockSchemaService(context));
45     }
46
47     /**
48      * Test that paths are generated according to the model.
49      */
50     @Test
51     public void testPaths() {
52         final List<String> expectedPaths = Arrays.asList("/restconf/config",
53                 "/restconf/config/toaster2:toaster",
54                 "/restconf/config/toaster2:toaster/toasterSlot/{slotId}",
55                 "/restconf/config/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
56                 "/restconf/operational/toaster2:toaster",
57                 "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}",
58                 "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
59                 "/restconf/config/toaster2:lst",
60                 "/restconf/config/toaster2:lst/cont1",
61                 "/restconf/config/toaster2:lst/cont1/cont11",
62                 "/restconf/config/toaster2:lst/cont1/lst11",
63                 "/restconf/config/toaster2:lst/lst1/{key1}/{key2}",
64                 "/restconf/operational/toaster2:lst",
65                 "/restconf/operational/toaster2:lst/cont1",
66                 "/restconf/operational/toaster2:lst/cont1/cont11",
67                 "/restconf/operational/toaster2:lst/cont1/lst11",
68                 "/restconf/operational/toaster2:lst/lst1/{key1}/{key2}",
69                 "/restconf/operations/toaster2:make-toast",
70                 "/restconf/operations/toaster2:cancel-toast",
71                 "/restconf/operations/toaster2:restock-toaster");
72
73         final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
74         assertTrue("Desired module not found", module.isPresent());
75         final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
76                 "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
77         final List<String> actualPaths = new ArrayList<>();
78         doc.getPaths().fieldNames().forEachRemaining(actualPaths::add);
79
80         assertEquals(expectedPaths, actualPaths);
81     }
82
83     /**
84      * Test that generated configuration paths allow to use operations: get, put, delete and post.
85      */
86     @Test
87     public void testConfigPaths() {
88         final List<String> configPaths = Arrays.asList("/restconf/config/toaster2:lst",
89                 "/restconf/config/toaster2:lst/cont1",
90                 "/restconf/config/toaster2:lst/cont1/cont11",
91                 "/restconf/config/toaster2:lst/cont1/lst11",
92                 "/restconf/config/toaster2:lst/lst1/{key1}/{key2}");
93
94         final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
95         assertTrue("Desired module not found", module.isPresent());
96         final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
97                 "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
98
99         for (final String path : configPaths) {
100             final JsonNode node = doc.getPaths().get(path);
101             assertFalse(node.path("get").isMissingNode());
102             assertFalse(node.path("put").isMissingNode());
103             assertFalse(node.path("delete").isMissingNode());
104             assertFalse(node.path("post").isMissingNode());
105         }
106     }
107
108     /**
109      * Test that generated document contains the following definitions.
110      */
111     @Test
112     public void testDefinitions() {
113         final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
114         assertTrue("Desired module not found", module.isPresent());
115         final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
116                 "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
117
118         final ObjectNode definitions = doc.getDefinitions();
119         assertNotNull(definitions);
120
121         final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
122         assertNotNull(configLstTop);
123         DocGenTestHelper.containsReferences(configLstTop,
124                 "lst", "#/definitions/toaster2_config_lst");
125
126         final JsonNode configLst = definitions.get("toaster2_config_lst");
127         assertNotNull(configLst);
128         DocGenTestHelper.containsReferences(configLst,
129                 "lst1", "#/definitions/toaster2_lst_config_lst1");
130         DocGenTestHelper.containsReferences(configLst,
131                 "cont1", "#/definitions/toaster2_lst_config_cont1");
132
133         final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
134         assertNotNull(configLst1Top);
135         DocGenTestHelper.containsReferences(configLst1Top,
136                 "lst1", "#/definitions/toaster2_lst_config_lst1");
137
138         final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
139         assertNotNull(configLst1);
140
141         final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
142         assertNotNull(configCont1Top);
143         DocGenTestHelper.containsReferences(configCont1Top,
144                 "cont1", "#/definitions/toaster2_lst_config_cont1");
145
146         final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
147         assertNotNull(configCont1);
148         DocGenTestHelper.containsReferences(configCont1,
149                 "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
150         DocGenTestHelper.containsReferences(configCont1,
151
152                 "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
153
154         final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
155         assertNotNull(configCont11Top);
156         DocGenTestHelper.containsReferences(configCont11Top,
157                 "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
158
159         final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
160         assertNotNull(configCont11);
161
162         final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
163         assertNotNull(configLst11Top);
164         DocGenTestHelper.containsReferences(configLst11Top,
165                 "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
166
167         final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
168         assertNotNull(configLst11);
169     }
170
171     /**
172      * Test that generated document contains RPC definition for "make-toast" with correct input.
173      */
174     @Test
175     public void testRPC() {
176         final Optional<? extends Module> module = context.findModule(NAME_2, Revision.of(REVISION_DATE_2));
177         assertTrue("Desired module not found", module.isPresent());
178         final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
179                 "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
180         assertNotNull(doc);
181
182         final ObjectNode definitions = doc.getDefinitions();
183         final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
184         assertNotNull(inputTop);
185         final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
186         assertEquals(testString, inputTop.get("properties").toString());
187         final JsonNode input = definitions.get("toaster_make-toast_input");
188         final JsonNode properties = input.get("properties");
189         assertTrue(properties.has("toasterDoneness"));
190         assertTrue(properties.has("toasterToastType"));
191     }
192 }