2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import com.google.common.collect.ImmutableSet;
19 import java.util.List;
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.parser.impl.DefaultReactors;
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;
37 public class YangDataExtensionTest {
39 private static final StatementStreamSource FOO_MODULE = sourceForResource(
40 "/yang-data-extension-test/foo.yang");
41 private static final StatementStreamSource FOO_INVALID_1_MODULE = sourceForResource(
42 "/yang-data-extension-test/foo-invalid-1.yang");
43 private static final StatementStreamSource FOO_INVALID_2_MODULE = sourceForResource(
44 "/yang-data-extension-test/foo-invalid-2.yang");
45 private static final StatementStreamSource FOO_INVALID_3_MODULE = sourceForResource(
46 "/yang-data-extension-test/foo-invalid-3.yang");
47 private static final StatementStreamSource BAR_MODULE = sourceForResource(
48 "/yang-data-extension-test/bar.yang");
49 private static final StatementStreamSource BAZ_MODULE = sourceForResource(
50 "/yang-data-extension-test/baz.yang");
51 private static final StatementStreamSource FOOBAR_MODULE = sourceForResource(
52 "/yang-data-extension-test/foobar.yang");
53 private static final StatementStreamSource IETF_RESTCONF_MODULE = sourceForResource(
54 "/yang-data-extension-test/ietf-restconf.yang");
56 private static final Revision REVISION = Revision.of("2017-06-01");
57 private static final QNameModule FOO_QNAMEMODULE = QNameModule.create(URI.create("foo"), REVISION);
58 private static final QName MY_YANG_DATA_A = QName.create(FOO_QNAMEMODULE, "my-yang-data-a");
59 private static final QName MY_YANG_DATA_B = QName.create(FOO_QNAMEMODULE, "my-yang-data-b");
62 public void testYangData() throws Exception {
63 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
64 .addSources(FOO_MODULE, IETF_RESTCONF_MODULE)
66 assertNotNull(schemaContext);
68 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
69 assertEquals(1, extensions.size());
71 final Module foo = schemaContext.findModule(FOO_QNAMEMODULE).get();
72 final List<UnknownSchemaNode> unknownSchemaNodes = foo.getUnknownSchemaNodes();
73 assertEquals(2, unknownSchemaNodes.size());
75 YangDataSchemaNode myYangDataANode = null;
76 YangDataSchemaNode myYangDataBNode = null;
77 for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
78 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
79 final YangDataSchemaNode yangDataSchemaNode = (YangDataSchemaNode) unknownSchemaNode;
80 if (MY_YANG_DATA_A.equals(yangDataSchemaNode.getQName())) {
81 myYangDataANode = yangDataSchemaNode;
82 } else if (MY_YANG_DATA_B.equals(yangDataSchemaNode.getQName())) {
83 myYangDataBNode = yangDataSchemaNode;
87 assertNotNull(myYangDataANode);
88 assertNotNull(myYangDataBNode);
90 assertNotNull(myYangDataANode.getContainer());
91 assertNotNull(myYangDataBNode.getContainer());
95 public void testConfigStatementBeingIgnoredInYangDataBody() throws Exception {
96 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
97 .addSources(BAZ_MODULE, IETF_RESTCONF_MODULE)
99 assertNotNull(schemaContext);
101 final Module baz = schemaContext.findModule("baz", REVISION).get();
102 final List<UnknownSchemaNode> unknownSchemaNodes = baz.getUnknownSchemaNodes();
103 assertEquals(1, unknownSchemaNodes.size());
105 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
106 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
107 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
108 assertNotNull(myYangDataNode);
110 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
111 assertNotNull(contInYangData);
112 assertTrue(contInYangData.isConfiguration());
113 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
114 QName.create(baz.getQNameModule(), "inner-cont"));
115 assertNotNull(innerCont);
116 assertTrue(innerCont.isConfiguration());
117 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
118 QName.create(baz.getQNameModule(), "grp-cont"));
119 assertNotNull(grpCont);
120 assertTrue(grpCont.isConfiguration());
124 public void testIfFeatureStatementBeingIgnoredInYangDataBody() throws Exception {
125 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
126 .setSupportedFeatures(ImmutableSet.of())
127 .addSources(FOOBAR_MODULE, IETF_RESTCONF_MODULE)
129 assertNotNull(schemaContext);
131 final Module foobar = schemaContext.findModule("foobar", REVISION).get();
132 final List<UnknownSchemaNode> unknownSchemaNodes = foobar.getUnknownSchemaNodes();
133 assertEquals(1, unknownSchemaNodes.size());
135 final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
136 assertTrue(unknownSchemaNode instanceof YangDataSchemaNode);
137 final YangDataSchemaNode myYangDataNode = (YangDataSchemaNode) unknownSchemaNode;
138 assertNotNull(myYangDataNode);
140 final ContainerSchemaNode contInYangData = myYangDataNode.getContainer();
141 assertNotNull(contInYangData);
142 final ContainerSchemaNode innerCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
143 QName.create(foobar.getQNameModule(), "inner-cont"));
144 assertNotNull(innerCont);
145 final ContainerSchemaNode grpCont = (ContainerSchemaNode) contInYangData.getDataChildByName(
146 QName.create(foobar.getQNameModule(), "grp-cont"));
147 assertNotNull(grpCont);
151 public void testYangDataBeingIgnored() throws Exception {
152 // yang-data statement is ignored if it does not appear as a top-level statement
153 // i.e., it will not appear in the final SchemaContext
154 final SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
155 .addSources(BAR_MODULE, IETF_RESTCONF_MODULE)
157 assertNotNull(schemaContext);
159 final Module bar = schemaContext.findModule("bar", REVISION).get();
160 final ContainerSchemaNode cont = (ContainerSchemaNode) bar.getDataChildByName(
161 QName.create(bar.getQNameModule(), "cont"));
164 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
165 assertEquals(1, extensions.size());
167 final List<UnknownSchemaNode> unknownSchemaNodes = cont.getUnknownSchemaNodes();
168 assertEquals(0, unknownSchemaNodes.size());
172 public void testYangDataWithMissingTopLevelContainer() {
174 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_1_MODULE, IETF_RESTCONF_MODULE)
176 fail("Exception should have been thrown because of missing top-level container in yang-data statement.");
177 } catch (final ReactorException ex) {
178 final Throwable cause = ex.getCause();
179 assertTrue(cause instanceof MissingSubstatementException);
180 assertTrue(cause.getMessage().startsWith("YANG_DATA is missing CONTAINER. Minimal count is 1."));
185 public void testYangDataWithTwoTopLevelContainers() {
187 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_2_MODULE, IETF_RESTCONF_MODULE)
189 fail("Exception should have been thrown because of two top-level containers in yang-data statement.");
190 } catch (final ReactorException ex) {
191 final Throwable cause = ex.getCause();
192 assertTrue(cause instanceof InvalidSubstatementException);
193 assertTrue(cause.getMessage().startsWith("Maximal count of CONTAINER for YANG_DATA is 1, detected 2."));
198 public void testYangDataWithInvalidToplevelNode() {
200 DefaultReactors.defaultReactor().newBuild().addSources(FOO_INVALID_3_MODULE, IETF_RESTCONF_MODULE)
202 fail("Exception should have been thrown because of invalid top-level node in yang-data statement.");
203 } catch (final ReactorException ex) {
204 final Throwable cause = ex.getCause();
205 assertTrue(cause instanceof InvalidSubstatementException);
206 assertTrue(cause.getMessage().startsWith("LEAF is not valid for YANG_DATA."));