Bug 6867: Extend yang statement parser to support different yang versions
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / semver / SemanticVersionComplexTest.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
14 import java.io.FileNotFoundException;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.concepts.SemVer;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
23 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
24 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
28
29 public class SemanticVersionComplexTest {
30
31     @Test
32     public void complexTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException,
33             ParseException {
34         final SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/complex/complex-1",
35                 StatementParserMode.SEMVER_MODE);
36         verifySchemaContextTest1(context);
37     }
38
39     @Test
40     public void complexTest1Yang1_1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException,
41             ParseException {
42         final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/semantic-version/complex-1",
43                 StatementParserMode.SEMVER_MODE);
44         verifySchemaContextTest1(context);
45     }
46
47     private void verifySchemaContextTest1(final SchemaContext context) throws URISyntaxException {
48         assertNotNull(context);
49
50         final Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
51         final Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
52                 .iterator().next();
53
54         // check module versions
55         assertEquals(SemVer.valueOf("1.3.95"), semVer.getSemanticVersion());
56         assertEquals(SemVer.valueOf("1.50.2"), foo.getSemanticVersion());
57
58         final Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
59         assertEquals(SemVer.valueOf("1.2.6"), bar.getSemanticVersion());
60
61         final Module foobar = StmtTestUtils.findImportedModule(context, bar, "foobar");
62         assertEquals(SemVer.valueOf("2.26.465"), foobar.getSemanticVersion());
63
64         // check imported components
65         assertNotNull("This component should be present", SchemaContextUtil.findDataSchemaNode(context, foo,
66                 new RevisionAwareXPathImpl("/bar:root/bar:test-container/bar:number", true)));
67
68         assertNotNull("This component should be present", SchemaContextUtil.findDataSchemaNode(context, foo,
69                 new RevisionAwareXPathImpl("/bar:should-present", true)));
70
71         // check not imported components
72         assertNull("This component should not be present", SchemaContextUtil.findDataSchemaNode(context, foo,
73                 new RevisionAwareXPathImpl("/bar:root/bar:test-container/bar:oldnumber", true)));
74
75         assertNull("This component should not be present", SchemaContextUtil.findDataSchemaNode(context, foo,
76                 new RevisionAwareXPathImpl("/bar:should-not-be-present", true)));
77     }
78
79     @Test
80     public void complexTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException,
81             ParseException {
82         final SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/complex/complex-2",
83                 StatementParserMode.SEMVER_MODE);
84         verifySchemaContextTest2(context);
85     }
86
87     @Test
88     public void complexTest2Yang1_1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException,
89             ParseException {
90         final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/semantic-version/complex-2",
91                 StatementParserMode.SEMVER_MODE);
92         verifySchemaContextTest2(context);
93     }
94
95     private void verifySchemaContextTest2(final SchemaContext context) throws URISyntaxException {
96         assertNotNull(context);
97
98         final Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
99         final Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
100                 .iterator().next();
101
102         // check module versions
103         assertEquals(SemVer.valueOf("2.5.50"), semVer.getSemanticVersion());
104         assertEquals(SemVer.valueOf("2.32.2"), foo.getSemanticVersion());
105
106         final Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
107         assertEquals(SemVer.valueOf("4.9.8"), bar.getSemanticVersion());
108
109         final Module foobar = StmtTestUtils.findImportedModule(context, bar, "foobar");
110         assertEquals(SemVer.valueOf("7.13.99"), foobar.getSemanticVersion());
111
112         // check used augmentations
113         assertNotNull("This component should be present", SchemaContextUtil.findDataSchemaNode(context, bar,
114                 new RevisionAwareXPathImpl("/foobar:root/foobar:test-container/bar:should-present-leaf-1", true)));
115
116         assertNotNull("This component should be present", SchemaContextUtil.findDataSchemaNode(context, bar,
117                 new RevisionAwareXPathImpl("/foobar:root/foobar:test-container/bar:should-present-leaf-2", true)));
118
119         // check not used augmentations
120         assertNull("This component should not be present",
121                 SchemaContextUtil.findDataSchemaNode(context, bar, new RevisionAwareXPathImpl(
122                         "/foobar:root/foobar:test-container/bar:should-not-be-present-leaf-1", true)));
123
124         assertNull("This component should not be present",
125                 SchemaContextUtil.findDataSchemaNode(context, bar, new RevisionAwareXPathImpl(
126                         "/foobar:root/foobar:test-container/bar:should-not-be-present-leaf-2", true)));
127
128         // check if correct foobar module was included
129         assertNotNull("This component should be present", SchemaContextUtil.findDataSchemaNode(context, bar,
130                 new RevisionAwareXPathImpl("/foobar:root/foobar:included-correct-mark", true)));
131
132         assertNull("This component should not be present", SchemaContextUtil.findDataSchemaNode(context, bar,
133                 new RevisionAwareXPathImpl("/foobar:root/foobar:included-not-correct-mark", true)));
134     }
135 }