package com.mobify.astro;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.mobify.astro.messaging.EventManager;
import com.mobify.astro.messaging.MessageSender;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

@RunWith(AndroidJUnit4.class)
public class SettingsStoreTest {

    SettingsStore settingsStore;

    @Before
    public void setUp() {
        EventManager mockEventManager = mock(EventManager.class);
        MessageSender mockMessageSender = mock(MessageSender.class);

        settingsStore = new SettingsStore(InstrumentationRegistry.getTargetContext(), mockEventManager, mockMessageSender);
    }

    @Test
    public void getInstanceAddressReturnsExpectedAddressForSingleInstance() {
        String instanceAddress = settingsStore.getInstanceAddress();
        assertEquals(settingsStore.ADDRESS, instanceAddress);
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void setMethodThrowsExceptionForNullKey() throws Exception {
        settingsStore.set(null, "Hello");
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void setMethodThrowsExceptionForEmptyKey() throws Exception {
        settingsStore.set("", "Hello");
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void setMethodThrowsExceptionForNullValue() throws Exception {
        settingsStore.set("Key", null);
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void getMethodThrowsExceptionForNullKey() throws Exception {
        settingsStore.get(null);
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void getMethodThrowsExceptionForEmptyKey() throws Exception {
        settingsStore.get("");
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void deleteMethodThrowsExceptionForNullKey() throws Exception {
        settingsStore.delete(null);
    }

    @Test(expected = SettingsStore.SettingsStoreException.class)
    public void deleteMethodThrowsExceptionForEmptyKey() throws Exception {
        settingsStore.delete("");
    }

    @Test
    public void setGetDeleteValueForKey() throws Exception {
        String key = "testKey";
        String value = "testValue";

        settingsStore.set(key, value);
        String returnedValue = settingsStore.get(key);

        assertEquals(value, returnedValue);

        settingsStore.delete(key);

        returnedValue = settingsStore.get(key);
        assertNull(returnedValue);
    }
}
