6d09dfe0088e2195059ad176970cc886f61cd82e
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5481Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
25
26 public class Bug5481Test {
27     @Test
28     public void test() throws Exception {
29         SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5481");
30         assertNotNull(context);
31
32         ContainerSchemaNode topContainer = verifyTopContainer(context);
33         verifyExtendedLeaf(topContainer);
34     }
35
36     private static ContainerSchemaNode verifyTopContainer(final SchemaContext context) {
37         QName top = QName.create("http://example.com/module1", "2016-03-09", "top");
38         DataSchemaNode dataChildByName = context.getDataChildByName(top);
39         assertTrue(dataChildByName instanceof ContainerSchemaNode);
40
41         ContainerSchemaNode topContainer = (ContainerSchemaNode) dataChildByName;
42         RevisionAwareXPath whenConditionTopContainer = topContainer.getConstraints().getWhenCondition();
43
44         assertNull(whenConditionTopContainer);
45         assertEquals(Status.CURRENT, topContainer.getStatus());
46         assertNull(topContainer.getDescription());
47         assertNull(topContainer.getReference());
48         return topContainer;
49     }
50
51     private static void verifyExtendedLeaf(final ContainerSchemaNode topContainer) {
52         DataSchemaNode dataChildByName2 = topContainer.getDataChildByName(QName.create("http://example.com/module2",
53                 "2016-03-09", "extended-leaf"));
54         assertTrue(dataChildByName2 instanceof LeafSchemaNode);
55
56         LeafSchemaNode extendedLeaf = (LeafSchemaNode) dataChildByName2;
57         RevisionAwareXPath whenConditionExtendedLeaf = extendedLeaf.getConstraints().getWhenCondition();
58
59         assertEquals(new RevisionAwareXPathImpl("module1:top = 'extended'", false), whenConditionExtendedLeaf);
60         assertEquals(Status.DEPRECATED, extendedLeaf.getStatus());
61         assertEquals("text", extendedLeaf.getDescription());
62         assertEquals("ref", extendedLeaf.getReference());
63     }
64 }