/**
* Copyright (C) 2017 Lukas Zaruba, lukas.zaruba@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package cz.lzaruba.icu4j.spring;

import static org.junit.Assert.assertEquals;

import java.util.Collections;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.WebApplicationContext;

import cz.lzaruba.icu4j.spring.MapBasedLocaleResolver.LocaleConfiguration;

/**
* Test suite for {@link MapBasedLocaleResolver}
*
* @author Lukas Zaruba, lukas.zaruba@gmail.com
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {MapBasedLocaleResolverTestConfig.class})
public class MapBasedLocaleResolverTest {

@Autowired
private WebApplicationContext applicationContext;

@Test
public void testLocalesResolved() {
MapBasedLocaleResolver resolver = applicationContext.getBean(MapBasedLocaleResolver.class);

assertEquals(“cs_CZ”, resolver.resolveLocale(“cs_CZ”));
assertEquals(“fr_FR”, resolver.resolveLocale(“fr_FR”));
assertEquals(“sk_SK”, resolver.resolveLocale(“sk_SK”));

// test resolving with suffix
assertEquals(“cs_CZ”, resolver.resolveLocale(“en-en_US_FOO.utf8-cs_CZ”));
assertEquals(“fr_FR”, resolver.resolveLocale(“en-en_US_FOO.utf8-fr_FR”));
assertEquals(“sk_SK”, resolver.resolveLocale(“en-en_US_FOO.utf8-sk_SK”));
}

@Test
public void testDefaultFallback() {
MapBasedLocaleResolver resolver = applicationContext.getBean(MapBasedLocaleResolver.class);

assertEquals(“en_US”, resolver.resolveLocale(“en_US”));
assertEquals(“en_US”, resolver.resolveLocale(“en”));
assertEquals(“en_US”, resolver.resolveLocale(“en_UK”));
assertEquals(“en_US”, resolver.resolveLocale(“sk”));
assertEquals(“en_US”, resolver.resolveLocale(“asdfasdf”));

// test resolving with suffix
assertEquals(“en_US”, resolver.resolveLocale(“en-en_US_FOO.utf8-en_UK”));
assertEquals(“en_US”, resolver.resolveLocale(“en-en_US_FOO.utf8-sk”));
assertEquals(“en_US”, resolver.resolveLocale(“en-en_US_FOO.utf8-asdfasdf”));

}

@Test
public void testOverrideLocale() {
MapBasedLocaleResolver resolver = applicationContext.getBean(MapBasedLocaleResolver.class);
LocaleConfiguration configuration = new MapBasedLocaleResolver.LocaleConfiguration();
configuration.defaultLocale = “as_AS_FOO”;
configuration.mapping = Collections.singletonMap(“sk”, “sk_SK”);
resolver.setLocaleConfiguration(configuration);

assertEquals(“sk_SK”, resolver.resolveLocale(“sk”));
assertEquals(“as_AS_FOO”, resolver.resolveLocale(“cs_CZ”));
assertEquals(“as_AS_FOO”, resolver.resolveLocale(“en_US”));

// test resolving with suffix
assertEquals(“sk_SK”, resolver.resolveLocale(“en-en_US_FOO.utf8-sk”));
assertEquals(“as_AS_FOO”, resolver.resolveLocale(“en-en_US_FOO.utf8-cs_CZ”));
assertEquals(“as_AS_FOO”, resolver.resolveLocale(“en-en_US_FOO.utf8-en_US”));
}

@Test
public void testCreateLocaleFromRequest() {
MapBasedLocaleResolver resolver = applicationContext.getBean(MapBasedLocaleResolver.class);

assertEquals(“cs_CZ”, resolver.createLocaleFromRequest(“cs_CZ”));
assertEquals(“en_US”, resolver.createLocaleFromRequest(“en_US”));
assertEquals(“en_UK”, resolver.createLocaleFromRequest(“en_UK”));

// test with suffix
assertEquals(“cs_CZ”, resolver.createLocaleFromRequest(“en-en_US_FOO.utf8-cs_CZ”));
assertEquals(“en_US”, resolver.createLocaleFromRequest(“en-en_US_FOO.utf8-en_US”));
assertEquals(“en_UK”, resolver.createLocaleFromRequest(“en-en_US_FOO.utf8-en_UK”));

// test without underscore
assertEquals(“cs_CZ”, resolver.createLocaleFromRequest(“csCZ”));
assertEquals(“en_US”, resolver.createLocaleFromRequest(“enUS”));
assertEquals(“en_UK”, resolver.createLocaleFromRequest(“enUK”));

// test with non-exsupcted suffix
assertEquals(“cs_CZ”, resolver.createLocaleFromRequest(“csCZ_FOO”));
assertEquals(“en_US”, resolver.createLocaleFromRequest(“enUS_FOO”));
assertEquals(“en_UK”, resolver.createLocaleFromRequest(“enUK_FOO”));

// test multiple instances of “_”
assertEquals(“cs_CZ”, resolver.createLocaleFromRequest(“cs_CZ_FOO”));
assertEquals(“en_US”, resolver.createLocaleFromRequest(“en_US_FOO”));
assertEquals(“en_UK”, resolver.createLocaleFromRequest(“en_UK_FOO”));
}

}