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 StatementStreamSource YIN_FILE = createSource("test.yin");
33 private static final StatementStreamSource EXT_FILE = createSource("extension.yin");
34 private static final StatementStreamSource EXT_USE_FILE = createSource("extension-use.yin");
35 private static final StatementStreamSource INVALID_YIN_FILE = createSource("incorrect-foo.yin");
36 private static final StatementStreamSource INVALID_YIN_FILE_2 = createSource("incorrect-bar.yin");
38 private Set<Module> modules;
40 private static StatementStreamSource createSource(final String name) {
41 return new YinStatementSourceImpl("/semantic-statement-parser/yin/" + name, false);
45 public void init() throws URISyntaxException, ReactorException {
46 modules = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
50 public void readAndParseYinFileTestModel() throws SourceException, ReactorException {
51 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
52 addSources(reactor, YIN_FILE, EXT_FILE, EXT_USE_FILE);
53 EffectiveSchemaContext result = reactor.buildEffective();
54 assertNotNull(result);
57 // parsing yin file whose import statement references a module which does not exist
58 @Test(expected = SomeModifiersUnresolvedException.class)
59 public void readAndParseInvalidYinFileTest() throws ReactorException {
60 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
61 addSources(reactor, INVALID_YIN_FILE);
62 EffectiveSchemaContext result = reactor.buildEffective();
63 assertNotNull(result);
66 // parsing yin file with duplicate key name in a list statement
67 public void readAndParseInvalidYinFileTest2() {
68 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
69 addSources(reactor, INVALID_YIN_FILE_2);
72 reactor.buildEffective();
73 fail("Reactor exception should have been thrown");
74 } catch (ReactorException e) {
75 final Throwable cause = e.getCause();
76 assertTrue(cause instanceof SourceException);
77 assertTrue(cause.getMessage().startsWith(
78 "Key argument 'testing-string testing-string' contains duplicates"));
83 public void testModulesSize() {
84 assertEquals(modules.size(), 9);
87 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final
88 StatementStreamSource... sources) {
89 for (StatementStreamSource source : sources) {
90 reactor.addSource(source);