Populate data/ hierarchy
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1050Test.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 import static org.junit.Assert.assertSame;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class YT1050Test {
28     private static final QName SECONDARY = QName.create("yt1050", "secondary");
29     private static final QName TYPE = QName.create(SECONDARY, "type");
30     private static final QName GRP_USES = QName.create(SECONDARY, "grp-uses");
31
32     private EffectiveModelContext context;
33     private LeafSchemaNode secondaryType;
34     private LeafSchemaNode primaryType;
35     private Module module;
36
37     @Before
38     public void before() {
39         context = YangParserTestUtils.parseYangResource("/yt1050.yang");
40         module = context.getModules().iterator().next();
41
42         final ListSchemaNode grpUses = (ListSchemaNode) module.findDataChildByName(GRP_USES).get();
43         primaryType = (LeafSchemaNode) grpUses.findDataChildByName(TYPE).get();
44
45         final GroupingDefinition grp = module.getGroupings().iterator().next();
46         secondaryType = (LeafSchemaNode) ((ListSchemaNode) grp.findDataChildByName(SECONDARY).get())
47                 .findDataChildByName(TYPE).get();
48     }
49
50     @Test
51     public void testFindDataSchemaNodeForRelativeXPathWithDeref() {
52         final TypeDefinition<?> typeNodeType = secondaryType.getType();
53         assertThat(typeNodeType, isA(LeafrefTypeDefinition.class));
54
55         final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
56         stack.enterGrouping(QName.create(module.getQNameModule(), "grp"));
57         stack.enterSchemaTree(QName.create(module.getQNameModule(), "secondary"));
58         stack.enterSchemaTree(secondaryType.getQName());
59         final EffectiveStatement<?, ?> found = stack.resolvePathExpression(((LeafrefTypeDefinition) typeNodeType)
60                 .getPathStatement());
61         assertSame(primaryType, found);
62     }
63 }