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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.DescriptionStatementImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IncludeStatementImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ModuleStatementImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ReferenceStatementImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
30 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
32 public class Bug6874Test {
34 private static final StatementStreamSource ROOT_MODULE = sourceForResource(
35 "/rfc7950/include-import-stmt-test/valid-11/root-module.yang");
36 private static final StatementStreamSource CHILD_MODULE = sourceForResource(
37 "/rfc7950/include-import-stmt-test/valid-11/child-module.yang");
38 private static final StatementStreamSource CHILD_MODULE_1 = sourceForResource(
39 "/rfc7950/include-import-stmt-test/valid-11/child-module-1.yang");
40 private static final StatementStreamSource IMPORTED_MODULE = sourceForResource(
41 "/rfc7950/include-import-stmt-test/valid-11/imported-module.yang");
44 public void valid11Test() throws Exception {
45 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
46 "/rfc7950/include-import-stmt-test/valid-11");
47 assertNotNull(schemaContext);
49 // Test for valid include statement
50 final Module testModule = schemaContext.findModuleByName("root-module", null);
51 assertNotNull(testModule);
53 // Test for valid import statement
54 ModuleImport importStmt = testModule.getImports().iterator().next();
55 assertEquals("Yang 1.1: Allow description and reference in include and import.", importStmt.getDescription());
56 assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", importStmt.getReference());
60 public void invalid10IncludeStmtTest() throws Exception {
62 StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-include-10");
63 fail("Test must fail: DESCRIPTION/REFERENCE are not valid for INCLUDE in Yang 1.0");
64 } catch (final SomeModifiersUnresolvedException e) {
65 final String msg = e.getCause().getMessage();
66 assertTrue(msg.startsWith("DESCRIPTION is not valid for INCLUDE")
67 || msg.startsWith("REFERENCE is not valid for INCLUDE"));
72 public void invalid10ImportStmtTest() throws Exception {
74 StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-import-10");
75 fail("Test must fail: DESCRIPTION/REFERENCE are not valid for IMPORT in Yang 1.0");
76 } catch (final SomeModifiersUnresolvedException e) {
77 final String msg = e.getCause().getMessage();
78 assertTrue(msg.startsWith("DESCRIPTION is not valid for IMPORT")
79 || msg.startsWith("REFERENCE is not valid for IMPORT"));
84 public void descriptionAndReferenceTest11() throws ReactorException {
85 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
86 reactor.addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE);
88 reactor.build().getRootStatements().forEach(declaredStmt -> {
89 if (declaredStmt instanceof ModuleStatementImpl) {
90 declaredStmt.declaredSubstatements().forEach(subStmt -> {
91 if (subStmt instanceof IncludeStatementImpl
92 && subStmt.rawArgument().equals("child-module")) {
93 subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
100 @SuppressWarnings("rawtypes")
101 private static void verifyDescAndRef(final DeclaredStatement stmt) {
102 if (stmt instanceof DescriptionStatementImpl) {
103 assertEquals("Yang 1.1: Allow description and reference in include and import.",
104 ((DescriptionStatementImpl) stmt).argument());
106 if (stmt instanceof ReferenceStatementImpl) {
107 assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatementImpl) stmt).argument());