d0dffbf3323ded1c2d77824c7486dfa54f3f6e35
[yangtools.git] / parser / 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.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;
18
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;
34
35 public class Bug6874Test {
36
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");
45
46     @Test
47     public void valid11Test() throws Exception {
48         final var schemaContext = StmtTestUtils.parseYangSources("/rfc7950/include-import-stmt-test/valid-11");
49         assertNotNull(schemaContext);
50
51         // Test for valid include statement
52         final Module testModule = schemaContext.findModules("root-module").iterator().next();
53         assertNotNull(testModule);
54
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());
61     }
62
63     @Test
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")));
71     }
72
73     @Test
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")));
81     }
82
83     @Test
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);
92                         }
93                     });
94                 }
95             });
96     }
97
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());
103         }
104         if (stmt instanceof ReferenceStatement) {
105             assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatement) stmt).argument());
106         }
107     }
108 }