Move SimpleSchemaContext
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1100Test.java
1 /*
2  * Copyright (c) 2020 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.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.PathExpression;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 public class YT1100Test {
29     @Test
30     public void testChoiceCaseRelativeLeafref() {
31         final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt1100.yang");
32         final Module module = context.findModule("yt1100").orElseThrow();
33         final QNameModule qnm = module.getQNameModule();
34         final DataSchemaNode leaf = module.findDataTreeChild(
35             QName.create(qnm, "foo"), QName.create(qnm, "scheduler-node"), QName.create(qnm, "child-scheduler-nodes"),
36             QName.create(qnm, "name")).orElseThrow();
37         assertThat(leaf, instanceOf(LeafSchemaNode.class));
38
39         final TypeDefinition<?> type = ((LeafSchemaNode) leaf).getType();
40         assertThat(type, instanceOf(LeafrefTypeDefinition.class));
41         final PathExpression leafref = ((LeafrefTypeDefinition) type).getPathStatement();
42
43         final SchemaNode ref = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf, leafref);
44         assertThat(ref, instanceOf(LeafSchemaNode.class));
45         final LeafSchemaNode targetLeaf = (LeafSchemaNode) ref;
46         assertEquals(QName.create(qnm, "name"), targetLeaf.getQName());
47         assertThat(targetLeaf.getType(), instanceOf(StringTypeDefinition.class));
48     }
49 }