2 * Copyright (c) 2016 Inocybe Technologies 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.stmt.rfc7950;
10 import static org.hamcrest.CoreMatchers.anyOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16 import java.util.Optional;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
30 class Bug6874Test extends AbstractYangTest {
31 private static final StatementStreamSource ROOT_MODULE = sourceForResource(
32 "/rfc7950/include-import-stmt-test/valid-11/root-module.yang");
33 private static final StatementStreamSource CHILD_MODULE = sourceForResource(
34 "/rfc7950/include-import-stmt-test/valid-11/child-module.yang");
35 private static final StatementStreamSource CHILD_MODULE_1 = sourceForResource(
36 "/rfc7950/include-import-stmt-test/valid-11/child-module-1.yang");
37 private static final StatementStreamSource IMPORTED_MODULE = sourceForResource(
38 "/rfc7950/include-import-stmt-test/valid-11/imported-module.yang");
42 final var schemaContext = assertEffectiveModelDir("/rfc7950/include-import-stmt-test/valid-11");
44 // Test for valid include statement
45 final Module testModule = schemaContext.findModules("root-module").iterator().next();
46 assertNotNull(testModule);
48 // Test for valid import statement
49 ModuleImport importStmt = testModule.getImports().iterator().next();
50 assertEquals(Optional.of("Yang 1.1: Allow description and reference in include and import."),
51 importStmt.getDescription());
52 assertEquals(Optional.of("https://tools.ietf.org/html/rfc7950 section-7.1.5/6"),
53 importStmt.getReference());
57 void invalid10IncludeStmtTest() {
58 assertInvalidSubstatementExceptionDir("/rfc7950/include-import-stmt-test/invalid-include-10", anyOf(
59 startsWith("DESCRIPTION is not valid for INCLUDE"),
60 startsWith("REFERENCE is not valid for INCLUDE")));
64 void invalid10ImportStmtTest() {
65 assertInvalidSubstatementExceptionDir("/rfc7950/include-import-stmt-test/invalid-import-10", anyOf(
66 startsWith("DESCRIPTION is not valid for IMPORT"),
67 startsWith("REFERENCE is not valid for IMPORT")));
71 void descriptionAndReferenceTest11() throws ReactorException {
72 RFC7950Reactors.defaultReactor().newBuild()
73 .addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE)
76 .forEach(declaredStmt -> {
77 if (declaredStmt instanceof ModuleStatement) {
78 declaredStmt.declaredSubstatements().forEach(subStmt -> {
79 if (subStmt instanceof IncludeStatement && "child-module".equals(subStmt.rawArgument())) {
80 subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
87 private static void verifyDescAndRef(final DeclaredStatement<?> stmt) {
88 if (stmt instanceof DescriptionStatement) {
89 assertEquals("Yang 1.1: Allow description and reference in include and import.",
90 ((DescriptionStatement) stmt).argument());
92 if (stmt instanceof ReferenceStatement) {
93 assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatement) stmt).argument());