Migrate YANG inputs for yang-model-util
[yangtools.git] / model / 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.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import org.junit.jupiter.api.BeforeEach;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.PathExpression;
23 import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.ResolvedQNameStep;
27
28 class YT1060Test {
29     private static final QName CONT = QName.create("parent", "cont");
30     private static final QName LEAF1 = QName.create(CONT, "leaf1");
31
32     private EffectiveModelContext context;
33     private PathExpression path;
34
35     @BeforeEach
36     void before() {
37         context = YangParserTestUtils.parseYang("""
38             submodule child {
39               belongs-to parent {
40                 prefix par;
41               }
42
43               import imported {
44                 prefix imp;
45               }
46
47               container cont {
48                 leaf leaf1 {
49                   type leafref {
50                     path "/imp:root/imp:leaf1";
51                   }
52                 }
53                 leaf leaf2 {
54                     type imp:foo;
55                 }
56               }
57             }""", """
58             module imported {
59               namespace "imported";
60               prefix imp;
61
62               typedef foo {
63                 type string {
64                   pattern 'S(\\d+G)?(\\d+M)?';
65                 }
66               }
67
68               container root {
69                 leaf leaf1 {
70                   type string;
71                 }
72               }
73             }""", """
74             module parent {
75               namespace "parent";
76               prefix par;
77
78               include child;
79             }""");
80
81         final var module = context.findModule(CONT.getModule()).orElseThrow();
82         final var cont = assertInstanceOf(ContainerSchemaNode.class, module.getDataChildByName(CONT));
83         final var leaf1 = assertInstanceOf(LeafSchemaNode.class, cont.getDataChildByName(LEAF1));
84         path = assertInstanceOf(LeafrefTypeDefinition.class, leaf1.getType()).getPathStatement();
85
86         // Quick checks before we get to the point
87         final var pathSteps = assertInstanceOf(LocationPathSteps.class, path.getSteps());
88         final var locationPath = pathSteps.getLocationPath();
89         assertTrue(locationPath.isAbsolute());
90         final var steps = locationPath.getSteps();
91         assertEquals(2, steps.size());
92         steps.forEach(step -> assertInstanceOf(ResolvedQNameStep.class, step));
93     }
94
95     @Test
96     void testFindDataSchemaNodeAbsolutePathImportedModule() {
97         final var foundStmt = assertInstanceOf(LeafSchemaNode.class,
98             SchemaInferenceStack.ofDataTreePath(context, CONT, LEAF1).resolvePathExpression(path));
99         assertEquals(QName.create(XMLNamespace.of("imported"), "leaf1"), foundStmt.getQName());
100
101         // since this is absolute path with prefixes stack should be able to resolve it from any state
102         assertSame(foundStmt, SchemaInferenceStack.of(context).resolvePathExpression(path));
103     }
104 }