Remove deprecated Yin/YangStatementSourceImpl
[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.rfc6020.repo.YinStatementStreamSource;
22 import org.opendaylight.yangtools.yang.parser.rfc6020.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;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
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 SourceException, ReactorException {
58         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59
60         addSources(reactor, YIN_FILE, EXT_FILE, EXT_USE_FILE);
61         SchemaContext result = reactor.buildEffective();
62         assertNotNull(result);
63     }
64
65     // parsing yin file whose import statement references a module which does not exist
66     @Test(expected = SomeModifiersUnresolvedException.class)
67     public void readAndParseInvalidYinFileTest() throws ReactorException {
68         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
69         addSources(reactor, INVALID_YIN_FILE);
70         SchemaContext result = reactor.buildEffective();
71         assertNotNull(result);
72     }
73
74     // parsing yin file with duplicate key name in a list statement
75     public void readAndParseInvalidYinFileTest2() {
76         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
77         addSources(reactor, INVALID_YIN_FILE_2);
78
79         try {
80             reactor.buildEffective();
81             fail("Reactor exception should have been thrown");
82         } catch (ReactorException e) {
83             final Throwable cause = e.getCause();
84             assertTrue(cause instanceof SourceException);
85             assertTrue(cause.getMessage().startsWith(
86                 "Key argument 'testing-string testing-string' contains duplicates"));
87         }
88     }
89
90     @Test
91     public void testModulesSize() {
92         assertEquals(context.getModules().size(), 9);
93     }
94
95     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,  final
96     StatementStreamSource... sources) {
97         for (StatementStreamSource source : sources) {
98             reactor.addSource(source);
99         }
100     }
101
102 }