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