package com.khmelenko.lab.travisclient;

import com.khmelenko.lab.travisclient.network.retrofit.GithubApiService;
import com.khmelenko.lab.travisclient.network.retrofit.RestClient;
import com.khmelenko.lab.travisclient.network.retrofit.TravisApiService;
import com.khmelenko.lab.travisclient.task.LoaderAsyncTask;
import com.khmelenko.lab.travisclient.task.TaskHelper;
import com.khmelenko.lab.travisclient.task.TaskManager;
import com.khmelenko.lab.travisclient.task.travis.RestartBuildTask;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import de.greenrobot.event.EventBus;

import static org.mockito.Mockito.*;

/**
 * Testing TaskManager class
 *
 * @author Dmytro Khmelenko (d.khmelenko@gmail.com)
 */
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest({RestClient.class, TaskHelper.class, RestartBuildTask.class})
public class TestTaskManager {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    private TaskManager mTaskManager;
    private EventBus mEventBus;
    private RestClient mRestClient;

    @Before
    public void setupMock() {
        mRestClient = mock(RestClient.class);
        TravisApiService apiService = mock(TravisApiService.class);
        when(mRestClient.getApiService()).thenReturn(apiService);
        GithubApiService githubApiService = mock(GithubApiService.class);
        when(mRestClient.getGithubApiService()).thenReturn(githubApiService);

        mEventBus = mock(EventBus.class);

        TaskHelper taskHelper = mock(TaskHelper.class);
        mTaskManager = new TaskManager(taskHelper);
    }

    @Test
    public void testLoaderAsyncTask() {

        RestartBuildTask task = spy(new RestartBuildTask(0));
        TaskHelper taskHelper = new TaskHelper(mRestClient, mEventBus);

        LoaderAsyncTask.executeTask(task, taskHelper);

        verify(task).execute();
    }

}

@@ -34,16 +34,84 @@
 lient;%0A%0A
+import com.khmelenko.lab.travisclient.network.retrofit.EmptyOutput;%0A
 import c
@@ -659,16 +659,63 @@
 unWith;%0A
+import org.powermock.api.mockito.PowerMockito;%0A
 import o
@@ -1375,16 +1375,35 @@
 r.class,
+ TaskManager.class,
  Restart
@@ -1554,16 +1554,52 @@
 anager;%0A
+    private TaskHelper mTaskHelper;%0A
     priv
@@ -2077,32 +2077,33 @@
 lass);%0A%0A        
+m
 TaskHelper taskH
@@ -2101,42 +2101,52 @@
 per 
-taskHelper = mock(TaskHelper.class
+= spy(new TaskHelper(mRestClient, mEventBus)
 );%0A 
@@ -2163,24 +2163,28 @@
 skManager = 
+spy(
 new TaskMana
@@ -2187,17 +2187,18 @@
 Manager(
-t
+mT
 askHelpe
@@ -2199,16 +2199,17 @@
 kHelper)
+)
 ;%0A    %7D%0A
@@ -2243,29 +2243,26 @@
 test
-LoaderAsyncTask
+TaskExecution
 () %7B%0A
-%0A
     
@@ -2447,17 +2447,16 @@
 elper);%0A
-%0A
         
@@ -2486,10 +2486,405 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testRestartBuildTask() %7B%0A        mTaskManager.restartBuild(anyLong());%0A        verify(mRestClient.getApiService()).restartBuild(anyLong(), eq(EmptyOutput.INSTANCE));%0A    %7D%0A%0A    @Test%0A    public void testCancelBuildTask() %7B%0A        mTaskManager.cancelBuild(anyLong());%0A        verify(mRestClient.getApiService()).cancelBuild(anyLong(), eq(EmptyOutput.INSTANCE));%0A    %7D%0A%0A
 %7D%0A

Add tests for build cancel request and for build restart request