2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18 import java.net.URISyntaxException;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
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.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
38 public class EffectiveUsesRefineAndConstraintsTest {
40 private static final StatementStreamSource REFINE_TEST = sourceForResource("/stmt-test/uses/refine-test.yang");
43 public void refineTest() throws SourceException, ReactorException,
45 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
48 reactor.addSources(REFINE_TEST);
50 EffectiveSchemaContext result = reactor.buildEffective();
52 assertNotNull(result);
54 Set<Module> modules = result.getModules();
55 assertNotNull(modules);
56 assertEquals(1, modules.size());
58 Module module = modules.iterator().next();
60 QNameModule qnameModule = module.getQNameModule();
61 QName rootContainer = QName.create(qnameModule, "root-container");
62 QName grp1 = QName.create(qnameModule, "grp-1");
64 QName containerFromGrouping = QName.create(qnameModule,
65 "container-from-grouping");
66 QName listInContainer = QName.create(qnameModule, "list-in-container");
67 QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
69 QName containerFromGrouping2 = QName.create(qnameModule,
70 "container-from-grouping2");
71 QName presenceContainer = QName.create(qnameModule,
72 "presence-container");
74 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
75 containerFromGrouping, listInContainer);
76 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
77 containerFromGrouping, choiceFromGrp);
78 SchemaPath presenceContainerPath = SchemaPath.create(true,
79 rootContainer, containerFromGrouping2, presenceContainer);
81 checkRefinedList(result, listInContainerPath);
82 checkRefinedChoice(result, choiceFromGrpPath);
83 checkRefinedContainer(result, presenceContainerPath);
85 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
86 containerFromGrouping, listInContainer);
87 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
88 containerFromGrouping, choiceFromGrp);
89 SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
90 grp1, containerFromGrouping2, presenceContainer);
92 checkOriginalList(result, originalListInContainerPath);
93 checkOriginalChoice(result, originalChoiceFromGrpPath);
94 checkOriginalContainer(result, originalPresenceContainerPath);
98 private static void checkOriginalContainer(final EffectiveSchemaContext result,
99 final SchemaPath path) {
100 SchemaNode containerInContainerNode = SchemaContextUtil
101 .findDataSchemaNode(result, path);
102 assertNotNull(containerInContainerNode);
104 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
105 assertNull(containerSchemaNode.getReference());
106 assertNull(containerSchemaNode.getDescription());
107 assertEquals(true, containerSchemaNode.isConfiguration());
108 assertEquals(false, containerSchemaNode.isPresenceContainer());
110 ConstraintDefinition containerConstraints = containerSchemaNode
112 assertEquals(0, containerConstraints.getMustConstraints().size());
115 private static void checkOriginalChoice(final EffectiveSchemaContext result,
116 final SchemaPath path) {
117 SchemaNode choiceInContainerNode = SchemaContextUtil
118 .findDataSchemaNode(result, path);
119 assertNotNull(choiceInContainerNode);
121 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
123 ConstraintDefinition choiceConstraints = choiceSchemaNode
125 assertFalse(choiceConstraints.isMandatory());
126 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
129 private static void checkOriginalList(final EffectiveSchemaContext result,
130 final SchemaPath path) {
131 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
133 assertNotNull(listInContainerNode);
135 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
136 assertEquals("original reference", listSchemaNode.getReference());
137 assertEquals("original description", listSchemaNode.getDescription());
138 assertEquals(false, listSchemaNode.isConfiguration());
140 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
141 assertTrue(listConstraints.getMinElements() == 10);
142 assertTrue(listConstraints.getMaxElements() == 20);
143 assertEquals(1, listConstraints.getMustConstraints().size());
146 private static void checkRefinedContainer(final EffectiveSchemaContext result,
147 final SchemaPath path) {
148 SchemaNode containerInContainerNode = SchemaContextUtil
149 .findDataSchemaNode(result, path);
150 assertNotNull(containerInContainerNode);
152 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
153 assertEquals("new reference", containerSchemaNode.getReference());
154 assertEquals("new description", containerSchemaNode.getDescription());
155 assertEquals(true, containerSchemaNode.isConfiguration());
156 assertEquals(true, containerSchemaNode.isPresenceContainer());
158 ConstraintDefinition containerConstraints = containerSchemaNode
160 assertEquals(1, containerConstraints.getMustConstraints().size());
163 private static void checkRefinedChoice(final EffectiveSchemaContext result,
164 final SchemaPath path) {
165 SchemaNode choiceInContainerNode = SchemaContextUtil
166 .findDataSchemaNode(result, path);
167 assertNotNull(choiceInContainerNode);
169 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
171 ConstraintDefinition choiceConstraints = choiceSchemaNode
173 assertTrue(choiceConstraints.isMandatory());
174 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
177 private static void checkRefinedList(final EffectiveSchemaContext result, final SchemaPath path) {
178 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
180 assertNotNull(listInContainerNode);
182 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
183 assertEquals("new reference", listSchemaNode.getReference());
184 assertEquals("new description", listSchemaNode.getDescription());
185 assertEquals(true, listSchemaNode.isConfiguration());
187 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
188 assertTrue(listConstraints.getMinElements() == 5);
189 assertTrue(listConstraints.getMaxElements() == 7);
190 assertEquals(2, listConstraints.getMustConstraints().size());