YANGTOOLS-706: Split out yang-parser-rfc7950
[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
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.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
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.rfc7950.reactor.RFC7950Reactors;
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 = RFC7950Reactors.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         assertEquals(0, containerSchemaNode.getMustConstraints().size());
90     }
91
92     private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
93         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
94         assertNotNull(choiceInContainerNode);
95
96         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
97         assertFalse(choiceSchemaNode.isMandatory());
98     }
99
100     private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
101         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
102         assertNotNull(listInContainerNode);
103
104         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
105         assertEquals(Optional.of("original reference"), listSchemaNode.getReference());
106         assertEquals(Optional.of("original description"), listSchemaNode.getDescription());
107         assertFalse(listSchemaNode.isConfiguration());
108
109         ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
110         assertEquals(10, listConstraints.getMinElements().intValue());
111         assertEquals(20, listConstraints.getMaxElements().intValue());
112         assertEquals(1, listSchemaNode.getMustConstraints().size());
113     }
114
115     private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
116         SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
117         assertNotNull(containerInContainerNode);
118
119         ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
120         assertEquals(Optional.of("new reference"), containerSchemaNode.getReference());
121         assertEquals(Optional.of("new description"), containerSchemaNode.getDescription());
122         assertTrue(containerSchemaNode.isConfiguration());
123         assertTrue(containerSchemaNode.isPresenceContainer());
124         assertEquals(1, containerSchemaNode.getMustConstraints().size());
125     }
126
127     private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
128         SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
129         assertNotNull(choiceInContainerNode);
130
131         ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
132         assertTrue(choiceSchemaNode.isMandatory());
133     }
134
135     private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
136         SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
137         assertNotNull(listInContainerNode);
138
139         ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
140         assertEquals(Optional.of("new reference"), listSchemaNode.getReference());
141         assertEquals(Optional.of("new description"), listSchemaNode.getDescription());
142         assertTrue(listSchemaNode.isConfiguration());
143
144         ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
145         assertEquals(5, listConstraints.getMinElements().intValue());
146         assertEquals(7, listConstraints.getMaxElements().intValue());
147         assertEquals(2, listSchemaNode.getMustConstraints().size());
148     }
149 }