Promote SchemaSourceRepresentation
[yangtools.git] / parser / odlext-parser-support / src / test / java / org / opendaylight / yangtools / odlext / parser / ContextReferenceTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.odlext.parser;
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
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yangtools.odlext.model.api.ContextInstanceEffectiveStatement;
16 import org.opendaylight.yangtools.odlext.model.api.ContextReferenceEffectiveStatement;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
25 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
29
30 class ContextReferenceTest {
31     private static final QNameModule FOO = QNameModule.create(XMLNamespace.of("foo"));
32     private static final QName LEAF_TYPE = QName.create(FOO, "leaf-type");
33     private static final QName LIST_TYPE = QName.create(FOO, "list-type");
34
35     @Test
36     void test() throws Exception {
37         final var reactor = RFC7950Reactors.vanillaReactorBuilder()
38             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
39                 new ContextInstanceStatementSupport(YangParserConfiguration.DEFAULT))
40             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
41                 new ContextReferenceStatementSupport(YangParserConfiguration.DEFAULT))
42             .build();
43
44         final var foo = reactor.newBuild()
45             .addSource(YangStatementStreamSource.create(YangTextSource.forResource(
46                 ContextReferenceTest.class, "/yang-ext.yang")))
47             .addSource(YangStatementStreamSource.create(YangTextSource.forResource(
48                 ContextReferenceTest.class, "/ctxref.yang")))
49             .buildEffective()
50             .getModuleStatements()
51             .get(FOO);
52
53         final var list = assertInstanceOf(ListEffectiveStatement.class,
54             foo.findDataTreeNode(QName.create(FOO, "list")).orElseThrow());
55
56         final var listType = list.findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
57         assertEquals(LIST_TYPE, listType.argument());
58         assertEquals(LIST_TYPE, listType.contextType().argument());
59
60         final var leafType = list
61             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
62             .findFirstEffectiveSubstatement(ContextInstanceEffectiveStatement.class).orElseThrow();
63         assertEquals(LEAF_TYPE, leafType.argument());
64         assertEquals(LEAF_TYPE, leafType.contextType().argument());
65
66         final var groupings = foo.streamEffectiveSubstatements(GroupingEffectiveStatement.class).toList();
67         assertEquals(2, groupings.size());
68
69         final var listRef = groupings.get(1)
70             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow()
71             .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
72         assertEquals(LIST_TYPE, listType.argument());
73         assertSame(listType.contextType(), listRef.contextType());
74
75         final var leafRef = groupings.get(0)
76             .findFirstEffectiveSubstatement(LeafListEffectiveStatement.class).orElseThrow()
77             .findFirstEffectiveSubstatement(ContextReferenceEffectiveStatement.class).orElseThrow();
78         assertEquals(LEAF_TYPE, leafType.argument());
79         assertSame(leafType.contextType(), leafRef.contextType());
80     }
81 }