2 * Copyright (c) 2016 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;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
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;
26 public class YangParserNegativeTest {
28 @SuppressWarnings("checkstyle:regexpSinglelineJava")
29 private final PrintStream stdout = System.out;
30 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
31 private String testLog;
34 public void setUp() throws UnsupportedEncodingException {
35 System.setOut(new PrintStream(output, true, "UTF-8"));
39 public void cleanUp() {
40 System.setOut(stdout);
44 public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
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."));
57 public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
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."));
70 public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
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"));
85 public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
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'."));
97 public void testInvalidLength() throws IOException, YangSyntaxErrorException {
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();
104 assertTrue(message.contains("Invalid length constraint [4..10]"));
109 public void testInvalidRange() throws IOException, YangSyntaxErrorException {
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]]"));
119 public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
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));
131 public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
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));
143 public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
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));
155 public void testDuplicateTypedef() throws IOException, YangSyntaxErrorException {
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"));
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"));
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"));
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"));
196 public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
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));