Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
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;
31
32 public class YinFileStmtTest {
33
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");
39
40     private SchemaContext context;
41
42     private static StatementStreamSource createSource(final String name) {
43         try {
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);
48         }
49     }
50
51     @Before
52     public void init() throws URISyntaxException, ReactorException, SAXException, IOException {
53         context = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
54     }
55
56     @Test
57     public void readAndParseYinFileTestModel() throws ReactorException {
58         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
59                 .addSources(YIN_FILE, EXT_FILE, EXT_USE_FILE)
60                 .buildEffective();
61         assertNotNull(result);
62     }
63
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)
69                 .buildEffective();
70         assertNotNull(result);
71     }
72
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);
76
77         try {
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"));
85         }
86     }
87
88     @Test
89     public void testModulesSize() {
90         assertEquals(context.getModules().size(), 9);
91     }
92 }