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.controller.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.controller.yang.parser.util.YangParseException;
20 import org.opendaylight.controller.yang.parser.util.YangValidationException;
22 public class YangParserNegativeTest {
25 public void testInvalidImport() throws IOException {
27 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
29 TestUtils.loadModule(stream);
30 fail("ValidationException should by thrown");
32 } catch (YangValidationException e) {
33 assertTrue(e.getMessage().contains("Not existing module imported"));
38 public void testTypeNotFound() throws IOException {
40 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
42 TestUtils.loadModule(stream);
43 fail("YangParseException should by thrown");
45 } catch (YangParseException e) {
46 assertTrue(e.getMessage().contains(
47 "Error in module 'test2' on line 24: Referenced type 'int-ext' not found."));
52 public void testInvalidAugmentTarget() throws IOException {
54 final List<InputStream> streams = new ArrayList<>(2);
55 try (InputStream testFile0 = new FileInputStream(getClass()
56 .getResource("/negative-scenario/testfile0.yang").getPath())) {
57 streams.add(testFile0);
58 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
59 "/negative-scenario/testfile3.yang").getPath())) {
60 streams.add(testFile3);
61 assertEquals("Expected loaded files count is 2", 2, streams.size());
62 TestUtils.loadModules(streams);
63 fail("YangParseException should by thrown");
66 } catch (YangParseException e) {
67 assertTrue(e.getMessage().contains("Failed to resolve augments in module 'test3'."));
72 public void testInvalidRefine() throws IOException {
74 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
76 TestUtils.loadModule(stream);
77 fail("YangParseException should by thrown");
79 } catch (YangParseException e) {
80 assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
85 public void testInvalidLength() throws IOException {
87 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
89 TestUtils.loadModule(stream);
90 fail("YangParseException should by thrown");
92 } catch (YangParseException e) {
93 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
98 public void testInvalidRange() throws IOException {
100 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
102 TestUtils.loadModule(stream);
103 fail("YangParseException should by thrown");
105 } catch (YangParseException e) {
106 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
111 public void testDuplicateContainer() throws IOException {
113 try (InputStream stream = new FileInputStream(getClass().getResource(
114 "/negative-scenario/duplicity/container.yang").getPath())) {
115 TestUtils.loadModule(stream);
116 fail("YangParseException should by thrown");
118 } catch (YangParseException e) {
119 assertTrue(e.getMessage()
120 .contains("Error in module 'container' on line 10: Duplicate node found at line 6"));
125 public void testDuplicateContainerList() throws IOException {
127 try (InputStream stream = new FileInputStream(getClass().getResource(
128 "/negative-scenario/duplicity/container-list.yang").getPath())) {
129 TestUtils.loadModule(stream);
130 fail("YangParseException should by thrown");
132 } catch (YangParseException e) {
133 assertTrue(e.getMessage().contains(
134 "Error in module 'container-list' on line 10: Duplicate node found at line 6"));
139 public void testDuplicateContainerLeaf() throws IOException {
141 try (InputStream stream = new FileInputStream(getClass().getResource(
142 "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
143 TestUtils.loadModule(stream);
144 fail("YangParseException should by thrown");
146 } catch (YangParseException e) {
147 assertTrue(e.getMessage().contains(
148 "Error in module 'container-leaf' on line 10: Duplicate node found at line 6"));
153 public void testDuplicateTypedef() throws IOException {
155 try (InputStream stream = new FileInputStream(getClass().getResource(
156 "/negative-scenario/duplicity/typedef.yang").getPath())) {
157 TestUtils.loadModule(stream);
158 fail("YangParseException should by thrown");
160 } catch (YangParseException e) {
161 assertTrue(e.getMessage().contains("Error in module 'typedef' on line 10: Duplicate node found at line 6"));