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