Bug 4662: Introduce a SemanticVersion concept - pre-linkage phase
[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
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
26
27 public class ImportResolutionBasicTest {
28
29     private static final YangStatementSourceImpl ROOT_WITHOUT_IMPORT = new YangStatementSourceImpl(
30             "/semantic-statement-parser/import-arg-parsing/nature.yang", false);
31     private static final YangStatementSourceImpl IMPORT_ROOT = new YangStatementSourceImpl(
32             "/semantic-statement-parser/import-arg-parsing/mammal.yang", false);
33     private static final YangStatementSourceImpl IMPORT_DERIVED = new YangStatementSourceImpl(
34             "/semantic-statement-parser/import-arg-parsing/human.yang", false);
35     private static final YangStatementSourceImpl IMPORT_SELF = new YangStatementSourceImpl(
36             "/semantic-statement-parser/import-arg-parsing/egocentric.yang", false);
37     private static final YangStatementSourceImpl CYCLE_YIN = new YangStatementSourceImpl(
38             "/semantic-statement-parser/import-arg-parsing/cycle-yin.yang", false);
39     private static final YangStatementSourceImpl CYCLE_YANG = new YangStatementSourceImpl(
40             "/semantic-statement-parser/import-arg-parsing/cycle-yang.yang", false);
41     private static final YangStatementSourceImpl FOO = new YangStatementSourceImpl(
42             "/semantic-statement-parser/bug2649/foo.yang", false);
43     private static final YangStatementSourceImpl IMPORT = new YangStatementSourceImpl(
44             "/semantic-statement-parser/bug2649/import-module.yang", false);
45
46
47     @Test
48     public void inImportOrderTest() throws SourceException, ReactorException {
49         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
50         addSources(reactor, ROOT_WITHOUT_IMPORT, IMPORT_ROOT, IMPORT_DERIVED);
51         EffectiveModelContext result = reactor.build();
52         assertNotNull(result);
53     }
54
55     @Test
56     public void inInverseOfImportOrderTest() throws SourceException, ReactorException {
57         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         addSources(reactor, IMPORT_DERIVED, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
59         EffectiveModelContext result = reactor.build();
60         assertNotNull(result);
61     }
62
63     @Test
64     public void missingImportedSourceTest() throws SourceException {
65         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
66         addSources(reactor, 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() throws SourceException {
79         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
80         addSources(reactor, 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() throws SourceException {
92         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
93         addSources(reactor, 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 SourceException, ReactorException{
105         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
106         addSources(reactor, FOO, IMPORT);
107
108         EffectiveSchemaContext buildEffective = reactor.buildEffective();
109         assertNotNull(buildEffective);
110     }
111
112     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
113         for (YangStatementSourceImpl source : sources) {
114             reactor.addSource(source);
115         }
116     }
117
118 }