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.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
38 public class AugmentProcessTest {
40 private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
41 private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
43 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
44 URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
45 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
46 .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
48 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
50 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
52 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
55 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
57 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
60 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
61 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
63 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
65 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
68 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
69 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
71 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
72 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
74 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
76 private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
77 "/stmt-test/augments/multiple-augment-test.yang");
79 private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
80 "/stmt-test/augments/multiple-augment-root.yang");
81 private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
82 "/stmt-test/augments/multiple-augment-imported.yang");
83 private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
84 "/stmt-test/augments/multiple-augment-submodule.yang");
86 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
87 "/stmt-test/augments/multiple-augment-incorrect.yang");
89 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
90 "/stmt-test/augments/multiple-augment-incorrect2.yang");
93 public void multipleAugmentsAndMultipleModulesTest() throws SourceException, ReactorException {
94 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
96 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
97 MULTIPLE_AUGMENT_SUBMODULE);
99 SchemaContext result = reactor.buildEffective();
100 assertNotNull(result);
104 public void multipleAugmentTest() throws SourceException, ReactorException {
105 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
107 addSources(reactor, MULTIPLE_AUGMENT);
109 SchemaContext result = reactor.buildEffective();
110 assertNotNull(result);
113 @Test(expected = SomeModifiersUnresolvedException.class)
114 public void multipleAugmentIncorrectPathTest() throws SourceException, ReactorException {
115 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
117 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
119 SchemaContext result = reactor.buildEffective();
123 @Test(expected = SomeModifiersUnresolvedException.class)
124 public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException, ReactorException {
125 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
127 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
128 SchemaContext result = reactor.buildEffective();
133 public void readAndParseYangFileTest() throws SourceException, ReactorException {
134 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
136 addSources(reactor, AUGMENTED, ROOT);
138 final SchemaContext root = reactor.buildEffective();
141 final Module augmentedModule = root.findModuleByName("augmented", null);
142 assertNotNull(augmentedModule);
144 final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
145 .getDataChildByName(augParent1);
146 final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
147 .getDataChildByName(augParent2);
148 final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
149 .getDataChildByName(contTarget);
150 assertNotNull(targetContNode);
152 assertNotNull(targetContNode.getChildNodes());
153 assertEquals(3, targetContNode.getChildNodes().size());
155 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
156 .getDataChildByName(contAdded1);
157 assertNotNull(contAdded1Node);
158 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
159 .getDataChildByName(list1);
160 assertNotNull(list1Node);
162 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
163 .getDataChildByName(contAdded2);
164 assertNotNull(contAdded2Node);
165 final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
166 .getDataChildByName(axml);
167 assertNotNull(axmlNode);
169 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
170 .getDataChildByName(contGrp);
171 assertNotNull(contGrpNode);
172 final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
173 .getDataChildByName(axmlGrp);
174 assertNotNull(axmlGrpNode);
176 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
177 .getDataChildByName(augCont1);
178 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
179 .getDataChildByName(augCont2);
180 assertNotNull(augCont2Node);
182 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
183 .getDataChildByName(grpCont2);
184 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
185 .getDataChildByName(grpCont22);
186 assertNotNull(grpCont22Node);
188 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
189 .getDataChildByName(grpAdd);
190 assertNotNull(grpAddNode);
193 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
194 final StatementStreamSource... sources) {
195 for (final StatementStreamSource source : sources) {
196 reactor.addSource(source);
201 public void caseShortHandAugmentingTest() throws Exception {
202 final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
204 assertNotNull(context);
206 final String rev = "2013-07-01";
207 final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
208 final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
210 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
212 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
214 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
216 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
218 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
219 .create(ns, rev, "lock-type"));
221 final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
222 assertTrue(leafAugCase.isAugmenting());
223 final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
224 assertFalse(leafAug.isAugmenting());