Fix get{Min,Max}Elements() usage
[yangtools.git] / yang / yang-parser-rfc7950 / 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15
16 import java.util.Collection;
17 import java.util.Optional;
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.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
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.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32
33 public class EffectiveUsesRefineAndConstraintsTest {
34
35     @Test
36     public void refineTest() throws ReactorException {
37         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
38                 .addSource(sourceForResource("/stmt-test/uses/refine-test.yang"))
39                 .buildEffective();
40         assertNotNull(result);
41
42         Collection<? extends Module> modules = result.getModules();
43         assertNotNull(modules);
44         assertEquals(1, modules.size());
45
46         Module module = modules.iterator().next();
47
48         final QNameModule qnameModule = module.getQNameModule();
49         final QName rootContainer = QName.create(qnameModule, "root-container");
50         final QName grp1 = QName.create(qnameModule, "grp-1");
51
52         final QName containerFromGrouping = QName.create(qnameModule, "container-from-grouping");
53         final QName listInContainer = QName.create(qnameModule, "list-in-container");
54         final QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
55
56         final QName containerFromGrouping2 = QName.create(qnameModule, "container-from-grouping2");
57         final QName presenceContainer = QName.create(qnameModule, "presence-container");
58
59         SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping, listInContainer);
60         SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer, containerFromGrouping, choiceFromGrp);
61         SchemaPath presenceContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping2,
62             presenceContainer);
63
64         checkRefinedList(result, listInContainerPath);
65         checkRefinedChoice(result, choiceFromGrpPath);
66         checkRefinedContainer(result, presenceContainerPath);
67
68         SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1, containerFromGrouping, listInContainer);
69         SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1, containerFromGrouping, choiceFromGrp);
70         SchemaPath originalPresenceContainerPath = SchemaPath.create(true, grp1, containerFromGrouping2,
71             presenceContainer);
72
73         checkOriginalList(result, originalListInContainerPath);
74         checkOriginalChoice(result, originalChoiceFromGrpPath);
75         checkOriginalContainer(result, originalPresenceContainerPath);
76     }
77
78     private static void checkOriginalContainer(final SchemaContext result, final SchemaPath path) {
79         SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
80         assertNotNull(containerInContainerNode);
81
82         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
83         assertFalse(containerSchemaNode.getReference().isPresent());
84         assertFalse(containerSchemaNode.getDescription().isPresent());
85         assertTrue(containerSchemaNode.isConfiguration());
86         assertFalse(containerSchemaNode.isPresenceContainer());
87
88         assertEquals(0, containerSchemaNode.getMustConstraints().size());
89     }
90
91     private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
92         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
93         assertNotNull(choiceInContainerNode);
94
95         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
96         assertFalse(choiceSchemaNode.isMandatory());
97     }
98
99     private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
100         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
101         assertNotNull(listInContainerNode);
102
103         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
104         assertEquals(Optional.of("original reference"), listSchemaNode.getReference());
105         assertEquals(Optional.of("original description"), listSchemaNode.getDescription());
106         assertFalse(listSchemaNode.isConfiguration());
107
108         ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
109         assertEquals((Object) 10, listConstraints.getMinElements());
110         assertEquals((Object) 20, listConstraints.getMaxElements());
111         assertEquals(1, listSchemaNode.getMustConstraints().size());
112     }
113
114     private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
115         SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
116         assertNotNull(containerInContainerNode);
117
118         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
119         assertEquals(Optional.of("new reference"), containerSchemaNode.getReference());
120         assertEquals(Optional.of("new description"), containerSchemaNode.getDescription());
121         assertTrue(containerSchemaNode.isConfiguration());
122         assertTrue(containerSchemaNode.isPresenceContainer());
123         assertEquals(1, containerSchemaNode.getMustConstraints().size());
124     }
125
126     private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
127         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
128         assertNotNull(choiceInContainerNode);
129
130         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
131         assertTrue(choiceSchemaNode.isMandatory());
132     }
133
134     private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
135         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
136         assertNotNull(listInContainerNode);
137
138         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
139         assertEquals(Optional.of("new reference"), listSchemaNode.getReference());
140         assertEquals(Optional.of("new description"), listSchemaNode.getDescription());
141         assertTrue(listSchemaNode.isConfiguration());
142
143         ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
144         assertEquals((Object) 5, listConstraints.getMinElements());
145         assertEquals((Object) 7, listConstraints.getMaxElements());
146         assertEquals(2, listSchemaNode.getMustConstraints().size());
147     }
148 }