package it.near.sdk.recipes;

import com.google.common.collect.Maps;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Map;

import it.near.sdk.GlobalConfig;

import static it.near.sdk.recipes.RecipesManager.PULSE_ACTION_ID_KEY;
import static it.near.sdk.recipes.RecipesManager.PULSE_BUNDLE_ID_KEY;
import static it.near.sdk.recipes.RecipesManager.PULSE_PLUGIN_ID_KEY;
import static junit.framework.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.when;

/**
 * Created by cattaneostefano on 01/03/2017.
 */
@RunWith(MockitoJUnitRunner.class)
public class RecipesManagerTest {

    @Mock
    GlobalConfig mockGlobalConfig;
    @Mock
    RecipeCooler mockRecipeCooler;

    @Before
    public void setUp() {
        when(mockGlobalConfig.getAppId()).thenReturn("app_id");
        when(mockGlobalConfig.getInstallationId()).thenReturn("installation_id");
        when(mockGlobalConfig.getProfileId()).thenReturn("profile_id");
    }

    @Test
    public void failTest() {
        fail();
    }

    /*@Test
    public void buildCorrectEvaluationBody_noCoolerNoPulse() throws JSONException {
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, null, null, null, null);
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat(actualObj.has(PULSE_PLUGIN_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_ACTION_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_BUNDLE_ID_KEY), is(false));
        assertThat(actualObj = actualObj.getJSONObject("core"), is(notNullValue()));
        assertThat((String) actualObj.get("profile_id"), is("profile_id"));
        assertThat((String) actualObj.get("installation_id"), is("installation_id"));
        assertThat((String) actualObj.get("app_id"), is("app_id"));
        assertThat(actualObj.has("cooldown"), is(false));
    }

    @Test
    public void buildCorrectEvaluationBody_noCooldownNoPulse() throws JSONException {
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, mockRecipeCooler, null, null, null);
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat(actualObj.has(PULSE_PLUGIN_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_ACTION_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_BUNDLE_ID_KEY), is(false));
        assertThat(actualObj = actualObj.getJSONObject("core"), is(notNullValue()));
        assertThat((String) actualObj.get("profile_id"), is("profile_id"));
        assertThat((String) actualObj.get("installation_id"), is("installation_id"));
        assertThat((String) actualObj.get("app_id"), is("app_id"));
        assertThat(actualObj = actualObj.getJSONObject("cooldown"), is(notNullValue()));
        assertThat(Long.valueOf((Integer)actualObj.get("last_notified_at")), is(0L));
        assertThat(actualObj.getJSONObject("recipes_notified_at").length(), is(0));
    }

    @Test
    public void buildCorrectEvaluationBody_withPulse() throws JSONException {
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, mockRecipeCooler, "plugin", "action", "bundle");
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat((String) actualObj.get(PULSE_PLUGIN_ID_KEY), is("plugin"));
        assertThat((String) actualObj.get(PULSE_ACTION_ID_KEY), is("action"));
        assertThat((String) actualObj.get(PULSE_BUNDLE_ID_KEY), is("bundle"));
    }

    @Test
    public void buildCorrectEvaluationBody_withCooldown() throws JSONException {
        Map<String, Long> logMap = Maps.newHashMap();
        logMap.put("recipe_id_1", 0L);
        logMap.put("recipe_id_2", 1000L);
        when(mockRecipeCooler.getRecipeLogMap()).thenReturn(logMap);
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, mockRecipeCooler, "plugin", "action", "bundle");
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("core"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("cooldown"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("recipes_notified_at"), is(notNullValue()));
        assertThat(actualObj.length(), is(2));
        assertThat(Long.valueOf((Integer)actualObj.get("recipe_id_1")), is(0L));
        assertThat(Long.valueOf((Integer)actualObj.get("recipe_id_2")), is(1000L));
    }
*/
}

@@ -19,24 +19,99 @@
 k.recipes;%0A%0A
+import android.content.Context;%0Aimport android.content.SharedPreferences;%0A%0A
 import com.g
@@ -222,16 +222,39 @@
 Before;%0A
+import org.junit.Rule;%0A
 import o
@@ -370,16 +370,54 @@
 tRunner;
+%0Aimport org.mockito.junit.MockitoRule;
 %0A%0Aimport
@@ -465,16 +465,58 @@
 lConfig;
+%0Aimport it.near.sdk.utils.NearMockitoRule;
 %0A%0Aimport
@@ -1069,16 +1069,99 @@
 Test %7B%0A%0A
+    /*@Rule%0A    public NearMockitoRule mockitoRule = new NearMockitoRule();%0A    */%0A
     @Moc
@@ -1241,16 +1241,142 @@
 eCooler;
+%0A    @Mock%0A    Context mockContext;%0A    @Mock%0A    SharedPreferences mockSharedPreferences;%0A%0A    RecipesManager recipesManager;
 %0A%0A    @B
@@ -1633,63 +1633,118 @@
     
-%7D%0A%0A
     
-@Test%0A    public void failTest() %7B%0A        fail(
+recipesManager = new RecipesManager(mockContext, mockGlobalConfig, mockRecipeCooler, mockSharedPreferences
 );%0A 
@@ -1753,18 +1753,16 @@
  %7D%0A%0A    
-/*
 @Test%0A  
@@ -1859,33 +1859,33 @@
 String actual = 
-R
+r
 ecipesManager.bu
@@ -2747,32 +2747,39 @@
 false));%0A    %7D%0A%0A
+    /*%0A
     @Test%0A    pu

test creates instance, still fails