Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / IncludeResolutionTest.java
1 package org.opendaylight.yangtools.yang.stmt.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7 import java.util.logging.Logger;
8 import org.junit.Test;
9 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
10 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
11 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
12 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
13 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
14 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
15 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
16 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
17
18 public class IncludeResolutionTest {
19
20     private static final Logger log = Logger.getLogger(IncludeResolutionTest.class.getName());
21
22     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
23             "/semantic-statement-parser/include-arg-parsing/root-module.yang", false);
24     private static final YangStatementSourceImpl SUBMODULE1 = new YangStatementSourceImpl(
25             "/semantic-statement-parser/include-arg-parsing/submodule-1.yang", false);
26     private static final YangStatementSourceImpl SUBMODULE2 = new YangStatementSourceImpl(
27             "/semantic-statement-parser/include-arg-parsing/submodule-2.yang", false);
28     private static final YangStatementSourceImpl ERROR_MODULE = new YangStatementSourceImpl(
29             "/semantic-statement-parser/include-arg-parsing/error-module.yang", false);
30     private static final YangStatementSourceImpl ERROR_SUBMODULE = new YangStatementSourceImpl(
31             "/semantic-statement-parser/include-arg-parsing/error-submodule.yang", false);
32
33     private static final YangStatementSourceImpl MISSING_PARENT_MODULE = new YangStatementSourceImpl(
34             "/semantic-statement-parser/include-arg-parsing/missing-parent.yang", false);
35
36     @Test
37     public void includeTest() throws SourceException, ReactorException {
38         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
39         addSources(reactor, ROOT, SUBMODULE1, SUBMODULE2);
40         EffectiveModelContext result = reactor.build();
41         assertNotNull(result);
42     }
43
44     @Test
45     public void missingIncludedSourceTest() throws SourceException {
46         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
47         addSources(reactor, ERROR_MODULE);
48         try {
49             reactor.build();
50             fail("reactor.process should fail due to missing included source");
51         } catch (ReactorException e) {
52             assertTrue(e instanceof SomeModifiersUnresolvedException);
53             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
54             log.info(e.getMessage());
55         }
56
57     }
58
59     @Test
60     public void missingIncludedSourceTest2() throws SourceException {
61         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
62         addSources(reactor, ERROR_SUBMODULE);
63         try {
64             reactor.build();
65             fail("reactor.process should fail due to missing included source");
66         } catch (ReactorException e) {
67             assertTrue(e instanceof SomeModifiersUnresolvedException);
68             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
69             log.info(e.getMessage());
70         }
71
72     }
73
74     @Test
75     public void missingIncludedSourceTest3() throws SourceException, ReactorException {
76         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
77         addSources(reactor, MISSING_PARENT_MODULE);
78         try {
79             reactor.build();
80             fail("reactor.process should fail due to missing belongsTo source");
81         } catch (ReactorException e) {
82             log.info(e.getMessage());
83         }
84
85     }
86
87     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
88         for (YangStatementSourceImpl source : sources) {
89             reactor.addSource(source);
90         }
91     }
92 }