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 java.net.URISyntaxException;
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.ConstraintDefinition;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
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.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
36 public class EffectiveUsesRefineAndConstraintsTest {
38 private static final YangStatementSourceImpl REFINE_TEST = new YangStatementSourceImpl(
39 "/stmt-test/uses/refine-test.yang",false);
42 public void refineTest() throws SourceException, ReactorException,
44 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
47 StmtTestUtils.addSources(reactor, REFINE_TEST);
49 EffectiveSchemaContext result = reactor.buildEffective();
51 assertNotNull(result);
53 Set<Module> modules = result.getModules();
54 assertNotNull(modules);
55 assertEquals(1, modules.size());
57 Module module = modules.iterator().next();
59 QNameModule qnameModule = module.getQNameModule();
60 QName rootContainer = QName.create(qnameModule, "root-container");
61 QName grp1 = QName.create(qnameModule, "grp-1");
63 QName containerFromGrouping = QName.create(qnameModule,
64 "container-from-grouping");
65 QName listInContainer = QName.create(qnameModule, "list-in-container");
66 QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
68 QName containerFromGrouping2 = QName.create(qnameModule,
69 "container-from-grouping2");
70 QName presenceContainer = QName.create(qnameModule,
71 "presence-container");
73 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
74 containerFromGrouping, listInContainer);
75 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
76 containerFromGrouping, choiceFromGrp);
77 SchemaPath presenceContainerPath = SchemaPath.create(true,
78 rootContainer, containerFromGrouping2, presenceContainer);
80 checkRefinedList(result, listInContainerPath);
81 checkRefinedChoice(result, choiceFromGrpPath);
82 checkRefinedContainer(result, presenceContainerPath);
84 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
85 containerFromGrouping, listInContainer);
86 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
87 containerFromGrouping, choiceFromGrp);
88 SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
89 grp1, containerFromGrouping2, presenceContainer);
91 checkOriginalList(result, originalListInContainerPath);
92 checkOriginalChoice(result, originalChoiceFromGrpPath);
93 checkOriginalContainer(result, originalPresenceContainerPath);
97 private static void checkOriginalContainer(final EffectiveSchemaContext result,
98 final SchemaPath path) {
99 SchemaNode containerInContainerNode = SchemaContextUtil
100 .findDataSchemaNode(result, path);
101 assertNotNull(containerInContainerNode);
103 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
104 assertNull(containerSchemaNode.getReference());
105 assertNull(containerSchemaNode.getDescription());
106 assertEquals(true, containerSchemaNode.isConfiguration());
107 assertEquals(false, containerSchemaNode.isPresenceContainer());
109 ConstraintDefinition containerConstraints = containerSchemaNode
111 assertEquals(0, containerConstraints.getMustConstraints().size());
114 private static void checkOriginalChoice(final EffectiveSchemaContext result,
115 final SchemaPath path) {
116 SchemaNode choiceInContainerNode = SchemaContextUtil
117 .findDataSchemaNode(result, path);
118 assertNotNull(choiceInContainerNode);
120 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
122 ConstraintDefinition choiceConstraints = choiceSchemaNode
124 assertFalse(choiceConstraints.isMandatory());
125 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
128 private static void checkOriginalList(final EffectiveSchemaContext result,
129 final SchemaPath path) {
130 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
132 assertNotNull(listInContainerNode);
134 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
135 assertEquals("original reference", listSchemaNode.getReference());
136 assertEquals("original description", listSchemaNode.getDescription());
137 assertEquals(false, listSchemaNode.isConfiguration());
139 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
140 assertTrue(listConstraints.getMinElements() == 10);
141 assertTrue(listConstraints.getMaxElements() == 20);
142 assertEquals(1, listConstraints.getMustConstraints().size());
145 private static void checkRefinedContainer(final EffectiveSchemaContext result,
146 final SchemaPath path) {
147 SchemaNode containerInContainerNode = SchemaContextUtil
148 .findDataSchemaNode(result, path);
149 assertNotNull(containerInContainerNode);
151 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
152 assertEquals("new reference", containerSchemaNode.getReference());
153 assertEquals("new description", containerSchemaNode.getDescription());
154 assertEquals(true, containerSchemaNode.isConfiguration());
155 assertEquals(true, containerSchemaNode.isPresenceContainer());
157 ConstraintDefinition containerConstraints = containerSchemaNode
159 assertEquals(1, containerConstraints.getMustConstraints().size());
162 private static void checkRefinedChoice(final EffectiveSchemaContext result,
163 final SchemaPath path) {
164 SchemaNode choiceInContainerNode = SchemaContextUtil
165 .findDataSchemaNode(result, path);
166 assertNotNull(choiceInContainerNode);
168 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
170 ConstraintDefinition choiceConstraints = choiceSchemaNode
172 assertTrue(choiceConstraints.isMandatory());
173 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
176 private static void checkRefinedList(final EffectiveSchemaContext result, final SchemaPath path) {
177 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(
179 assertNotNull(listInContainerNode);
181 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
182 assertEquals("new reference", listSchemaNode.getReference());
183 assertEquals("new description", listSchemaNode.getDescription());
184 assertEquals(true, listSchemaNode.isConfiguration());
186 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
187 assertTrue(listConstraints.getMinElements() == 5);
188 assertTrue(listConstraints.getMaxElements() == 7);
189 assertEquals(2, listConstraints.getMustConstraints().size());