Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / 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     @SuppressWarnings("checkstyle:regexpSinglelineJava")
29     private final PrintStream stdout = System.out;
30     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
31     private String testLog;
32
33     @Before
34     public void setUp() throws UnsupportedEncodingException {
35         System.setOut(new PrintStream(output, true, "UTF-8"));
36     }
37
38     @After
39     public void cleanUp() {
40         System.setOut(stdout);
41     }
42
43     @Test
44     public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
45         try {
46             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang");
47             fail("SomeModifiersUnresolvedException should be thrown");
48         } catch (final SomeModifiersUnresolvedException e) {
49             final Throwable rootCause = Throwables.getRootCause(e);
50             assertTrue(rootCause instanceof InferenceException);
51             assertTrue(rootCause.getMessage().startsWith("Imported module"));
52             assertTrue(rootCause.getMessage().contains("was not found."));
53         }
54     }
55
56     @Test
57     public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
58         try {
59             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang");
60             fail("InferenceException should be thrown");
61         } catch (final SomeModifiersUnresolvedException e) {
62             final Throwable rootCause = Throwables.getRootCause(e);
63             assertTrue(rootCause instanceof InferenceException);
64             assertTrue(rootCause.getMessage()
65                     .startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
66         }
67     }
68
69     @Test
70     public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
71         try {
72             TestUtils.loadModuleResources(getClass(),
73                 "/negative-scenario/testfile0.yang",
74                 "/negative-scenario/testfile3.yang");
75             fail("SomeModifiersUnresolvedException should be thrown");
76         } catch (final SomeModifiersUnresolvedException e) {
77             final Throwable rootCause = Throwables.getRootCause(e);
78             assertTrue(rootCause instanceof InferenceException);
79             assertTrue(rootCause.getMessage().startsWith(
80                 "Augment target 'Absolute{path=[(urn:simple.container.demo)unknown]}' not found"));
81         }
82     }
83
84     @Test
85     public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
86         try {
87             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang");
88             fail("ReactorException should be thrown");
89         } catch (final ReactorException e) {
90             assertTrue(e.getCause().getMessage().contains("Error in module 'test4' in the refine of uses "
91                     + "'Relative{path=[(urn:simple.container.demo)node]}': can not perform refine of 'PRESENCE' for"
92                     + " the target 'LEAF_LIST'."));
93         }
94     }
95
96     @Test
97     public void testInvalidLength() throws IOException, YangSyntaxErrorException {
98         try {
99             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang");
100             fail("ReactorException should be thrown");
101         } catch (final ReactorException e) {
102             final String message = e.getCause().getMessage();
103
104             assertTrue(message.contains("Invalid length constraint [4..10]"));
105         }
106     }
107
108     @Test
109     public void testInvalidRange() throws IOException, YangSyntaxErrorException {
110         try {
111             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang");
112             fail("ReactorException should be thrown");
113         } catch (final ReactorException e) {
114             assertTrue(e.getCause().getMessage().startsWith("Invalid range constraint: [[5..20]]"));
115         }
116     }
117
118     @Test
119     public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
120         try {
121             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang");
122             fail("SourceException should be thrown");
123         } catch (final ReactorException e) {
124             final String expected = "Error in module 'container': cannot add '(urn:simple.container.demo)foo'. "
125                     + "Node name collision: '(urn:simple.container.demo)foo' already declared";
126             assertTrue(e.getCause().getMessage().contains(expected));
127         }
128     }
129
130     @Test
131     public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
132         try {
133             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang");
134             fail("SourceException should be thrown");
135         } catch (final ReactorException e) {
136             final String expected = "Error in module 'container-list': cannot add '(urn:simple.container.demo)foo'. "
137                     + "Node name collision: '(urn:simple.container.demo)foo' already declared";
138             assertTrue(e.getCause().getMessage().contains(expected));
139         }
140     }
141
142     @Test
143     public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
144         try {
145             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-leaf.yang");
146             fail("SourceException should be thrown");
147         } catch (final ReactorException e) {
148             final String expected = "Error in module 'container-leaf': cannot add '(urn:simple.container.demo)foo'. "
149                     + "Node name collision: '(urn:simple.container.demo)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)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(
172             "An augment cannot add node named 'id' because this name is already used in target"));
173     }
174
175     @Test
176     public void testDuplicityInAugmentTarget2() throws IOException, ReactorException, YangSyntaxErrorException {
177         TestUtils.loadModuleResources(getClass(),
178             "/negative-scenario/duplicity/augment0.yang",
179                 "/negative-scenario/duplicity/augment2.yang");
180         testLog = output.toString();
181         assertTrue(testLog.contains(
182             "An augment cannot add node named 'delta' because this name is already used in target"));
183     }
184
185     @Test
186     public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException {
187         TestUtils.loadModuleResources(getClass(),
188             "/negative-scenario/testfile8.yang",
189                 "/negative-scenario/testfile7.yang");
190         testLog = output.toString();
191         assertTrue(testLog.contains(
192             "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
193     }
194
195     @Test
196     public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
197         try {
198             TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang");
199             fail("InferenceException should be thrown");
200         } catch (final ReactorException e) {
201             final String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def)application-map'";
202             assertTrue(e.getCause().getMessage().startsWith(expected));
203         }
204     }
205 }