Validate cross-revision imports and includes
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / YT1339Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.stmt;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertThrows;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.YangVersion;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException;
19
20 /**
21  * Tests for {@code MUST NOT} statements around include/import interop of RFC6020 and RFC7950, as per
22  * <a href="https://datatracker.ietf.org/doc/html/rfc7950#section-12">RFC7950 section 12</a>.
23  */
24 public class YT1339Test {
25     @Test
26     public void testInclude() {
27         // A YANG version 1.1 module MUST NOT include a YANG version 1 submodule,
28         assertFailedInclude("old-sub", YangVersion.VERSION_1, YangVersion.VERSION_1_1);
29         // ... and a YANG version 1 module MUST NOT include a YANG version 1.1 submodule
30         assertFailedInclude("new-sub", YangVersion.VERSION_1_1, YangVersion.VERSION_1);
31     }
32
33     @Test
34     public void testImportNewByRev() throws Exception {
35         // A YANG version 1 module or submodule MUST NOT import a YANG version 1.1 module by revision.
36         assertFailedImport("import-rev");
37         assertFailedImport("import-rev-sub");
38     }
39
40     @Test
41     public void testImportOldByRev() throws Exception {
42         // A YANG version 1.1 module or submodule MAY import a YANG version 1 module by revision.
43         compile("import");
44     }
45
46     @Test
47     public void testImportNoRev() throws Exception {
48         // no language forbidding imports without revision
49         compile("import-norev");
50     }
51
52     private static void assertFailedImport(final String subdir) {
53         assertThat(assertYangVersionLinkageException(subdir),
54             startsWith("Cannot import by revision version 1.1 module new [at "));
55     }
56
57     private static void assertFailedInclude(final String subdir, final YangVersion subVer, final YangVersion modVer) {
58         assertThat(assertYangVersionLinkageException(subdir),
59             startsWith("Cannot include a version " + subVer + " submodule in a version " + modVer + " module [at "));
60     }
61
62     private static String assertYangVersionLinkageException(final String subdir) {
63         final var cause = assertThrows(ReactorException.class, () -> compile(subdir)).getCause();
64         assertThat(cause, instanceOf(YangVersionLinkageException.class));
65         return cause.getMessage();
66     }
67
68     private static void compile(final String subdir) throws Exception {
69         StmtTestUtils.parseYangSources("/bugs/YT1339/" + subdir);
70     }
71 }