package com.khmelenko.lab.travisclient;

import com.khmelenko.lab.travisclient.network.request.AccessTokenRequest;
import com.khmelenko.lab.travisclient.network.response.AccessToken;
import com.khmelenko.lab.travisclient.network.response.Build;
import com.khmelenko.lab.travisclient.network.response.BuildHistory;
import com.khmelenko.lab.travisclient.network.response.Requests;
import com.khmelenko.lab.travisclient.network.retrofit.EmptyOutput;
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.api.mockito.PowerMockito;
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, TaskManager.class, RestartBuildTask.class,
        BuildHistory.class, Requests.class, AccessTokenRequest.class, AccessToken.class})
public class TestTaskManager {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    private TaskManager mTaskManager;
    private TaskHelper mTaskHelper;
    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);

        mTaskHelper = spy(new TaskHelper(mRestClient, mEventBus));
        mTaskManager = spy(new TaskManager(mTaskHelper));
    }

    @Test
    public void testTaskExecution() {
        RestartBuildTask task = spy(new RestartBuildTask(0));
        TaskHelper taskHelper = new TaskHelper(mRestClient, mEventBus);

        LoaderAsyncTask.executeTask(task, taskHelper);
        verify(task).execute();
    }

    @Test
    public void testRestartBuildTask() {
        mTaskManager.restartBuild(anyLong());
        verify(mRestClient.getApiService()).restartBuild(anyLong(), eq(EmptyOutput.INSTANCE));
    }

    @Test
    public void testCancelBuildTask() {
        mTaskManager.cancelBuild(anyLong());
        verify(mRestClient.getApiService()).cancelBuild(anyLong(), eq(EmptyOutput.INSTANCE));
    }

    @Test
    public void testSearchRepo() {
        mTaskManager.findRepos("test");
        verify(mRestClient.getApiService()).getRepos("test");

        mTaskManager.findRepos(eq(""));
        verify(mRestClient.getApiService()).getRepos();
    }

    @Test
    public void testGetUserRepos() {
        mTaskManager.userRepos(anyString());
        verify(mRestClient.getApiService()).getUserRepos(anyString());
    }

    @Test
    public void testGetBuildHistory() {
        mTaskManager.getBuildHistory("test");
        verify(mRestClient.getApiService()).getBuilds("test");
    }

    @Test
    public void testGetBranches() {
        mTaskManager.getBranches("test");
        verify(mRestClient.getApiService()).getBranches("test");
    }

    @Test
    public void testGetRequests() {
        when(mRestClient.getApiService().getRequests(anyString())).thenReturn(spy(new Requests()));
        when(mRestClient.getApiService().getPullRequestBuilds(anyString())).thenReturn(mock(BuildHistory.class));

        mTaskManager.getRequests("test");
        verify(mRestClient.getApiService()).getRequests("test");
        verify(mRestClient.getApiService()).getPullRequestBuilds("test");
    }

    @Test
    public void testGetBuildDetails() {
        mTaskManager.getBuildDetails("test", 0);
        verify(mRestClient.getApiService()).getBuild("test", 0);
    }

    @Test
    public void testUser() {
        mTaskManager.getUser();
        verify(mRestClient.getApiService()).getUser();
    }

}

@@ -505,32 +505,102 @@
 thubApiService;%0A
+import com.khmelenko.lab.travisclient.network.retrofit.RawApiService;%0A
 import com.khmel
@@ -650,16 +650,16 @@
 Client;%0A
-
 import c
@@ -1434,16 +1434,49 @@
 ventBus;
+%0Aimport retrofit.client.Response;
 %0A%0Aimport
@@ -1794,24 +1794,40 @@
 lient.class,
+ Response.class,
  TaskHelper.
@@ -2578,16 +2578,153 @@
 ervice);
+%0A        RawApiService rawApiService = mock(RawApiService.class);%0A        when(mRestClient.getRawApiService()).thenReturn(rawApiService);
 %0A%0A      
@@ -4998,24 +4998,24 @@
 .getUser();%0A
-
         veri
@@ -5064,10 +5064,636 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testGetLogs() %7B%0A        Response response = mock(Response.class);%0A        when(response.getStatus()).thenReturn(200);%0A        when(response.getUrl()).thenReturn(%22url%22);%0A        when(mRestClient.getRawApiService().getLog(anyString())).thenReturn(response);%0A        when(mRestClient.getRawApiService().getLog(anyString(), anyString())).thenReturn(response);%0A%0A        mTaskManager.getLogUrl(anyLong());%0A        verify(mRestClient.getRawApiService()).getLog(anyString());%0A%0A        mTaskManager.getLogUrl(%22test%22, 0);%0A        verify(mRestClient.getRawApiService()).getLog(anyString(), anyString());%0A    %7D%0A%0A
 %7D%0A

Add tests for getting log file of the build