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;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
26 public class ImportResolutionBasicTest {
28 private static final YangStatementSourceImpl ROOT_WITHOUT_IMPORT = new YangStatementSourceImpl(
29 "/semantic-statement-parser/import-arg-parsing/nature.yang", false);
30 private static final YangStatementSourceImpl IMPORT_ROOT = new YangStatementSourceImpl(
31 "/semantic-statement-parser/import-arg-parsing/mammal.yang", false);
32 private static final YangStatementSourceImpl IMPORT_DERIVED = new YangStatementSourceImpl(
33 "/semantic-statement-parser/import-arg-parsing/human.yang", false);
34 private static final YangStatementSourceImpl IMPORT_SELF = new YangStatementSourceImpl(
35 "/semantic-statement-parser/import-arg-parsing/egocentric.yang", false);
36 private static final YangStatementSourceImpl CYCLE_YIN = new YangStatementSourceImpl(
37 "/semantic-statement-parser/import-arg-parsing/cycle-yin.yang", false);
38 private static final YangStatementSourceImpl CYCLE_YANG = new YangStatementSourceImpl(
39 "/semantic-statement-parser/import-arg-parsing/cycle-yang.yang", false);
40 private static final YangStatementSourceImpl FOO = new YangStatementSourceImpl(
41 "/semantic-statement-parser/bug2649/foo.yang", false);
42 private static final YangStatementSourceImpl IMPORT = new YangStatementSourceImpl(
43 "/semantic-statement-parser/bug2649/import-module.yang", false);
47 public void inImportOrderTest() throws SourceException, ReactorException {
48 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49 addSources(reactor, ROOT_WITHOUT_IMPORT, IMPORT_ROOT, IMPORT_DERIVED);
50 EffectiveModelContext result = reactor.build();
51 assertNotNull(result);
55 public void inInverseOfImportOrderTest() throws SourceException, ReactorException {
56 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57 addSources(reactor, IMPORT_DERIVED, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
58 EffectiveModelContext result = reactor.build();
59 assertNotNull(result);
63 public void missingImportedSourceTest() throws SourceException {
64 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
65 addSources(reactor, IMPORT_DERIVED, ROOT_WITHOUT_IMPORT);
68 fail("reactor.process should fail due to missing imported source");
69 } catch (ReactorException e) {
70 assertTrue(e instanceof SomeModifiersUnresolvedException);
71 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
77 public void circularImportsTest() throws SourceException {
78 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
79 addSources(reactor, CYCLE_YIN, CYCLE_YANG);
82 fail("reactor.process should fail due to circular import");
83 } catch (ReactorException e) {
84 assertTrue(e instanceof SomeModifiersUnresolvedException);
85 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
90 public void selfImportTest() throws SourceException {
91 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
92 addSources(reactor, IMPORT_SELF, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
95 fail("reactor.process should fail due to self import");
96 } catch (ReactorException e) {
97 assertTrue(e instanceof SomeModifiersUnresolvedException);
98 assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
103 public void bug2649Test() throws SourceException, ReactorException{
104 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
105 addSources(reactor, FOO, IMPORT);
107 EffectiveSchemaContext buildEffective = reactor.buildEffective();
108 assertNotNull(buildEffective);
111 private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
112 for (YangStatementSourceImpl source : sources) {
113 reactor.addSource(source);