Rename opendaylight.mdsal.binding.runtime.spi
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / IncludeResolutionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertNull;
16 import static org.junit.jupiter.api.Assertions.assertThrows;
17 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18
19 import java.util.concurrent.Callable;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28
29 class IncludeResolutionTest {
30     private static final StatementStreamSource ROOT = sourceForResource(
31         "/semantic-statement-parser/include-arg-parsing/root-module.yang");
32     private static final StatementStreamSource SUBMODULE1 = sourceForResource(
33         "/semantic-statement-parser/include-arg-parsing/submodule-1.yang");
34     private static final StatementStreamSource SUBMODULE2 = sourceForResource(
35         "/semantic-statement-parser/include-arg-parsing/submodule-2.yang");
36     private static final StatementStreamSource ERROR_MODULE = sourceForResource(
37         "/semantic-statement-parser/include-arg-parsing/error-module.yang");
38     private static final StatementStreamSource ERROR_SUBMODULE = sourceForResource(
39         "/semantic-statement-parser/include-arg-parsing/error-submodule.yang");
40     private static final StatementStreamSource ERROR_SUBMODULE_ROOT = sourceForResource(
41         "/semantic-statement-parser/include-arg-parsing/error-submodule-root.yang");
42     private static final StatementStreamSource MISSING_PARENT_MODULE = sourceForResource(
43         "/semantic-statement-parser/include-arg-parsing/missing-parent.yang");
44
45     @Test
46     void includeTest() throws ReactorException {
47         var result = RFC7950Reactors.defaultReactor().newBuild()
48             .addSources(ROOT, SUBMODULE1, SUBMODULE2)
49             .build();
50         assertNotNull(result);
51     }
52
53     @Test
54     void missingIncludedSourceTest() {
55         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(ERROR_MODULE);
56         assertNull(assertFailedSourceLinkage(reactor::build, "Included submodule 'foo' was not found [at ").getCause());
57     }
58
59     @Test
60     void missingIncludedSourceTest2() {
61         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(ERROR_SUBMODULE, ERROR_SUBMODULE_ROOT);
62         var cause = assertFailedSourceLinkage(reactor::build, "Included submodule 'foo' was not found [at ");
63         assertNull(cause.getCause());
64     }
65
66     @Test
67     void missingIncludedSourceTest3() throws SourceException, ReactorException {
68         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(MISSING_PARENT_MODULE);
69         assertNull(assertFailedSourceLinkage(reactor::build,
70             "Module 'Unqualified{localName=foo}' from belongs-to was not found [at ").getCause());
71     }
72
73     private static InferenceException assertFailedSourceLinkage(final Callable<?> callable, final String startStr) {
74         final var ex = assertThrows(SomeModifiersUnresolvedException.class, callable::call);
75         assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, ex.getPhase());
76         var cause = assertInstanceOf(InferenceException.class, ex.getCause());
77         assertThat(cause.getMessage(), startsWith(startStr));
78         return cause;
79     }
80 }