1 package org.opendaylight.yangtools.yang.stmt.effective.build.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
8 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
10 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
11 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
12 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
14 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import java.net.URISyntaxException;
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;
28 import org.junit.Test;
30 public class EffectiveUsesRefineAndConstraintsTest {
32 private static final YangStatementSourceImpl REFINE_TEST = new YangStatementSourceImpl(
33 "/stmt-test/uses/refine-test.yang",false);
36 public void refineTest() throws SourceException, ReactorException,
38 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
41 StmtTestUtils.addSources(reactor, REFINE_TEST);
43 EffectiveSchemaContext result = reactor.buildEffective();
45 assertNotNull(result);
47 Set<Module> modules = result.getModules();
48 assertNotNull(modules);
49 assertEquals(1, modules.size());
51 Module module = modules.iterator().next();
53 QNameModule qnameModule = module.getQNameModule();
54 QName rootContainer = QName.create(qnameModule, "root-container");
55 QName grp1 = QName.create(qnameModule, "grp-1");
57 QName containerFromGrouping = QName.create(qnameModule,
58 "container-from-grouping");
59 QName listInContainer = QName.create(qnameModule, "list-in-container");
60 QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
62 QName containerFromGrouping2 = QName.create(qnameModule,
63 "container-from-grouping2");
64 QName presenceContainer = QName.create(qnameModule,
65 "presence-container");
67 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
68 containerFromGrouping, listInContainer);
69 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
70 containerFromGrouping, choiceFromGrp);
71 SchemaPath presenceContainerPath = SchemaPath.create(true,
72 rootContainer, containerFromGrouping2, presenceContainer);
74 checkRefinedList(result, listInContainerPath);
75 checkRefinedChoice(result, choiceFromGrpPath);
76 checkRefinedContainer(result, presenceContainerPath);
78 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
79 containerFromGrouping, listInContainer);
80 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
81 containerFromGrouping, choiceFromGrp);
82 SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
83 grp1, containerFromGrouping2, presenceContainer);
85 checkOriginalList(result, originalListInContainerPath);
86 checkOriginalChoice(result, originalChoiceFromGrpPath);
87 checkOriginalContainer(result, originalPresenceContainerPath);
91 private void checkOriginalContainer(EffectiveSchemaContext result,
93 SchemaNode containerInContainerNode = SchemaContextUtil
94 .findDataSchemaNode(result, path);
95 assertNotNull(containerInContainerNode);
97 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
98 assertNull(containerSchemaNode.getReference());
99 assertNull(containerSchemaNode.getDescription());
100 assertEquals(true, containerSchemaNode.isConfiguration());
101 assertEquals(false, containerSchemaNode.isPresenceContainer());
103 ConstraintDefinition containerConstraints = containerSchemaNode
105 assertEquals(0, containerConstraints.getMustConstraints().size());
108 private void checkOriginalChoice(EffectiveSchemaContext result,
110 SchemaNode choiceInContainerNode = SchemaContextUtil
111 .findDataSchemaNode(result, path);
112 assertNotNull(choiceInContainerNode);
114 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
116 ConstraintDefinition choiceConstraints = choiceSchemaNode
118 assertTrue(choiceConstraints.isMandatory() == false);
119 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
122 private void checkOriginalList(EffectiveSchemaContext result,
124 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
126 assertNotNull(listInContainerNode);
128 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
129 assertEquals("original reference", listSchemaNode.getReference());
130 assertEquals("original description", listSchemaNode.getDescription());
131 assertEquals(false, listSchemaNode.isConfiguration());
133 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
134 assertTrue(listConstraints.getMinElements() == 10);
135 assertTrue(listConstraints.getMaxElements() == 20);
136 assertEquals(1, listConstraints.getMustConstraints().size());
139 private void checkRefinedContainer(EffectiveSchemaContext result,
141 SchemaNode containerInContainerNode = SchemaContextUtil
142 .findDataSchemaNode(result, path);
143 assertNotNull(containerInContainerNode);
145 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
146 assertEquals("new reference", containerSchemaNode.getReference());
147 assertEquals("new description", containerSchemaNode.getDescription());
148 assertEquals(true, containerSchemaNode.isConfiguration());
149 assertEquals(true, containerSchemaNode.isPresenceContainer());
151 ConstraintDefinition containerConstraints = containerSchemaNode
153 assertEquals(1, containerConstraints.getMustConstraints().size());
156 private void checkRefinedChoice(EffectiveSchemaContext result,
158 SchemaNode choiceInContainerNode = SchemaContextUtil
159 .findDataSchemaNode(result, path);
160 assertNotNull(choiceInContainerNode);
162 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
164 ConstraintDefinition choiceConstraints = choiceSchemaNode
166 assertTrue(choiceConstraints.isMandatory());
167 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
170 private void checkRefinedList(EffectiveSchemaContext result, SchemaPath path) {
171 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
173 assertNotNull(listInContainerNode);
175 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
176 assertEquals("new reference", listSchemaNode.getReference());
177 assertEquals("new description", listSchemaNode.getDescription());
178 assertEquals(true, listSchemaNode.isConfiguration());
180 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
181 assertTrue(listConstraints.getMinElements() == 5);
182 assertTrue(listConstraints.getMaxElements() == 7);
183 assertEquals(2, listConstraints.getMustConstraints().size());