2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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.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;
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";
38 private EffectiveModelContext context;
39 private ApiDocGeneratorDraftO2 generator;
43 context = YangParserTestUtils.parseYangResourceDirectory("/yang");
44 generator = new ApiDocGeneratorDraftO2(DocGenTestHelper.createMockSchemaService(context));
48 * Test that paths are generated according to the model.
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");
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);
80 assertEquals(expectedPaths, actualPaths);
84 * Test that generated configuration paths allow to use operations: get, put, delete and post.
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}");
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);
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());
109 * Test that generated document contains the following definitions.
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);
118 final ObjectNode definitions = doc.getDefinitions();
119 assertNotNull(definitions);
121 final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
122 assertNotNull(configLstTop);
123 DocGenTestHelper.containsReferences(configLstTop,
124 "lst", "#/definitions/toaster2_config_lst");
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");
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");
138 final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
139 assertNotNull(configLst1);
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");
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,
152 "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
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");
159 final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
160 assertNotNull(configCont11);
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");
167 final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
168 assertNotNull(configLst11);
172 * Test that generated document contains RPC definition for "make-toast" with correct input.
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);
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"));