YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangDataExtensionTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.collect.ImmutableSet;
17 import java.io.IOException;
18 import java.net.URI;
19 import java.util.List;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.rfc8040.model.api.YangDataSchemaNode;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
39
40 public class YangDataExtensionTest {
41
42     private static final StatementStreamSource FOO_MODULE = sourceForResource(
43             "/yang-data-extension-test/foo.yang");
44     private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
45             "/yang-data-extension-test/foo-invalid-1.yang");
46     private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
47             "/yang-data-extension-test/foo-invalid-2.yang");
48     private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
49             "/yang-data-extension-test/foo-invalid-3.yang");
50     private static final StatementStreamSource BAR_MODULE = sourceForResource(
51             "/yang-data-extension-test/bar.yang");
52     private static final StatementStreamSource BAZ_MODULE = sourceForResource(
53             "/yang-data-extension-test/baz.yang");
54     private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
55             "/yang-data-extension-test/foobar.yang");
56     private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
57             "/yang-data-extension-test/ietf-restconf.yang");
58
59     private static final Revision REVISION = Revision.of("2017-06-01");
60     private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
61     private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
62     private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
63
64     @Test
65     public void testYangData() throws Exception {
66         final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
67                 .addSources(FOO_MODULE, IETF_RESTCONF_MODULE)
68                 .buildEffective();
69         assertNotNull(schemaContext);
70
71         final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
72         assertEquals(1, extensions.size());
73
74         final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
75         final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
76         assertEquals(2, unknownSchemaNodes.size());
77
78         YangDataSchemaNode myYangDataANode = null;
79         YangDataSchemaNode myYangDataBNode = null;
80         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
81             assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
82             final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
83             if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
84                 myYangDataANode = yangDataSchemaNode;
85             } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
86                 myYangDataBNode = yangDataSchemaNode;
87             }
88         }
89
90         assertNotNull(myYangDataANode);
91         assertNotNull(myYangDataBNode);
92
93         assertNotNull(myYangDataANode.getContainer());
94         assertNotNull(myYangDataBNode.getContainer());
95     }
96
97     @Test
98     public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
99         final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
100                 .addSources(BAZ_MODULE, IETF_RESTCONF_MODULE)
101                 .buildEffective();
102         assertNotNull(schemaContext);
103
104         final Module baz = schemaContext.findModule("baz", REVISION).get();
105         final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
106         assertEquals(1, unknownSchemaNodes.size());
107
108         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
109         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
110         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
111         assertNotNull(myYangDataNode);
112
113         final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
114         assertNotNull(contInYangData);
115         assertTrue(contInYangData.isConfiguration());
116         final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
117                 QName.create(baz.getQNameModule(), "inner-cont"));
118         assertNotNull(innerCont);
119         assertTrue(innerCont.isConfiguration());
120         final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
121                 QName.create(baz.getQNameModule(), "grp-cont"));
122         assertNotNull(grpCont);
123         assertTrue(grpCont.isConfiguration());
124     }
125
126     @Test
127     public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
128         final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
129                 .setSupportedFeatures(ImmutableSet.of())
130                 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE)
131                 .buildEffective();
132         assertNotNull(schemaContext);
133
134         final Module foobar = schemaContext.findModule("foobar", REVISION).get();
135         final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
136         assertEquals(1, unknownSchemaNodes.size());
137
138         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
139         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
140         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
141         assertNotNull(myYangDataNode);
142
143         final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
144         assertNotNull(contInYangData);
145         final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
146                 QName.create(foobar.getQNameModule(), "inner-cont"));
147         assertNotNull(innerCont);
148         final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
149                 QName.create(foobar.getQNameModule(), "grp-cont"));
150         assertNotNull(grpCont);
151     }
152
153     @Test
154     public void testYangDataBeingIgnored() throws Exception {
155         // yang-data statement is ignored if it does not appear as a top-level statement
156         // i.e., it will not appear in the final SchemaContext
157         final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
158                 .addSources(BAR_MODULE, IETF_RESTCONF_MODULE)
159                 .buildEffective();
160         assertNotNull(schemaContext);
161
162         final Module bar = schemaContext.findModule("bar", REVISION).get();
163         final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
164                 QName.create(bar.getQNameModule(), "cont"));
165         assertNotNull(cont);
166
167         final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
168         assertEquals(1, extensions.size());
169
170         final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
171         assertEquals(0, unknownSchemaNodes.size());
172     }
173
174     @Test
175     public void testYangDataWithMissingTopLevelContainer() {
176         try {
177             DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE)
178             .buildEffective();
179             fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
180         } catch (final ReactorException ex) {
181             final Throwable cause = ex.getCause();
182             assertTrue(cause instanceof MissingSubstatementException);
183             assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
184         }
185     }
186
187     @Test
188     public void testYangDataWithTwoTopLevelContainers() {
189         try {
190             DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE)
191             .buildEffective();
192             fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
193         } catch (final ReactorException ex) {
194             final Throwable cause = ex.getCause();
195             assertTrue(cause instanceof InvalidSubstatementException);
196             assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
197         }
198     }
199
200     @Test
201     public void testYangDataWithInvalidToplevelNode() {
202         try {
203             DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE)
204             .buildEffective();
205             fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
206         } catch (final ReactorException ex) {
207             final Throwable cause = ex.getCause();
208             assertTrue(cause instanceof InvalidSubstatementException);
209             assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));
210         }
211     }
212
213     private static StatementStreamSource sourceForResource(final String resourceName) {
214         try {
215             return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
216         } catch (IOException | YangSyntaxErrorException e) {
217             throw new IllegalArgumentException("Failed to create source", e);
218         }
219     }
220 }