Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / yin / YinFileStmtTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt.yin;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15
16 import java.io.IOException;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.model.spi.source.YinTextSource;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
26 import org.xml.sax.SAXException;
27
28 class YinFileStmtTest {
29
30     private static final StatementStreamSource YIN_FILE = createSource("test.yin");
31     private static final StatementStreamSource EXT_FILE = createSource("extension.yin");
32     private static final StatementStreamSource EXT_USE_FILE = createSource("extension-use.yin");
33     private static final StatementStreamSource INVALID_YIN_FILE = createSource("incorrect-foo.yin");
34     private static final StatementStreamSource INVALID_YIN_FILE_2 = createSource("incorrect-bar.yin");
35
36     private static StatementStreamSource createSource(final String name) {
37         try {
38             return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
39                 YinTextSource.forResource(YinFileStmtTest.class, "/semantic-statement-parser/yin/" + name)));
40         } catch (SAXException | IOException e) {
41             throw new IllegalArgumentException(e);
42         }
43     }
44
45     @Test
46     void readAndParseYinFileTestModel() throws ReactorException {
47         assertNotNull(RFC7950Reactors.defaultReactor().newBuild()
48             .addSources(YIN_FILE, EXT_FILE, EXT_USE_FILE)
49             .buildEffective());
50     }
51
52     // parsing yin file whose import statement references a module which does not exist
53     @Test
54     void readAndParseInvalidYinFileTest() {
55         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(INVALID_YIN_FILE);
56         assertThrows(SomeModifiersUnresolvedException.class, reactor::buildEffective);
57     }
58
59     // parsing yin file with duplicate key name in a list statement
60     @Test
61     void readAndParseInvalidYinFileTest2() {
62         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(INVALID_YIN_FILE_2);
63         final var cause = assertThrows(SomeModifiersUnresolvedException.class, reactor::buildEffective).getCause();
64         assertInstanceOf(SourceException.class, cause);
65         assertThat(cause.getMessage(), startsWith("Key argument 'testing-string testing-string' contains duplicates"));
66     }
67 }