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