2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt.test;
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;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
27 public class ImportResolutionBasicTest {
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);
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);
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);
64 public void missingImportedSourceTest() throws SourceException {
65 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
66 addSources(reactor, IMPORT_DERIVED, ROOT_WITHOUT_IMPORT);
69 fail("reactor.process should fail due to missing imported source");
70 } catch (ReactorException e) {
71 assertTrue(e instanceof SomeModifiersUnresolvedException);
72 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
78 public void circularImportsTest() throws SourceException {
79 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
80 addSources(reactor, CYCLE_YIN, CYCLE_YANG);
83 fail("reactor.process should fail due to circular import");
84 } catch (ReactorException e) {
85 assertTrue(e instanceof SomeModifiersUnresolvedException);
86 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
91 public void selfImportTest() throws SourceException {
92 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
93 addSources(reactor, IMPORT_SELF, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
96 fail("reactor.process should fail due to self import");
97 } catch (ReactorException e) {
98 assertTrue(e instanceof SomeModifiersUnresolvedException);
99 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
104 public void bug2649Test() throws SourceException, ReactorException{
105 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
106 addSources(reactor, FOO, IMPORT);
108 EffectiveSchemaContext buildEffective = reactor.buildEffective();
109 assertNotNull(buildEffective);
112 private void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
113 for (YangStatementSourceImpl source : sources) {
114 reactor.addSource(source);