BUG-4688: eliminate QName(Module).getFormattedRevision()
[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.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
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.newBuild();
46
47         reactor.addSources(REFINE_TEST);
48
49         SchemaContext 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, "container-from-grouping");
64         QName listInContainer = QName.create(qnameModule, "list-in-container");
65         QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
66
67         QName containerFromGrouping2 = QName.create(qnameModule, "container-from-grouping2");
68         QName presenceContainer = QName.create(qnameModule, "presence-container");
69
70         SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
71                 containerFromGrouping, listInContainer);
72         SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
73                 containerFromGrouping, choiceFromGrp);
74         SchemaPath presenceContainerPath = SchemaPath.create(true,
75                 rootContainer, containerFromGrouping2, presenceContainer);
76
77         checkRefinedList(result, listInContainerPath);
78         checkRefinedChoice(result, choiceFromGrpPath);
79         checkRefinedContainer(result, presenceContainerPath);
80
81         SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
82                 containerFromGrouping, listInContainer);
83         SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
84                 containerFromGrouping, choiceFromGrp);
85         SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
86                 grp1, containerFromGrouping2, presenceContainer);
87
88         checkOriginalList(result, originalListInContainerPath);
89         checkOriginalChoice(result, originalChoiceFromGrpPath);
90         checkOriginalContainer(result, originalPresenceContainerPath);
91     }
92
93     private static void checkOriginalContainer(final SchemaContext result, final SchemaPath path) {
94         SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
95         assertNotNull(containerInContainerNode);
96
97         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
98         assertNull(containerSchemaNode.getReference());
99         assertNull(containerSchemaNode.getDescription());
100         assertTrue(containerSchemaNode.isConfiguration());
101         assertFalse(containerSchemaNode.isPresenceContainer());
102
103         ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
104         assertEquals(0, containerConstraints.getMustConstraints().size());
105     }
106
107     private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
108         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
109         assertNotNull(choiceInContainerNode);
110
111         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
112
113         ConstraintDefinition choiceConstraints = choiceSchemaNode
114                 .getConstraints();
115         assertFalse(choiceConstraints.isMandatory());
116         assertTrue(choiceConstraints.getMustConstraints().isEmpty());
117     }
118
119     private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
120         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
121         assertNotNull(listInContainerNode);
122
123         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
124         assertEquals("original reference", listSchemaNode.getReference());
125         assertEquals("original description", listSchemaNode.getDescription());
126         assertFalse(listSchemaNode.isConfiguration());
127
128         ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
129         assertEquals(10, listConstraints.getMinElements().intValue());
130         assertEquals(20, listConstraints.getMaxElements().intValue());
131         assertEquals(1, listConstraints.getMustConstraints().size());
132     }
133
134     private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
135         SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
136         assertNotNull(containerInContainerNode);
137
138         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
139         assertEquals("new reference", containerSchemaNode.getReference());
140         assertEquals("new description", containerSchemaNode.getDescription());
141         assertTrue(containerSchemaNode.isConfiguration());
142         assertTrue(containerSchemaNode.isPresenceContainer());
143
144         ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
145         assertEquals(1, containerConstraints.getMustConstraints().size());
146     }
147
148     private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
149         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
150         assertNotNull(choiceInContainerNode);
151
152         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
153
154         ConstraintDefinition choiceConstraints = choiceSchemaNode
155                 .getConstraints();
156         assertTrue(choiceConstraints.isMandatory());
157         assertTrue(choiceConstraints.getMustConstraints().isEmpty());
158     }
159
160     private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
161         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
162         assertNotNull(listInContainerNode);
163
164         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
165         assertEquals("new reference", listSchemaNode.getReference());
166         assertEquals("new description", listSchemaNode.getDescription());
167         assertTrue(listSchemaNode.isConfiguration());
168
169         ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
170         assertEquals(5, listConstraints.getMinElements().intValue());
171         assertEquals(7, listConstraints.getMaxElements().intValue());
172         assertEquals(2, listConstraints.getMustConstraints().size());
173     }
174 }