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.SEMVER_MODE);
35 assertNotNull(context);
37 Module foo = context.findModules(new URI("foo")).iterator().next();
38 Module bar = context.findModules(new URI("bar")).iterator().next();
39 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
41 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
42 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
43 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
47 public void basicTest2() throws Exception {
48 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-2",
49 StatementParserMode.SEMVER_MODE);
50 assertNotNull(context);
52 Module foo = context.findModules(new URI("foo")).iterator().next();
53 Module bar = context.findModules(new URI("bar")).iterator().next();
54 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
56 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
57 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
58 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
62 public void basicTest3() throws Exception {
63 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-3",
64 StatementParserMode.SEMVER_MODE);
65 assertNotNull(context);
67 Module foo = context.findModules(new URI("foo")).iterator().next();
68 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
70 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
71 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
75 public void basicImportTest1() throws Exception {
76 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-import-1",
77 StatementParserMode.SEMVER_MODE);
78 assertNotNull(context);
80 Module foo = context.findModules(new URI("foo")).iterator().next();
81 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
83 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
84 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
85 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
86 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
90 public void multipleModulesTest() throws Exception {
91 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple-modules",
92 StatementParserMode.SEMVER_MODE);
93 assertNotNull(context);
95 Module foo = context.findModules(new URI("foo")).iterator().next();
96 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
98 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
99 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
100 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
101 assertEquals(SemVer.valueOf("0.10.4"), bar.getSemanticVersion().get());
105 public void basicImportErrTest1() throws Exception {
107 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-1",
108 StatementParserMode.SEMVER_MODE);
109 fail("Test should fail due to invalid openconfig version");
110 } catch (ReactorException e) {
111 assertTrue(e.getCause().getCause().getMessage()
112 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
117 public void basicImportErrTest2() throws Exception {
119 StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-2", StatementParserMode.SEMVER_MODE);
120 fail("Test should fail due to invalid openconfig version");
121 } catch (ReactorException e) {
122 assertTrue(e.getCause().getCause().getMessage()
123 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
128 public void nodeTest() throws Exception {
129 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/node-test",
130 StatementParserMode.SEMVER_MODE);
131 assertNotNull(context);
133 Module foo = context.findModules(new URI("foo")).iterator().next();
134 Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
136 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
137 assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion().get());
138 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
139 assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion().get());
141 QName root = QName.create("foo", "2016-01-01", "foo-root");
142 QName container20160404 = QName.create("foo", "2016-01-01", "con20160404");
143 SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
144 SchemaPath.create(true, root, container20160404));
145 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
147 QName container20160405 = QName.create("foo", "2016-01-01", "con20160405");
148 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
149 SchemaPath.create(true, root, container20160405));
150 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
152 QName container20160406 = QName.create("foo", "2016-01-01", "con20160406");
153 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
154 SchemaPath.create(true, root, container20160406));
155 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
157 QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
158 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
159 SchemaPath.create(true, root, container20170406));
160 assertNull(findDataSchemaNode);