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