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