2 * Copyright (c) 2015 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.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
15 import com.google.common.collect.ImmutableList;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
33 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 public class AugmentProcessTest {
37 private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
38 "/stmt-test/augments/augmented.yang", false);
39 private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
40 "/stmt-test/augments/aug-root.yang", false);
42 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
43 URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
44 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
45 .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
47 private static GroupingDefinition grp2Def;
49 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
51 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
53 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
56 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
58 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
61 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
62 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
64 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
66 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
69 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
70 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
72 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
73 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
75 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
77 private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
78 "/stmt-test/augments/multiple-augment-test.yang",false);
80 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_ROOT = new YangStatementSourceImpl(
81 "/stmt-test/augments/multiple-augment-root.yang",false);
82 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_IMPORTED = new YangStatementSourceImpl(
83 "/stmt-test/augments/multiple-augment-imported.yang",false);
84 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_SUBMODULE = new YangStatementSourceImpl(
85 "/stmt-test/augments/multiple-augment-submodule.yang",false);
87 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
88 "/stmt-test/augments/multiple-augment-incorrect.yang",false);
90 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
91 "/stmt-test/augments/multiple-augment-incorrect2.yang",false);
94 public void multipleAugmentsAndMultipleModulesTest()
95 throws SourceException, ReactorException {
96 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
98 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
99 MULTIPLE_AUGMENT_SUBMODULE);
101 EffectiveSchemaContext result = null;
103 result = reactor.buildEffective();
104 } catch (Exception e) {
107 assertNotNull(result);
111 public void multipleAugmentTest() throws SourceException, ReactorException {
112 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
114 addSources(reactor, MULTIPLE_AUGMENT);
116 EffectiveSchemaContext result = null;
118 result = reactor.buildEffective();
119 } catch (Exception e) {
122 assertNotNull(result);
126 public void multipleAugmentIncorrectPathTest() throws SourceException,
128 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
130 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
132 EffectiveSchemaContext result = null;
134 result = reactor.buildEffective();
135 } catch (Exception e) {
143 public void multipleAugmentIncorrectPathAndGrpTest()
144 throws SourceException, ReactorException {
145 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
147 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
149 EffectiveSchemaContext result = null;
151 result = reactor.buildEffective();
152 } catch (Exception e) {
159 private void log(final Throwable e, final String indent) {
160 System.out.println(indent + e.getMessage());
162 Throwable[] suppressed = e.getSuppressed();
163 for (Throwable throwable : suppressed) {
164 log(throwable, indent + " ");
169 public void readAndParseYangFileTest() throws SourceException,
172 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
174 addSources(reactor, AUGMENTED, ROOT);
176 final EffectiveSchemaContext root = reactor.buildEffective();
179 Module augmentedModule = root.findModuleByName("augmented", null);
180 assertNotNull(augmentedModule);
182 ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
183 .getDataChildByName(augParent1);
184 ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
185 .getDataChildByName(augParent2);
186 ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
187 .getDataChildByName(contTarget);
188 assertNotNull(targetContNode);
190 assertNotNull(targetContNode.getChildNodes());
191 assertEquals(3, targetContNode.getChildNodes().size());
193 ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
194 .getDataChildByName(contAdded1);
195 assertNotNull(contAdded1Node);
196 ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
197 .getDataChildByName(list1);
198 assertNotNull(list1Node);
200 ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
201 .getDataChildByName(contAdded2);
202 assertNotNull(contAdded2Node);
203 AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
204 .getDataChildByName(axml);
205 assertNotNull(axmlNode);
207 ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
208 .getDataChildByName(contGrp);
209 assertNotNull(contGrpNode);
210 AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
211 .getDataChildByName(axmlGrp);
212 assertNotNull(axmlGrpNode);
214 ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
215 .getDataChildByName(augCont1);
216 ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
217 .getDataChildByName(augCont2);
218 assertNotNull(augCont2Node);
220 ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
221 .getDataChildByName(grpCont2);
222 ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
223 .getDataChildByName(grpCont22);
224 assertNotNull(grpCont22Node);
226 ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
227 .getDataChildByName(grpAdd);
228 assertNotNull(grpAddNode);
231 private static <T extends ModelStatement<?>> T findInStatements(final QName target,
232 final ImmutableList<T> statements) {
234 for (final T statement : statements) {
236 .equals(statement.statementDefinition().getArgumentName())) {
244 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
245 final StatementStreamSource... sources) {
246 for (StatementStreamSource source : sources) {
247 reactor.addSource(source);