Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / effective / build / test / EffectiveUsesRefineAndConstraintsTest.java
1 package org.opendaylight.yangtools.yang.stmt.effective.build.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
7
8 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
9
10 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
11 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
12 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
14 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import java.net.URISyntaxException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
27 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
28 import org.junit.Test;
29
30 public class EffectiveUsesRefineAndConstraintsTest {
31
32     private static final YangStatementSourceImpl REFINE_TEST = new YangStatementSourceImpl(
33             "/stmt-test/uses/refine-test.yang",false);
34
35     @Test
36     public void refineTest() throws SourceException, ReactorException,
37             URISyntaxException {
38         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
39                 .newBuild();
40
41         StmtTestUtils.addSources(reactor, REFINE_TEST);
42
43         EffectiveSchemaContext result = reactor.buildEffective();
44
45         assertNotNull(result);
46
47         Set<Module> modules = result.getModules();
48         assertNotNull(modules);
49         assertEquals(1, modules.size());
50
51         Module module = modules.iterator().next();
52
53         QNameModule qnameModule = module.getQNameModule();
54         QName rootContainer = QName.create(qnameModule, "root-container");
55         QName grp1 = QName.create(qnameModule, "grp-1");
56
57         QName containerFromGrouping = QName.create(qnameModule,
58                 "container-from-grouping");
59         QName listInContainer = QName.create(qnameModule, "list-in-container");
60         QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
61
62         QName containerFromGrouping2 = QName.create(qnameModule,
63                 "container-from-grouping2");
64         QName presenceContainer = QName.create(qnameModule,
65                 "presence-container");
66
67         SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
68                 containerFromGrouping, listInContainer);
69         SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
70                 containerFromGrouping, choiceFromGrp);
71         SchemaPath presenceContainerPath = SchemaPath.create(true,
72                 rootContainer, containerFromGrouping2, presenceContainer);
73
74         checkRefinedList(result, listInContainerPath);
75         checkRefinedChoice(result, choiceFromGrpPath);
76         checkRefinedContainer(result, presenceContainerPath);
77
78         SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
79                 containerFromGrouping, listInContainer);
80         SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
81                 containerFromGrouping, choiceFromGrp);
82         SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
83                 grp1, containerFromGrouping2, presenceContainer);
84
85         checkOriginalList(result, originalListInContainerPath);
86         checkOriginalChoice(result, originalChoiceFromGrpPath);
87         checkOriginalContainer(result, originalPresenceContainerPath);
88
89     }
90
91     private void checkOriginalContainer(EffectiveSchemaContext result,
92             SchemaPath path) {
93         SchemaNode containerInContainerNode = SchemaContextUtil
94                 .findDataSchemaNode(result, path);
95         assertNotNull(containerInContainerNode);
96
97         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
98         assertNull(containerSchemaNode.getReference());
99         assertNull(containerSchemaNode.getDescription());
100         assertEquals(true, containerSchemaNode.isConfiguration());
101         assertEquals(false, containerSchemaNode.isPresenceContainer());
102
103         ConstraintDefinition containerConstraints = containerSchemaNode
104                 .getConstraints();
105         assertEquals(0, containerConstraints.getMustConstraints().size());
106     }
107
108     private void checkOriginalChoice(EffectiveSchemaContext result,
109             SchemaPath path) {
110         SchemaNode choiceInContainerNode = SchemaContextUtil
111                 .findDataSchemaNode(result, path);
112         assertNotNull(choiceInContainerNode);
113
114         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
115
116         ConstraintDefinition choiceConstraints = choiceSchemaNode
117                 .getConstraints();
118         assertTrue(choiceConstraints.isMandatory() == false);
119         assertTrue(choiceConstraints.getMustConstraints().isEmpty());
120     }
121
122     private void checkOriginalList(EffectiveSchemaContext result,
123             SchemaPath path) {
124         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
125                 result, path);
126         assertNotNull(listInContainerNode);
127
128         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
129         assertEquals("original reference", listSchemaNode.getReference());
130         assertEquals("original description", listSchemaNode.getDescription());
131         assertEquals(false, listSchemaNode.isConfiguration());
132
133         ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
134         assertTrue(listConstraints.getMinElements() == 10);
135         assertTrue(listConstraints.getMaxElements() == 20);
136         assertEquals(1, listConstraints.getMustConstraints().size());
137     }
138
139     private void checkRefinedContainer(EffectiveSchemaContext result,
140             SchemaPath path) {
141         SchemaNode containerInContainerNode = SchemaContextUtil
142                 .findDataSchemaNode(result, path);
143         assertNotNull(containerInContainerNode);
144
145         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
146         assertEquals("new reference", containerSchemaNode.getReference());
147         assertEquals("new description", containerSchemaNode.getDescription());
148         assertEquals(true, containerSchemaNode.isConfiguration());
149         assertEquals(true, containerSchemaNode.isPresenceContainer());
150
151         ConstraintDefinition containerConstraints = containerSchemaNode
152                 .getConstraints();
153         assertEquals(1, containerConstraints.getMustConstraints().size());
154     }
155
156     private void checkRefinedChoice(EffectiveSchemaContext result,
157             SchemaPath path) {
158         SchemaNode choiceInContainerNode = SchemaContextUtil
159                 .findDataSchemaNode(result, path);
160         assertNotNull(choiceInContainerNode);
161
162         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
163
164         ConstraintDefinition choiceConstraints = choiceSchemaNode
165                 .getConstraints();
166         assertTrue(choiceConstraints.isMandatory());
167         assertTrue(choiceConstraints.getMustConstraints().isEmpty());
168     }
169
170     private void checkRefinedList(EffectiveSchemaContext result, SchemaPath path) {
171         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
172                 result, path);
173         assertNotNull(listInContainerNode);
174
175         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
176         assertEquals("new reference", listSchemaNode.getReference());
177         assertEquals("new description", listSchemaNode.getDescription());
178         assertEquals(true, listSchemaNode.isConfiguration());
179
180         ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
181         assertTrue(listConstraints.getMinElements() == 5);
182         assertTrue(listConstraints.getMaxElements() == 7);
183         assertEquals(2, listConstraints.getMustConstraints().size());
184     }
185 }