a672030f0dd7f0fbee100d0d25afaba81ea62528
[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 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;
31
32 public class Bug6874Test {
33
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");
42
43     @Test
44     public void valid11Test() throws Exception {
45         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/valid-11");
46         assertNotNull(schemaContext);
47
48         // Test for valid include statement
49         final Module testModule = schemaContext.findModuleByName("root-module", null);
50         assertNotNull(testModule);
51
52         // Test for valid import statement
53         ModuleImport importStmt = testModule.getImports().iterator().next();
54         assertEquals("Yang 1.1: Allow description and reference in include and import.", importStmt.getDescription());
55         assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", importStmt.getReference());
56     }
57
58     @Test
59     public void invalid10IncludeStmtTest() throws Exception {
60         try {
61             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-include-10");
62             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for INCLUDE in Yang 1.0");
63         } catch (final SomeModifiersUnresolvedException e) {
64             final String msg = e.getCause().getMessage();
65             assertTrue(msg.startsWith("DESCRIPTION is not valid for INCLUDE")
66                 || msg.startsWith("REFERENCE is not valid for INCLUDE"));
67         }
68     }
69
70     @Test
71     public void invalid10ImportStmtTest() throws Exception {
72         try {
73             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-import-10");
74             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for IMPORT in Yang 1.0");
75         } catch (final SomeModifiersUnresolvedException e) {
76             final String msg = e.getCause().getMessage();
77             assertTrue(msg.startsWith("DESCRIPTION is not valid for IMPORT")
78                 || msg.startsWith("REFERENCE is not valid for IMPORT"));
79         }
80     }
81
82     @Test
83     public void descriptionAndReferenceTest11() throws ReactorException {
84         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85         reactor.addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE);
86
87         reactor.build().getRootStatements().forEach(declaredStmt -> {
88             if(declaredStmt instanceof ModuleStatementImpl) {
89                 declaredStmt.declaredSubstatements().forEach(subStmt -> {
90                     if (subStmt instanceof IncludeStatementImpl &&
91                             subStmt.rawArgument().equals("child-module")) {
92                         subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
93                     }
94                 });
95             }
96         });
97     }
98
99     @SuppressWarnings("rawtypes")
100     private static void verifyDescAndRef (final DeclaredStatement stmt) {
101         if (stmt instanceof DescriptionStatementImpl) {
102             assertEquals("Yang 1.1: Allow description and reference in include and import.",
103                 ((DescriptionStatementImpl) stmt).argument());
104         }
105         if (stmt instanceof ReferenceStatementImpl) {
106             assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatementImpl) stmt).argument());
107         }
108     }
109 }