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.impl.DefaultReactors;
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 public class AugmentProcessTest {
37 private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
38 private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
40 private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(URI.create("root"));
41 private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"));
43 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
45 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
47 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
50 private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
52 private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
55 private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
56 private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
58 private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
60 private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
63 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
64 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
66 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
67 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
69 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
71 private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
72 "/stmt-test/augments/multiple-augment-test.yang");
74 private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
75 "/stmt-test/augments/multiple-augment-root.yang");
76 private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
77 "/stmt-test/augments/multiple-augment-imported.yang");
78 private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
79 "/stmt-test/augments/multiple-augment-submodule.yang");
81 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
82 "/stmt-test/augments/multiple-augment-incorrect.yang");
84 private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
85 "/stmt-test/augments/multiple-augment-incorrect2.yang");
88 public void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
89 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
90 .addSources(MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE)
92 assertNotNull(result);
96 public void multipleAugmentTest() throws ReactorException {
97 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
98 .addSource(MULTIPLE_AUGMENT)
100 assertNotNull(result);
103 @Test(expected = SomeModifiersUnresolvedException.class)
104 public void multipleAugmentIncorrectPathTest() throws ReactorException {
105 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
106 .addSource(MULTIPLE_AUGMENT_INCORRECT)
111 @Test(expected = SomeModifiersUnresolvedException.class)
112 public void multipleAugmentIncorrectPathAndGrpTest() throws ReactorException {
113 SchemaContext result = DefaultReactors.defaultReactor().newBuild()
114 .addSource(MULTIPLE_AUGMENT_INCORRECT2)
120 public void readAndParseYangFileTest() throws ReactorException {
121 final SchemaContext root = DefaultReactors.defaultReactor().newBuild()
122 .addSources(AUGMENTED, ROOT)
126 final Module augmentedModule = root.findModules("augmented").iterator().next();
127 assertNotNull(augmentedModule);
129 final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
130 final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
131 final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
132 assertNotNull(targetContNode);
134 assertNotNull(targetContNode.getChildNodes());
135 assertEquals(3, targetContNode.getChildNodes().size());
137 final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
138 assertNotNull(contAdded1Node);
139 final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
140 assertNotNull(list1Node);
142 final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
143 assertNotNull(contAdded2Node);
144 final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
145 assertNotNull(axmlNode);
147 final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
148 assertNotNull(contGrpNode);
149 final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
150 assertNotNull(axmlGrpNode);
152 final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
153 final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
154 assertNotNull(augCont2Node);
156 final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
157 final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
158 assertNotNull(grpCont22Node);
160 final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
161 assertNotNull(grpAddNode);
165 public void caseShortHandAugmentingTest() throws Exception {
166 final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
168 assertNotNull(context);
170 final String rev = "2013-07-01";
171 final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
172 final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
174 final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
176 final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
178 final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
180 final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
182 final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
183 .create(ns, rev, "lock-type"));
185 final ChoiceCaseNode leafAugCase = lockType.findCaseNodes("leaf-aug-case").iterator().next();
186 assertTrue(leafAugCase.isAugmenting());
187 final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
188 assertFalse(leafAug.isAugmenting());