package com.mobify.astro;

import android.support.test.runner.AndroidJUnit4;
import android.webkit.CookieManager;

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.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

@RunWith(AndroidJUnit4.class)
public class CookiesStoreTest {

    private final String TESTURL = "https://mobify.com";

    CookiesStore cookiesStore;

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

        cookiesStore = new CookiesStore(mockEventManager, mockMessageSender);
    }

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

    @Test(expected = CookiesStore.CookiesStoreException.class)
    public void setMethodThrowsExceptionForNullKey() throws Exception {
        cookiesStore.setCookie(TESTURL, null, "Hello");
    }

    @Test(expected = CookiesStore.CookiesStoreException.class)
    public void setMethodThrowsExceptionForEmptyKey() throws Exception {
        cookiesStore.setCookie(TESTURL, "", "Hello");
    }

    @Test(expected = CookiesStore.CookiesStoreException.class)
    public void setMethodThrowsExceptionForNullValue() throws Exception {
        cookiesStore.setCookie(TESTURL, "Key", null);
    }

    @Test(expected = CookiesStore.CookiesStoreException.class)
    public void setMethodThrowsExceptionForNullURL() throws Exception {
        cookiesStore.setCookie(null, "Key", "value");
    }

    @Test(expected = CookiesStore.CookiesStoreException.class)
    public void setMethodThrowsExceptionForEmptyURL() throws Exception {
        cookiesStore.setCookie("", "Key", "value");
    }

    @Test
    public void setGetValueForCookie() throws Exception {
        CookieManager cm = CookieManager.getInstance();
        cm.removeAllCookie();
        assertNull(cm.getCookie(TESTURL));
        cookiesStore.setCookie(TESTURL, "key", "Hello");
        assertNotNull(cm.getCookie(TESTURL));
    }
}
