Bug 6244: Add context to exceptions thrown by yang statement parser
[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.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.concepts.SemVer;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
32
33 public class SemanticVersionTest {
34     @Test
35     public void basicTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
36         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic", StatementParserMode.SEMVER_MODE);
37         assertNotNull(context);
38
39         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
40         Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
41         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
42                 .iterator().next();
43
44         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
45         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
46         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
47     }
48
49     @Test
50     public void basicTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
51         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-2", StatementParserMode.SEMVER_MODE);
52         assertNotNull(context);
53
54         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
55         Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
56         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
57                 .iterator().next();
58
59         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
60         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
61         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
62     }
63
64     @Test
65     public void basicTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
66         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-3", 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 SourceException, FileNotFoundException, ReactorException, URISyntaxException {
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 SourceException, FileNotFoundException, ReactorException,
95             URISyntaxException {
96         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple-modules",
97                 StatementParserMode.SEMVER_MODE);
98         assertNotNull(context);
99
100         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
101         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
102                 .iterator().next();
103
104         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
105         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
106         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
107         assertEquals(SemVer.valueOf("0.10.4"), bar.getSemanticVersion());
108     }
109
110     @Test
111     public void basicImportErrTest1() throws SourceException, FileNotFoundException, ReactorException,
112             URISyntaxException {
113         try {
114             StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-1", StatementParserMode.SEMVER_MODE);
115             fail("Test should fail due to invalid semantic version");
116         } catch (ReactorException e) {
117             assertTrue(e.getCause().getMessage()
118                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
119         }
120     }
121
122     @Test
123     public void basicImportErrTest2() throws SourceException, FileNotFoundException, ReactorException,
124             URISyntaxException {
125         try {
126             StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-2", StatementParserMode.SEMVER_MODE);
127             fail("Test should fail due to invalid semantic version");
128         } catch (ReactorException e) {
129             assertTrue(e.getCause().getMessage()
130                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
131         }
132     }
133
134     @Test
135     public void nodeTest() throws Exception {
136         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/node-test", StatementParserMode.SEMVER_MODE);
137         assertNotNull(context);
138
139         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
140         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
141                 .iterator().next();
142
143         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
144         assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion());
145         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
146         assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion());
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 }