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;
27 import com.google.common.base.Throwables;
29 public class YangParserNegativeTest {
32 public void testInvalidImport() throws Exception {
33 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
35 try (InputStream stream = new FileInputStream(yang)) {
36 TestUtils.loadModule(stream);
37 fail("SomeModifiersUnresolvedException should be thrown");
39 } catch (SomeModifiersUnresolvedException e) {
40 Throwable rootCause = Throwables.getRootCause(e);
41 assertTrue(rootCause instanceof InferenceException);
42 assertTrue(rootCause.getMessage().startsWith("Imported module"));
43 assertTrue(rootCause.getMessage().endsWith("was not found."));
48 public void testTypeNotFound() throws Exception {
49 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
51 try (InputStream stream = new FileInputStream(yang)) {
52 TestUtils.loadModule(stream);
53 fail("IllegalArgumentException should be thrown");
55 } catch (SomeModifiersUnresolvedException e) {
56 Throwable rootCause = Throwables.getRootCause(e);
57 assertTrue(rootCause instanceof InferenceException);
58 assertTrue(rootCause.getMessage()
59 .startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
64 public void testInvalidAugmentTarget() throws Exception {
65 File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
66 File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
68 final List<InputStream> streams = new ArrayList<>(2);
69 try (InputStream testFile0 = new FileInputStream(yang1)) {
70 streams.add(testFile0);
71 try (InputStream testFile3 = new FileInputStream(yang2)) {
72 streams.add(testFile3);
73 assertEquals("Expected loaded files count is 2", 2, streams.size());
74 TestUtils.loadModules(streams);
75 fail("SomeModifiersUnresolvedException should be thrown");
78 } catch (SomeModifiersUnresolvedException e) {
79 final Throwable rootCause = Throwables.getRootCause(e);
80 assertTrue(rootCause instanceof InferenceException);
82 "Augment target not found: Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}",
83 rootCause.getMessage());
88 public void testInvalidRefine() throws Exception {
89 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
91 try (InputStream stream = new FileInputStream(yang)) {
92 TestUtils.loadModule(stream);
93 fail("SourceException should be thrown");
95 } catch (SourceException e) {
99 "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'."));
104 public void testInvalidLength() throws Exception {
105 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
107 try (InputStream stream = new FileInputStream(yang)) {
108 TestUtils.loadModule(stream);
109 fail("YangParseException should be thrown");
111 } catch (SourceException e) {
112 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
117 public void testInvalidRange() throws Exception {
118 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
120 try (InputStream stream = new FileInputStream(yang)) {
121 TestUtils.loadModule(stream);
122 fail("Exception should be thrown");
124 } catch (SourceException e) {
125 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
130 public void testDuplicateContainer() throws Exception {
131 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
133 try (InputStream stream = new FileInputStream(yang)) {
134 TestUtils.loadModule(stream);
135 fail("SourceException should be thrown");
137 } catch (SourceException e) {
138 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.";
139 assertEquals(expected, e.getMessage());
144 public void testDuplicateContainerList() throws Exception {
145 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
147 try (InputStream stream = new FileInputStream(yang)) {
148 TestUtils.loadModule(stream);
149 fail("SourceException should be thrown");
151 } catch (SourceException e) {
152 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.";
153 assertEquals(expected, e.getMessage());
158 public void testDuplicateContainerLeaf() throws Exception {
159 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
161 try (InputStream stream = new FileInputStream(yang)) {
162 TestUtils.loadModule(stream);
163 fail("SourceException should be thrown");
165 } catch (SourceException e) {
166 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.";
167 assertEquals(expected, e.getMessage());
172 public void testDuplicateTypedef() throws Exception {
173 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
175 try (InputStream stream = new FileInputStream(yang)) {
176 TestUtils.loadModule(stream);
177 fail("IllegalArgumentException should be thrown");
179 } catch (IllegalArgumentException e) {
180 String expected = "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext";
181 assertEquals(expected, e.getMessage());
186 public void testDuplicityInAugmentTarget1() throws Exception {
187 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
188 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
190 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
191 TestUtils.loadModules(Arrays.asList(stream1, stream2));
192 fail("IllegalStateException should be thrown");
194 } catch (IllegalStateException e) {
195 assertEquals(e.getMessage(),
196 "An augment cannot add node named 'id' because this name is already used in target");
201 public void testDuplicityInAugmentTarget2() throws Exception {
202 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
203 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
205 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
206 TestUtils.loadModules(Arrays.asList(stream1, stream2));
207 fail("IllegalStateException should be thrown");
209 } catch (IllegalStateException e) {
210 assertEquals(e.getMessage(),
211 "An augment cannot add node named 'delta' because this name is already used in target");
216 public void testMandatoryInAugment() throws Exception {
217 File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
218 File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
220 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
221 TestUtils.loadModules(Arrays.asList(stream1, stream2));
222 fail("IllegalArgumentException should be thrown");
224 } catch (IllegalArgumentException e) {
225 String expected = "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target";
226 assertEquals(expected, e.getMessage());
231 public void testWrongDependenciesDir() throws Exception {
233 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
234 File dependenciesDir = new File("/invalid");
235 YangContextParser parser = new YangParserImpl();
236 parser.parseFile(yangFile, dependenciesDir);
237 fail("Exception should be thrown");
238 } catch (IllegalStateException e) {
239 String expected = File.separator + "invalid does not exists";
240 assertEquals(expected, e.getMessage());
245 public void testWrongDependenciesDir2() throws Exception {
247 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
248 File dependenciesDir = new File(getClass().getResource("/model").toURI());
249 YangContextParser parser = new YangParserImpl();
250 parser.parseFile(yangFile, dependenciesDir);
251 fail("Exception should be thrown");
252 } catch (YangValidationException e) {
253 String expected = "Not existing module imported";
254 assertTrue(e.getMessage().contains(expected));
259 public void testInvalidListKeyDefinition() throws Exception {
260 File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
262 try (InputStream stream1 = new FileInputStream(yang1)) {
263 TestUtils.loadModule(stream1);
264 fail("IllegalArgumentException should be thrown");
266 } catch (IllegalArgumentException e) {
267 String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)application-map'";
268 assertTrue(e.getMessage().startsWith(expected));