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