Use local variable type inference
[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.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.isA;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.IOException;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
28 import org.xml.sax.SAXException;
29
30 public class YinFileStmtTest {
31
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");
37
38     private static StatementStreamSource createSource(final String name) {
39         try {
40             return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
41                 YinTextSchemaSource.forResource(YinFileStmtTest.class, "/semantic-statement-parser/yin/" + name)));
42         } catch (SAXException | IOException e) {
43             throw new IllegalArgumentException(e);
44         }
45     }
46
47     @Test
48     public void readAndParseYinFileTestModel() throws ReactorException {
49         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
50                 .addSources(YIN_FILE, EXT_FILE, EXT_USE_FILE)
51                 .buildEffective();
52         assertNotNull(result);
53     }
54
55     // parsing yin file whose import statement references a module which does not exist
56     @Test(expected = SomeModifiersUnresolvedException.class)
57     public void readAndParseInvalidYinFileTest() throws ReactorException {
58         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
59                 .addSource(INVALID_YIN_FILE)
60                 .buildEffective();
61         assertNotNull(result);
62     }
63
64     // parsing yin file with duplicate key name in a list statement
65     @Test
66     public void readAndParseInvalidYinFileTest2() {
67         BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(INVALID_YIN_FILE_2);
68
69         try {
70             reactor.buildEffective();
71             fail("Reactor exception should have been thrown");
72         } catch (ReactorException e) {
73             final Throwable cause = e.getCause();
74             assertThat(cause, isA(SourceException.class));
75             assertTrue(cause.getMessage().startsWith(
76                 "Key argument 'testing-string testing-string' contains duplicates"));
77         }
78     }
79 }