2 * Copyright (c) 2016 Cisco Systems, Inc. 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.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 public class AugmentProcessTest {
38 private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
39 private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
41 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(URI.create("root"), null);
42 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"), null);
44 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
46 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
48 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
51 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
53 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
56 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
57 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
59 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
61 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
64 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
65 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
67 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
68 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
70 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
72 private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
73 "/stmt-test/augments/multiple-augment-test.yang");
75 private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
76 "/stmt-test/augments/multiple-augment-root.yang");
77 private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
78 "/stmt-test/augments/multiple-augment-imported.yang");
79 private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
80 "/stmt-test/augments/multiple-augment-submodule.yang");
82 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
83 "/stmt-test/augments/multiple-augment-incorrect.yang");
85 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
86 "/stmt-test/augments/multiple-augment-incorrect2.yang");
89 public void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
90 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
92 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
93 MULTIPLE_AUGMENT_SUBMODULE);
95 SchemaContext result = reactor.buildEffective();
96 assertNotNull(result);
100 public void multipleAugmentTest() throws ReactorException {
101 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
103 addSources(reactor, MULTIPLE_AUGMENT);
105 SchemaContext result = reactor.buildEffective();
106 assertNotNull(result);
109 @Test(expected = SomeModifiersUnresolvedException.class)
110 public void multipleAugmentIncorrectPathTest() throws ReactorException {
111 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
113 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
115 SchemaContext result = reactor.buildEffective();
119 @Test(expected = SomeModifiersUnresolvedException.class)
120 public void multipleAugmentIncorrectPathAndGrpTest() throws ReactorException {
121 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
123 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
124 SchemaContext result = reactor.buildEffective();
129 public void readAndParseYangFileTest() throws ReactorException {
130 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
132 addSources(reactor, AUGMENTED, ROOT);
134 final SchemaContext root = reactor.buildEffective();
137 final Module augmentedModule = root.findModules("augmented").iterator().next();
138 assertNotNull(augmentedModule);
140 final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
141 final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
142 final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
143 assertNotNull(targetContNode);
145 assertNotNull(targetContNode.getChildNodes());
146 assertEquals(3, targetContNode.getChildNodes().size());
148 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
149 assertNotNull(contAdded1Node);
150 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
151 assertNotNull(list1Node);
153 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
154 assertNotNull(contAdded2Node);
155 final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
156 assertNotNull(axmlNode);
158 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
159 assertNotNull(contGrpNode);
160 final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
161 assertNotNull(axmlGrpNode);
163 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
164 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
165 assertNotNull(augCont2Node);
167 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
168 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
169 assertNotNull(grpCont22Node);
171 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
172 assertNotNull(grpAddNode);
175 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
176 final StatementStreamSource... sources) {
177 for (final StatementStreamSource source : sources) {
178 reactor.addSource(source);
183 public void caseShortHandAugmentingTest() throws Exception {
184 final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
186 assertNotNull(context);
188 final String rev = "2013-07-01";
189 final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
190 final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
192 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
194 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
196 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
198 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
200 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
201 .create(ns, rev, "lock-type"));
203 final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
204 assertTrue(leafAugCase.isAugmenting());
205 final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
206 assertFalse(leafAug.isAugmenting());