Add DataNodeContainer.dataChildByName()
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / NameCollisionWithinCaseTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.fail;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18
19 public class NameCollisionWithinCaseTest {
20     @Test
21     public void testChildNameCollisionOfAugmentCase() throws Exception {
22         try {
23             StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/foo.yang");
24             fail("Expected failure due to node name collision");
25         } catch (ReactorException e) {
26             final Throwable cause = e.getCause();
27             assertThat(cause, instanceOf(SourceException.class));
28             assertThat(cause.getMessage(), startsWith(
29                 "Cannot add data tree child with name (foo?revision=2018-02-11)bar, a conflicting child already exists "
30                         + "[at "));
31         }
32     }
33
34     @Test
35     public void testChildNameCollisionOfAugmentChoice() throws Exception {
36         try {
37             StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/bar.yang");
38             fail("Expected failure due to node name collision");
39         } catch (ReactorException e) {
40             final Throwable cause = e.getCause();
41             assertThat(cause, instanceOf(SourceException.class));
42             assertThat(cause.getMessage(), startsWith(
43                 "Cannot add data tree child with name (bar?revision=2018-02-11)bar, a conflicting child already exists "
44                         + "[at "));
45         }
46     }
47
48     @Test
49     public void testChildNameCollisionNormal() throws Exception {
50         try {
51             StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/baz.yang");
52             fail("Expected failure due to node name collision");
53         } catch (ReactorException e) {
54             final Throwable cause = e.getCause();
55             assertThat(cause, instanceOf(SourceException.class));
56             assertThat(cause.getMessage(), startsWith(
57                 "Error in module 'baz': cannot add '(baz?revision=2018-02-28)bar'. Node name collision: "));
58         }
59     }
60 }