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.impl.DefaultReactors;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 public class EffectiveUsesRefineAndConstraintsTest {
37 public void refineTest() throws ReactorException {
38 SchemaContext result = DefaultReactors.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 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
90 assertEquals(0, containerConstraints.getMustConstraints().size());
93 private static void checkOriginalChoice(final SchemaContext result, final SchemaPath path) {
94 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
95 assertNotNull(choiceInContainerNode);
97 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
98 assertFalse(choiceSchemaNode.isMandatory());
100 ConstraintDefinition choiceConstraints = choiceSchemaNode.getConstraints();
101 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
104 private static void checkOriginalList(final SchemaContext result, final SchemaPath path) {
105 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
106 assertNotNull(listInContainerNode);
108 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
109 assertEquals(Optional.of("original reference"), listSchemaNode.getReference());
110 assertEquals(Optional.of("original description"), listSchemaNode.getDescription());
111 assertFalse(listSchemaNode.isConfiguration());
113 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
114 assertEquals(10, listConstraints.getMinElements().intValue());
115 assertEquals(20, listConstraints.getMaxElements().intValue());
116 assertEquals(1, listConstraints.getMustConstraints().size());
119 private static void checkRefinedContainer(final SchemaContext result, final SchemaPath path) {
120 SchemaNode containerInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
121 assertNotNull(containerInContainerNode);
123 ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) containerInContainerNode;
124 assertEquals(Optional.of("new reference"), containerSchemaNode.getReference());
125 assertEquals(Optional.of("new description"), containerSchemaNode.getDescription());
126 assertTrue(containerSchemaNode.isConfiguration());
127 assertTrue(containerSchemaNode.isPresenceContainer());
129 ConstraintDefinition containerConstraints = containerSchemaNode.getConstraints();
130 assertEquals(1, containerConstraints.getMustConstraints().size());
133 private static void checkRefinedChoice(final SchemaContext result, final SchemaPath path) {
134 SchemaNode choiceInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
135 assertNotNull(choiceInContainerNode);
137 ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) choiceInContainerNode;
138 assertTrue(choiceSchemaNode.isMandatory());
140 ConstraintDefinition choiceConstraints = choiceSchemaNode.getConstraints();
141 assertTrue(choiceConstraints.getMustConstraints().isEmpty());
144 private static void checkRefinedList(final SchemaContext result, final SchemaPath path) {
145 SchemaNode listInContainerNode = SchemaContextUtil.findDataSchemaNode(result, path);
146 assertNotNull(listInContainerNode);
148 ListSchemaNode listSchemaNode = (ListSchemaNode) listInContainerNode;
149 assertEquals(Optional.of("new reference"), listSchemaNode.getReference());
150 assertEquals(Optional.of("new description"), listSchemaNode.getDescription());
151 assertTrue(listSchemaNode.isConfiguration());
153 ConstraintDefinition listConstraints = listSchemaNode.getConstraints();
154 assertEquals(5, listConstraints.getMinElements().intValue());
155 assertEquals(7, listConstraints.getMaxElements().intValue());
156 assertEquals(2, listConstraints.getMustConstraints().size());