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