package com.atlassian.stash.sal.api.message;

import com.atlassian.bitbucket.i18n.I18nService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.Serializable;
import java.util.Locale;
import java.util.Map;

import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DefaultI18nResolverTest {

    @Mock
    private I18nService i18nService;

    private DefaultI18nResolver i18nResolver;

    @Before
    public void setUp() throws Exception {
        i18nResolver = new DefaultI18nResolver(i18nService);
    }

    @Test
    public void testResolveText() throws Exception {
        when(i18nService.getMessage("key1", new Serializable[0])).thenReturn("text");
        assertEquals("text", i18nResolver.resolveText("key1", new Serializable[0]));
    }

    @Test
    public void testResolveTextWithLocale() throws Exception {
        Locale loc = Locale.CHINESE;
        when(i18nService.getText(loc, "key1", null, new Serializable[0])).thenReturn("text");
        assertEquals("text", i18nResolver.resolveText(loc, "key1", new Serializable[0]));
    }

    @Test
    public void testGetRawText() throws Exception {
        when(i18nService.getMessagePattern("key2")).thenReturn("text2");
        assertEquals("text2", i18nResolver.getRawText("key2"));
    }

    @Test
    public void testGetAllTranslationsForPrefix() throws Exception {
        final Map<String, String> translations = singletonMap("prefix1", "match1");
        when(i18nService.getAllTranslationsForPrefix("prefix1")).thenReturn(translations);
        assertEquals(translations, i18nResolver.getAllTranslationsForPrefix("prefix1"));
    }

    @Test
    public void testGetAllTranslationsForPrefixAndLocale() throws Exception {
        final Locale locale = Locale.FRENCH;
        final Map<String, String> translations = singletonMap("prefix2", "match2");
        when(i18nService.getAllTranslationsForPrefix("prefix2", locale)).thenReturn(translations);
        assertEquals(translations, i18nResolver.getAllTranslationsForPrefix("prefix2", locale));
    }
}
