YANGTOOLS-706: Refactor YangInferencePipeline
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / ImportResolutionBasicTest.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.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.parser.impl.DefaultReactors;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
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
27 public class ImportResolutionBasicTest {
28
29     private static final StatementStreamSource ROOT_WITHOUT_IMPORT = sourceForResource(
30             "/semantic-statement-parser/import-arg-parsing/nature.yang");
31     private static final StatementStreamSource IMPORT_ROOT = sourceForResource(
32             "/semantic-statement-parser/import-arg-parsing/mammal.yang");
33     private static final StatementStreamSource IMPORT_DERIVED = sourceForResource(
34             "/semantic-statement-parser/import-arg-parsing/human.yang");
35     private static final StatementStreamSource IMPORT_SELF = sourceForResource(
36             "/semantic-statement-parser/import-arg-parsing/egocentric.yang");
37     private static final StatementStreamSource CYCLE_YIN = sourceForResource(
38             "/semantic-statement-parser/import-arg-parsing/cycle-yin.yang");
39     private static final StatementStreamSource CYCLE_YANG = sourceForResource(
40             "/semantic-statement-parser/import-arg-parsing/cycle-yang.yang");
41     private static final StatementStreamSource FOO = sourceForResource(
42             "/semantic-statement-parser/bug2649/foo.yang");
43     private static final StatementStreamSource IMPORT = sourceForResource(
44             "/semantic-statement-parser/bug2649/import-module.yang");
45
46
47     @Test
48     public void inImportOrderTest() throws ReactorException {
49         EffectiveModelContext result = DefaultReactors.defaultReactor().newBuild()
50                 .addSources(ROOT_WITHOUT_IMPORT, IMPORT_ROOT, IMPORT_DERIVED)
51                 .build();
52         assertNotNull(result);
53     }
54
55     @Test
56     public void inInverseOfImportOrderTest() throws ReactorException {
57         EffectiveModelContext result = DefaultReactors.defaultReactor().newBuild()
58                 .addSources(IMPORT_DERIVED, IMPORT_ROOT, ROOT_WITHOUT_IMPORT)
59                 .build();
60         assertNotNull(result);
61     }
62
63     @Test
64     public void missingImportedSourceTest() {
65         BuildAction reactor = DefaultReactors.defaultReactor().newBuild()
66                 .addSources(IMPORT_DERIVED, ROOT_WITHOUT_IMPORT);
67         try {
68             reactor.build();
69             fail("reactor.process should fail due to missing imported source");
70         } catch (ReactorException e) {
71             assertTrue(e instanceof SomeModifiersUnresolvedException);
72             assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, e.getPhase());
73         }
74
75     }
76
77     @Test
78     public void circularImportsTest() {
79         BuildAction reactor = DefaultReactors.defaultReactor().newBuild()
80                 .addSources(CYCLE_YIN, CYCLE_YANG);
81         try {
82             reactor.build();
83             fail("reactor.process should fail due to circular import");
84         } catch (ReactorException e) {
85             assertTrue(e instanceof SomeModifiersUnresolvedException);
86             assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, e.getPhase());
87         }
88     }
89
90     @Test
91     public void selfImportTest() {
92         BuildAction reactor = DefaultReactors.defaultReactor().newBuild()
93                 .addSources(IMPORT_SELF, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
94         try {
95             reactor.build();
96             fail("reactor.process should fail due to self import");
97         } catch (ReactorException e) {
98             assertTrue(e instanceof SomeModifiersUnresolvedException);
99             assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, e.getPhase());
100         }
101     }
102
103     @Test
104     public void bug2649Test() throws ReactorException {
105         SchemaContext buildEffective = DefaultReactors.defaultReactor().newBuild()
106                 .addSources(FOO, IMPORT)
107                 .buildEffective();
108         assertNotNull(buildEffective);
109     }
110 }