Bug 2366: new parser API extensions support implemented
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / ImportResolutionBasicTest.java
1 /*
2  * Copyright (c) 2015 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.test;
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
25 public class ImportResolutionBasicTest {
26
27     private static final ImportBasicTestStatementSource ROOT_WITHOUT_IMPORT = new ImportBasicTestStatementSource("nature");
28     private static final ImportBasicTestStatementSource IMPORT_ROOT = new ImportBasicTestStatementSource("mammal","nature");
29     private static final ImportBasicTestStatementSource IMPORT_DERIVED = new ImportBasicTestStatementSource("human", "mammal");
30     private static final ImportBasicTestStatementSource IMPORT_SELF = new ImportBasicTestStatementSource("egocentric", "egocentric");
31     private static final ImportBasicTestStatementSource CICLE_YIN = new ImportBasicTestStatementSource("cycle-yin", "cycle-yang");
32     private static final ImportBasicTestStatementSource CICLE_YANG = new ImportBasicTestStatementSource("cycle-yang", "cycle-yin");
33
34
35     @Test
36     public void inImportOrderTest() throws SourceException, ReactorException {
37         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
38         addSources(reactor,ROOT_WITHOUT_IMPORT,IMPORT_ROOT,IMPORT_DERIVED);
39         EffectiveModelContext result = reactor.build();
40         assertNotNull(result);
41     }
42
43     @Test
44     public void inInverseOfImportOrderTest() throws SourceException, ReactorException {
45         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
46         addSources(reactor,IMPORT_DERIVED,IMPORT_ROOT,ROOT_WITHOUT_IMPORT);
47         EffectiveModelContext result = reactor.build();
48         assertNotNull(result);
49     }
50
51     @Test
52     public void missingImportedSourceTest() throws SourceException {
53         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
54         addSources(reactor,IMPORT_DERIVED,ROOT_WITHOUT_IMPORT);
55         try {
56             reactor.build();
57             fail("reactor.process should fail doe to misssing imported source");
58         } catch (ReactorException e) {
59             assertTrue(e instanceof SomeModifiersUnresolvedException);
60             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE,e.getPhase());
61         }
62
63     }
64
65     @Test
66     public void circularImportsTest() throws SourceException {
67         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
68         addSources(reactor,CICLE_YIN,CICLE_YANG);
69         try {
70             reactor.build();
71             fail("reactor.process should fail doe to circular import");
72         } catch (ReactorException e) {
73             assertTrue(e instanceof SomeModifiersUnresolvedException);
74             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE,e.getPhase());
75         }
76     }
77
78     @Test
79     public void selfImportTest() throws SourceException {
80         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
81         addSources(reactor,IMPORT_SELF,IMPORT_ROOT,ROOT_WITHOUT_IMPORT);
82         try {
83             reactor.build();
84             fail("reactor.process should fail doe to self import");
85         } catch (ReactorException e) {
86             assertTrue(e instanceof SomeModifiersUnresolvedException);
87             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE,e.getPhase());
88         }
89     }
90
91
92     private void addSources(BuildAction reactor, ImportBasicTestStatementSource... sources) {
93         for(ImportBasicTestStatementSource source : sources) {
94             reactor.addSource(source);
95         }
96     }
97
98 }