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.assertTrue;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 public class EffectiveUsesRefineAndConstraintsTest {
38 private static final StatementStreamSource REFINE_TEST = sourceForResource("/stmt-test/uses/refine-test.yang");
41 public void refineTest() throws ReactorException {
42 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
44 reactor.addSources(REFINE_TEST);
46 SchemaContext result = reactor.buildEffective();
48 assertNotNull(result);
50 Set<Module> modules = result.getModules();
51 assertNotNull(modules);
52 assertEquals(1, modules.size());
54 Module module = modules.iterator().next();
56 final QNameModule qnameModule = module.getQNameModule();
57 final QName rootContainer = QName.create(qnameModule, "root-container");
58 final QName grp1 = QName.create(qnameModule, "grp-1");
60 final QName containerFromGrouping = QName.create(qnameModule, "container-from-grouping");
61 final QName listInContainer = QName.create(qnameModule, "list-in-container");
62 final QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
64 final QName containerFromGrouping2 = QName.create(qnameModule, "container-from-grouping2");
65 final QName presenceContainer = QName.create(qnameModule, "presence-container");
67 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping, listInContainer);
68 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer, containerFromGrouping, choiceFromGrp);
69 SchemaPath presenceContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping2,
72 checkRefinedList(result, listInContainerPath);
73 checkRefinedChoice(result, choiceFromGrpPath);
74 checkRefinedContainer(result, presenceContainerPath);
76 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1, containerFromGrouping, listInContainer);
77 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1, containerFromGrouping, choiceFromGrp);
78 SchemaPath originalPresenceContainerPath = SchemaPath.create(true, grp1, containerFromGrouping2,
81 checkOriginalList(result, originalListInContainerPath);
82 checkOriginalChoice(result, originalChoiceFromGrpPath);
83 checkOriginalContainer(result, originalPresenceContainerPath);
86 private static void checkOriginalContainer(final SchemaContext result, final SchemaPath path) {
87 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
88 assertNotNull(containerInContainerNode);
90 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
91 assertFalse(containerSchemaNode.getReference().isPresent());
92 assertFalse(containerSchemaNode.getDescription().isPresent());
93 assertTrue(containerSchemaNode.isConfiguration());
94 assertFalse(containerSchemaNode.isPresenceContainer());
96 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
97 assertEquals(0, containerConstraints.getMustConstraints().size());
100 private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
101 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
102 assertNotNull(choiceInContainerNode);
104 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
106 ConstraintDefinition choiceConstraints = choiceSchemaNode.getConstraints();
107 assertFalse(choiceConstraints.isMandatory());
108 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
111 private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
112 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
113 assertNotNull(listInContainerNode);
115 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
116 assertEquals(Optional.of("original reference"), listSchemaNode.getReference());
117 assertEquals(Optional.of("original description"), listSchemaNode.getDescription());
118 assertFalse(listSchemaNode.isConfiguration());
120 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
121 assertEquals(10, listConstraints.getMinElements().intValue());
122 assertEquals(20, listConstraints.getMaxElements().intValue());
123 assertEquals(1, listConstraints.getMustConstraints().size());
126 private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
127 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
128 assertNotNull(containerInContainerNode);
130 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
131 assertEquals(Optional.of("new reference"), containerSchemaNode.getReference());
132 assertEquals(Optional.of("new description"), containerSchemaNode.getDescription());
133 assertTrue(containerSchemaNode.isConfiguration());
134 assertTrue(containerSchemaNode.isPresenceContainer());
136 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
137 assertEquals(1, containerConstraints.getMustConstraints().size());
140 private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
141 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
142 assertNotNull(choiceInContainerNode);
144 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
146 ConstraintDefinition choiceConstraints = choiceSchemaNode.getConstraints();
147 assertTrue(choiceConstraints.isMandatory());
148 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
151 private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
152 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
153 assertNotNull(listInContainerNode);
155 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
156 assertEquals(Optional.of("new reference"), listSchemaNode.getReference());
157 assertEquals(Optional.of("new description"), listSchemaNode.getDescription());
158 assertTrue(listSchemaNode.isConfiguration());
160 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
161 assertEquals(5, listConstraints.getMinElements().intValue());
162 assertEquals(7, listConstraints.getMaxElements().intValue());
163 assertEquals(2, listConstraints.getMustConstraints().size());