4b1a020a850a7994e93a1ddec7e1e31007c00683
[yangtools.git] / yang / yang-parser-impl / 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 java.net.URI;
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;
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(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();
40
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());
44     }
45
46     @Test
47     public void basicTest2() throws Exception {
48         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-2",
49             StatementParserMode.SEMVER_MODE);
50         assertNotNull(context);
51
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();
55
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());
59     }
60
61     @Test
62     public void basicTest3() throws Exception {
63         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-3",
64             StatementParserMode.SEMVER_MODE);
65         assertNotNull(context);
66
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();
69
70         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
71         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
72     }
73
74     @Test
75     public void basicImportTest1() throws Exception {
76         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/basic-import-1",
77                 StatementParserMode.SEMVER_MODE);
78         assertNotNull(context);
79
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();
82
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());
87     }
88
89     @Test
90     public void multipleModulesTest() throws Exception {
91         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple-modules",
92                 StatementParserMode.SEMVER_MODE);
93         assertNotNull(context);
94
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();
97
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());
102     }
103
104     @Test
105     public void basicImportErrTest1() throws Exception {
106         try {
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)]."));
113         }
114     }
115
116     @Test
117     public void basicImportErrTest2() throws Exception {
118         try {
119             StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-2",
120                 StatementParserMode.SEMVER_MODE);
121             fail("Test should fail due to invalid openconfig version");
122         } catch (ReactorException e) {
123             assertTrue(e.getCause().getCause().getMessage()
124                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
125         }
126     }
127
128     @Test
129     public void nodeTest() throws Exception {
130         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/node-test",
131             StatementParserMode.SEMVER_MODE);
132         assertNotNull(context);
133
134         Module foo = context.findModules(new URI("foo")).iterator().next();
135         Module semVer = context.findModules(new URI("http://openconfig.net/yang/openconfig-ext")).iterator().next();
136
137         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
138         assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion().get());
139         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
140         assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion().get());
141
142         QName root = QName.create("foo", "2016-01-01", "foo-root");
143         QName container20160404 = QName.create("foo", "2016-01-01", "con20160404");
144         SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
145                 SchemaPath.create(true, root, container20160404));
146         assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
147
148         QName container20160405 = QName.create("foo", "2016-01-01", "con20160405");
149         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
150                 SchemaPath.create(true, root, container20160405));
151         assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
152
153         QName container20160406 = QName.create("foo", "2016-01-01", "con20160406");
154         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
155                 SchemaPath.create(true, root, container20160406));
156         assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
157
158         QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
159         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
160                 SchemaPath.create(true, root, container20170406));
161         assertNull(findDataSchemaNode);
162     }
163 }