60e67073ce0c864e6f639e6155e00cfb69723df3
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentProcessTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
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;
17
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.model.api.AnyxmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
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
34 class AugmentProcessTest extends AbstractYangTest {
35     private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
36     private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
37
38     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.of("root");
39     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.of("aug");
40
41     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent1");
42     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent2");
43     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE, "cont-target");
44
45     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE, "cont-added1");
46     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE, "cont-added2");
47
48     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
49     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
50
51     private final QName contGrp = QName.create(ROOT_QNAME_MODULE, "cont-grp");
52     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE, "axml-grp");
53
54     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
55     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
56
57     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
58     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE, "grp-cont22");
59     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
60
61     private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
62         "/stmt-test/augments/multiple-augment-test.yang");
63
64     private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
65         "/stmt-test/augments/multiple-augment-root.yang");
66     private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
67         "/stmt-test/augments/multiple-augment-imported.yang");
68     private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
69         "/stmt-test/augments/multiple-augment-submodule.yang");
70
71     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
72         "/stmt-test/augments/multiple-augment-incorrect.yang");
73
74     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
75         "/stmt-test/augments/multiple-augment-incorrect2.yang");
76
77     @Test
78     void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
79         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
80             .addSources(MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE)
81             .buildEffective();
82         assertNotNull(result);
83     }
84
85     @Test
86     void multipleAugmentTest() throws ReactorException {
87         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
88             .addSource(MULTIPLE_AUGMENT)
89             .buildEffective();
90         assertNotNull(result);
91     }
92
93     @Test
94     void multipleAugmentIncorrectPathTest() throws ReactorException {
95         assertThrows(SomeModifiersUnresolvedException.class, () -> {
96             SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
97                 .addSource(MULTIPLE_AUGMENT_INCORRECT)
98                 .buildEffective();
99             assertNull(result);
100         });
101     }
102
103     @Test
104     void multipleAugmentIncorrectPathAndGrpTest() throws ReactorException {
105         assertThrows(SomeModifiersUnresolvedException.class, () -> {
106             SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
107                 .addSource(MULTIPLE_AUGMENT_INCORRECT2)
108                 .buildEffective();
109             assertNull(result);
110         });
111     }
112
113     @Test
114     void readAndParseYangFileTest() throws ReactorException {
115         final SchemaContext root = RFC7950Reactors.defaultReactor().newBuild()
116             .addSources(AUGMENTED, ROOT)
117             .buildEffective();
118         assertNotNull(root);
119
120         final Module augmentedModule = root.findModules("augmented").iterator().next();
121         assertNotNull(augmentedModule);
122
123         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
124         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
125         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
126         assertEquals(3, targetContNode.getChildNodes().size());
127
128         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
129         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
130
131         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
132         final AnyxmlSchemaNode axmlNode = (AnyxmlSchemaNode) contAdded2Node.getDataChildByName(axml);
133
134         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
135         final AnyxmlSchemaNode axmlGrpNode = (AnyxmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
136
137         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
138         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
139         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
140         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
141         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
142     }
143
144     @Test
145     void caseShortHandAugmentingTest() {
146         final SchemaContext context = assertEffectiveModelDir("/choice-case-type-test-models");
147
148         assertNotNull(context);
149
150         final String rev = "2013-07-01";
151         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
152         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
153
154         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
155             "netconf-state"));
156         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
157             "datastores"));
158         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
159             "datastore"));
160         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
161             "locks"));
162         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
163             .create(ns, rev, "lock-type"));
164
165         final CaseSchemaNode leafAugCase = lockType.findCaseNodes("leaf-aug-case").iterator().next();
166         assertTrue(leafAugCase.isAugmenting());
167         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
168         assertFalse(leafAug.isAugmenting());
169     }
170 }