/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.settings.loader;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.test.ESTestCase;

import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class YamlSettingsLoaderTests extends ESTestCase {

    public void testSimpleYamlSettings() throws Exception {
        String yaml = "/org/elasticsearch/common/settings/loader/test-settings.yml";
        Settings settings = settingsBuilder()
                .loadFromStream(yaml, getClass().getResourceAsStream(yaml))
                .build();

        assertThat(settings.get("test1.value1"), equalTo("value1"));
        assertThat(settings.get("test1.test2.value2"), equalTo("value2"));
        assertThat(settings.getAsInt("test1.test2.value3", -1), equalTo(2));

        // check array
        assertThat(settings.get("test1.test3.0"), equalTo("test3-1"));
        assertThat(settings.get("test1.test3.1"), equalTo("test3-2"));
        assertThat(settings.getAsArray("test1.test3").length, equalTo(2));
        assertThat(settings.getAsArray("test1.test3")[0], equalTo("test3-1"));
        assertThat(settings.getAsArray("test1.test3")[1], equalTo("test3-2"));
    }

    public void testIndentation() {
        String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml";
        try {
            settingsBuilder()
                .loadFromStream(yaml, getClass().getResourceAsStream(yaml))
                .build();
            fail("Expected SettingsException");
        } catch(SettingsException e ) {
            assertThat(e.getMessage(), containsString("Failed to load settings"));
        }
    }

    public void testIndentationWithExplicitDocumentStart() {
        String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml";
        try {
            settingsBuilder()
                    .loadFromStream(yaml, getClass().getResourceAsStream(yaml))
                    .build();
            fail("Expected SettingsException");
        } catch (SettingsException e) {
            assertThat(e.getMessage(), containsString("Failed to load settings"));
        }
    }

    public void testDuplicateKeysThrowsException() {
        String yaml = "foo: bar\nfoo: baz";
        try {
            settingsBuilder()
                    .loadFromSource(yaml)
                    .build();
            fail("expected exception");
        } catch (SettingsException e) {
            assertEquals(e.getCause().getClass(), ElasticsearchParseException.class);
            assertThat(
                    e.toString(),
                    containsString("duplicate settings key [foo] " +
                            "found at line number [2], " +
                            "column number [6], " +
                            "previous value [bar], " +
                            "current value [baz]"));
        }
    }

    public void testNullValuedSettingThrowsException() {
        final String yaml = "foo:";
        final ElasticsearchParseException e =
                expectThrows(ElasticsearchParseException.class, () -> new YamlSettingsLoader(false).load(yaml));
        assertThat(e.toString(), containsString("null-valued setting found for key [foo] found at line number [1], column number [5]"));
    }

}

@@ -2230,32 +2230,38 @@
 ion() %7B%0A        
+final 
 String yaml = %22/
@@ -2340,238 +2340,232 @@
     
-try %7B%0A            settingsBuilder()%0A                .loadFromStream(yaml, getClass().getResourceAsStream(yaml))%0A                .build();%0A            fail(%22Expected SettingsException%22);%0A        %7D catch(SettingsException e ) %7B%0A    
+final SettingsException e =%0A                expectThrows(%0A                        SettingsException.class,%0A                        () -%3E settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build());%0A
     
@@ -2635,34 +2635,24 @@
 ettings%22));%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -2707,32 +2707,38 @@
 art() %7B%0A        
+final 
 String yaml = %22/
@@ -2846,246 +2846,232 @@
     
-try %7B%0A            settingsBuilder()%0A                    .loadFromStream(yaml, getClass().getResourceAsStream(yaml))%0A                    .build();%0A            fail(%22Expected SettingsException%22);%0A        %7D catch (SettingsException e) %7B%0A    
+final SettingsException e =%0A                expectThrows(%0A                        SettingsException.class,%0A                        () -%3E settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build());%0A
     
@@ -3141,34 +3141,24 @@
 ettings%22));%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -3205,32 +3205,38 @@
 ion() %7B%0A        
+final 
 String yaml = %22f
@@ -3267,200 +3267,129 @@
     
-try %7B%0A            settingsBuilder()%0A                    .loadFromSource(yaml)%0A                    .build();%0A            fail(%22expected exception%22);%0A        %7D catch (SettingsException e) %7B%0A    
+final SettingsException e = expectThrows(SettingsException.class, () -%3E settingsBuilder().loadFromSource(yaml).build());%0A
     
@@ -3466,20 +3466,16 @@
 class);%0A
-    
         
@@ -3502,20 +3502,16 @@
         
-    
 e.toStri
@@ -3516,20 +3516,16 @@
 ring(),%0A
-    
         
@@ -3605,20 +3605,16 @@
         
-   
- 
 %22found a
@@ -3632,28 +3632,24 @@
 er %5B2%5D, %22 +%0A
-    
             
@@ -3708,20 +3708,16 @@
         
-    
 %22previou
@@ -3759,20 +3759,16 @@
         
-   
- 
 %22current
@@ -3788,18 +3788,8 @@
 ));%0A
-        %7D%0A
     

Use expectThrows in YamlSettingsLoaderTests