2 * Copyright (c) 2016 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
8 package org.opendaylight.yangtools.yang.stmt.yin;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.net.URISyntaxException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
28 import org.opendaylight.yangtools.yang.stmt.TestUtils;
30 public class YinFileStmtTest {
32 private static final YinStatementSourceImpl YIN_FILE = new YinStatementSourceImpl
33 ("/semantic-statement-parser/yin/test.xml", false);
34 private static final YinStatementSourceImpl EXT_FILE = new YinStatementSourceImpl
35 ("/semantic-statement-parser/yin/extension.xml", false);
36 private static final YinStatementSourceImpl EXT_USE_FILE = new YinStatementSourceImpl
37 ("/semantic-statement-parser/yin/extension-use.xml", false);
38 private static final YinStatementSourceImpl INVALID_YIN_FILE = new YinStatementSourceImpl
39 ("/semantic-statement-parser/yin/incorrect-foo.xml", false);
40 private static final YinStatementSourceImpl INVALID_YIN_FILE_2 = new YinStatementSourceImpl
41 ("/semantic-statement-parser/yin/incorrect-bar.xml", false);
43 private Set<Module> modules;
46 public void init() throws URISyntaxException, ReactorException {
47 modules = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
51 public void readAndParseYinFileTestModel() throws SourceException, ReactorException {
52 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
53 addSources(reactor, YIN_FILE, EXT_FILE, EXT_USE_FILE);
54 EffectiveSchemaContext result = reactor.buildEffective();
55 assertNotNull(result);
58 // parsing yin file whose import statement references a module which does not exist
59 @Test(expected = SomeModifiersUnresolvedException.class)
60 public void readAndParseInvalidYinFileTest() throws ReactorException {
61 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
62 addSources(reactor, INVALID_YIN_FILE);
63 EffectiveSchemaContext result = reactor.buildEffective();
64 assertNotNull(result);
67 // parsing yin file with duplicate key name in a list statement
68 public void readAndParseInvalidYinFileTest2() {
69 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
70 addSources(reactor, INVALID_YIN_FILE_2);
73 reactor.buildEffective();
74 fail("Reactor exception should have been thrown");
75 } catch (ReactorException e) {
76 final Throwable cause = e.getCause();
77 assertTrue(cause instanceof SourceException);
78 assertTrue(cause.getMessage().startsWith(
79 "Key argument 'testing-string testing-string' contains duplicates"));
84 public void testModulesSize() {
85 assertEquals(modules.size(), 9);
88 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final
89 StatementStreamSource... sources) {
90 for (StatementStreamSource source : sources) {
91 reactor.addSource(source);