Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / IncludeRevisionsTest.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 org.junit.Test;
8 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
9 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
10 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
11 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
12 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
13 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
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 IncludeRevisionsTest {
19
20     private static final YangStatementSourceImpl EQUAL_ROOT = new YangStatementSourceImpl("/revisions/equal-root.yang",
21             false);
22     private static final YangStatementSourceImpl EQUAL_REV = new YangStatementSourceImpl("/revisions/equal-rev.yang",
23             false);
24     private static final YangStatementSourceImpl UNEQUAL_ROOT = new YangStatementSourceImpl(
25             "/revisions/unequal-root.yang", false);
26     private static final YangStatementSourceImpl UNEQUAL_REV = new YangStatementSourceImpl(
27             "/revisions/unequal-rev.yang", false);
28     private static final YangStatementSourceImpl SUBMOD_ONLY_ROOT = new YangStatementSourceImpl(
29             "/revisions/submod-only-root.yang", false);
30     private static final YangStatementSourceImpl SUBMOD_ONLY_REV = new YangStatementSourceImpl(
31             "/revisions/submod-only-rev.yang", false);
32     private static final YangStatementSourceImpl MOD_ONLY_ROOT = new YangStatementSourceImpl(
33             "/revisions/mod-only-root.yang", false);
34     private static final YangStatementSourceImpl MOD_ONLY_REV = new YangStatementSourceImpl(
35             "/revisions/mod-only-rev.yang", false);
36     private static final YangStatementSourceImpl MOD_ONLY_1970_ROOT = new YangStatementSourceImpl(
37             "/revisions/mod-1970-root.yang", false);
38     private static final YangStatementSourceImpl MOD_ONLY_1970_REV = new YangStatementSourceImpl(
39             "/revisions/mod-1970-rev.yang", false);
40     private static final YangStatementSourceImpl NOWHERE_ROOT = new YangStatementSourceImpl(
41             "/revisions/nowhere-root.yang", false);
42     private static final YangStatementSourceImpl NOWHERE_REV = new YangStatementSourceImpl(
43             "/revisions/nowhere-rev.yang", false);
44
45     @Test
46     public void revsEqualTest() throws SourceException, ReactorException {
47
48         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         addSources(reactor, EQUAL_REV, EQUAL_ROOT);
50
51         EffectiveModelContext result = reactor.build();
52         assertNotNull(result);
53     }
54
55     @Test
56     public void revsUnequalTest() throws SourceException, ReactorException {
57
58         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59         addSources(reactor, UNEQUAL_REV, UNEQUAL_ROOT);
60
61         try {
62             reactor.build();
63             fail("reactor.process should fail due to unequal revisions in include and submodule");
64         } catch (ReactorException e) {
65             assertTrue(e instanceof SomeModifiersUnresolvedException);
66             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
67         }
68     }
69
70     @Test
71     public void revIncludeOnly() throws SourceException, ReactorException {
72
73         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
74         addSources(reactor, SUBMOD_ONLY_REV, SUBMOD_ONLY_ROOT);
75
76         EffectiveModelContext result = reactor.build();
77         assertNotNull(result);
78     }
79
80     @Test
81     public void revInModuleOnly() throws SourceException, ReactorException {
82
83         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
84         addSources(reactor, MOD_ONLY_REV, MOD_ONLY_ROOT);
85
86         try {
87             reactor.build();
88             fail("reactor.process should fail due to missing revision in included submodule");
89         } catch (ReactorException e) {
90             assertTrue(e instanceof SomeModifiersUnresolvedException);
91             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
92         }
93     }
94
95     @Test
96     public void rev1970InModuleOnlyTest() throws SourceException, ReactorException {
97
98         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
99         addSources(reactor, MOD_ONLY_1970_REV, MOD_ONLY_1970_ROOT);
100
101         EffectiveModelContext result = reactor.build();
102         assertNotNull(result);
103     }
104
105     @Test
106     public void revNowhereTest() throws SourceException, ReactorException {
107
108         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
109         addSources(reactor, NOWHERE_REV, NOWHERE_ROOT);
110
111         EffectiveModelContext result = reactor.build();
112         assertNotNull(result);
113     }
114
115     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final StatementStreamSource... sources) {
116         for (StatementStreamSource source : sources) {
117             reactor.addSource(source);
118         }
119     }
120 }