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