Migrate yang-model-util to JUnit5
[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.parseYangResourceDirectory("/yt1060");
38
39         final var module = context.findModule(CONT.getModule()).orElseThrow();
40         final var cont = assertInstanceOf(ContainerSchemaNode.class, module.getDataChildByName(CONT));
41         final var leaf1 = assertInstanceOf(LeafSchemaNode.class, cont.getDataChildByName(LEAF1));
42         path = assertInstanceOf(LeafrefTypeDefinition.class, leaf1.getType()).getPathStatement();
43
44         // Quick checks before we get to the point
45         final var pathSteps = assertInstanceOf(LocationPathSteps.class, path.getSteps());
46         final var locationPath = pathSteps.getLocationPath();
47         assertTrue(locationPath.isAbsolute());
48         final var steps = locationPath.getSteps();
49         assertEquals(2, steps.size());
50         steps.forEach(step -> assertInstanceOf(ResolvedQNameStep.class, step));
51     }
52
53     @Test
54     void testFindDataSchemaNodeAbsolutePathImportedModule() {
55         final var foundStmt = assertInstanceOf(LeafSchemaNode.class,
56             SchemaInferenceStack.ofDataTreePath(context, CONT, LEAF1).resolvePathExpression(path));
57         assertEquals(QName.create(XMLNamespace.of("imported"), "leaf1"), foundStmt.getQName());
58
59         // since this is absolute path with prefixes stack should be able to resolve it from any state
60         assertSame(foundStmt, SchemaInferenceStack.of(context).resolvePathExpression(path));
61     }
62 }