2 * Copyright (c) 2013 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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Date;
20 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
37 import com.google.common.collect.Lists;
39 public class AugmentTest {
41 private final URI types1NS = URI.create("urn:simple.nodes.test");
42 private final URI types2NS = URI.create("urn:simple.types.test");
43 private final URI types3NS = URI.create("urn:custom.nodes.test");
44 private Date types1Rev;
45 private Date types2Rev;
46 private Date types3Rev;
47 private final String t1 = "n";
48 private final String t2 = "t";
49 private final String t3 = "c";
54 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
55 private Set<Module> modules;
58 public void init() throws FileNotFoundException, ParseException {
59 types1Rev = simpleDateFormat.parse("2013-02-27");
60 types2Rev = simpleDateFormat.parse("2013-07-03");
61 types3Rev = simpleDateFormat.parse("2013-02-27");
63 q0 = new QName(types2NS, types2Rev, t2, "interfaces");
64 q1 = new QName(types2NS, types2Rev, t2, "ifEntry");
65 q2 = new QName(types3NS, types3Rev, t3, "augment-holder");
67 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
68 assertEquals(3, modules.size());
72 public void testAugmentParsing() {
73 SchemaPath expectedPath = null;
74 QName[] qnames = null;
77 Module module1 = TestUtils.findModule(modules, "nodes");
78 Set<AugmentationSchema> augmentations = module1.getAugmentations();
79 assertEquals(1, augmentations.size());
80 AugmentationSchema augment = augmentations.iterator().next();
82 Set<DataSchemaNode> augmentChildren = augment.getChildNodes();
83 assertEquals(5, augmentChildren.size());
84 for(DataSchemaNode dsn : augmentChildren) {
85 assertTrue(dsn.isAugmenting());
88 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber");
89 LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
90 LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
91 ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas");
92 ChoiceNode odl = (ChoiceNode)augment.getDataChildByName("odl");
94 assertNotNull(ds0ChannelNumber);
95 assertNotNull(interfaceId);
96 assertNotNull(myType);
97 assertNotNull(schemas);
100 qnames = new QName[4];
105 // leaf ds0ChannelNumber
106 qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
107 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
108 assertEquals(expectedPath, ds0ChannelNumber.getPath());
109 // type of leaf ds0ChannelNumber
110 QName typeQName = BaseTypes.constructQName("string");
111 List<QName> typePath = new ArrayList<>(Arrays.asList(qnames));
112 typePath.add(typeQName);
113 expectedPath = new SchemaPath(typePath, true);
114 assertEquals(expectedPath, ds0ChannelNumber.getType().getPath());
117 qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
118 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
119 assertEquals(expectedPath, interfaceId.getPath());
122 qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
123 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
124 assertEquals(expectedPath, myType.getPath());
127 qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
128 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
129 assertEquals(expectedPath, schemas.getPath());
132 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
133 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
134 assertEquals(expectedPath, odl.getPath());
138 Module module3 = TestUtils.findModule(modules, "custom");
139 augmentations = module3.getAugmentations();
140 assertEquals(3, augmentations.size());
141 AugmentationSchema augment1 = null;
142 AugmentationSchema augment2 = null;
143 AugmentationSchema augment3 = null;
144 for (AugmentationSchema as : augmentations) {
145 if("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
147 } else if("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
149 } else if ("if:leafType='ds1'".equals(as.getWhenCondition().toString())) {
153 assertNotNull(augment1);
154 assertNotNull(augment2);
155 assertNotNull(augment3);
157 assertEquals(1, augment1.getChildNodes().size());
158 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder");
159 assertTrue(augmentHolder.isAugmenting());
161 assertEquals(1, augment2.getChildNodes().size());
162 ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2");
163 assertTrue(augmentHolder2.isAugmenting());
165 assertEquals(1, augment3.getChildNodes().size());
166 LeafSchemaNode linkleaf = (LeafSchemaNode) augment3.getDataChildByName("linkleaf");
167 assertTrue(linkleaf.isAugmenting());
171 public void testAugmentResolving() throws ParseException {
172 SchemaPath expectedPath = null;
173 QName[] qnames = null;
175 Module module2 = TestUtils.findModule(modules, "types");
176 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
177 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
178 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
181 // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
182 LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
183 LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id");
184 LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type");
185 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
186 ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
188 assertNotNull(ds0ChannelNumber);
189 assertNotNull(interfaceId);
190 assertNotNull(myType);
191 assertNotNull(schemas);
194 qnames = new QName[4];
199 // leaf ds0ChannelNumber
200 qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber");
201 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
202 assertEquals(expectedPath, ds0ChannelNumber.getPath());
205 qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id");
206 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
207 assertEquals(expectedPath, interfaceId.getPath());
210 qnames[3] = new QName(types1NS, types1Rev, t1, "my-type");
211 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
212 assertEquals(expectedPath, myType.getPath());
215 qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
216 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
217 assertEquals(expectedPath, schemas.getPath());
220 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
221 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
222 assertEquals(expectedPath, odl.getPath());
225 // augment "/data:interfaces/data:ifEntry/t3:augment-holder/t1:schemas"
226 LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
227 assertNotNull(linkleaf);
229 qnames = new QName[5];
233 qnames[3] = new QName(types1NS, types1Rev, t1, "schemas");
234 qnames[4] = new QName(types3NS, types3Rev, t3, "linkleaf");
235 expectedPath = new SchemaPath(Arrays.asList(qnames), true);
236 assertEquals(expectedPath, linkleaf.getPath());
240 public void testAugmentChoice() throws ParseException {
241 SchemaPath expectedPath = null;
242 QName[] qnames = null;
244 Module module2 = TestUtils.findModule(modules, "types");
245 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
246 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
247 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
250 // augment "/data:interfaces/data:ifEntry/t3:augment-holder"
251 ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl");
253 Set<ChoiceCaseNode> cases = odl.getCases();
254 assertEquals(4, cases.size());
256 ChoiceCaseNode id = null;
257 ChoiceCaseNode node1 = null;
258 ChoiceCaseNode node2 = null;
259 ChoiceCaseNode node3 = null;
261 for(ChoiceCaseNode ccn : cases) {
262 if("id".equals(ccn.getQName().getLocalName())) {
264 } else if("node1".equals(ccn.getQName().getLocalName())) {
266 } else if("node2".equals(ccn.getQName().getLocalName())) {
268 } else if("node3".equals(ccn.getQName().getLocalName())) {
274 assertNotNull(node1);
275 assertNotNull(node2);
276 assertNotNull(node3);
278 qnames = new QName[5];
282 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
285 qnames[4] = new QName(types1NS, types1Rev, t1, "id");
286 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
287 assertEquals(expectedPath, id.getPath());
288 Set<DataSchemaNode> idChildren = id.getChildNodes();
289 assertEquals(1, idChildren.size());
292 qnames[4] = new QName(types1NS, types1Rev, t1, "node1");
293 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
294 assertEquals(expectedPath, node1.getPath());
295 Set<DataSchemaNode> node1Children = node1.getChildNodes();
296 assertTrue(node1Children.isEmpty());
299 qnames[4] = new QName(types1NS, types1Rev, t1, "node2");
300 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
301 assertEquals(expectedPath, node2.getPath());
302 Set<DataSchemaNode> node2Children = node2.getChildNodes();
303 assertTrue(node2Children.isEmpty());
306 qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
307 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
308 assertEquals(expectedPath, node3.getPath());
309 Set<DataSchemaNode> node3Children = node3.getChildNodes();
310 assertEquals(1, node3Children.size());
313 qnames = new QName[6];
317 qnames[3] = new QName(types1NS, types1Rev, t1, "odl");
320 qnames[4] = new QName(types1NS, types1Rev, t1, "id");
321 qnames[5] = new QName(types1NS, types1Rev, t1, "id");
322 LeafSchemaNode caseIdChild = (LeafSchemaNode)idChildren.iterator().next();
323 assertNotNull(caseIdChild);
324 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
325 assertEquals(expectedPath, caseIdChild.getPath());
328 qnames[4] = new QName(types1NS, types1Rev, t1, "node3");
329 qnames[5] = new QName(types1NS, types1Rev, t1, "node3");
330 ContainerSchemaNode caseNode3Child = (ContainerSchemaNode)node3Children.iterator().next();
331 assertNotNull(caseNode3Child);
332 expectedPath = new SchemaPath(Lists.newArrayList(qnames), true);
333 assertEquals(expectedPath, caseNode3Child.getPath());