Add XMLNamespace
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / OpenconfigVersionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt.openconfigver;
9
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;
15
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;
29
30 public class OpenconfigVersionTest {
31     @Test
32     public void basicTest() throws Exception {
33         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic",
34             StatementParserMode.SEMVER_MODE);
35         assertNotNull(context);
36
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"))
40             .iterator().next();
41
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());
45     }
46
47     @Test
48     public void basicTest2() throws Exception {
49         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-2",
50             StatementParserMode.SEMVER_MODE);
51         assertNotNull(context);
52
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"))
56             .iterator().next();
57
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());
61     }
62
63     @Test
64     public void basicTest3() throws Exception {
65         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-3",
66             StatementParserMode.SEMVER_MODE);
67         assertNotNull(context);
68
69         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
70         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
71             .iterator().next();
72
73         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
74         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
75     }
76
77     @Test
78     public void basicImportTest1() throws Exception {
79         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-import-1",
80                 StatementParserMode.SEMVER_MODE);
81         assertNotNull(context);
82
83         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
84         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
85             .iterator().next();
86
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());
91     }
92
93     @Test
94     public void multipleModulesTest() throws Exception {
95         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple-modules",
96                 StatementParserMode.SEMVER_MODE);
97         assertNotNull(context);
98
99         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
100         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
101             .iterator().next();
102
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());
107     }
108
109     @Test
110     public void basicImportErrTest1() throws Exception {
111         try {
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)]."));
118         }
119     }
120
121     @Test
122     public void basicImportErrTest2() throws Exception {
123         try {
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)]."));
130         }
131     }
132
133     @Test
134     public void nodeTest() throws Exception {
135         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/node-test",
136             StatementParserMode.SEMVER_MODE);
137         assertNotNull(context);
138
139         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
140         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
141             .iterator().next();
142
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());
147
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);
153
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);
158
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);
163
164         QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
165         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
166                 SchemaPath.create(true, root, container20170406));
167         assertNull(findDataSchemaNode);
168     }
169 }