Do do not check unique in unsupported lists
[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.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;
34
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");
38
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"));
41
42     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
43         "aug-parent1");
44     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
45         "aug-parent2");
46     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
47         "cont-target");
48
49     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
50         "cont-added1");
51     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
52         "cont-added2");
53
54     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
55     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
56
57     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
58         "cont-grp");
59     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
60         "axml-grp");
61
62     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
63     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
64
65     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
66     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
67         "grp-cont22");
68     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
69
70     private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
71         "/stmt-test/augments/multiple-augment-test.yang");
72
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");
79
80     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
81         "/stmt-test/augments/multiple-augment-incorrect.yang");
82
83     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
84         "/stmt-test/augments/multiple-augment-incorrect2.yang");
85
86     @Test
87     void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
88         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
89             .addSources(MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE)
90             .buildEffective();
91         assertNotNull(result);
92     }
93
94     @Test
95     void multipleAugmentTest() throws ReactorException {
96         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
97             .addSource(MULTIPLE_AUGMENT)
98             .buildEffective();
99         assertNotNull(result);
100     }
101
102     @Test
103     void multipleAugmentIncorrectPathTest() throws ReactorException {
104         assertThrows(SomeModifiersUnresolvedException.class, () -> {
105             SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
106                 .addSource(MULTIPLE_AUGMENT_INCORRECT)
107                 .buildEffective();
108             assertNull(result);
109         });
110     }
111
112     @Test
113     void multipleAugmentIncorrectPathAndGrpTest() throws ReactorException {
114         assertThrows(SomeModifiersUnresolvedException.class, () -> {
115             SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
116                 .addSource(MULTIPLE_AUGMENT_INCORRECT2)
117                 .buildEffective();
118             assertNull(result);
119         });
120     }
121
122     @Test
123     void readAndParseYangFileTest() throws ReactorException {
124         final SchemaContext root = RFC7950Reactors.defaultReactor().newBuild()
125             .addSources(AUGMENTED, ROOT)
126             .buildEffective();
127         assertNotNull(root);
128
129         final Module augmentedModule = root.findModules("augmented").iterator().next();
130         assertNotNull(augmentedModule);
131
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());
136
137         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
138         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
139
140         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
141         final AnyxmlSchemaNode axmlNode = (AnyxmlSchemaNode) contAdded2Node.getDataChildByName(axml);
142
143         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
144         final AnyxmlSchemaNode axmlGrpNode = (AnyxmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
145
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);
151     }
152
153     @Test
154     void caseShortHandAugmentingTest() {
155         final SchemaContext context = assertEffectiveModelDir("/choice-case-type-test-models");
156
157         assertNotNull(context);
158
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";
162
163         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
164             "netconf-state"));
165         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
166             "datastores"));
167         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
168             "datastore"));
169         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
170             "locks"));
171         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
172             .create(ns, rev, "lock-type"));
173
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());
178     }
179 }