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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.hamcrest.Matchers.isA;
14 import static org.junit.Assert.assertThrows;
16 import com.google.common.base.Throwables;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 public class YangParserNegativeTest {
25 public void testInvalidImport() {
26 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
27 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang"));
29 final Throwable rootCause = Throwables.getRootCause(ex);
30 assertThat(rootCause, isA(InferenceException.class));
31 assertThat(rootCause.getMessage(), startsWith("Imported module"));
32 assertThat(rootCause.getMessage(), containsString("was not found."));
36 public void testTypeNotFound() {
37 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
38 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang"));
39 final Throwable rootCause = Throwables.getRootCause(ex);
40 assertThat(rootCause, isA(InferenceException.class));
41 assertThat(rootCause.getMessage(),
42 startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
46 public void testInvalidAugmentTarget() {
47 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
48 () -> TestUtils.loadModuleResources(getClass(),
49 "/negative-scenario/testfile0.yang", "/negative-scenario/testfile3.yang"));
50 final Throwable rootCause = Throwables.getRootCause(ex);
51 assertThat(rootCause, isA(InferenceException.class));
52 assertThat(rootCause.getMessage(), startsWith(
53 "Augment target 'Absolute{qnames=[(urn:simple.container.demo)unknown]}' not found"));
57 public void testInvalidRefine() {
58 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
59 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang"));
60 final Throwable cause = ex.getCause();
61 assertThat(cause, isA(SourceException.class));
62 assertThat(cause.getMessage(), containsString("Error in module 'test4' in the refine of uses "
63 + "'Descendant{qnames=[(urn:simple.container.demo)node]}': can not perform refine of 'PRESENCE' for"
64 + " the target 'LEAF_LIST'."));
68 public void testInvalidLength() {
69 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
70 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang"));
71 final Throwable cause = ex.getCause();
72 assertThat(cause, isA(SourceException.class));
73 assertThat(cause.getMessage(), containsString("Invalid length constraint [4..10]"));
77 public void testInvalidRange() {
78 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
79 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang"));
80 final Throwable cause = ex.getCause();
81 assertThat(cause, isA(SourceException.class));
82 assertThat(cause.getMessage(), startsWith("Invalid range constraint: [[5..20]]"));
86 public void testDuplicateContainer() {
87 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
88 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang"));
89 final Throwable cause = ex.getCause();
90 assertThat(cause, isA(SourceException.class));
91 assertThat(cause.getMessage(), containsString("Error in module 'container': cannot add "
92 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
97 public void testDuplicateContainerList() {
98 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
99 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang"));
100 final Throwable cause = ex.getCause();
101 assertThat(cause, isA(SourceException.class));
102 assertThat(cause.getMessage(), containsString("Error in module 'container-list': cannot add "
103 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
108 public void testDuplicateContainerLeaf() {
109 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
110 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-leaf.yang"));
111 final Throwable cause = ex.getCause();
112 assertThat(cause, isA(SourceException.class));
113 assertThat(cause.getMessage(), containsString("Error in module 'container-leaf': cannot add "
114 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
119 public void testDuplicateTypedef() {
120 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
121 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/typedef.yang"));
122 final Throwable cause = ex.getCause();
123 assertThat(cause, isA(SourceException.class));
124 assertThat(cause.getMessage(), startsWith(
125 "Duplicate name for typedef (urn:simple.container.demo)int-ext [at"));
129 public void testDuplicityInAugmentTarget1() {
130 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
131 () -> TestUtils.loadModuleResources(getClass(),
132 "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment1.yang"));
133 final Throwable cause = ex.getCause();
134 assertThat(cause, isA(InferenceException.class));
135 assertThat(cause.getMessage(), startsWith(
136 "An augment cannot add node named 'id' because this name is already used in target"));
140 public void testDuplicityInAugmentTarget2() {
141 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
142 () -> TestUtils.loadModuleResources(getClass(),
143 "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment2.yang"));
144 final Throwable rootCause = Throwables.getRootCause(ex);
145 assertThat(rootCause, isA(SubstatementIndexingException.class));
146 assertThat(rootCause.getMessage(), containsString("Cannot add schema tree child with name "
147 + "(urn:simple.augment2.demo?revision=2014-06-02)delta, a conflicting child already exists"));
151 public void testMandatoryInAugment() {
152 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
153 () -> TestUtils.loadModuleResources(getClass(),
154 "/negative-scenario/testfile8.yang", "/negative-scenario/testfile7.yang"));
155 final Throwable cause = ex.getCause();
156 assertThat(cause, isA(InferenceException.class));
157 assertThat(cause.getMessage(), startsWith(
158 "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
162 public void testInvalidListKeyDefinition() {
163 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
164 () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang"));
165 final Throwable cause = ex.getCause();
166 assertThat(cause, isA(InferenceException.class));
167 assertThat(cause.getMessage(), startsWith(
168 "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def)application-map'"));