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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertNull;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
22 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
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.rfc7950.reactor.RFC7950Reactors;
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.StatementStreamSource;
35 class AugmentProcessTest extends AbstractYangTest {
36 private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
37 private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
39 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(XMLNamespace.of("root"));
40 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(XMLNamespace.of("aug"));
42 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
44 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
46 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
49 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
51 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
54 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
55 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
57 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
59 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
62 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
63 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
65 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
66 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
68 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
70 private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
71 "/stmt-test/augments/multiple-augment-test.yang");
73 private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
74 "/stmt-test/augments/multiple-augment-root.yang");
75 private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
76 "/stmt-test/augments/multiple-augment-imported.yang");
77 private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
78 "/stmt-test/augments/multiple-augment-submodule.yang");
80 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
81 "/stmt-test/augments/multiple-augment-incorrect.yang");
83 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
84 "/stmt-test/augments/multiple-augment-incorrect2.yang");
87 void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
88 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
89 .addSources(MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE)
91 assertNotNull(result);
95 void multipleAugmentTest() throws ReactorException {
96 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
97 .addSource(MULTIPLE_AUGMENT)
99 assertNotNull(result);
103 void multipleAugmentIncorrectPathTest() throws ReactorException {
104 assertThrows(SomeModifiersUnresolvedException.class, () -> {
105 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
106 .addSource(MULTIPLE_AUGMENT_INCORRECT)
113 void multipleAugmentIncorrectPathAndGrpTest() throws ReactorException {
114 assertThrows(SomeModifiersUnresolvedException.class, () -> {
115 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
116 .addSource(MULTIPLE_AUGMENT_INCORRECT2)
123 void readAndParseYangFileTest() throws ReactorException {
124 final SchemaContext root = RFC7950Reactors.defaultReactor().newBuild()
125 .addSources(AUGMENTED, ROOT)
129 final Module augmentedModule = root.findModules("augmented").iterator().next();
130 assertNotNull(augmentedModule);
132 final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
133 final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
134 final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
135 assertEquals(3, targetContNode.getChildNodes().size());
137 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
138 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
140 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
141 final AnyxmlSchemaNode axmlNode = (AnyxmlSchemaNode) contAdded2Node.getDataChildByName(axml);
143 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
144 final AnyxmlSchemaNode axmlGrpNode = (AnyxmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
146 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
147 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
148 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
149 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
150 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
154 void caseShortHandAugmentingTest() {
155 final SchemaContext context = assertEffectiveModelDir("/choice-case-type-test-models");
157 assertNotNull(context);
159 final String rev = "2013-07-01";
160 final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
161 final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
163 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
165 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
167 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
169 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
171 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
172 .create(ns, rev, "lock-type"));
174 final CaseSchemaNode leafAugCase = lockType.findCaseNodes("leaf-aug-case").iterator().next();
175 assertTrue(leafAugCase.isAugmenting());
176 final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
177 assertFalse(leafAug.isAugmenting());