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