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.restconf.openapi.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.io.IOException;
16 import java.math.BigDecimal;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public final class DefinitionGeneratorTest {
25 private static EffectiveModelContext context;
26 private static DOMSchemaService schemaService;
29 public static void beforeClass() {
30 schemaService = mock(DOMSchemaService.class);
31 context = YangParserTestUtils.parseYangResourceDirectory("/yang");
32 when(schemaService.getGlobalContext()).thenReturn(context);
36 public void testConvertToSchemas() throws IOException {
37 final var module = context.findModule("opflex", Revision.of("2014-05-28")).orElseThrow();
38 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
39 assertNotNull(schemas);
43 public void testActionTypes() throws IOException {
44 final var module = context.findModule("action-types").orElseThrow();
45 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
46 assertNotNull(schemas);
50 public void testStringTypes() throws IOException {
51 final var module = context.findModule("string-types").orElseThrow();
52 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
53 assertNotNull(schemas);
57 public void testEnumType() throws IOException {
58 final var module = context.findModule("definition-test").orElseThrow();
59 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
60 assertNotNull(schemas);
62 final var properties = schemas.get("definition-test_enum-container").properties();
63 assertEquals("up", properties.get("status").defaultValue());
67 public void testUnionTypes() throws IOException {
68 final var module = context.findModule("definition-test").orElseThrow();
69 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
70 assertNotNull(schemas);
72 final var properties = schemas.get("definition-test_union-container").properties();
73 assertEquals("5", properties.get("testUnion1").defaultValue());
74 assertEquals("integer", properties.get("testUnion1").type());
75 assertEquals(-2147483648, properties.get("testUnion1").example());
76 assertEquals("false", properties.get("testUnion2").defaultValue());
77 assertEquals("string", properties.get("testUnion2").type());
78 assertEquals("Some testUnion2", properties.get("testUnion2").example());
79 assertEquals("integer", properties.get("testUnion3").type());
80 assertEquals(-2147483648, properties.get("testUnion3").example());
81 assertEquals("false", properties.get("testUnion3").defaultValue());
85 public void testBinaryType() throws IOException {
86 final var module = context.findModule("definition-test").orElseThrow();
87 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
88 assertNotNull(schemas);
90 final var properties = schemas.get("definition-test_binary-container").properties();
91 assertEquals("SGVsbG8gdGVzdCE=", properties.get("binary-data").defaultValue());
95 public void testBooleanType() throws IOException {
96 final var module = context.findModule("definition-test").orElseThrow();
97 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
98 assertNotNull(schemas);
100 final var properties = schemas.get("definition-test_union-container").properties();
101 assertEquals(true, properties.get("testBoolean").defaultValue());
102 assertEquals(true, properties.get("testBoolean").example());
106 public void testNumberType() throws IOException {
107 final var module = context.findModule("definition-test").orElseThrow();
108 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
109 assertNotNull(schemas);
111 final var properties = schemas.get("definition-test_number-container").properties();
112 assertEquals(42L, properties.get("testInteger").defaultValue());
113 assertEquals(42L, properties.get("testInt64").defaultValue());
114 assertEquals(BigDecimal.valueOf(42), properties.get("testUint64").defaultValue());
115 assertEquals(100L, properties.get("testUnsignedInteger").defaultValue());
116 assertEquals(BigDecimal.valueOf(3.14), properties.get("testDecimal").defaultValue());
117 assertEquals(BigDecimal.valueOf(3.14159265359), properties.get("testDouble").defaultValue());
121 public void testInstanceIdentifierType() throws IOException {
122 final var module = context.findModule("definition-test").orElseThrow();
123 final var schemas = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
124 assertNotNull(schemas);
126 final var properties = schemas.get("definition-test_network-container").properties();
127 final var networkRef = properties.get("network-ref");
129 assertNotNull(networkRef);
130 assertEquals("string", networkRef.type());
132 assertEquals("/network/nodes[node-id='node1']", networkRef.defaultValue());
133 assertEquals("/sample:binary-container", networkRef.example());
137 public void testStringFromRegex() throws IOException {
138 final var module = context.findModule("strings-from-regex").orElseThrow();
139 final var jsonObject = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
140 assertNotNull(jsonObject);
142 final var properties = jsonObject.get("strings-from-regex_test").properties();
143 assertEquals("00:00:00:00:00:00", properties.get("mac-address").example().toString());
144 assertEquals("0000-00-00T00:00:00Z", properties.get("login-date-time").example().toString());
145 assertEquals("0.0.0.0", properties.get("ipv4-address").example().toString());
149 * Test that checks if namespace for rpc is present.
152 public void testRpcNamespace() throws Exception {
153 final var module = context.findModule("toaster", Revision.of("2009-11-20")).orElseThrow();
154 final var jsonObject = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
155 assertNotNull(jsonObject);
156 final var schema = jsonObject.get("toaster_make-toast_input");
157 assertNotNull(schema);
158 final var xml = schema.xml();
160 final var namespace = xml.namespace();
161 assertNotNull(namespace);
162 assertEquals("http://netconfcentral.org/ns/toaster", namespace);
166 * Test that checks if namespace for actions is present.
169 public void testActionsNamespace() throws IOException {
170 final var module = context.findModule("action-types").orElseThrow();
171 final var jsonObject = DefinitionGenerator.convertToSchemas(module, context, new DefinitionNames(), true);
172 assertNotNull(jsonObject);
173 final var schema = jsonObject.get("action-types_container-action_input");
174 assertNotNull(schema);
175 final var xml = schema.xml();
177 final var namespace = xml.namespace();
178 assertNotNull(namespace);
179 assertEquals("urn:ietf:params:xml:ns:yang:test:action:types", namespace);