package cz.senslog.connector.fetch.azure;

import cz.senslog.connector.model.config.DefaultConfig;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.HashMap;

import static cz.senslog.connector.tools.json.BasicJson.jsonToObject;
import static org.junit.jupiter.api.Assertions.*;

class AzureConfigTest {

    @Test
    void newConfig_Full_True() {

        DefaultConfig defaultConfig = new DefaultConfig("azure", null);
        defaultConfig.setProperty("startDate", LocalDateTime.MIN);
        defaultConfig.setProperty("limitPerSensor", 42);
        defaultConfig.setProperty("sensorInfoHost", new HashMap<String, Object>(){{
            put("domain", "http://test.com");
            put("path", "test");
        }});
        defaultConfig.setProperty("sensorDataHost", new HashMap<String, Object>(){{
            put("domain", "http://test.com");
            put("path", "test");
        }});
        defaultConfig.setProperty("authentication", new HashMap<String, Object>(){{
            put("username", "username");
            put("password", "password");
            put("refreshPeriodIfFail", 100);
            put("host", new HashMap<String, Object>(){{
                put("domain", "http://test.com");
                put("path", "test");
            }});
        }});

        defaultConfig.setProperty("allowedStation", new HashMap<String, Object>(){{
            put("unit_1", null);
        }});

        AzureConfig config = new AzureConfig(defaultConfig);

        assertEquals(42, config.getLimitPerSensor());
        assertEquals(LocalDateTime.MIN, config.getStartDate());
        assertNotNull(config.getSensorInfoHost());
        assertEquals("http://test.com", config.getSensorInfoHost().getDomain());
        assertEquals("test", config.getSensorInfoHost().getPath());
        assertNotNull(config.getSensorDataHost());
        assertEquals("http://test.com", config.getSensorDataHost().getDomain());
        assertEquals("test", config.getSensorDataHost().getPath());
        assertNotNull(config.getAuthentication());
        assertEquals("username", config.getAuthentication().getUsername());
        assertEquals("password", config.getAuthentication().getPassword());
        assertEquals(100, config.getAuthentication().getRefreshPeriodIfFail().orElse(0));
        assertNotNull(config.getAuthentication().getHost());
        assertEquals("http://test.com", config.getAuthentication().getHost().getDomain());
        assertEquals("test", config.getAuthentication().getHost().getPath());
        assertTrue(config.getAllowedStation().isAllowed("unit_1"));
    }

    @Test
    void toString_ConvertJson_True() {

        DefaultConfig defaultConfig = new DefaultConfig("azure",null);
        defaultConfig.setProperty("startDate", LocalDateTime.MIN);
        defaultConfig.setProperty("limitPerSensor", 42);
        defaultConfig.setProperty("sensorInfoHost", new HashMap<String, Object>(){{
            put("domain", "http://test.com");
            put("path", "test");
        }});
        defaultConfig.setProperty("sensorDataHost", new HashMap<String, Object>(){{
            put("domain", "http://test.com");
            put("path", "test");
        }});
        defaultConfig.setProperty("authentication", new HashMap<String, Object>(){{
            put("username", "username");
            put("password", "password");
            put("refreshPeriodIfFail", 100);
            put("host", new HashMap<String, Object>(){{
                put("domain", "http://test.com");
                put("path", "test");
            }});
        }});

        defaultConfig.setProperty("allowedStation", new HashMap<String, Object>(){{
            put("unit_1", null);
        }});

        String jsonConfig = new AzureConfig(defaultConfig).toString();
        AzureConfig config = jsonToObject(jsonConfig, AzureConfig.class);

        assertEquals(42, config.getLimitPerSensor());
        assertEquals(LocalDateTime.MIN, config.getStartDate());
        assertNotNull(config.getSensorInfoHost());
        assertEquals("http://test.com", config.getSensorInfoHost().getDomain());
        assertEquals("test", config.getSensorInfoHost().getPath());
        assertNotNull(config.getSensorDataHost());
        assertEquals("http://test.com", config.getSensorDataHost().getDomain());
        assertEquals("test", config.getSensorDataHost().getPath());
        assertNotNull(config.getAuthentication());
        assertEquals("username", config.getAuthentication().getUsername());
        assertEquals("password", config.getAuthentication().getPassword());
        assertEquals(100, config.getAuthentication().getRefreshPeriodIfFail().orElse(0));
        assertNotNull(config.getAuthentication().getHost());
        assertEquals("http://test.com", config.getAuthentication().getHost().getDomain());
        assertEquals("test", config.getAuthentication().getHost().getPath());
        assertTrue(config.getAllowedStation().isAllowed("unit_1"));
    }
}