Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / 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.rfc7950.reactor.RFC7950Reactors;
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 = RFC7950Reactors.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 = RFC7950Reactors.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 = RFC7950Reactors.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 = RFC7950Reactors.defaultReactor().newBuild().addSources(CYCLE_YIN, CYCLE_YANG);
80         try {
81             reactor.build();
82             fail("reactor.process should fail due to circular import");
83         } catch (ReactorException e) {
84             assertTrue(e instanceof SomeModifiersUnresolvedException);
85             assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, e.getPhase());
86         }
87     }
88
89     @Test
90     public void selfImportTest() {
91         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
92                 .addSources(IMPORT_SELF, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
93         try {
94             reactor.build();
95             fail("reactor.process should fail due to self import");
96         } catch (ReactorException e) {
97             assertTrue(e instanceof SomeModifiersUnresolvedException);
98             assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, e.getPhase());
99         }
100     }
101
102     @Test
103     public void bug2649Test() throws ReactorException {
104         SchemaContext buildEffective = RFC7950Reactors.defaultReactor().newBuild()
105                 .addSources(FOO, IMPORT)
106                 .buildEffective();
107         assertNotNull(buildEffective);
108     }
109 }