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)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)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 final String message = e.getCause().getMessage();
103 assertTrue(message.contains("Invalid length constraint [4..10]"));
108 public void testInvalidRange() throws IOException, YangSyntaxErrorException {
110 TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang");
111 fail("ReactorException should be thrown");
112 } catch (final ReactorException e) {
113 assertTrue(e.getCause().getMessage().contains("Invalid range constraint: <5, 20>"));
118 public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
120 TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang");
121 fail("SourceException should be thrown");
122 } catch (final ReactorException e) {
123 final String expected = "Error in module 'container': cannot add '(urn:simple.container" +
124 ".demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already declared";
125 assertTrue(e.getCause().getMessage().contains(expected));
130 public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
132 TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang");
133 fail("SourceException should be thrown");
134 } catch (final ReactorException e) {
135 final String expected = "Error in module 'container-list': cannot add '(urn:simple.container" +
136 ".demo)foo'. Node name collision: '(urn:simple.container.demo)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)foo'. Node name collision: '(urn:simple.container.demo)foo' already declared";
149 assertTrue(e.getCause().getMessage().contains(expected));
154 public void testDuplicateTypedef() throws IOException, YangSyntaxErrorException {
156 TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/typedef.yang");
157 fail("SourceException should be thrown");
158 } catch (final ReactorException e) {
159 assertTrue(e.getCause().getMessage().startsWith(
160 "Duplicate name for typedef (urn:simple.container.demo)int-ext [at"));
165 public void testDuplicityInAugmentTarget1() throws IOException, ReactorException, YangSyntaxErrorException {
166 TestUtils.loadModuleResources(getClass(),
167 "/negative-scenario/duplicity/augment0.yang",
168 "/negative-scenario/duplicity/augment1.yang");
169 testLog = output.toString();
170 assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
174 public void testDuplicityInAugmentTarget2() throws IOException, ReactorException, YangSyntaxErrorException {
175 TestUtils.loadModuleResources(getClass(),
176 "/negative-scenario/duplicity/augment0.yang",
177 "/negative-scenario/duplicity/augment2.yang");
178 testLog = output.toString();
179 assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
183 public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException {
184 TestUtils.loadModuleResources(getClass(),
185 "/negative-scenario/testfile8.yang",
186 "/negative-scenario/testfile7.yang");
187 testLog = output.toString();
188 assertTrue(testLog.contains(
189 "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
193 public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
195 TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang");
196 fail("InferenceException should be thrown");
197 } catch (final ReactorException e) {
198 final String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def)application-map'";
199 assertTrue(e.getCause().getMessage().startsWith(expected));