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 private final PrintStream stdout = System.out;
29 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
30 private String testLog;
33 public void setUp() throws UnsupportedEncodingException {
34 System.setOut(new PrintStream(output, true, "UTF-8"));
38 public void cleanUp() {
39 System.setOut(stdout);
43 public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
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."));
56 public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
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."));
69 public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
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"));
84 public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
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'."));
96 public void testInvalidLength() throws IOException, YangSyntaxErrorException {
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>"));
106 public void testInvalidRange() throws IOException, YangSyntaxErrorException {
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>"));
116 public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
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));
129 public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
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));
142 public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
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));
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?revision=1970-01-01)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("An augment cannot add node named 'id' because this name is already used in target"));
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"));
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"));
194 public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
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));