Revert "Bug 4640: Change semantic-version to openconfig-version"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / semver / SemanticVersionTest.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.semver;
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 SemanticVersionTest {
31     @Test
32     public void basicTest() throws Exception {
33         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic",
34             StatementParserMode.SEMVER_MODE);
35         assertNotNull(context);
36
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("urn:opendaylight:yang:extension:semantic-version"))
40                 .iterator().next();
41
42         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
43         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
44         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
45     }
46
47     @Test
48     public void basicTest2() throws Exception {
49         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-2",
50             StatementParserMode.SEMVER_MODE);
51         assertNotNull(context);
52
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("urn:opendaylight:yang:extension:semantic-version"))
56                 .iterator().next();
57
58         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
59         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
60         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
61     }
62
63     @Test
64     public void basicTest3() throws Exception {
65         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-3",
66             StatementParserMode.SEMVER_MODE);
67         assertNotNull(context);
68
69         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
70         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
71                 .iterator().next();
72
73         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
74         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
75     }
76
77     @Test
78     public void basicImportTest1() throws Exception {
79         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-import-1",
80                 StatementParserMode.SEMVER_MODE);
81         assertNotNull(context);
82
83         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
84         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
85                 .iterator().next();
86
87         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
88         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
89         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
90         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
91     }
92
93     @Test
94     public void multipleModulesTest() throws Exception {
95         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple-modules",
96                 StatementParserMode.SEMVER_MODE);
97         assertNotNull(context);
98
99         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
100         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
101                 .iterator().next();
102
103         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
104         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
105         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
106         assertEquals(SemVer.valueOf("0.10.4"), bar.getSemanticVersion());
107     }
108
109     @Test
110     public void basicImportErrTest1() throws Exception {
111         try {
112             StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-1", StatementParserMode.SEMVER_MODE);
113             fail("Test should fail due to invalid semantic version");
114         } catch (ReactorException e) {
115             assertTrue(e.getCause().getMessage()
116                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
117         }
118     }
119
120     @Test
121     public void basicImportErrTest2() throws Exception {
122         try {
123             StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-2", StatementParserMode.SEMVER_MODE);
124             fail("Test should fail due to invalid semantic version");
125         } catch (ReactorException e) {
126             assertTrue(e.getCause().getMessage()
127                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
128         }
129     }
130
131     @Test
132     public void nodeTest() throws Exception {
133         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/node-test",
134             StatementParserMode.SEMVER_MODE);
135         assertNotNull(context);
136
137         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
138         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
139                 .iterator().next();
140
141         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
142         assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion());
143         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
144         assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion());
145
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);
151
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);
156
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);
161
162         QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
163         findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
164                 SchemaPath.create(true, root, container20170406));
165         assertNull(findDataSchemaNode);
166     }
167 }