2 * Copyright (c) 2016 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.yangtools.yang.stmt.openconfigver;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.concepts.SemVer;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30 public class OpenconfigVersionTest {
32 public void basicTest() throws Exception {
33 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic",
34 StatementParserMode.OPENCONFIG_VER_MODE);
35 assertNotNull(context);
37 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
38 Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
39 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
42 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
43 assertEquals(SemVer.valueOf("0.1.1"), foo.getOpenconfigVersion());
44 assertEquals(SemVer.valueOf("0.1.2"), bar.getOpenconfigVersion());
48 public void basicTest2() throws Exception {
49 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-2",
50 StatementParserMode.OPENCONFIG_VER_MODE);
51 assertNotNull(context);
53 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
54 Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
55 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
58 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
59 assertEquals(SemVer.valueOf("0.1.1"), foo.getOpenconfigVersion());
60 assertEquals(SemVer.valueOf("0.1.2"), bar.getOpenconfigVersion());
64 public void basicTest3() throws Exception {
65 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-3",
66 StatementParserMode.OPENCONFIG_VER_MODE);
67 assertNotNull(context);
69 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
70 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
73 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
74 assertEquals(SemVer.valueOf("0.1.1"), foo.getOpenconfigVersion());
78 public void basicImportTest1() throws Exception {
79 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-import-1",
80 StatementParserMode.OPENCONFIG_VER_MODE);
81 assertNotNull(context);
83 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
84 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
87 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
88 assertEquals(SemVer.valueOf("0.1.1"), foo.getOpenconfigVersion());
89 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
90 assertEquals(SemVer.valueOf("0.1.2"), bar.getOpenconfigVersion());
94 public void multipleModulesTest() throws Exception {
95 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple-modules",
96 StatementParserMode.OPENCONFIG_VER_MODE);
97 assertNotNull(context);
99 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
100 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
103 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
104 assertEquals(SemVer.valueOf("0.1.1"), foo.getOpenconfigVersion());
105 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
106 assertEquals(SemVer.valueOf("0.10.4"), bar.getOpenconfigVersion());
110 public void basicImportErrTest1() throws Exception {
112 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-1", StatementParserMode.OPENCONFIG_VER_MODE);
113 fail("Test should fail due to invalid openconfig version");
114 } catch (ReactorException e) {
115 assertTrue(e.getCause().getMessage()
116 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
121 public void basicImportErrTest2() throws Exception {
123 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-2", StatementParserMode.OPENCONFIG_VER_MODE);
124 fail("Test should fail due to invalid openconfig version");
125 } catch (ReactorException e) {
126 assertTrue(e.getCause().getMessage()
127 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
132 public void nodeTest() throws Exception {
133 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/node-test",
134 StatementParserMode.OPENCONFIG_VER_MODE);
135 assertNotNull(context);
137 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
138 Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
141 assertEquals(SemVer.valueOf("0.0.1"), semVer.getOpenconfigVersion());
142 assertEquals(SemVer.valueOf("2016.1.1"), foo.getOpenconfigVersion());
143 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
144 assertEquals(SemVer.valueOf("2016.4.6"), bar.getOpenconfigVersion());
146 QName root = QName.create("foo", "2016-01-01", "foo-root");
147 QName container20160404 = QName.create("foo", "2016-01-01", "con20160404");
148 SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
149 SchemaPath.create(true, root, container20160404));
150 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
152 QName container20160405 = QName.create("foo", "2016-01-01", "con20160405");
153 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
154 SchemaPath.create(true, root, container20160405));
155 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
157 QName container20160406 = QName.create("foo", "2016-01-01", "con20160406");
158 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
159 SchemaPath.create(true, root, container20160406));
160 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
162 QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
163 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
164 SchemaPath.create(true, root, container20170406));
165 assertNull(findDataSchemaNode);