Update ChoiceSchemaNode design
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.stmt;
10
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;
17
18 import java.net.URI;
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.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
35
36 public class AugmentProcessTest {
37
38     private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
39     private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
40
41     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(URI.create("root"));
42     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"));
43
44     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
45             "aug-parent1");
46     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
47             "aug-parent2");
48     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
49             "cont-target");
50
51     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
52             "cont-added1");
53     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
54             "cont-added2");
55
56     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
57     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
58
59     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
60             "cont-grp");
61     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
62             "axml-grp");
63
64     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
65     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
66
67     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
68     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
69             "grp-cont22");
70     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
71
72     private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
73             "/stmt-test/augments/multiple-augment-test.yang");
74
75     private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
76             "/stmt-test/augments/multiple-augment-root.yang");
77     private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
78             "/stmt-test/augments/multiple-augment-imported.yang");
79     private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
80             "/stmt-test/augments/multiple-augment-submodule.yang");
81
82     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
83             "/stmt-test/augments/multiple-augment-incorrect.yang");
84
85     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
86             "/stmt-test/augments/multiple-augment-incorrect2.yang");
87
88     @Test
89     public void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
90         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
91                 .newBuild();
92         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
93                 MULTIPLE_AUGMENT_SUBMODULE);
94
95         SchemaContext result = reactor.buildEffective();
96         assertNotNull(result);
97     }
98
99     @Test
100     public void multipleAugmentTest() throws ReactorException {
101         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
102                 .newBuild();
103         addSources(reactor, MULTIPLE_AUGMENT);
104
105         SchemaContext result = reactor.buildEffective();
106         assertNotNull(result);
107     }
108
109     @Test(expected = SomeModifiersUnresolvedException.class)
110     public void multipleAugmentIncorrectPathTest() throws  ReactorException {
111         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
112                 .newBuild();
113         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
114
115         SchemaContext result = reactor.buildEffective();
116         assertNull(result);
117     }
118
119     @Test(expected = SomeModifiersUnresolvedException.class)
120     public void multipleAugmentIncorrectPathAndGrpTest() throws  ReactorException {
121         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
122                 .newBuild();
123         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
124         SchemaContext result = reactor.buildEffective();
125         assertNull(result);
126     }
127
128     @Test
129     public void readAndParseYangFileTest() throws ReactorException {
130         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
131                 .newBuild();
132         addSources(reactor, AUGMENTED, ROOT);
133
134         final SchemaContext root = reactor.buildEffective();
135         assertNotNull(root);
136
137         final Module augmentedModule = root.findModules("augmented").iterator().next();
138         assertNotNull(augmentedModule);
139
140         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
141         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
142         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
143         assertNotNull(targetContNode);
144
145         assertNotNull(targetContNode.getChildNodes());
146         assertEquals(3, targetContNode.getChildNodes().size());
147
148         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
149         assertNotNull(contAdded1Node);
150         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
151         assertNotNull(list1Node);
152
153         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
154         assertNotNull(contAdded2Node);
155         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
156         assertNotNull(axmlNode);
157
158         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
159         assertNotNull(contGrpNode);
160         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
161         assertNotNull(axmlGrpNode);
162
163         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
164         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
165         assertNotNull(augCont2Node);
166
167         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
168         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
169         assertNotNull(grpCont22Node);
170
171         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
172         assertNotNull(grpAddNode);
173     }
174
175     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
176             final StatementStreamSource... sources) {
177         for (final StatementStreamSource source : sources) {
178             reactor.addSource(source);
179         }
180     }
181
182     @Test
183     public void caseShortHandAugmentingTest() throws Exception {
184         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
185
186         assertNotNull(context);
187
188         final String rev = "2013-07-01";
189         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
190         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
191
192         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
193                 "netconf-state"));
194         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
195                 "datastores"));
196         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
197                 "datastore"));
198         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
199                 "locks"));
200         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
201                 .create(ns, rev, "lock-type"));
202
203         final ChoiceCaseNode leafAugCase = lockType.findCaseNodes("leaf-aug-case").iterator().next();
204         assertTrue(leafAugCase.isAugmenting());
205         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
206         assertFalse(leafAug.isAugmenting());
207     }
208
209 }