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.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
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.rfc7950.reactor.RFC7950Reactors;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 public class EffectiveUsesRefineAndConstraintsTest {
37 public void refineTest() throws ReactorException {
38 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
39 .addSource(sourceForResource("/stmt-test/uses/refine-test.yang"))
41 assertNotNull(result);
43 Set<Module> modules = result.getModules();
44 assertNotNull(modules);
45 assertEquals(1, modules.size());
47 Module module = modules.iterator().next();
49 final QNameModule qnameModule = module.getQNameModule();
50 final QName rootContainer = QName.create(qnameModule, "root-container");
51 final QName grp1 = QName.create(qnameModule, "grp-1");
53 final QName containerFromGrouping = QName.create(qnameModule, "container-from-grouping");
54 final QName listInContainer = QName.create(qnameModule, "list-in-container");
55 final QName choiceFromGrp = QName.create(qnameModule, "choice-from-grp");
57 final QName containerFromGrouping2 = QName.create(qnameModule, "container-from-grouping2");
58 final QName presenceContainer = QName.create(qnameModule, "presence-container");
60 SchemaPath listInContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping, listInContainer);
61 SchemaPath choiceFromGrpPath = SchemaPath.create(true, rootContainer, containerFromGrouping, choiceFromGrp);
62 SchemaPath presenceContainerPath = SchemaPath.create(true, rootContainer, containerFromGrouping2,
65 checkRefinedList(result, listInContainerPath);
66 checkRefinedChoice(result, choiceFromGrpPath);
67 checkRefinedContainer(result, presenceContainerPath);
69 SchemaPath originalListInContainerPath = SchemaPath.create(true, grp1, containerFromGrouping, listInContainer);
70 SchemaPath originalChoiceFromGrpPath = SchemaPath.create(true, grp1, containerFromGrouping, choiceFromGrp);
71 SchemaPath originalPresenceContainerPath = SchemaPath.create(true, grp1, containerFromGrouping2,
74 checkOriginalList(result, originalListInContainerPath);
75 checkOriginalChoice(result, originalChoiceFromGrpPath);
76 checkOriginalContainer(result, originalPresenceContainerPath);
79 private static void checkOriginalContainer(final SchemaContext result, final SchemaPath path) {
80 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
81 assertNotNull(containerInContainerNode);
83 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
84 assertFalse(containerSchemaNode.getReference().isPresent());
85 assertFalse(containerSchemaNode.getDescription().isPresent());
86 assertTrue(containerSchemaNode.isConfiguration());
87 assertFalse(containerSchemaNode.isPresenceContainer());
89 assertEquals(0, containerSchemaNode.getMustConstraints().size());
92 private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
93 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
94 assertNotNull(choiceInContainerNode);
96 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
97 assertFalse(choiceSchemaNode.isMandatory());
100 private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
101 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
102 assertNotNull(listInContainerNode);
104 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
105 assertEquals(Optional.of("original reference"), listSchemaNode.getReference());
106 assertEquals(Optional.of("original description"), listSchemaNode.getDescription());
107 assertFalse(listSchemaNode.isConfiguration());
109 ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
110 assertEquals(10, listConstraints.getMinElements().intValue());
111 assertEquals(20, listConstraints.getMaxElements().intValue());
112 assertEquals(1, listSchemaNode.getMustConstraints().size());
115 private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
116 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
117 assertNotNull(containerInContainerNode);
119 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
120 assertEquals(Optional.of("new reference"), containerSchemaNode.getReference());
121 assertEquals(Optional.of("new description"), containerSchemaNode.getDescription());
122 assertTrue(containerSchemaNode.isConfiguration());
123 assertTrue(containerSchemaNode.isPresenceContainer());
124 assertEquals(1, containerSchemaNode.getMustConstraints().size());
127 private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
128 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
129 assertNotNull(choiceInContainerNode);
131 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
132 assertTrue(choiceSchemaNode.isMandatory());
135 private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
136 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
137 assertNotNull(listInContainerNode);
139 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
140 assertEquals(Optional.of("new reference"), listSchemaNode.getReference());
141 assertEquals(Optional.of("new description"), listSchemaNode.getDescription());
142 assertTrue(listSchemaNode.isConfiguration());
144 ElementCountConstraint listConstraints = listSchemaNode.getElementCountConstraint().get();
145 assertEquals(5, listConstraints.getMinElements().intValue());
146 assertEquals(7, listConstraints.getMaxElements().intValue());
147 assertEquals(2, listSchemaNode.getMustConstraints().size());