Remove deprecated Yin/YangStatementSourceImpl
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserNegativeTest.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;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.base.Throwables;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.PrintStream;
17 import java.io.UnsupportedEncodingException;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
25
26 public class YangParserNegativeTest {
27
28     private final PrintStream stdout = System.out;
29     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
30     private String testLog;
31
32     @Before
33     public void setUp() throws UnsupportedEncodingException {
34         System.setOut(new PrintStream(output, true, "UTF-8"));
35     }
36
37     @After
38     public void cleanUp() {
39         System.setOut(stdout);
40     }
41
42     @Test
43     public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
44         try {
45             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang");
46             fail("SomeModifiersUnresolvedException should be thrown");
47         } catch (final SomeModifiersUnresolvedException e) {
48             final Throwable rootCause = Throwables.getRootCause(e);
49             assertTrue(rootCause instanceof InferenceException);
50             assertTrue(rootCause.getMessage().startsWith("Imported module"));
51             assertTrue(rootCause.getMessage().contains("was not found."));
52         }
53     }
54
55     @Test
56     public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
57         try {
58             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang");
59             fail("InferenceException should be thrown");
60         } catch (final SomeModifiersUnresolvedException e) {
61             final Throwable rootCause = Throwables.getRootCause(e);
62             assertTrue(rootCause instanceof InferenceException);
63             assertTrue(rootCause.getMessage()
64                     .startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
65         }
66     }
67
68     @Test
69     public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
70         try {
71             TestUtils.loadModuleResources(getClass(),
72                 "/negative-scenario/testfile0.yang",
73                 "/negative-scenario/testfile3.yang");
74             fail("SomeModifiersUnresolvedException should be thrown");
75         } catch (final SomeModifiersUnresolvedException e) {
76             final Throwable rootCause = Throwables.getRootCause(e);
77             assertTrue(rootCause instanceof InferenceException);
78             assertTrue(rootCause.getMessage().startsWith(
79                 "Augment target 'Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}' not found"));
80         }
81     }
82
83     @Test
84     public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
85         try {
86             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang");
87             fail("ReactorException should be thrown");
88         } catch (final ReactorException e) {
89             assertTrue(e.getCause().getMessage().contains("Error in module 'test4' in the refine of uses " +
90                     "'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for" +
91                     " the target 'LEAF_LIST'."));
92         }
93     }
94
95     @Test
96     public void testInvalidLength() throws IOException, YangSyntaxErrorException {
97         try {
98             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang");
99             fail("ReactorException should be thrown");
100         } catch (final ReactorException e) {
101             assertTrue(e.getCause().getMessage().contains("Invalid length constraint: <4, 10>"));
102         }
103     }
104
105     @Test
106     public void testInvalidRange() throws IOException, YangSyntaxErrorException {
107         try {
108             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang");
109             fail("ReactorException should be thrown");
110         } catch (final ReactorException e) {
111             assertTrue(e.getCause().getMessage().contains("Invalid range constraint: <5, 20>"));
112         }
113     }
114
115     @Test
116     public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
117         try {
118             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang");
119             fail("SourceException should be thrown");
120         } catch (final ReactorException e) {
121             final String expected = "Error in module 'container': cannot add '(urn:simple.container" +
122                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
123                     ".demo?revision=1970-01-01)foo' already declared";
124             assertTrue(e.getCause().getMessage().contains(expected));
125         }
126     }
127
128     @Test
129     public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
130         try {
131             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang");
132             fail("SourceException should be thrown");
133         } catch (final ReactorException e) {
134             final String expected = "Error in module 'container-list': cannot add '(urn:simple.container" +
135                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
136                     ".demo?revision=1970-01-01)foo' already declared";
137             assertTrue(e.getCause().getMessage().contains(expected));
138         }
139     }
140
141     @Test
142     public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
143         try {
144             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-leaf.yang");
145             fail("SourceException should be thrown");
146         } catch (final ReactorException e) {
147             final String expected = "Error in module 'container-leaf': cannot add '(urn:simple.container" +
148                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
149                     ".demo?revision=1970-01-01)foo' already declared";
150             assertTrue(e.getCause().getMessage().contains(expected));
151         }
152     }
153
154     @Test
155     public void testDuplicateTypedef() throws IOException, YangSyntaxErrorException {
156         try {
157             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/typedef.yang");
158             fail("SourceException should be thrown");
159         } catch (final ReactorException e) {
160             assertTrue(e.getCause().getMessage().startsWith(
161                 "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext [at"));
162         }
163     }
164
165     @Test
166     public void testDuplicityInAugmentTarget1() throws IOException, ReactorException, YangSyntaxErrorException {
167         TestUtils.loadModuleResources(getClass(),
168             "/negative-scenario/duplicity/augment0.yang",
169                 "/negative-scenario/duplicity/augment1.yang");
170         testLog = output.toString();
171         assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
172     }
173
174     @Test
175     public void testDuplicityInAugmentTarget2() throws IOException, ReactorException, YangSyntaxErrorException {
176         TestUtils.loadModuleResources(getClass(),
177             "/negative-scenario/duplicity/augment0.yang",
178                 "/negative-scenario/duplicity/augment2.yang");
179         testLog = output.toString();
180         assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
181     }
182
183     @Test
184     public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException {
185         TestUtils.loadModuleResources(getClass(),
186             "/negative-scenario/testfile8.yang",
187                 "/negative-scenario/testfile7.yang");
188         testLog = output.toString();
189         assertTrue(testLog.contains(
190             "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
191     }
192
193     @Test
194     public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
195         try {
196             TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang");
197             fail("InferenceException should be thrown");
198         } catch (final ReactorException e) {
199             final String expected = "Key 'rib-id' misses node 'rib-id' in list "
200                     + "'(invalid:list:key:def?revision=1970-01-01)application-map'";
201             assertTrue(e.getCause().getMessage().startsWith(expected));
202         }
203     }
204 }