Populate ietf-restconf operations container
[yangtools.git] / parser / rfc8040-parser-support / src / test / java / org / opendaylight / yangtools / rfc8040 / parser / 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 package org.opendaylight.yangtools.rfc8040.parser;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Collection;
20 import java.util.Optional;
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.common.XMLNamespace;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
37
38 public class YangDataExtensionTest extends AbstractYangDataTest {
39
40     private static final StatementStreamSource FOO_MODULE = sourceForResource(
41             "/yang-data-extension-test/foo.yang");
42     private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
43             "/yang-data-extension-test/foo-invalid-1.yang");
44     private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
45             "/yang-data-extension-test/foo-invalid-2.yang");
46     private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
47             "/yang-data-extension-test/foo-invalid-3.yang");
48     private static final StatementStreamSource BAR_MODULE = sourceForResource(
49             "/yang-data-extension-test/bar.yang");
50     private static final StatementStreamSource BAZ_MODULE = sourceForResource(
51             "/yang-data-extension-test/baz.yang");
52     private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
53             "/yang-data-extension-test/foobar.yang");
54
55     private static final Revision REVISION = Revision.of("2017-06-01");
56     private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(XMLNamespace.of("foo"), REVISION);
57     private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
58     private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
59
60     @Test
61     public void testYangData() throws Exception {
62         final SchemaContext schemaContext = REACTOR.newBuild().addSources(FOO_MODULE, IETF_RESTCONF_MODULE)
63                 .buildEffective();
64         assertNotNull(schemaContext);
65
66         final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
67         assertEquals(1, extensions.size());
68
69         final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
70         final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
71         assertEquals(2, unknownSchemaNodes.size());
72
73         YangDataSchemaNode myYangDataANode = null;
74         YangDataSchemaNode myYangDataBNode = null;
75         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
76             assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
77             final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
78             if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
79                 myYangDataANode = yangDataSchemaNode;
80             } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
81                 myYangDataBNode = yangDataSchemaNode;
82             }
83         }
84
85         assertNotNull(myYangDataANode);
86         assertNotNull(myYangDataBNode);
87
88         assertNotNull(myYangDataANode.getContainerSchemaNode());
89         assertNotNull(myYangDataBNode.getContainerSchemaNode());
90     }
91
92     @Test
93     public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
94         final SchemaContext schemaContext = REACTOR.newBuild().addSources(BAZ_MODULE, IETF_RESTCONF_MODULE)
95                 .buildEffective();
96         assertNotNull(schemaContext);
97
98         final Module baz = schemaContext.findModule("baz", REVISION).get();
99         final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
100         assertEquals(1, unknownSchemaNodes.size());
101
102         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
103         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
104         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
105         assertNotNull(myYangDataNode);
106
107         final ContainerSchemaNode contInYangData = myYangDataNode.getContainerSchemaNode();
108         assertNotNull(contInYangData);
109         assertEquals(Optional.empty(), contInYangData.effectiveConfig());
110         final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
111                 QName.create(baz.getQNameModule(), "inner-cont")).get();
112         assertNotNull(innerCont);
113         assertEquals(Optional.empty(), innerCont.effectiveConfig());
114         final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
115                 QName.create(baz.getQNameModule(), "grp-cont")).get();
116         assertNotNull(grpCont);
117         assertEquals(Optional.empty(), grpCont.effectiveConfig());
118     }
119
120     @Test
121     public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
122         final SchemaContext schemaContext = REACTOR.newBuild().setSupportedFeatures(ImmutableSet.of())
123                 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE).buildEffective();
124         assertNotNull(schemaContext);
125
126         final Module foobar = schemaContext.findModule("foobar", REVISION).get();
127         final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
128         assertEquals(1, unknownSchemaNodes.size());
129
130         final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
131         assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
132         final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
133         assertNotNull(myYangDataNode);
134
135         final ContainerSchemaNode contInYangData = myYangDataNode.getContainerSchemaNode();
136         assertNotNull(contInYangData);
137         final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
138                 QName.create(foobar.getQNameModule(), "inner-cont")).get();
139         assertNotNull(innerCont);
140         final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.findDataChildByName(
141                 QName.create(foobar.getQNameModule(), "grp-cont")).get();
142         assertNotNull(grpCont);
143     }
144
145     @Test
146     public void testYangDataBeingIgnored() throws Exception {
147         // yang-data statement is ignored if it does not appear as a top-level statement
148         // i.e., it will not appear in the final SchemaContext
149         final SchemaContext schemaContext = REACTOR.newBuild().addSources(BAR_MODULE, IETF_RESTCONF_MODULE)
150                 .buildEffective();
151         assertNotNull(schemaContext);
152
153         final Module bar = schemaContext.findModule("bar", REVISION).get();
154         final ContainerSchemaNode cont = (ContainerSchemaNode) bar.findDataChildByName(
155                 QName.create(bar.getQNameModule(), "cont")).get();
156         assertNotNull(cont);
157
158         final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
159         assertEquals(1, extensions.size());
160
161         final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
162         assertEquals(0, unknownSchemaNodes.size());
163     }
164
165     @Test
166     public void testYangDataWithMissingTopLevelContainer() {
167         final BuildAction build = REACTOR.newBuild().addSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE);
168         final ReactorException ex = assertThrows(ReactorException.class, () -> build.buildEffective());
169         final Throwable cause = ex.getCause();
170         assertThat(cause, instanceOf(MissingSubstatementException.class));
171         assertThat(cause.getMessage(), startsWith("yang-data requires exactly one container"));
172     }
173
174     @Test
175     public void testYangDataWithTwoTopLevelContainers() {
176         final BuildAction build = REACTOR.newBuild().addSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE);
177         final ReactorException ex = assertThrows(ReactorException.class, () -> build.buildEffective());
178         final Throwable cause = ex.getCause();
179         assertThat(cause, instanceOf(InvalidSubstatementException.class));
180         assertThat(cause.getMessage(), startsWith("yang-data requires exactly one data definition node, found 2"));
181     }
182 }