package com.iterable.iterableapi;

import com.iterable.iterableapi.unit.TestRunner;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(TestRunner.class)
public class HealthMonitorTest extends BaseTest {
    private IterableTaskStorage mockTaskStorage;
    private IterableTaskRunner mockTaskRunner;
    private TaskScheduler mockTaskScheduler;

    @Before
    public void setUp() {
        mockTaskStorage = mock(IterableTaskStorage.class);
        mockTaskRunner = mock(IterableTaskRunner.class);
        mockTaskScheduler = mock(TaskScheduler.class);
    }

    @Test
    public void testUseOfflineProcessorByDefault() throws Exception {
        IterableApiRequest request = new IterableApiRequest("apiKey", "api/test", new JSONObject(), "POST", null, null, null);
        assertEquals("Online", request.getProcessorType().toString());
    }


    @Test
    public void canScheduleFailWhenMaxCountReached() throws Exception {
        HealthMonitor healthMonitor = new HealthMonitor(mockTaskStorage);
        when(mockTaskStorage.numberOfTasks()).thenReturn(IterableConstants.MAX_OFFLINE_OPERATION);
        assertFalse(healthMonitor.canSchedule());
    }

    @Test
    public void canScheduleWhenMaxCountNotReached() throws Exception {
        HealthMonitor healthMonitor = new HealthMonitor(mockTaskStorage);
        when(mockTaskStorage.numberOfTasks()).thenReturn(IterableConstants.MAX_OFFLINE_OPERATION - 1);
        assertTrue(healthMonitor.canSchedule());
    }

    @Test
    public void canProcessReturnTrueIfDBok() throws Exception {
        HealthMonitor healthMonitor = new HealthMonitor(mockTaskStorage);
        assertTrue(healthMonitor.canProcess());
    }

    @Test
    public void canProcessReturnFalseIfDBError() throws Exception {
        IterableTaskStorage taskStorage = IterableTaskStorage.sharedInstance(getContext());
        HealthMonitor healthMonitor = new HealthMonitor(taskStorage);
        healthMonitor.onNextTaskError();
        assertFalse(healthMonitor.canProcess());
    }

}

@@ -840,294 +840,8 @@
  %7D%0A%0A
-    @Test%0A    public void testUseOfflineProcessorByDefault() throws Exception %7B%0A        IterableApiRequest request = new IterableApiRequest(%22apiKey%22, %22api/test%22, new JSONObject(), %22POST%22, null, null, null);%0A        assertEquals(%22Online%22, request.getProcessorType().toString());%0A    %7D%0A%0A%0A
     
@@ -2001,11 +2001,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

remove incomplete test