package com.atlassian.stash.internal.web.locale;

import com.atlassian.stash.internal.language.Language;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.util.List;
import java.util.Locale;
import java.util.Map;

@RunWith(MockitoJUnitRunner.class)
public class UserLocaleWebUtilsTest {

    @Test
    public void testToSoyLocaleMapping() {
        Language usEnglish = new Language(Locale.US);
        Language german = new Language(Locale.GERMANY);
        Language french = new Language(Locale.FRANCE);

        Iterable<Language> inputLanguages = Lists.newArrayList(usEnglish, german, french);
        Iterable<Locale> inputLocales = Lists.newArrayList(Locale.FRANCE, Locale.GERMANY, Locale.US);

        List<Map<String, Object>> output = UserLocaleWebUtils.toSoy(inputLanguages, inputLocales).toList();
        assertThat(output, is(not(empty())));

        // Check French is the selected language
        assertThat("Expected three languages in output", output, hasSize(3));

        assertThat("Expected first language to be English", output.get(0), hasEntry("locale", (Object) "en_US"));
        assertThat("Expected first language to be English", output.get(1), hasEntry("locale", (Object) "de_DE"));
        assertThat("Expected first language to be English", output.get(2), hasEntry("locale", (Object) "fr_FR"));

        assertThat("Expected English to not be selected", (Boolean) output.get(0).get("selected"), is(false));
        assertThat("Expected German to not be selected", (Boolean) output.get(1).get("selected"), is(false));
        assertThat("Expected French to be selected", (Boolean) output.get(2).get("selected"), is(true));
    }
}
