package com.quinientoscuarenta.simplekeyvaluestorage;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import static java.util.Collections.EMPTY_LIST;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class SimpleKeyValueStorageTest {

    public static final String TEST_KEY = "key";
    public static final String ANOTHER_TEST_KEY = "another_key";
    public static final String UNAVAILABLE_KEY = "unavailable_key";

    private SharedPreferences defaultSharedPreferences;

    private SimpleKeyValueStorage simpleKeyValueStorage;

    @Before
    public void setUp() throws Exception {
        defaultSharedPreferences = RuntimeEnvironment.application
                .getSharedPreferences("skvs_default_prefs", Context.MODE_PRIVATE);

        simpleKeyValueStorage = SimpleKeyValueStorage.initDefault(RuntimeEnvironment.application);
    }

    @Test
    public void returns_stored_object() throws Exception {
        TestClass testObject = TestClass.build1();
        storeTestObjectIntoDefaultSharedPrefs(testObject);

        TestClass storedObject = simpleKeyValueStorage.get(TEST_KEY, TestClass.class);

        assertThat(storedObject, equalTo(testObject));
    }

    @Test
    public void returns_another_stored_object() throws Exception {
        TestClass testObject = TestClass.build2();
        storeTestObjectIntoDefaultSharedPrefs(testObject);

        TestClass storedObject = simpleKeyValueStorage.get(TEST_KEY, TestClass.class);

        assertThat(storedObject, equalTo(testObject));
    }

    @Test
    public void returns_null_for_unavailable_key() throws Exception {
        assertNull(simpleKeyValueStorage.get(UNAVAILABLE_KEY, TestClass.class));
    }

    @Test
    public void saves_another_class_object() throws Exception {
        Locale locale = Locale.ENGLISH;

        simpleKeyValueStorage.set(ANOTHER_TEST_KEY, locale);

        assertThat(simpleKeyValueStorage.get(ANOTHER_TEST_KEY, Locale.class), equalTo(locale));
    }

    @Test
    public void set_value_is_set() throws Exception {
        TestClass testObject = TestClass.build1();

        simpleKeyValueStorage.set(TEST_KEY, testObject);

        assertThat(simpleKeyValueStorage.isSet(TEST_KEY), is(true));
    }

    @Test
    public void unavailable_key_is_not_set() throws Exception {
        assertThat(simpleKeyValueStorage.isSet(UNAVAILABLE_KEY), is(false));
    }

    @Test
    public void deletes_value() throws Exception {
        TestClass testObject = TestClass.build1();
        simpleKeyValueStorage.set(TEST_KEY, testObject);

        simpleKeyValueStorage.delete(TEST_KEY);

        assertThat(simpleKeyValueStorage.isSet(TEST_KEY), is(false));
    }

    @Test
    public void clears_all_values() throws Exception {
        simpleKeyValueStorage.set(TEST_KEY, TestClass.build1());
        simpleKeyValueStorage.set(ANOTHER_TEST_KEY, TestClass.build2());

        simpleKeyValueStorage.clear();

        assertThat(simpleKeyValueStorage.isSet(TEST_KEY), is(false));
        assertThat(simpleKeyValueStorage.isSet(ANOTHER_TEST_KEY), is(false));
    }

    @Test
    public void saves_list() throws Exception {
        List<TestClass> testObjects = Arrays.asList(TestClass.build1(), TestClass.build2());

        simpleKeyValueStorage.set(TEST_KEY, testObjects);

        assertThat(simpleKeyValueStorage.getList(TEST_KEY, TestClass[].class),
                equalTo(testObjects));
    }

    @Test
    public void returns_empty_list_for_unavailable_list_key() throws Exception {
        List<TestClass> list = simpleKeyValueStorage.getList(UNAVAILABLE_KEY, TestClass[].class);

        assertThat(list, is(EMPTY_LIST));
    }

    @Test
    public void initializes_with_custom_name() {
        SimpleKeyValueStorage customNameKeyValueStorage = SimpleKeyValueStorage.builder()
                .withName("custom_name").init(RuntimeEnvironment.application);
        storeTestObjectIntoDefaultSharedPrefs(TestClass.build1());

        TestClass storedObject = customNameKeyValueStorage.get(TEST_KEY, TestClass.class);

        assertThat(storedObject, nullValue());
    }

    private void storeTestObjectIntoDefaultSharedPrefs(TestClass testObject) {
        defaultSharedPreferences.edit().putString(TEST_KEY, testObject.toString()).apply();
    }

    @After
    public void tearDown() throws Exception {
        defaultSharedPreferences.edit().clear().apply();
    }
}

@@ -1547,25 +1547,23 @@
  returns
-_s
+S
 tored
-_o
+O
 bject() 
@@ -1879,25 +1879,22 @@
 urns
-_a
+A
 nother
-_s
+S
 tored
-_o
+O
 bjec
@@ -2214,19 +2214,16 @@
 urns
-_null_for_u
+NullForU
 nava
@@ -2228,18 +2228,17 @@
 vailable
-_k
+K
 ey() thr
@@ -2376,24 +2376,21 @@
 aves
-_a
+A
 nother
-_c
+C
 lass
-_o
+O
 bjec
@@ -2651,19 +2651,16 @@
  set
-_v
+V
 alue
-_is_s
+IsS
 et()
@@ -2906,21 +2906,17 @@
 able
-_key_is_not_s
+KeyIsNotS
 et()
@@ -3052,18 +3052,17 @@
  deletes
-_v
+V
 alue() t
@@ -3350,14 +3350,12 @@
 ears
-_all_v
+AllV
 alue
@@ -3742,18 +3742,17 @@
 id saves
-_l
+L
 ist() th
@@ -4083,25 +4083,21 @@
 urns
-_e
+E
 mpty
-_list_for_u
+ListForU
 nava
@@ -4106,15 +4106,13 @@
 able
-_list_k
+ListK
 ey()
@@ -4320,22 +4320,19 @@
 izes
-_with_c
+WithC
 ustom
-_n
+N
 ame(

Fix test naming