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;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.concepts.SemVer;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.SEMVER_MODE);
35 assertNotNull(context);
37 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
38 Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
39 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
42 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
43 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
44 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
48 public void basicTest2() throws Exception {
49 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-2",
50 StatementParserMode.SEMVER_MODE);
51 assertNotNull(context);
53 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
54 Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
55 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
58 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
59 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
60 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
64 public void basicTest3() throws Exception {
65 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-3",
66 StatementParserMode.SEMVER_MODE);
67 assertNotNull(context);
69 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
70 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
73 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
74 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
78 public void basicImportTest1() throws Exception {
79 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-import-1",
80 StatementParserMode.SEMVER_MODE);
81 assertNotNull(context);
83 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
84 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
87 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
88 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
89 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
90 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
94 public void multipleModulesTest() throws Exception {
95 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple-modules",
96 StatementParserMode.SEMVER_MODE);
97 assertNotNull(context);
99 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
100 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
103 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
104 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
105 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
106 assertEquals(SemVer.valueOf("0.10.4"), bar.getSemanticVersion().get());
110 public void basicImportErrTest1() throws Exception {
112 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-1",
113 StatementParserMode.SEMVER_MODE);
114 fail("Test should fail due to invalid openconfig version");
115 } catch (ReactorException e) {
116 assertTrue(e.getCause().getCause().getMessage()
117 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
122 public void basicImportErrTest2() throws Exception {
124 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-2",
125 StatementParserMode.SEMVER_MODE);
126 fail("Test should fail due to invalid openconfig version");
127 } catch (ReactorException e) {
128 assertTrue(e.getCause().getCause().getMessage()
129 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
134 public void nodeTest() throws Exception {
135 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/node-test",
136 StatementParserMode.SEMVER_MODE);
137 assertNotNull(context);
139 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
140 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
143 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
144 assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion().get());
145 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
146 assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion().get());
148 QName root = QName.create("foo", "2016-01-01", "foo-root");
149 QName container20160404 = QName.create("foo", "2016-01-01", "con20160404");
150 SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
151 SchemaPath.create(true, root, container20160404));
152 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
154 QName container20160405 = QName.create("foo", "2016-01-01", "con20160405");
155 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
156 SchemaPath.create(true, root, container20160405));
157 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
159 QName container20160406 = QName.create("foo", "2016-01-01", "con20160406");
160 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
161 SchemaPath.create(true, root, container20160406));
162 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
164 QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
165 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
166 SchemaPath.create(true, root, container20170406));
167 assertNull(findDataSchemaNode);