--- /dev/null
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.rest.doc.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
+import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public final class ApiDocGeneratorDraftO2Test {
+ private static final String NAME = "toaster2";
+ private static final String REVISION_DATE = "2009-11-20";
+ private static final String NAME_2 = "toaster";
+ private static final String REVISION_DATE_2 = "2009-11-20";
+
+ private EffectiveModelContext context;
+ private ApiDocGeneratorDraftO2 generator;
+
+ @Before
+ public void setUp() {
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ generator = new ApiDocGeneratorDraftO2(DocGenTestHelper.createMockSchemaService(context));
+ }
+
+ /**
+ * Test that paths are generated according to the model.
+ */
+ @Test
+ public void testPaths() {
+ final List<String> expectedPaths = Arrays.asList("/restconf/config",
+ "/restconf/config/toaster2:toaster",
+ "/restconf/config/toaster2:toaster/toasterSlot/{slotId}",
+ "/restconf/config/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
+ "/restconf/operational/toaster2:toaster",
+ "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}",
+ "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
+ "/restconf/config/toaster2:lst",
+ "/restconf/config/toaster2:lst/cont1",
+ "/restconf/config/toaster2:lst/cont1/cont11",
+ "/restconf/config/toaster2:lst/cont1/lst11",
+ "/restconf/config/toaster2:lst/lst1/{key1}/{key2}",
+ "/restconf/operational/toaster2:lst",
+ "/restconf/operational/toaster2:lst/cont1",
+ "/restconf/operational/toaster2:lst/cont1/cont11",
+ "/restconf/operational/toaster2:lst/cont1/lst11",
+ "/restconf/operational/toaster2:lst/lst1/{key1}/{key2}",
+ "/restconf/operations/toaster2:make-toast",
+ "/restconf/operations/toaster2:cancel-toast",
+ "/restconf/operations/toaster2:restock-toaster");
+
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
+ final List<String> actualPaths = new ArrayList<>();
+ doc.getPaths().fieldNames().forEachRemaining(actualPaths::add);
+
+ assertEquals(expectedPaths, actualPaths);
+ }
+
+ /**
+ * Test that generated configuration paths allow to use operations: get, put, delete and post.
+ */
+ @Test
+ public void testConfigPaths() {
+ final List<String> configPaths = Arrays.asList("/restconf/config/toaster2:lst",
+ "/restconf/config/toaster2:lst/cont1",
+ "/restconf/config/toaster2:lst/cont1/cont11",
+ "/restconf/config/toaster2:lst/cont1/lst11",
+ "/restconf/config/toaster2:lst/lst1/{key1}/{key2}");
+
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
+
+ for (final String path : configPaths) {
+ final JsonNode node = doc.getPaths().get(path);
+ assertFalse(node.path("get").isMissingNode());
+ assertFalse(node.path("put").isMissingNode());
+ assertFalse(node.path("delete").isMissingNode());
+ assertFalse(node.path("post").isMissingNode());
+ }
+ }
+
+ /**
+ * Test that generated document contains the following definitions.
+ */
+ @Test
+ public void testDefinitions() {
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
+
+ final ObjectNode definitions = doc.getDefinitions();
+ assertNotNull(definitions);
+
+ final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
+ assertNotNull(configLstTop);
+ DocGenTestHelper.containsReferences(configLstTop,
+ "lst", "#/definitions/toaster2_config_lst");
+
+ final JsonNode configLst = definitions.get("toaster2_config_lst");
+ assertNotNull(configLst);
+ DocGenTestHelper.containsReferences(configLst,
+ "lst1", "#/definitions/toaster2_lst_config_lst1");
+ DocGenTestHelper.containsReferences(configLst,
+ "cont1", "#/definitions/toaster2_lst_config_cont1");
+
+ final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
+ assertNotNull(configLst1Top);
+ DocGenTestHelper.containsReferences(configLst1Top,
+ "lst1", "#/definitions/toaster2_lst_config_lst1");
+
+ final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
+ assertNotNull(configLst1);
+
+ final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
+ assertNotNull(configCont1Top);
+ DocGenTestHelper.containsReferences(configCont1Top,
+ "cont1", "#/definitions/toaster2_lst_config_cont1");
+
+ final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
+ assertNotNull(configCont1);
+ DocGenTestHelper.containsReferences(configCont1,
+ "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
+ DocGenTestHelper.containsReferences(configCont1,
+
+ "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
+
+ final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
+ assertNotNull(configCont11Top);
+ DocGenTestHelper.containsReferences(configCont11Top,
+ "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
+
+ final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
+ assertNotNull(configCont11);
+
+ final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
+ assertNotNull(configLst11Top);
+ DocGenTestHelper.containsReferences(configLst11Top,
+ "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
+
+ final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
+ assertNotNull(configLst11);
+ }
+
+ /**
+ * Test that generated document contains RPC definition for "make-toast" with correct input.
+ */
+ @Test
+ public void testRPC() {
+ final Optional<? extends Module> module = context.findModule(NAME_2, Revision.of(REVISION_DATE_2));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
+ assertNotNull(doc);
+
+ final ObjectNode definitions = doc.getDefinitions();
+ final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
+ assertNotNull(inputTop);
+ final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
+ assertEquals(testString, inputTop.get("properties").toString());
+ final JsonNode input = definitions.get("toaster_make-toast_input");
+ final JsonNode properties = input.get("properties");
+ assertTrue(properties.has("toasterDoneness"));
+ assertTrue(properties.has("toasterToastType"));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.rest.doc.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
+import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public final class ApiDocGeneratorRFC8040Test {
+ private static final String NAME = "toaster2";
+ private static final String REVISION_DATE = "2009-11-20";
+ private static final String NAME_2 = "toaster";
+ private static final String REVISION_DATE_2 = "2009-11-20";
+
+ private EffectiveModelContext context;
+ private ApiDocGeneratorRFC8040 generator;
+
+ @Before
+ public void setUp() {
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ generator = new ApiDocGeneratorRFC8040(DocGenTestHelper.createMockSchemaService(context));
+ }
+
+ /**
+ * Test that paths are generated according to the model.
+ */
+ @Test
+ public void testPaths() {
+ final List<String> expectedPaths = Arrays.asList("/rests/data",
+ "/rests/data/toaster2:toaster",
+ "/rests/data/toaster2:toaster/toasterSlot={slotId}",
+ "/rests/data/toaster2:toaster/toasterSlot={slotId}/toaster-augmented:slotInfo",
+ "/rests/data/toaster2:lst",
+ "/rests/data/toaster2:lst/cont1",
+ "/rests/data/toaster2:lst/cont1/cont11",
+ "/rests/data/toaster2:lst/cont1/lst11",
+ "/rests/data/toaster2:lst/lst1={key1},{key2}",
+ "/rests/operations/toaster2:make-toast",
+ "/rests/operations/toaster2:cancel-toast",
+ "/rests/operations/toaster2:restock-toaster");
+
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
+ final List<String> actualPaths = new ArrayList<>();
+ doc.getPaths().fieldNames().forEachRemaining(actualPaths::add);
+
+ assertEquals(expectedPaths, actualPaths);
+ }
+
+ /**
+ * Test that generated configuration paths allow to use operations: get, put, delete and post.
+ */
+ @Test
+ public void testConfigPaths() {
+ final List<String> configPaths = Arrays.asList("/rests/data/toaster2:lst",
+ "/rests/data/toaster2:lst/cont1",
+ "/rests/data/toaster2:lst/cont1/cont11",
+ "/rests/data/toaster2:lst/cont1/lst11",
+ "/rests/data/toaster2:lst/lst1={key1},{key2}");
+
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
+
+ for (final String path : configPaths) {
+ final JsonNode node = doc.getPaths().get(path);
+ assertFalse(node.path("get").isMissingNode());
+ assertFalse(node.path("put").isMissingNode());
+ assertFalse(node.path("delete").isMissingNode());
+ assertFalse(node.path("post").isMissingNode());
+ }
+ }
+
+ /**
+ * Test that generated document contains the following definitions.
+ */
+ @Test
+ public void testDefinitions() {
+ final Optional<? extends Module> module = context.findModule(NAME, Revision.of(REVISION_DATE));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
+
+ final ObjectNode definitions = doc.getDefinitions();
+ assertNotNull(definitions);
+
+ final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
+ assertNotNull(configLstTop);
+ DocGenTestHelper.containsReferences(configLstTop, "lst",
+ "#/definitions/toaster2_config_lst");
+
+ final JsonNode configLst = definitions.get("toaster2_config_lst");
+ assertNotNull(configLst);
+ DocGenTestHelper.containsReferences(configLst,
+ "lst1", "#/definitions/toaster2_lst_config_lst1");
+ DocGenTestHelper.containsReferences(configLst,
+ "cont1", "#/definitions/toaster2_lst_config_cont1");
+
+ final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
+ assertNotNull(configLst1Top);
+ DocGenTestHelper.containsReferences(configLst1Top,
+ "lst1", "#/definitions/toaster2_lst_config_lst1");
+
+ final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
+ assertNotNull(configLst1);
+
+ final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
+ assertNotNull(configCont1Top);
+ DocGenTestHelper.containsReferences(configCont1Top,
+ "cont1", "#/definitions/toaster2_lst_config_cont1");
+
+ final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
+ assertNotNull(configCont1);
+ DocGenTestHelper.containsReferences(configCont1,
+ "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
+ DocGenTestHelper.containsReferences(configCont1,
+ "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
+
+ final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
+ assertNotNull(configCont11Top);
+ DocGenTestHelper.containsReferences(configCont11Top,
+ "cont11", "#/definitions/toaster2_lst_cont1_config_cont11");
+
+ final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
+ assertNotNull(configCont11);
+
+ final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
+ assertNotNull(configLst11Top);
+ DocGenTestHelper.containsReferences(configLst11Top,
+ "lst11", "#/definitions/toaster2_lst_cont1_config_lst11");
+
+ final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
+ assertNotNull(configLst11);
+ }
+
+ /**
+ * Test that generated document contains RPC definition for "make-toast" with correct input.
+ */
+ @Test
+ public void testRPC() {
+ final Optional<? extends Module> module = context.findModule(NAME_2, Revision.of(REVISION_DATE_2));
+ assertTrue("Desired module not found", module.isPresent());
+ final SwaggerObject doc = generator.getSwaggerDocSpec(module.get(), "http","localhost:8181",
+ "/", "", context, URIType.RFC8040, ApiDocServiceImpl.OAversion.V2_0);
+ assertNotNull(doc);
+
+ final ObjectNode definitions = doc.getDefinitions();
+ final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
+ assertNotNull(inputTop);
+ final String testString = "{\"input\":{\"$ref\":\"#/definitions/toaster_make-toast_input\"}}";
+ assertEquals(testString, inputTop.get("properties").toString());
+ final JsonNode input = definitions.get("toaster_make-toast_input");
+ final JsonNode properties = input.get("properties");
+ assertTrue(properties.has("toasterDoneness"));
+ assertTrue(properties.has("toasterToastType"));
+ }
+}
+++ /dev/null
-/*
- * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.controller.sal.rest.doc.impl;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Preconditions;
-import java.sql.Date;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.TreeSet;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
-import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
-import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
-import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.Module;
-
-public class ApiDocGeneratorTest {
-
- private static final String NAMESPACE = "http://netconfcentral.org/ns/toaster2";
- private static final String STRING_DATE = "2009-11-20";
- private static final Date DATE = Date.valueOf(STRING_DATE);
- private static final String NAMESPACE_2 = "http://netconfcentral.org/ns/toaster";
- private static final Date REVISION_2 = Date.valueOf(STRING_DATE);
- private ApiDocGeneratorDraftO2 generator;
- private DocGenTestHelper helper;
- private EffectiveModelContext schemaContext;
-
- @Before
- public void setUp() throws Exception {
- this.helper = new DocGenTestHelper();
- this.helper.setUp();
-
- this.schemaContext = this.helper.getSchemaContext();
-
- this.generator = new ApiDocGeneratorDraftO2(this.helper.createMockSchemaService(this.schemaContext));
- }
-
- /**
- * Method: getApiDeclaration(String module, String revision, UriInfo uriInfo).
- */
- @Test
- public void testGetModuleDoc() throws Exception {
- Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
-
- for (final Module m : this.helper.getSchemaContext().getModules()) {
- if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE)
- && m.getQNameModule().getRevision().equals(DATE)) {
- final SwaggerObject doc = this.generator.getSwaggerDocSpec(m, "http","localhost:8181", "/", "",
- this.schemaContext, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
- validateToaster(doc);
- validateSwaggerModules(doc);
- }
- }
- }
-
- /**
- * Validates whether doc {@code doc} contains concrete specified models.
- */
- private static void validateSwaggerModules(final SwaggerObject doc) {
- final ObjectNode definitions = doc.getDefinitions();
- assertNotNull(definitions);
-
- final JsonNode configLstTop = definitions.get("toaster2_config_lst_TOP");
- assertNotNull(configLstTop);
-
- containsReferences(configLstTop, "lst", "toaster2_config_lst");
-
- final JsonNode configLst = definitions.get("toaster2_config_lst");
- assertNotNull(configLst);
-
- containsReferences(configLst, "lst1", "toaster2_lst_config_lst1");
- containsReferences(configLst, "cont1", "toaster2_lst_config_cont1");
-
- final JsonNode configLst1Top = definitions.get("toaster2_lst_config_lst1_TOP");
- assertNotNull(configLst1Top);
-
- containsReferences(configLst1Top, "lst1", "toaster2_config_lst");
-
- final JsonNode configLst1 = definitions.get("toaster2_lst_config_lst1");
- assertNotNull(configLst1);
-
- final JsonNode configCont1Top = definitions.get("toaster2_lst_config_cont1_TOP");
- assertNotNull(configCont1Top);
-
- containsReferences(configCont1Top, "cont1", "toaster2_config_lst");
- final JsonNode configCont1 = definitions.get("toaster2_lst_config_cont1");
- assertNotNull(configCont1);
-
- containsReferences(configCont1, "cont11", "toaster2_lst_config_cont1");
- containsReferences(configCont1, "lst11", "toaster2_lst_config_lst1");
-
- final JsonNode configCont11Top = definitions.get("toaster2_lst_cont1_config_cont11_TOP");
- assertNotNull(configCont11Top);
-
- containsReferences(configCont11Top, "cont11", "toaster2_lst_config_cont1");
- final JsonNode configCont11 = definitions.get("toaster2_lst_cont1_config_cont11");
- assertNotNull(configCont11);
-
- final JsonNode configLst11Top = definitions.get("toaster2_lst_cont1_config_lst11_TOP");
- assertNotNull(configLst11Top);
-
- containsReferences(configLst11Top, "lst11", "toaster2_lst_config_cont1");
- final JsonNode configLst11 = definitions.get("toaster2_lst_cont1_config_lst11");
- assertNotNull(configLst11);
- }
-
- /**
- * Checks whether object {@code mainObject} contains in properties/items key $ref with concrete value.
- */
- private static void containsReferences(final JsonNode mainObject, final String childObject,
- final String expectedRef) {
- final JsonNode properties = mainObject.get("properties");
- assertNotNull(properties);
-
- final JsonNode childNode = properties.get(childObject);
- assertNotNull(childNode);
-
- //list case
- JsonNode refWrapper = childNode.get("items");
- if (refWrapper == null) {
- //container case
- refWrapper = childNode;
- }
- assertEquals(expectedRef, refWrapper.get("$ref").asText());
- }
-
- @Test
- public void testEdgeCases() throws Exception {
- Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
-
- for (final Module m : this.helper.getModules()) {
- if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE_2)
- && m.getQNameModule().getRevision().equals(REVISION_2)) {
- final SwaggerObject doc = this.generator.getSwaggerDocSpec(m, "http", "localhost:8080", "/restconf", "",
- this.schemaContext, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
- assertNotNull(doc);
-
- // testing bugs.opendaylight.org bug 1290. UnionType model type.
- final String jsonString = doc.getDefinitions().toString();
- assertTrue(jsonString.contains("testUnion\":{\"required\":false,\"type\":\"-2147483648\","
- + "\"enum\":[\"-2147483648\",\"Some testUnion\"]}"));
- }
- }
- }
-
- @Test
- public void testRPCsModel() throws Exception {
- Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
-
- for (final Module m : this.helper.getModules()) {
- if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE_2)
- && m.getQNameModule().getRevision().equals(REVISION_2)) {
- final SwaggerObject doc = this.generator.getSwaggerDocSpec(m, "http","localhost:8181", "/", "",
- this.schemaContext, URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
- assertNotNull(doc);
-
- final ObjectNode definitions = doc.getDefinitions();
- final JsonNode inputTop = definitions.get("toaster_make-toast_input_TOP");
- assertNotNull(inputTop);
- final String testString = "{\"input\":{\"$ref\":\"toaster_make-toast_input\"}}";
- assertEquals(testString, inputTop.get("properties").toString());
- final JsonNode input = definitions.get("toaster_make-toast_input");
- final JsonNode properties = input.get("properties");
- assertTrue(properties.has("toasterDoneness"));
- assertTrue(properties.has("toasterToastType"));
- }
- }
- }
-
- /**
- * Tests whether from yang files are generated all required paths for HTTP operations (GET, DELETE, PUT, POST)
- *
- * <p>
- * If container | list is augmented then in path there should be specified module name followed with colon (e. g.
- * "/config/module1:element1/element2/module2:element3")
- *
- * @param doc Api declaration
- * @throws Exception if operation fails
- */
- private static void validateToaster(final SwaggerObject doc) throws Exception {
- final Set<String> expectedUrls =
- new TreeSet<>(Arrays.asList("/restconf/config", "/restconf/config/toaster2:toaster",
- "/restconf/config/toaster2:toaster/toasterSlot/{slotId}",
- "/restconf/config/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
- "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}",
- "/restconf/operational/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo",
- "/restconf/config/toaster2:lst",
- "/restconf/config/toaster2:lst/cont1",
- "/restconf/config/toaster2:lst/cont1/cont11",
- "/restconf/config/toaster2:lst/cont1/lst11",
- "/restconf/config/toaster2:lst/lst1/{key1}/{key2}",
- "/restconf/operational/toaster2:lst",
- "/restconf/operational/toaster2:lst/cont1",
- "/restconf/operational/toaster2:lst/cont1/cont11",
- "/restconf/operational/toaster2:lst/cont1/lst11",
- "/restconf/operational/toaster2:lst/lst1/{key1}/{key2}",
- "/restconf/operational/toaster2:lst/lst1/{key1}/{key2}",
- "/restconf/operations/toaster2:make-toast",
- "/restconf/operations/toaster2:cancel-toast"));
-
-
- final Set<String> actualUrls = new TreeSet<>();
-
- final Set<JsonNode> configActualPathItems = new HashSet<>();
- for (final JsonNode pathItem : doc.getPaths()) {
- final String actualUrl = pathItem.fieldNames().next();
- actualUrls.add(actualUrl);
- if (actualUrl.contains("/config/toaster2:toaster/")) {
- configActualPathItems.add(pathItem);
- }
- }
-
- final boolean isAllDocumented = actualUrls.containsAll(expectedUrls);
-
- if (!isAllDocumented) {
- expectedUrls.removeAll(actualUrls);
- fail("Missing expected urls: " + expectedUrls);
- }
-
- final Set<String> expectedConfigMethods = new TreeSet<>(Arrays.asList("get", "put", "delete", "post"));
-
- for (final JsonNode configPathItem : configActualPathItems) {
- final Set<String> actualConfigMethods = new TreeSet<>();
- final JsonNode operations = configPathItem.get(0);
- final Iterator<String> it = operations.fieldNames();
- while (it.hasNext()) {
- actualConfigMethods.add(it.next());
- }
- final boolean isAllMethods = actualConfigMethods.containsAll(expectedConfigMethods);
- if (!isAllMethods) {
- expectedConfigMethods.removeAll(actualConfigMethods);
- fail("Missing expected method on config API: " + expectedConfigMethods);
- }
- }
-
- // TODO: we should really do some more validation of the
- // documentation...
- /*
- * Missing validation: Explicit validation of URLs, and their methods Input / output models.
- */
- }
-
-}
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Optional;
import javax.ws.rs.core.UriInfo;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public class ApiDocServiceImplTest {
+public final class ApiDocServiceImplTest {
private static final String HTTP_URL = "http://localhost/path";
private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
.node(QName.create("", "nodes"))
.node(QName.create("", "node"))
.nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
- private DocGenTestHelper helper;
- private ApiDocService apiDocService;
+ private ApiDocService apiDocService;
- @SuppressWarnings("resource")
@Before
- public void setUp() throws Exception {
- this.helper = new DocGenTestHelper();
- this.helper.setUp();
-
- final EffectiveModelContext context = this.helper.createMockSchemaContext();
- final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
+ public void setUp() {
+ final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ final DOMSchemaService schemaService = DocGenTestHelper.createMockSchemaService(context);
final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
mountPointDraft02.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
final AllModulesDocGenerator allModulesDocGenerator = new AllModulesDocGenerator(apiDocGeneratorDraftO2,
apiDocGeneratorRFC8040);
- this.apiDocService = new ApiDocServiceImpl(mountPointDraft02, mountPointRFC8040, apiDocGeneratorDraftO2,
+ apiDocService = new ApiDocServiceImpl(mountPointDraft02, mountPointRFC8040, apiDocGeneratorDraftO2,
apiDocGeneratorRFC8040, allModulesDocGenerator);
}
@Test
- public void getListOfMounts() throws java.net.URISyntaxException, JsonProcessingException {
- final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
+ public void getListOfMounts() throws Exception {
+ final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
// simulate the behavior of JacksonJaxbJsonProvider
final ObjectMapper mapper = new ObjectMapper();
- final String result = mapper.writer().writeValueAsString(
- apiDocService.getListOfMounts(mockInfo).getEntity());
+ final String result = mapper.writer().writeValueAsString(apiDocService.getListOfMounts(mockInfo).getEntity());
Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);
}
}
*/
package org.opendaylight.controller.sal.rest.doc.impl;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
-import java.io.File;
-import java.io.FileNotFoundException;
+import com.fasterxml.jackson.databind.JsonNode;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Optional;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.mockito.ArgumentCaptor;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public class DocGenTestHelper {
+final class DocGenTestHelper {
- private Collection<? extends Module> modules;
- private ObjectMapper mapper;
- private EffectiveModelContext schemaContext;
-
- public Collection<? extends Module> loadModules(final String resourceDirectory)
- throws URISyntaxException, FileNotFoundException {
-
- final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
- final File testDir = new File(resourceDirUri);
- final String[] fileList = testDir.list();
- if (fileList == null) {
- throw new FileNotFoundException(resourceDirectory.toString());
- }
- final List<File> files = new ArrayList<>();
- for (final String fileName : fileList) {
- files.add(new File(testDir, fileName));
- }
-
- this.schemaContext = YangParserTestUtils.parseYangFiles(files);
- return this.schemaContext.getModules();
- }
-
- public Collection<? extends Module> getModules() {
- return this.modules;
- }
-
- public void setUp() throws Exception {
- this.modules = loadModules("/yang");
- this.mapper = new ObjectMapper();
- this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
- }
-
- public EffectiveModelContext getSchemaContext() {
- return this.schemaContext;
+ private DocGenTestHelper() {
+ // hidden on purpose
}
- public DOMSchemaService createMockSchemaService(EffectiveModelContext mockContext) {
- if (mockContext == null) {
- mockContext = createMockSchemaContext();
- }
-
+ static DOMSchemaService createMockSchemaService(final EffectiveModelContext mockContext) {
final DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
return mockSchemaService;
}
- public EffectiveModelContext createMockSchemaContext() {
- final EffectiveModelContext mockContext = mock(EffectiveModelContext.class);
- doReturn(this.modules).when(mockContext).getModules();
-
- final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
- final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
- final ArgumentCaptor<XMLNamespace> namespaceCapture = ArgumentCaptor.forClass(XMLNamespace.class);
- when(mockContext.findModule(moduleCapture.capture(), dateCapture.capture())).then(
- invocation -> {
- final String module = moduleCapture.getValue();
- final Optional<?> date = dateCapture.getValue();
- for (final Module m : DocGenTestHelper.this.modules) {
- if (m.getName().equals(module) && m.getRevision().equals(date)) {
- return Optional.of(m);
- }
- }
- return Optional.empty();
- });
- when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
- invocation -> {
- final XMLNamespace namespace = namespaceCapture.getValue();
- final Optional<?> date = dateCapture.getValue();
- for (final Module m : DocGenTestHelper.this.modules) {
- if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
- return Optional.of(m);
- }
- }
- return Optional.empty();
- });
- return mockContext;
- }
-
- public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
+ static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
final URI uri = new URI(urlPrefix);
-
final UriBuilder mockBuilder = mock(UriBuilder.class);
final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
final UriInfo info = mock(UriInfo.class);
-
when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
when(info.getBaseUri()).thenReturn(uri);
+
return info;
}
+ /**
+ * Checks whether object {@code mainObject} contains in properties/items key $ref with concrete value.
+ */
+ static void containsReferences(final JsonNode mainObject, final String childObject, final String expectedRef) {
+ final JsonNode properties = mainObject.get("properties");
+ assertNotNull(properties);
+
+ final JsonNode childNode = properties.get(childObject);
+ assertNotNull(childNode);
+
+ //list case
+ JsonNode refWrapper = childNode.get("items");
+ if (refWrapper == null) {
+ //container case
+ refWrapper = childNode;
+ }
+ assertEquals(expectedRef, refWrapper.get("$ref").asText());
+ }
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.sal.rest.doc.impl;
import static org.junit.Assert.assertEquals;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public class MountPointSwaggerTest {
-
+public final class MountPointSwaggerTest {
private static final String HTTP_URL = "http://localhost/path";
private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
.node(QName.create("", "nodes"))
.node(QName.create("", "node"))
.nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
private static final String INSTANCE_URL = "/nodes/node/123/";
+
private MountPointSwagger swagger;
- private DocGenTestHelper helper;
- @SuppressWarnings("resource")
@Before
- public void setUp() throws Exception {
- this.helper = new DocGenTestHelper();
- this.helper.setUp();
-
+ public void setUp() {
// We are sharing the global schema service and the mount schema service
// in our test.
// OK for testing - real thing would have separate instances.
- final EffectiveModelContext context = this.helper.createMockSchemaContext();
- final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
+ final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ final DOMSchemaService schemaService = DocGenTestHelper.createMockSchemaService(context);
final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
final MountPointSwaggerGeneratorDraft02 generator =
new MountPointSwaggerGeneratorDraft02(schemaService, service);
- this.swagger = generator.getMountPointSwagger();
+ swagger = generator.getMountPointSwagger();
}
@Test()
- public void getInstanceIdentifiers() throws Exception {
- assertEquals(0, this.swagger.getInstanceIdentifiers().size());
- this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
- assertEquals(1, this.swagger.getInstanceIdentifiers().size());
- assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
+ public void getInstanceIdentifiers() {
+ assertEquals(0, swagger.getInstanceIdentifiers().size());
+ swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
+ assertEquals(1, swagger.getInstanceIdentifiers().size());
+ assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
.getValue());
- assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
+ assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
.getKey());
- this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
- assertEquals(0, this.swagger.getInstanceIdentifiers().size());
+ swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
+ assertEquals(0, swagger.getInstanceIdentifiers().size());
}
@Test
public void testGetDataStoreApi() throws Exception {
- final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
- this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
+ final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
+ swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
- final SwaggerObject mountPointApi = (SwaggerObject) this.swagger.getMountPointApi(mockInfo, 1L, "Datastores",
+ final SwaggerObject mountPointApi = (SwaggerObject) swagger.getMountPointApi(mockInfo, 1L, "Datastores",
"-", URIType.DRAFT02, OAversion.V2_0);
assertNotNull("failed to find Datastore API", mountPointApi);
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.sal.rest.doc.impl;
+import static org.junit.Assert.assertTrue;
+
import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Preconditions;
-import java.sql.Date;
+import java.util.Optional;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
import org.opendaylight.netconf.sal.rest.doc.impl.DefinitionGenerator;
import org.opendaylight.netconf.sal.rest.doc.impl.DefinitionNames;
+import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-public class SwaggerObjectTest {
-
- private static final String NAMESPACE = "urn:opendaylight:groupbasedpolicy:opflex";
- private static final String STRING_DATE = "2014-05-28";
- private static final Date REVISION = Date.valueOf(STRING_DATE);
- private DocGenTestHelper helper;
- private EffectiveModelContext schemaContext;
+public final class SwaggerObjectTest {
+ private EffectiveModelContext context;
@Before
- public void setUp() throws Exception {
- this.helper = new DocGenTestHelper();
- this.helper.setUp();
- this.schemaContext = this.helper.getSchemaContext();
+ public void setUp() {
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
}
@Test
public void testConvertToJsonSchema() throws Exception {
-
- Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
-
+ final Optional<? extends Module> module = context.findModule("opflex", Revision.of("2014-05-28"));
+ assertTrue("Desired module not found", module.isPresent());
final DefinitionGenerator generator = new DefinitionGenerator();
-
- for (final Module m : this.helper.getModules()) {
- if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE)
- && m.getQNameModule().getRevision().equals(REVISION)) {
-
- final ObjectNode jsonObject = generator.convertToJsonSchema(m, this.schemaContext,
- new DefinitionNames(), ApiDocServiceImpl.OAversion.V2_0, true);
- Assert.assertNotNull(jsonObject);
- }
- }
+ final ObjectNode jsonObject = generator.convertToJsonSchema(module.get(), context,
+ new DefinitionNames(), ApiDocServiceImpl.OAversion.V2_0, true);
+ Assert.assertNotNull(jsonObject);
}
@Test
public void testStringTypes() throws Exception {
- Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
- Module strTypes = this.helper.getModules().stream()
- .filter(module -> module.getName().equals("string-types"))
- .findFirst()
- .orElseThrow(() -> new IllegalArgumentException("String types module not found"));
-
+ final Optional<? extends Module> module = context.findModule("string-types");
+ assertTrue("Desired module not found", module.isPresent());
final DefinitionGenerator generator = new DefinitionGenerator();
- final ObjectNode jsonObject = generator.convertToJsonSchema(strTypes, this.schemaContext, new DefinitionNames(),
+ final ObjectNode jsonObject = generator.convertToJsonSchema(module.get(), context, new DefinitionNames(),
ApiDocServiceImpl.OAversion.V2_0, true);
Assert.assertNotNull(jsonObject);