2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
15 import java.util.Iterator;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationInText;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
26 public class YT1193Test {
28 public void testDeclarationReference() throws Exception {
29 final List<DeclaredStatement<?>> declaredRoots = new DefaultYangParserFactory()
30 .createParser(YangParserConfiguration.builder().retainDeclarationReferences(true).build())
31 .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/foo.yang"))
32 .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/bar.yang"))
33 .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/baz.yang"))
34 .buildDeclaredModel();
35 assertEquals(3, declaredRoots.size());
37 for (DeclaredStatement<?> stmt : declaredRoots) {
38 switch (stmt.rawArgument()) {
40 assertFooReferences(stmt);
43 assertBarReferences(stmt);
46 assertBazReferences(stmt);
49 throw new IllegalStateException("Unexpected statement " + stmt);
54 private static void assertFooReferences(final DeclaredStatement<?> foo) {
55 assertReference(foo, YangStmtMapping.MODULE, 1, 1);
57 final Iterator<? extends DeclaredStatement<?>> it = foo.declaredSubstatements().iterator();
58 assertReference(it.next(), YangStmtMapping.NAMESPACE, 2, 3);
59 assertReference(it.next(), YangStmtMapping.PREFIX, 3, 3);
60 assertReference(it.next(), YangStmtMapping.YANG_VERSION, 4, 3);
61 assertReference(it.next(), YangStmtMapping.REVISION, 6, 3);
62 assertReference(it.next(), YangStmtMapping.ORGANIZATION, 8, 3);
63 assertReference(it.next(), YangStmtMapping.DESCRIPTION, 9, 3);
64 assertReference(it.next(), YangStmtMapping.REFERENCE, 10, 3);
65 assertReference(it.next(), YangStmtMapping.CONTACT, 11, 3);
66 assertFooContainerReferences(it.next());
67 assertReference(it.next(), YangStmtMapping.RPC, 25, 3);
68 assertReference(it.next(), YangStmtMapping.NOTIFICATION, 26, 3);
69 assertDeprLeafListReferences(it.next());
70 assertObsoTypedefReferences(it.next());
71 assertFalse(it.hasNext());
74 private static void assertFooContainerReferences(final DeclaredStatement<?> foo) {
75 assertReference(foo, YangStmtMapping.CONTAINER, 13, 3);
77 final Iterator<? extends DeclaredStatement<?>> it = foo.declaredSubstatements().iterator();
78 assertReference(it.next(), YangStmtMapping.ACTION, 14, 5);
79 assertReference(it.next(), YangStmtMapping.PRESENCE, 22, 5);
80 assertFalse(it.hasNext());
83 private static void assertDeprLeafListReferences(final DeclaredStatement<?> depr) {
84 assertReference(depr, YangStmtMapping.LEAF_LIST, 28, 3);
86 final Iterator<? extends DeclaredStatement<?>> it = depr.declaredSubstatements().iterator();
87 assertReference(it.next(), YangStmtMapping.TYPE, 29, 5);
88 assertReference(it.next(), YangStmtMapping.UNITS, 36, 5);
89 assertReference(it.next(), YangStmtMapping.STATUS, 37, 5);
90 assertFalse(it.hasNext());
93 private static void assertObsoTypedefReferences(final DeclaredStatement<?> obso) {
94 assertReference(obso, YangStmtMapping.TYPEDEF, 40, 3);
96 final Iterator<? extends DeclaredStatement<?>> it = obso.declaredSubstatements().iterator();
97 assertReference(it.next(), YangStmtMapping.TYPE, 41, 5);
98 assertReference(it.next(), YangStmtMapping.STATUS, 44, 5);
99 assertFalse(it.hasNext());
102 private static void assertBarReferences(final DeclaredStatement<?> bar) {
103 assertReference(bar, YangStmtMapping.MODULE, 1, 1);
105 final Iterator<? extends DeclaredStatement<?>> it = bar.declaredSubstatements().iterator();
106 assertReference(it.next(), YangStmtMapping.NAMESPACE, 2, 3);
107 assertReference(it.next(), YangStmtMapping.PREFIX, 3, 3);
108 assertReference(it.next(), YangStmtMapping.YANG_VERSION, 4, 3);
109 assertReference(it.next(), YangStmtMapping.IMPORT, 6, 3);
110 assertReference(it.next(), YangStmtMapping.IDENTITY, 11, 3);
111 assertReference(it.next(), YangStmtMapping.IDENTITY, 13, 3);
112 assertReference(it.next(), YangStmtMapping.ANYDATA, 17, 3);
113 assertReference(it.next(), YangStmtMapping.ANYXML, 18, 3);
114 assertReference(it.next(), YangStmtMapping.INCLUDE, 20, 3);
115 assertFalse(it.hasNext());
118 private static void assertBazReferences(final DeclaredStatement<?> baz) {
119 assertReference(baz, YangStmtMapping.SUBMODULE, 1, 1);
121 final Iterator<? extends DeclaredStatement<?>> it = baz.declaredSubstatements().iterator();
122 assertReference(it.next(), YangStmtMapping.BELONGS_TO, 2, 3);
123 assertReference(it.next(), YangStmtMapping.EXTENSION, 6, 3);
124 assertFalse(it.hasNext());
127 private static void assertReference(final DeclaredStatement<?> foo, final StatementDefinition def, final int line,
129 assertEquals(def, foo.statementDefinition());
131 final DeclarationReference ref = foo.declarationReference().orElseThrow();
132 assertThat(ref, instanceOf(DeclarationInText.class));
133 final DeclarationInText inText = (DeclarationInText) ref;
135 assertEquals(line, inText.startLine());
136 assertEquals(column, inText.startColumn());