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