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.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
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.newBuild();
47 reactor.addSources(REFINE_TEST);
49 SchemaContext 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, "container-from-grouping");
64 QName listInContainer = QName.create(qnameModule, "list-in-container");
65 QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
67 QName containerFromGrouping2 = QName.create(qnameModule, "container-from-grouping2");
68 QName presenceContainer = QName.create(qnameModule, "presence-container");
70 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer,
71 containerFromGrouping, listInContainer);
72 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer,
73 containerFromGrouping, choiceFromGrp);
74 SchemaPath presenceContainerPath = SchemaPath.create(true,
75 rootContainer, containerFromGrouping2, presenceContainer);
77 checkRefinedList(result, listInContainerPath);
78 checkRefinedChoice(result, choiceFromGrpPath);
79 checkRefinedContainer(result, presenceContainerPath);
81 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1,
82 containerFromGrouping, listInContainer);
83 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1,
84 containerFromGrouping, choiceFromGrp);
85 SchemaPath originalPresenceContainerPath = SchemaPath.create(true,
86 grp1, containerFromGrouping2, presenceContainer);
88 checkOriginalList(result, originalListInContainerPath);
89 checkOriginalChoice(result, originalChoiceFromGrpPath);
90 checkOriginalContainer(result, originalPresenceContainerPath);
93 private static void checkOriginalContainer(final SchemaContext result, final SchemaPath path) {
94 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
95 assertNotNull(containerInContainerNode);
97 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
98 assertNull(containerSchemaNode.getReference());
99 assertNull(containerSchemaNode.getDescription());
100 assertTrue(containerSchemaNode.isConfiguration());
101 assertFalse(containerSchemaNode.isPresenceContainer());
103 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
104 assertEquals(0, containerConstraints.getMustConstraints().size());
107 private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
108 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
109 assertNotNull(choiceInContainerNode);
111 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
113 ConstraintDefinition choiceConstraints = choiceSchemaNode
115 assertFalse(choiceConstraints.isMandatory());
116 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
119 private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
120 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
121 assertNotNull(listInContainerNode);
123 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
124 assertEquals("original reference", listSchemaNode.getReference());
125 assertEquals("original description", listSchemaNode.getDescription());
126 assertFalse(listSchemaNode.isConfiguration());
128 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
129 assertEquals(10, listConstraints.getMinElements().intValue());
130 assertEquals(20, listConstraints.getMaxElements().intValue());
131 assertEquals(1, listConstraints.getMustConstraints().size());
134 private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
135 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
136 assertNotNull(containerInContainerNode);
138 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
139 assertEquals("new reference", containerSchemaNode.getReference());
140 assertEquals("new description", containerSchemaNode.getDescription());
141 assertTrue(containerSchemaNode.isConfiguration());
142 assertTrue(containerSchemaNode.isPresenceContainer());
144 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
145 assertEquals(1, containerConstraints.getMustConstraints().size());
148 private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
149 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
150 assertNotNull(choiceInContainerNode);
152 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
154 ConstraintDefinition choiceConstraints = choiceSchemaNode
156 assertTrue(choiceConstraints.isMandatory());
157 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
160 private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
161 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
162 assertNotNull(listInContainerNode);
164 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
165 assertEquals("new reference", listSchemaNode.getReference());
166 assertEquals("new description", listSchemaNode.getDescription());
167 assertTrue(listSchemaNode.isConfiguration());
169 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
170 assertEquals(5, listConstraints.getMinElements().intValue());
171 assertEquals(7, listConstraints.getMaxElements().intValue());
172 assertEquals(2, listConstraints.getMustConstraints().size());