05ec86613e0dde903637d4c921486ec2070958bd
[yangtools.git] / yang / yang-parser-rfc7950 / 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.model.api.stmt.DescriptionStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
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(
46             "/rfc7950/include-import-stmt-test/valid-11");
47         assertNotNull(schemaContext);
48
49         // Test for valid include statement
50         final Module testModule = schemaContext.findModules("root-module").iterator().next();
51         assertNotNull(testModule);
52
53         // Test for valid import statement
54         ModuleImport importStmt = testModule.getImports().iterator().next();
55         assertEquals(Optional.of("Yang 1.1: Allow description and reference in include and import."),
56             importStmt.getDescription());
57         assertEquals(Optional.of("https://tools.ietf.org/html/rfc7950 section-7.1.5/6"),
58             importStmt.getReference());
59     }
60
61     @Test
62     public void invalid10IncludeStmtTest() throws Exception {
63         try {
64             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-include-10");
65             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for INCLUDE in Yang 1.0");
66         } catch (final SomeModifiersUnresolvedException e) {
67             final String msg = e.getCause().getMessage();
68             assertTrue(msg.startsWith("DESCRIPTION is not valid for INCLUDE")
69                 || msg.startsWith("REFERENCE is not valid for INCLUDE"));
70         }
71     }
72
73     @Test
74     public void invalid10ImportStmtTest() throws Exception {
75         try {
76             StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/invalid-import-10");
77             fail("Test must fail: DESCRIPTION/REFERENCE are not valid for IMPORT in Yang 1.0");
78         } catch (final SomeModifiersUnresolvedException e) {
79             final String msg = e.getCause().getMessage();
80             assertTrue(msg.startsWith("DESCRIPTION is not valid for IMPORT")
81                 || msg.startsWith("REFERENCE is not valid for IMPORT"));
82         }
83     }
84
85     @Test
86     public void descriptionAndReferenceTest11() throws ReactorException {
87         RFC7950Reactors.defaultReactor().newBuild()
88             .addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE)
89             .build().getRootStatements().forEach(declaredStmt -> {
90                 if (declaredStmt instanceof ModuleStatement) {
91                     declaredStmt.declaredSubstatements().forEach(subStmt -> {
92                         if (subStmt instanceof IncludeStatement && subStmt.rawArgument().equals("child-module")) {
93                             subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
94                         }
95                     });
96                 }
97             });
98     }
99
100     @SuppressWarnings("rawtypes")
101     private static void verifyDescAndRef(final DeclaredStatement stmt) {
102         if (stmt instanceof DescriptionStatement) {
103             assertEquals("Yang 1.1: Allow description and reference in include and import.",
104                 ((DescriptionStatement) stmt).argument());
105         }
106         if (stmt instanceof ReferenceStatement) {
107             assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatement) stmt).argument());
108         }
109     }
110 }