1 package org.opendaylight.yangtools.yang.stmt.effective.build.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertTrue;
8 import java.net.URISyntaxException;
10 import org.junit.Test;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
27 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
29 public class EffectiveUsesRefineAndConstraintsTest {
31 private static final YangStatementSourceImpl REFINE_TEST = new YangStatementSourceImpl(
32 "/stmt-test/uses/refine-test.yang",false);
35 public void refineTest() throws SourceException, ReactorException,
37 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
40 StmtTestUtils.addSources(reactor, REFINE_TEST);
42 EffectiveSchemaContext result = reactor.buildEffective();
44 assertNotNull(result);
46 Set<Module> modules = result.getModules();
47 assertNotNull(modules);
48 assertEquals(1, modules.size());
50 Module module = modules.iterator().next();
52 QNameModule qnameModule = module.getQNameModule();
53 QName rootContainer = QName.create(qnameModule, "root-container");
54 QName grp1 = QName.create(qnameModule, "grp-1");
56 QName containerFromGrouping = QName.create(qnameModule,
57 "container-from-grouping");
58 QName listInContainer = QName.create(qnameModule, "list-in-container");
59 QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
61 QName containerFromGrouping2 = QName.create(qnameModule,
62 "container-from-grouping2");
63 QName presenceContainer = QName.create(qnameModule,
64 "presence-container");
66 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
67 containerFromGrouping, listInContainer);
68 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
69 containerFromGrouping, choiceFromGrp);
70 SchemaPath presenceContainerPath = SchemaPath.create(true,
71 rootContainer, containerFromGrouping2, presenceContainer);
73 checkRefinedList(result, listInContainerPath);
74 checkRefinedChoice(result, choiceFromGrpPath);
75 checkRefinedContainer(result, presenceContainerPath);
77 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
78 containerFromGrouping, listInContainer);
79 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
80 containerFromGrouping, choiceFromGrp);
81 SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
82 grp1, containerFromGrouping2, presenceContainer);
84 checkOriginalList(result, originalListInContainerPath);
85 checkOriginalChoice(result, originalChoiceFromGrpPath);
86 checkOriginalContainer(result, originalPresenceContainerPath);
90 private static void checkOriginalContainer(final EffectiveSchemaContext result,
91 final SchemaPath path) {
92 SchemaNode containerInContainerNode = SchemaContextUtil
93 .findDataSchemaNode(result, path);
94 assertNotNull(containerInContainerNode);
96 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
97 assertNull(containerSchemaNode.getReference());
98 assertNull(containerSchemaNode.getDescription());
99 assertEquals(true, containerSchemaNode.isConfiguration());
100 assertEquals(false, containerSchemaNode.isPresenceContainer());
102 ConstraintDefinition containerConstraints = containerSchemaNode
104 assertEquals(0, containerConstraints.getMustConstraints().size());
107 private static void checkOriginalChoice(final EffectiveSchemaContext result,
108 final SchemaPath path) {
109 SchemaNode choiceInContainerNode = SchemaContextUtil
110 .findDataSchemaNode(result, path);
111 assertNotNull(choiceInContainerNode);
113 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
115 ConstraintDefinition choiceConstraints = choiceSchemaNode
117 assertFalse(choiceConstraints.isMandatory());
118 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
121 private static void checkOriginalList(final EffectiveSchemaContext result,
122 final SchemaPath path) {
123 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
125 assertNotNull(listInContainerNode);
127 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
128 assertEquals("original reference", listSchemaNode.getReference());
129 assertEquals("original description", listSchemaNode.getDescription());
130 assertEquals(false, listSchemaNode.isConfiguration());
132 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
133 assertTrue(listConstraints.getMinElements() == 10);
134 assertTrue(listConstraints.getMaxElements() == 20);
135 assertEquals(1, listConstraints.getMustConstraints().size());
138 private static void checkRefinedContainer(final EffectiveSchemaContext result,
139 final SchemaPath path) {
140 SchemaNode containerInContainerNode = SchemaContextUtil
141 .findDataSchemaNode(result, path);
142 assertNotNull(containerInContainerNode);
144 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
145 assertEquals("new reference", containerSchemaNode.getReference());
146 assertEquals("new description", containerSchemaNode.getDescription());
147 assertEquals(true, containerSchemaNode.isConfiguration());
148 assertEquals(true, containerSchemaNode.isPresenceContainer());
150 ConstraintDefinition containerConstraints = containerSchemaNode
152 assertEquals(1, containerConstraints.getMustConstraints().size());
155 private static void checkRefinedChoice(final EffectiveSchemaContext result,
156 final SchemaPath path) {
157 SchemaNode choiceInContainerNode = SchemaContextUtil
158 .findDataSchemaNode(result, path);
159 assertNotNull(choiceInContainerNode);
161 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
163 ConstraintDefinition choiceConstraints = choiceSchemaNode
165 assertTrue(choiceConstraints.isMandatory());
166 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
169 private static void checkRefinedList(final EffectiveSchemaContext result, final SchemaPath path) {
170 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
172 assertNotNull(listInContainerNode);
174 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
175 assertEquals("new reference", listSchemaNode.getReference());
176 assertEquals("new description", listSchemaNode.getDescription());
177 assertEquals(true, listSchemaNode.isConfiguration());
179 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
180 assertTrue(listConstraints.getMinElements() == 5);
181 assertTrue(listConstraints.getMaxElements() == 7);
182 assertEquals(2, listConstraints.getMustConstraints().size());