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.instanceOf;
12 import static org.hamcrest.CoreMatchers.startsWith;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThrows;
17 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
19 import java.util.Optional;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
35 public class Bug6874Test {
37 private static final StatementStreamSource ROOT_MODULE = sourceForResource(
38 "/rfc7950/include-import-stmt-test/valid-11/root-module.yang");
39 private static final StatementStreamSource CHILD_MODULE = sourceForResource(
40 "/rfc7950/include-import-stmt-test/valid-11/child-module.yang");
41 private static final StatementStreamSource CHILD_MODULE_1 = sourceForResource(
42 "/rfc7950/include-import-stmt-test/valid-11/child-module-1.yang");
43 private static final StatementStreamSource IMPORTED_MODULE = sourceForResource(
44 "/rfc7950/include-import-stmt-test/valid-11/imported-module.yang");
47 public void valid11Test() throws Exception {
48 final var schemaContext = StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/valid-11");
49 assertNotNull(schemaContext);
51 // Test for valid include statement
52 final Module testModule = schemaContext.findModules("root-module").iterator().next();
53 assertNotNull(testModule);
55 // Test for valid import statement
56 ModuleImport importStmt = testModule.getImports().iterator().next();
57 assertEquals(Optional.of("Yang 1.1: Allow description and reference in include and import."),
58 importStmt.getDescription());
59 assertEquals(Optional.of("https://tools.ietf.org/html/rfc7950 section-7.1.5/6"),
60 importStmt.getReference());
64 public void invalid10IncludeStmtTest() {
65 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
66 () -> StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-include-10")).getCause();
67 assertThat(ex, instanceOf(InvalidSubstatementException.class));
68 assertThat(ex.getMessage(), anyOf(
69 startsWith("DESCRIPTION is not valid for INCLUDE"),
70 startsWith("REFERENCE is not valid for INCLUDE")));
74 public void invalid10ImportStmtTest() throws Exception {
75 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
76 () -> StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-import-10")).getCause();
77 assertThat(ex, instanceOf(InvalidSubstatementException.class));
78 assertThat(ex.getMessage(), anyOf(
79 startsWith("DESCRIPTION is not valid for IMPORT"),
80 startsWith("REFERENCE is not valid for IMPORT")));
84 public void descriptionAndReferenceTest11() throws ReactorException {
85 RFC7950Reactors.defaultReactor().newBuild()
86 .addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE)
87 .build().getRootStatements().forEach(declaredStmt -> {
88 if (declaredStmt instanceof ModuleStatement) {
89 declaredStmt.declaredSubstatements().forEach(subStmt -> {
90 if (subStmt instanceof IncludeStatement && "child-module".equals(subStmt.rawArgument())) {
91 subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
98 @SuppressWarnings("rawtypes")
99 private static void verifyDescAndRef(final DeclaredStatement stmt) {
100 if (stmt instanceof DescriptionStatement) {
101 assertEquals("Yang 1.1: Allow description and reference in include and import.",
102 ((DescriptionStatement) stmt).argument());
104 if (stmt instanceof ReferenceStatement) {
105 assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatement) stmt).argument());