2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt.retest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
21 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
26 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
28 public class YangParserNegativeTest {
31 public void testInvalidImport() throws Exception {
32 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
34 try (InputStream stream = new FileInputStream(yang)) {
35 TestUtils.loadModule(stream);
36 fail("SomeModifiersUnresolvedException should be thrown");
38 } catch (SomeModifiersUnresolvedException e) {
39 final Throwable suppressed2levelsDown = e.getSuppressed()[0].getSuppressed()[0];
40 assertTrue(suppressed2levelsDown instanceof InferenceException);
41 assertTrue(suppressed2levelsDown.getMessage().startsWith("Imported module"));
42 assertTrue(suppressed2levelsDown.getMessage().endsWith("was not found."));
47 public void testTypeNotFound() throws Exception {
48 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
50 try (InputStream stream = new FileInputStream(yang)) {
51 TestUtils.loadModule(stream);
52 fail("IllegalArgumentException should be thrown");
54 } catch (IllegalStateException e) {
55 assertTrue(e.getMessage().startsWith(
56 "Type '(urn:simple.types.data.demo?revision=2013-02-27)int-ext' was not found"));
61 public void testInvalidAugmentTarget() throws Exception {
62 File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
63 File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
65 final List<InputStream> streams = new ArrayList<>(2);
66 try (InputStream testFile0 = new FileInputStream(yang1)) {
67 streams.add(testFile0);
68 try (InputStream testFile3 = new FileInputStream(yang2)) {
69 streams.add(testFile3);
70 assertEquals("Expected loaded files count is 2", 2, streams.size());
71 TestUtils.loadModules(streams);
72 fail("SomeModifiersUnresolvedException should be thrown");
75 } catch (SomeModifiersUnresolvedException e) {
76 final Throwable suppressed2levelsDown = e.getSuppressed()[0].getSuppressed()[0];
77 assertTrue(suppressed2levelsDown instanceof InferenceException);
79 "Augment target not found: Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}",
80 suppressed2levelsDown.getMessage());
85 public void testInvalidRefine() throws Exception {
86 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
88 try (InputStream stream = new FileInputStream(yang)) {
89 TestUtils.loadModule(stream);
90 fail("SourceException should be thrown");
92 } catch (SourceException e) {
96 "Error in module 'test4' in the refine of uses 'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for the target 'LEAF_LIST'."));
101 public void testInvalidLength() throws Exception {
102 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
104 try (InputStream stream = new FileInputStream(yang)) {
105 TestUtils.loadModule(stream);
106 fail("YangParseException should be thrown");
108 } catch (YangParseException e) {
109 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
114 public void testInvalidRange() throws Exception {
115 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
117 try (InputStream stream = new FileInputStream(yang)) {
118 TestUtils.loadModule(stream);
119 fail("Exception should be thrown");
121 } catch (YangParseException e) {
122 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
127 public void testDuplicateContainer() throws Exception {
128 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
130 try (InputStream stream = new FileInputStream(yang)) {
131 TestUtils.loadModule(stream);
132 fail("SourceException should be thrown");
134 } catch (SourceException e) {
135 String expected = "Error in module 'container': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
136 assertEquals(expected, e.getMessage());
141 public void testDuplicateContainerList() throws Exception {
142 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
144 try (InputStream stream = new FileInputStream(yang)) {
145 TestUtils.loadModule(stream);
146 fail("SourceException should be thrown");
148 } catch (SourceException e) {
149 String expected = "Error in module 'container-list': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
150 assertEquals(expected, e.getMessage());
155 public void testDuplicateContainerLeaf() throws Exception {
156 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
158 try (InputStream stream = new FileInputStream(yang)) {
159 TestUtils.loadModule(stream);
160 fail("SourceException should be thrown");
162 } catch (SourceException e) {
163 String expected = "Error in module 'container-leaf': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
164 assertEquals(expected, e.getMessage());
169 public void testDuplicateTypedef() throws Exception {
170 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
172 try (InputStream stream = new FileInputStream(yang)) {
173 TestUtils.loadModule(stream);
174 fail("IllegalArgumentException should be thrown");
176 } catch (IllegalArgumentException e) {
177 String expected = "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext";
178 assertEquals(expected, e.getMessage());
183 public void testDuplicityInAugmentTarget1() throws Exception {
184 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
185 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
187 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
188 TestUtils.loadModules(Arrays.asList(stream1, stream2));
189 fail("IllegalStateException should be thrown");
191 } catch (IllegalStateException e) {
192 assertEquals(e.getMessage(),
193 "An augment cannot add node named 'id' because this name is already used in target");
198 public void testDuplicityInAugmentTarget2() throws Exception {
199 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
200 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
202 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
203 TestUtils.loadModules(Arrays.asList(stream1, stream2));
204 fail("IllegalStateException should be thrown");
206 } catch (IllegalStateException e) {
207 assertEquals(e.getMessage(),
208 "An augment cannot add node named 'delta' because this name is already used in target");
213 public void testMandatoryInAugment() throws Exception {
214 File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
215 File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
217 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
218 TestUtils.loadModules(Arrays.asList(stream1, stream2));
219 fail("IllegalArgumentException should be thrown");
221 } catch (IllegalArgumentException e) {
222 String expected = "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target";
223 assertEquals(expected, e.getMessage());
228 public void testWrongDependenciesDir() throws Exception {
230 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
231 File dependenciesDir = new File("/invalid");
232 YangContextParser parser = new YangParserImpl();
233 parser.parseFile(yangFile, dependenciesDir);
234 fail("Exception should be thrown");
235 } catch (IllegalStateException e) {
236 String expected = File.separator + "invalid does not exists";
237 assertEquals(expected, e.getMessage());
242 public void testWrongDependenciesDir2() throws Exception {
244 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
245 File dependenciesDir = new File(getClass().getResource("/model").toURI());
246 YangContextParser parser = new YangParserImpl();
247 parser.parseFile(yangFile, dependenciesDir);
248 fail("Exception should be thrown");
249 } catch (YangValidationException e) {
250 String expected = "Not existing module imported";
251 assertTrue(e.getMessage().contains(expected));
256 public void testInvalidListKeyDefinition() throws Exception {
257 File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
259 try (InputStream stream1 = new FileInputStream(yang1)) {
260 TestUtils.loadModule(stream1);
261 fail("IllegalArgumentException should be thrown");
263 } catch (IllegalArgumentException e) {
264 String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)application-map'";
265 assertTrue(e.getMessage().startsWith(expected));