package com.google.graphgeckos.dashboard.buildbot;

import com.google.graphgeckos.dashboard.datatypes.BuildBotData;
import com.google.graphgeckos.dashboard.fetchers.buildbot.BuildBotClient;
import com.google.graphgeckos.dashboard.storage.DatastoreRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BuildBotClientTest {

  static class BuildBotDataMatcher implements ArgumentMatcher<BuildBotData> {

    @Override
    public boolean matches(BuildBotData data) {
      return data.isValid();
    }
  }

  @InjectMocks
  BuildBotClient client;

  @Mock
  DatastoreRepository datastoreRepository;

  /** Enables Mockito. */
  @Before
  public void init() {
    MockitoAnnotations.initMocks(this);
  }

  public final String VALID_BUILD_BOT = "clang-x86_64-debian-fast";
  public final int VALID_BUILD_ID = 36624;

  @Test
  public void verifyValidUpdateCallToRepository() {
    client.run(VALID_BUILD_BOT, VALID_BUILD_ID);
    Mockito.verify(datastoreRepository, Mockito.after(client.getRequestFrequency() * 2000 - 10))
      .updateRevisionEntry(Mockito.argThat(new BuildBotDataMatcher()));
  }

}

@@ -687,17 +687,16 @@
 Data%3E %7B%0A
-%0A
     @Ove
@@ -827,16 +827,88 @@
 t client
+ = new BuildBotClient(%0A    %22http://lab.llvm.org:8011/json/builders/%22, 1)
 ;%0A%0A  @Mo
@@ -1126,16 +1126,17 @@
 -fast%22;%0A
+%0A
   public
@@ -1169,16 +1169,57 @@
 = 36624;
+%0A  public final int INVALID_BUILD_ID = 0;
 %0A%0A  @Tes
@@ -1245,16 +1245,29 @@
 ifyValid
+ResponseCause
 UpdateCa
@@ -1508,10 +1508,230 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void verifyInvalidResponseCausesNoCallToReposiory() %7B%0A    client.run(VALID_BUILD_BOT, INVALID_BUILD_ID);%0A    Mockito.verify(datastoreRepository, Mockito.never()).updateRevisionEntry(Mockito.any());%0A  %7D%0A%0A
 %7D%0A

add test for invalid args