Populate data/ hierarchy
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT588Test.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.model.util;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.isA;
12
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class YT588Test {
26     private static final String NS = "foo";
27     private static final String REV = "2016-03-01";
28
29     @Test
30     public void test() {
31         EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt588.yang");
32
33         QName root = QName.create(NS, REV, "root");
34         QName leafRef2 = QName.create(NS, REV, "leaf-ref-2");
35         QName conGrp = QName.create(NS, REV, "con-grp");
36         QName leafRef = QName.create(NS, REV, "leaf-ref");
37
38         SchemaNode findDataSchemaNode = context.findDataTreeChild(root, conGrp, leafRef).get();
39         SchemaNode findDataSchemaNode2 = context.findDataTreeChild(root, leafRef2).get();
40         assertThat(findDataSchemaNode, isA(LeafSchemaNode.class));
41         assertThat(findDataSchemaNode2, isA(LeafSchemaNode.class));
42
43         LeafSchemaNode leafRefNode = (LeafSchemaNode) findDataSchemaNode;
44         LeafSchemaNode leafRefNode2 = (LeafSchemaNode) findDataSchemaNode2;
45
46         assertThat(leafRefNode.getType(), isA(LeafrefTypeDefinition.class));
47         assertThat(leafRefNode2.getType(), isA(LeafrefTypeDefinition.class));
48
49         EffectiveStatement<?, ?> found = SchemaInferenceStack.ofDataTreePath(context, root, conGrp, leafRef)
50                 .resolvePathExpression(((LeafrefTypeDefinition) leafRefNode.getType()).getPathStatement());
51         assertThat(((TypedDataSchemaNode)found).getType(), isA(BinaryTypeDefinition.class));
52
53         found = SchemaInferenceStack.ofDataTreePath(context, root, leafRef2)
54             .resolvePathExpression(((LeafrefTypeDefinition) leafRefNode2.getType()).getPathStatement());
55         assertThat(((TypedDataSchemaNode)found).getType(), isA(Int16TypeDefinition.class));
56     }
57 }