062e2cd62b7123b7086cb68c2edfc59f6c63c0c0
[yangtools.git] / parser / yang-parser-rfc7950 / 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Optional;
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.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
24
25 public class Bug5481Test {
26     @Test
27     public void test() throws Exception {
28         SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5481");
29         assertNotNull(context);
30
31         ContainerSchemaNode topContainer = verifyTopContainer(context);
32         verifyExtendedLeaf(topContainer);
33     }
34
35     private static ContainerSchemaNode verifyTopContainer(final SchemaContext context) {
36         QName top = QName.create("http://example.com/module1", "2016-03-09", "top");
37         DataSchemaNode dataChildByName = context.getDataChildByName(top);
38         assertTrue(dataChildByName instanceof ContainerSchemaNode);
39
40         ContainerSchemaNode topContainer = (ContainerSchemaNode) dataChildByName;
41
42         assertFalse(topContainer.getWhenCondition().isPresent());
43         assertEquals(Status.CURRENT, topContainer.getStatus());
44         assertFalse(topContainer.getDescription().isPresent());
45         assertFalse(topContainer.getReference().isPresent());
46         return topContainer;
47     }
48
49     private static void verifyExtendedLeaf(final ContainerSchemaNode topContainer) {
50         DataSchemaNode dataChildByName2 = topContainer.getDataChildByName(QName.create("http://example.com/module2",
51                 "2016-03-09", "extended-leaf"));
52         assertTrue(dataChildByName2 instanceof LeafSchemaNode);
53
54         LeafSchemaNode extendedLeaf = (LeafSchemaNode) dataChildByName2;
55         assertEquals(Status.DEPRECATED, extendedLeaf.getStatus());
56         assertEquals(Optional.of("text"), extendedLeaf.getDescription());
57         assertEquals(Optional.of("ref"), extendedLeaf.getReference());
58
59         QualifiedBound whenConditionExtendedLeaf = extendedLeaf.getWhenCondition().orElseThrow();
60         assertEquals("module1:top = 'extended'", whenConditionExtendedLeaf.toString());
61     }
62 }