Populate data/ hierarchy
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1060Test.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.assertEquals;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.ImmutableList;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.PathExpression;
26 import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
27 import org.opendaylight.yangtools.yang.model.api.PathExpression.Steps;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
32 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.ResolvedQNameStep;
33 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Step;
34
35 public class YT1060Test {
36     private static final QName CONT = QName.create("parent", "cont");
37     private static final QName LEAF1 = QName.create(CONT, "leaf1");
38
39     private EffectiveModelContext context;
40     private PathExpression path;
41
42     @Before
43     public void before() {
44         context = YangParserTestUtils.parseYangResourceDirectory("/yt1060");
45
46         final Module module = context.findModule(CONT.getModule()).get();
47         final ContainerSchemaNode cont = (ContainerSchemaNode) module.findDataChildByName(CONT).get();
48         final LeafSchemaNode leaf1 = (LeafSchemaNode) cont.findDataChildByName(LEAF1).get();
49         path = ((LeafrefTypeDefinition) leaf1.getType()).getPathStatement();
50
51         // Quick checks before we get to the point
52         final Steps pathSteps = path.getSteps();
53         assertThat(pathSteps, isA(LocationPathSteps.class));
54         final YangLocationPath locationPath = ((LocationPathSteps) pathSteps).getLocationPath();
55         assertTrue(locationPath.isAbsolute());
56         final ImmutableList<Step> steps = locationPath.getSteps();
57         assertEquals(2, steps.size());
58         steps.forEach(step -> assertThat(step, isA(ResolvedQNameStep.class)));
59     }
60
61     @Test
62     public void testFindDataSchemaNodeAbsolutePathImportedModule() {
63         final EffectiveStatement<?, ?> foundStmt = SchemaInferenceStack.ofDataTreePath(context, CONT, LEAF1)
64             .resolvePathExpression(path);
65         assertThat(foundStmt, isA(LeafSchemaNode.class));
66         assertEquals(QName.create(XMLNamespace.of("imported"), "leaf1"), ((LeafSchemaNode) foundStmt).getQName());
67
68         // since this is absolute path with prefixes stack should be able to resolve it from any state
69         final EffectiveStatement<?, ?> foundStmtSecond = SchemaInferenceStack.of(context).resolvePathExpression(path);
70         assertSame(foundStmt, foundStmtSecond);
71     }
72 }