/*
 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.stmt;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;

public class NameCollisionWithinCaseTest {
    @Test
    public void testChildNameCollisionOfAugmentCase() throws Exception {
        try {
            StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/foo.yang");
            fail("Expected failure due to node name collision");
        } catch (ReactorException e) {
            final Throwable cause = e.getCause();
            assertThat(cause, instanceOf(SourceException.class));
            assertThat(cause.getMessage(), startsWith(
                "Cannot add data tree child with name (foo?revision=2018-02-11)bar, a conflicting child already exists "
                        + "[at "));
        }
    }

    @Test
    public void testChildNameCollisionOfAugmentChoice() throws Exception {
        try {
            StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/bar.yang");
            fail("Expected failure due to node name collision");
        } catch (ReactorException e) {
            final Throwable cause = e.getCause();
            assertThat(cause, instanceOf(SourceException.class));
            assertThat(cause.getMessage(), startsWith(
                "Cannot add data tree child with name (bar?revision=2018-02-11)bar, a conflicting child already exists "
                        + "[at "));
        }
    }

    @Test
    public void testChildNameCollisionNormal() throws Exception {
        try {
            StmtTestUtils.parseYangSource("/bugs/name-collision-within-case/baz.yang");
            fail("Expected failure due to node name collision");
        } catch (ReactorException e) {
            final Throwable cause = e.getCause();
            assertThat(cause, instanceOf(SourceException.class));
            assertThat(cause.getMessage(), startsWith(
                "Error in module 'baz': cannot add '(baz?revision=2018-02-28)bar'. Node name collision: "));
        }
    }
}

@@ -546,20 +546,28 @@
 .Assert.
-fail
+assertThrows
 ;%0A%0Aimpor
@@ -835,31 +835,82 @@
 gmentCase() 
-throws 
+%7B%0A        final ReactorException ex = assertThrows(Reactor
 Exception %7B%0A
@@ -898,34 +898,39 @@
 ReactorException
- %7B
+.class,
 %0A        try %7B%0A 
@@ -922,33 +922,25 @@
         
-try %7B%0A           
+    () -%3E
  StmtTes
@@ -1010,118 +1010,11 @@
 ng%22)
+)
 ;%0A
-            fail(%22Expected failure due to node name collision%22);%0A        %7D catch (ReactorException e) %7B%0A    
     
@@ -1034,32 +1034,33 @@
 owable cause = e
+x
 .getCause();%0A   
@@ -1048,36 +1048,32 @@
  ex.getCause();%0A
-    
         assertTh
@@ -1118,36 +1118,32 @@
 lass));%0A        
-    
 assertThat(cause
@@ -1168,33 +1168,16 @@
 rtsWith(
-%0A                
 %22Cannot 
@@ -1228,32 +1228,49 @@
 2018-02-11)bar, 
+%22%0A            + %22
 a conflicting ch
@@ -1292,55 +1292,16 @@
 sts 
-%22%0A                        + %22%5Bat %22));%0A        %7D
+%5Bat %22));
 %0A   
@@ -1370,23 +1370,74 @@
 hoice() 
-throws 
+%7B%0A        final ReactorException ex = assertThrows(Reactor
 Exceptio
@@ -1429,34 +1429,39 @@
 ReactorException
- %7B
+.class,
 %0A        try %7B%0A 
@@ -1453,33 +1453,25 @@
         
-try %7B%0A           
+    () -%3E
  StmtTes
@@ -1541,118 +1541,11 @@
 ng%22)
+)
 ;%0A
-            fail(%22Expected failure due to node name collision%22);%0A        %7D catch (ReactorException e) %7B%0A    
     
@@ -1565,32 +1565,33 @@
 owable cause = e
+x
 .getCause();%0A   
@@ -1579,36 +1579,32 @@
  ex.getCause();%0A
-    
         assertTh
@@ -1649,36 +1649,32 @@
 lass));%0A        
-  
-  
 assertThat(cause
@@ -1699,33 +1699,16 @@
 rtsWith(
-%0A                
 %22Cannot 
@@ -1767,16 +1767,33 @@
 11)bar, 
+%22%0A            + %22
 a confli
@@ -1823,55 +1823,16 @@
 sts 
-%22%0A                        + %22%5Bat %22));%0A        %7D
+%5Bat %22));
 %0A   
@@ -1923,13 +1923,72 @@
     
-try %7B
+final ReactorException ex = assertThrows(ReactorException.class,
 %0A   
@@ -1995,16 +1995,22 @@
         
+ () -%3E
  StmtTes
@@ -2080,118 +2080,11 @@
 ng%22)
+)
 ;%0A
-            fail(%22Expected failure due to node name collision%22);%0A        %7D catch (ReactorException e) %7B%0A    
     
@@ -2112,16 +2112,17 @@
 ause = e
+x
 .getCaus
@@ -2126,36 +2126,32 @@
 ause();%0A        
-    
 assertThat(cause
@@ -2192,28 +2192,24 @@
 ));%0A        
-    
 assertThat(c
@@ -2251,20 +2251,16 @@
         
-   
- 
 %22Error i
@@ -2348,18 +2348,8 @@
 ));%0A
-        %7D%0A
     

Clean up NameCollisionWithinCaseTest