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