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.io.IOException;
16 import java.net.URISyntaxException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
29 import org.opendaylight.yangtools.yang.stmt.TestUtils;
30 import org.xml.sax.SAXException;
32 public class YinFileStmtTest {
34 private static final StatementStreamSource YIN_FILE = createSource("test.yin");
35 private static final StatementStreamSource EXT_FILE = createSource("extension.yin");
36 private static final StatementStreamSource EXT_USE_FILE = createSource("extension-use.yin");
37 private static final StatementStreamSource INVALID_YIN_FILE = createSource("incorrect-foo.yin");
38 private static final StatementStreamSource INVALID_YIN_FILE_2 = createSource("incorrect-bar.yin");
40 private SchemaContext context;
42 private static StatementStreamSource createSource(final String name) {
44 return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
45 YinTextSchemaSource.forResource(YinFileStmtTest.class, "/semantic-statement-parser/yin/" + name)));
46 } catch (SAXException | IOException e) {
47 throw new IllegalArgumentException(e);
52 public void init() throws URISyntaxException, ReactorException, SAXException, IOException {
53 context = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
57 public void readAndParseYinFileTestModel() throws ReactorException {
58 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
59 .addSources(YIN_FILE, EXT_FILE, EXT_USE_FILE)
61 assertNotNull(result);
64 // parsing yin file whose import statement references a module which does not exist
65 @Test(expected = SomeModifiersUnresolvedException.class)
66 public void readAndParseInvalidYinFileTest() throws ReactorException {
67 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
68 .addSource(INVALID_YIN_FILE)
70 assertNotNull(result);
73 // parsing yin file with duplicate key name in a list statement
74 public void readAndParseInvalidYinFileTest2() {
75 BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(INVALID_YIN_FILE_2);
78 reactor.buildEffective();
79 fail("Reactor exception should have been thrown");
80 } catch (ReactorException e) {
81 final Throwable cause = e.getCause();
82 assertTrue(cause instanceof SourceException);
83 assertTrue(cause.getMessage().startsWith(
84 "Key argument 'testing-string testing-string' contains duplicates"));
89 public void testModulesSize() {
90 assertEquals(context.getModules().size(), 9);