package com.google.graphgeckos.dashboard.api;

import com.google.cloud.Timestamp;
import com.google.graphgeckos.dashboard.datatypes.*;
import com.google.graphgeckos.dashboard.storage.DatastoreRepository;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = DashboardController.class)
@AutoConfigureMockMvc
public class DashboardContollerTest {

  @Autowired
  private MockMvc mvc;

  private final String COMMIT_HASH_A = "1234";
  private final String COMMIT_HASH_B = "abcd";

  private final Timestamp TIMESTAMP = Timestamp.now();

  private final String BRANCH_A = "branch a";
  private final String BRANCH_B = "branch b";

  private final List<Log> LOGS_A = Arrays.asList(new Log("type 1", "link1"),
                                               new Log("type 2", "link2"));
  private final List<Log> LOGS_B = Collections.singletonList(new Log("type 3", "link3"));

  private final GitHubData INITIAL_GITHUB_DATA_A = new GitHubData(COMMIT_HASH_A, TIMESTAMP, BRANCH_A);
  private final GitHubData INITIAL_GITHUB_DATA_B = new GitHubData(COMMIT_HASH_B, TIMESTAMP, BRANCH_B);

  private final BuildInfo BUILD_INFO_A = new BuildInfo(INITIAL_GITHUB_DATA_A);
  private final BuildInfo BUILD_INFO_B = new BuildInfo(INITIAL_GITHUB_DATA_B);

  private final BuildBotData BUILDER_A = new BuildBotData(
                                        COMMIT_HASH_A, "Builder A", LOGS_A, BuilderStatus.PASSED);
  private final BuildBotData BUILDER_B = new BuildBotData(
                                         COMMIT_HASH_B, "Builder B", LOGS_B, BuilderStatus.FAILED);

  private final int ONE_REVISION = 1;
  private final int TWO_REVISIONS = 2;
  private final int OFFSET_ZERO = 0;

  @BeforeClass
  public void createBuildInfos() {
    BUILD_INFO_A.addBuilder(BUILDER_A);
    BUILD_INFO_B.addBuilder(BUILDER_B);
  }

  @MockBean
  private DatastoreRepository datastoreRepository;


  @Before
  public void setUpRepository() {
    Mockito.when(
      datastoreRepository.getLastRevisionEntries(ONE_REVISION, OFFSET_ZERO))
        .thenReturn(Collections.singletonList(BUILD_INFO_A));

    Mockito.when(
      datastoreRepository.getLastRevisionEntries(TWO_REVISIONS, OFFSET_ZERO))
      .thenReturn(Arrays.asList(BUILD_INFO_A, BUILD_INFO_B));
  }

  @Test
  public void whenGivenOneRevisionOffsetZeroReturnsOneBuildInfo() throws Exception {
    ResultActions result = mvc.perform(
        MockMvcRequestBuilders.get("/builders/number={number}/offset={offset}",
          ONE_REVISION, OFFSET_ZERO)
       .contentType("application/json")
       .accept(MediaType.APPLICATION_JSON));
  }
}

@@ -916,36 +916,8 @@
 s;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1353,32 +1353,39 @@
 mvc;%0A%0A  private 
+static 
 final String COM
@@ -1407,32 +1407,39 @@
 234%22;%0A  private 
+static 
 final String COM
@@ -1466,24 +1466,31 @@
 %0A%0A  private 
+static 
 final Timest
@@ -1525,32 +1525,39 @@
 w();%0A%0A  private 
+static 
 final String BRA
@@ -1582,24 +1582,31 @@
 ;%0A  private 
+static 
 final String
@@ -1632,32 +1632,39 @@
  b%22;%0A%0A  private 
+static 
 final List%3CLog%3E 
@@ -1796,24 +1796,31 @@
 ;%0A  private 
+static 
 final List%3CL
@@ -1890,32 +1890,39 @@
 %22));%0A%0A  private 
+static 
 final GitHubData
@@ -2004,24 +2004,31 @@
 ;%0A  private 
+static 
 final GitHub
@@ -2111,32 +2111,39 @@
 _B);%0A%0A  private 
+static 
 final BuildInfo 
@@ -2197,32 +2197,39 @@
 A_A);%0A  private 
+static 
 final BuildInfo 
@@ -2284,32 +2284,39 @@
 _B);%0A%0A  private 
+static 
 final BuildBotDa
@@ -2453,24 +2453,31 @@
 ;%0A  private 
+static 
 final BuildB
@@ -2749,24 +2749,31 @@
 ss%0A  public 
+static 
 void createB
@@ -3632,18 +3632,161 @@
 ION_JSON));%0A
+    result.andExpect(content().contentType(MediaType.APPLICATION_JSON))%0A          .andExpect(jsonPath(%22%5B0%5D.commitHash%22).value(COMMIT_HASH_A));%0A
   %7D%0A%7D%0A

add checker