Cleanup DocumentedNode
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6874Test.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.yang.parser.stmt.rfc7950;
9
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;
15
16 import java.util.Optional;
17 import org.junit.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.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.DescriptionStatementImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IncludeStatementImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ModuleStatementImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ReferenceStatementImpl;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
32
33 public class Bug6874Test {
34
35     private static final StatementStreamSource ROOT_MODULE = sourceForResource(
36         "/rfc7950/include-import-stmt-test/valid-11/root-module.yang");
37     private static final StatementStreamSource CHILD_MODULE = sourceForResource(
38         "/rfc7950/include-import-stmt-test/valid-11/child-module.yang");
39     private static final StatementStreamSource CHILD_MODULE_1 = sourceForResource(
40         "/rfc7950/include-import-stmt-test/valid-11/child-module-1.yang");
41     private static final StatementStreamSource IMPORTED_MODULE = sourceForResource(
42         "/rfc7950/include-import-stmt-test/valid-11/imported-module.yang");
43
44     @Test
45     public void valid11Test() throws Exception {
46         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
47             "/rfc7950/include-import-stmt-test/valid-11");
48         assertNotNull(schemaContext);
49
50         // Test for valid include statement
51         final Module testModule = schemaContext.findModules("root-module").iterator().next();
52         assertNotNull(testModule);
53
54         // Test for valid import statement
55         ModuleImport importStmt = testModule.getImports().iterator().next();
56         assertEquals(Optional.of("Yang 1.1: Allow description and reference in include and import."),
57             importStmt.getDescription());
58         assertEquals(Optional.of("https://tools.ietf.org/html/rfc7950 section-7.1.5/6"),
59             importStmt.getReference());
60     }
61
62     @Test
63     public void invalid10IncludeStmtTest() throws Exception {
64         try {
65             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-include-10");
66             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for INCLUDE in Yang 1.0");
67         } catch (final SomeModifiersUnresolvedException e) {
68             final String msg = e.getCause().getMessage();
69             assertTrue(msg.startsWith("DESCRIPTION is not valid for INCLUDE")
70                 || msg.startsWith("REFERENCE is not valid for INCLUDE"));
71         }
72     }
73
74     @Test
75     public void invalid10ImportStmtTest() throws Exception {
76         try {
77             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-import-10");
78             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for IMPORT in Yang 1.0");
79         } catch (final SomeModifiersUnresolvedException e) {
80             final String msg = e.getCause().getMessage();
81             assertTrue(msg.startsWith("DESCRIPTION is not valid for IMPORT")
82                 || msg.startsWith("REFERENCE is not valid for IMPORT"));
83         }
84     }
85
86     @Test
87     public void descriptionAndReferenceTest11() throws ReactorException {
88         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
89         reactor.addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE);
90
91         reactor.build().getRootStatements().forEach(declaredStmt -> {
92             if (declaredStmt instanceof ModuleStatementImpl) {
93                 declaredStmt.declaredSubstatements().forEach(subStmt -> {
94                     if (subStmt instanceof IncludeStatementImpl
95                             && subStmt.rawArgument().equals("child-module")) {
96                         subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
97                     }
98                 });
99             }
100         });
101     }
102
103     @SuppressWarnings("rawtypes")
104     private static void verifyDescAndRef(final DeclaredStatement stmt) {
105         if (stmt instanceof DescriptionStatementImpl) {
106             assertEquals("Yang 1.1: Allow description and reference in include and import.",
107                 ((DescriptionStatementImpl) stmt).argument());
108         }
109         if (stmt instanceof ReferenceStatementImpl) {
110             assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatementImpl) stmt).argument());
111         }
112     }
113 }