/*
 * Copyright 2012 The Clustermeister Team.
 *
 * Licensed 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 com.github.nethad.clustermeister.api.impl;

import java.io.StringReader;
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import static org.hamcrest.Matchers.*;

/**
 *
 * @author thomas
 */
public class YamlConfigurationTest {
    private YamlConfiguration yamlConfiguration;
    
    private static final String DOCUMENT = 
              "amazon:\n"
            + "  imageid: micro\n"
            + "  location: euwest1\n"
            + "  profiles:\n"
            + "    - name: profile1\n"
            + "      key1: value1\n"
            + "      key2: value2\n"
            + "    - name: profile2\n"
            + "      key1: value3\n"
            + "      key2: value4\n"
            + "      otherkey: somevalue\n"
            + "torque: torquevalue\n";
    
    @Before
    public void setup() {
        yamlConfiguration = new YamlConfiguration();
        yamlConfiguration.load(new StringReader(DOCUMENT));
    }
    
    @Test
    public void nested() {
        String value = yamlConfiguration.getString("amazon.imageid");
        assertThat(value, is("micro"));
        value = yamlConfiguration.getString("amazon.location");
        assertThat(value, is("euwest1"));
    }
    
    @Test
    public void simpleKeyValue() {
        String value = yamlConfiguration.getString("torque");
        assertThat(value, is("torquevalue"));
    }
    
    @Test
    public void nestedList() {
        List<Object> rawprofiles = yamlConfiguration.getList("amazon.profiles");
        List<LinkedHashMap<String, String>> profiles = new ArrayList<LinkedHashMap<String, String>>();
        for (Object rawprofile : rawprofiles) {
            profiles.add((LinkedHashMap<String, String>)rawprofile);
        }
        assertThat(profiles.size(), is(2));
        
        LinkedHashMap<String, String> firstProfile = profiles.get(0);
        assertThat(firstProfile.size(), is(3));
        assertThat(firstProfile.get("name"), is("profile1"));
        assertThat(firstProfile.get("key1"), is("value1"));
        assertThat(firstProfile.get("key2"), is("value2"));
        
        LinkedHashMap<String, String> secondProfile = profiles.get(1);
        assertThat(secondProfile.size(), is(4));
        assertThat(secondProfile.get("name"), is("profile2"));
        assertThat(secondProfile.get("key1"), is("value3"));
        assertThat(secondProfile.get("key2"), is("value4"));
        assertThat(secondProfile.get("otherkey"), is("somevalue"));
    }
}

@@ -654,16 +654,103 @@
 .impl;%0A%0A
+import com.google.common.base.Function;%0Aimport com.google.common.collect.Collections2;%0A
 import j
@@ -770,16 +770,16 @@
 Reader;%0A
-
 import j
@@ -1512,16 +1512,142 @@
 value%5Cn%22
+%0A            + %22preload:%5Cn%22%0A            + %22  artifacts:%5Cn%22%0A            + %22    - artifact1%5Cn%22%0A            + %22    - artifact2%5Cn%22
 ;%0A    %0A 
@@ -2267,20 +2267,19 @@
 d nested
-List
+Map
 () %7B%0A   
@@ -3321,16 +3321,391 @@
 lue%22));%0A
-
     %7D%0A
+    %0A    @Test%0A    public void nestedList() %7B%0A        Collection%3CString%3E artifacts = Collections2.transform(yamlConfiguration.getList(%22preload.artifacts%22), new Function%3CObject, String%3E()%7B%0A            @Override%0A            public String apply(Object input) %7B return input.toString(); %7D%0A        %7D);%0A        assertThat(artifacts, hasItems(%22artifact1%22, %22artifact2%22));%0A    %7D%0A    %0A
 %7D%0A

add yaml test (list of strings)