package com.atlassian.stash.internal.setting;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class MapSettingsTest {

    @Test
    public void testGetBoolean() {
        MapSettings settings = new MapSettings(ImmutableMap.<String, Object>builder()
                .put("bool", true)
                .put("str", "foo")
                .put("int", 5)
                .build());
        assertEquals(true, settings.getBoolean("bool"));
        assertEquals(null, settings.getBoolean("fake"));
        assertEquals(false, settings.getBoolean("fake", false));
    }

    @Test
    public void testGetInt() {
        assertEquals(Integer.valueOf(5), createSettings("5").getInt("value"));
    }

    @Test
    public void testGetLong() {
        assertEquals(Long.valueOf(5), createSettings("5").getLong("value"));
    }

    @Test
    public void testGetDouble() {
        assertEquals(Double.valueOf(5.5d), createSettings("5.5").getDouble("value"));
    }

    @Test
    public void testGetIntAsString() {
        assertEquals(Integer.valueOf(5), createSettings(5).getInt("value"));
    }

    @Test
    public void testGetBooleanAsString() {
        assertEquals(Boolean.TRUE, createSettings("true").getBoolean("value"));
    }

    @Test
    public void testGetLongAsString() {
        assertEquals(Long.valueOf(5), createSettings(5).getLong("value"));
    }

    @Test
    public void testGetDoubleAsString() {
        assertEquals(Double.valueOf(5.5d), createSettings(5.5d).getDouble("value"));
    }

    @Test(expected = NumberFormatException.class)
    public void testGetIntAsStringBad() {
        createSettings("bad").getInt("value");
    }

    @Test
    public void testGetBooleanAsStringBad() {
        assertEquals(Boolean.FALSE, createSettings("bad").getBoolean("value"));
    }

    @Test(expected = NumberFormatException.class)
    public void testGetLongAsStringBad() {
        createSettings("bad").getLong("value");
    }

    @Test(expected = NumberFormatException.class)
    public void testGetDoubleAsStringBad() {
        createSettings("bad").getDouble("value");
    }

    @Test
    public void testGetIntAsStringNull() {
        assertNull(createNullSettings().getInt("value"));
    }

    @Test
    public void testGetBooleanAsStringNull() {
        assertNull(createNullSettings().getBoolean("value"));
    }

    @Test
    public void testGetLongAsStringNull() {
        assertNull(createNullSettings().getLong("value"));
    }

    @Test
    public void testGetDoubleAsStringNull() {
        assertNull(createNullSettings().getDouble("value"));
    }

    private MapSettings createSettings(Object value) {
        return new MapSettings(ImmutableMap.<String, Object>builder().put("value", "" + value).build());
    }

    private MapSettings createNullSettings() {
        return new MapSettings(ImmutableMap.<String, Object>builder().build());
    }

}
