Further cleanup of tests
[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.startsWith;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
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.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
29
30 public class Bug6874Test extends AbstractYangTest {
31     private static final StatementStreamSource ROOT_MODULE = sourceForResource(
32         "/rfc7950/include-import-stmt-test/valid-11/root-module.yang");
33     private static final StatementStreamSource CHILD_MODULE = sourceForResource(
34         "/rfc7950/include-import-stmt-test/valid-11/child-module.yang");
35     private static final StatementStreamSource CHILD_MODULE_1 = sourceForResource(
36         "/rfc7950/include-import-stmt-test/valid-11/child-module-1.yang");
37     private static final StatementStreamSource IMPORTED_MODULE = sourceForResource(
38         "/rfc7950/include-import-stmt-test/valid-11/imported-module.yang");
39
40     @Test
41     public void valid11Test() {
42         final var schemaContext = assertEffectiveModelDir("/rfc7950/include-import-stmt-test/valid-11");
43
44         // Test for valid include statement
45         final Module testModule = schemaContext.findModules("root-module").iterator().next();
46         assertNotNull(testModule);
47
48         // Test for valid import statement
49         ModuleImport importStmt = testModule.getImports().iterator().next();
50         assertEquals(Optional.of("Yang 1.1: Allow description and reference in include and import."),
51             importStmt.getDescription());
52         assertEquals(Optional.of("https://tools.ietf.org/html/rfc7950 section-7.1.5/6"),
53             importStmt.getReference());
54     }
55
56     @Test
57     public void invalid10IncludeStmtTest() {
58         assertInvalidSubstatementExceptionDir("/rfc7950/include-import-stmt-test/invalid-include-10", anyOf(
59             startsWith("DESCRIPTION is not valid for INCLUDE"),
60             startsWith("REFERENCE is not valid for INCLUDE")));
61     }
62
63     @Test
64     public void invalid10ImportStmtTest() {
65         assertInvalidSubstatementExceptionDir("/rfc7950/include-import-stmt-test/invalid-import-10", anyOf(
66             startsWith("DESCRIPTION is not valid for IMPORT"),
67             startsWith("REFERENCE is not valid for IMPORT")));
68     }
69
70     @Test
71     public void descriptionAndReferenceTest11() throws ReactorException {
72         RFC7950Reactors.defaultReactor().newBuild()
73             .addSources(ROOT_MODULE, CHILD_MODULE, CHILD_MODULE_1, IMPORTED_MODULE)
74             .build().getRootStatements().forEach(declaredStmt -> {
75                 if (declaredStmt instanceof ModuleStatement) {
76                     declaredStmt.declaredSubstatements().forEach(subStmt -> {
77                         if (subStmt instanceof IncludeStatement && "child-module".equals(subStmt.rawArgument())) {
78                             subStmt.declaredSubstatements().forEach(Bug6874Test::verifyDescAndRef);
79                         }
80                     });
81                 }
82             });
83     }
84
85     private static void verifyDescAndRef(final DeclaredStatement<?> stmt) {
86         if (stmt instanceof DescriptionStatement) {
87             assertEquals("Yang 1.1: Allow description and reference in include and import.",
88                 ((DescriptionStatement) stmt).argument());
89         }
90         if (stmt instanceof ReferenceStatement) {
91             assertEquals("https://tools.ietf.org/html/rfc7950 section-7.1.5/6", ((ReferenceStatement) stmt).argument());
92         }
93     }
94 }