Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / 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
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 java.util.logging.Logger;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
27
28 public class IncludeResolutionTest {
29
30     private static final Logger log = Logger.getLogger(IncludeResolutionTest.class.getName());
31
32     private static final StatementStreamSource ROOT = sourceForResource(
33             "/semantic-statement-parser/include-arg-parsing/root-module.yang");
34     private static final StatementStreamSource SUBMODULE1 = sourceForResource(
35             "/semantic-statement-parser/include-arg-parsing/submodule-1.yang");
36     private static final StatementStreamSource SUBMODULE2 = sourceForResource(
37             "/semantic-statement-parser/include-arg-parsing/submodule-2.yang");
38     private static final StatementStreamSource ERROR_MODULE = sourceForResource(
39             "/semantic-statement-parser/include-arg-parsing/error-module.yang");
40     private static final StatementStreamSource ERROR_SUBMODULE = sourceForResource(
41             "/semantic-statement-parser/include-arg-parsing/error-submodule.yang");
42
43     private static final StatementStreamSource MISSING_PARENT_MODULE = sourceForResource(
44             "/semantic-statement-parser/include-arg-parsing/missing-parent.yang");
45
46     @Test
47     public void includeTest() throws SourceException, ReactorException {
48         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         reactor.addSources(ROOT, SUBMODULE1, SUBMODULE2);
50         EffectiveModelContext result = reactor.build();
51         assertNotNull(result);
52     }
53
54     @Test
55     public void missingIncludedSourceTest() throws SourceException {
56         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57         reactor.addSources(ERROR_MODULE);
58         try {
59             reactor.build();
60             fail("reactor.process should fail due to missing included source");
61         } catch (ReactorException e) {
62             assertTrue(e instanceof SomeModifiersUnresolvedException);
63             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
64             log.info(e.getMessage());
65         }
66
67     }
68
69     @Test
70     public void missingIncludedSourceTest2() throws SourceException {
71         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
72         reactor.addSources(ERROR_SUBMODULE);
73         try {
74             reactor.build();
75             fail("reactor.process should fail due to missing included source");
76         } catch (ReactorException e) {
77             assertTrue(e instanceof SomeModifiersUnresolvedException);
78             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
79             log.info(e.getMessage());
80         }
81
82     }
83
84     @Test
85     public void missingIncludedSourceTest3() throws SourceException, ReactorException {
86         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87         reactor.addSources(MISSING_PARENT_MODULE);
88         try {
89             reactor.build();
90             fail("reactor.process should fail due to missing belongsTo source");
91         } catch (ReactorException e) {
92             log.info(e.getMessage());
93         }
94
95     }
96 }