/**
   * Tests a scenario when two builds are successful and both configured to be skipped. Expected
   * outcome is that {@link BuildMemory.MemoryImprint#wereAllBuildsSuccessful()} will return true.
   * As before the skip vote feature was implemented.
   */
  @Test
  public void testWereAllBuildsSuccessfulTwoSuccessfulBothSkipped() {
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

    AbstractProject project = mock(AbstractProject.class);
    SkipVote skipVote = new SkipVote(true, false, false, false);
    GerritTrigger trigger = mock(GerritTrigger.class);
    when(trigger.getSkipVote()).thenReturn(skipVote);
    when(project.getTrigger(eq(GerritTrigger.class))).thenReturn(trigger);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);
    when(build.getResult()).thenReturn(Result.SUCCESS);
    instance.started(event, build);

    AbstractProject project2 = mock(AbstractProject.class);
    skipVote = new SkipVote(true, false, false, false);
    trigger = mock(GerritTrigger.class);
    when(trigger.getSkipVote()).thenReturn(skipVote);
    when(project2.getTrigger(eq(GerritTrigger.class))).thenReturn(trigger);
    AbstractBuild build2 = mock(AbstractBuild.class);
    when(build2.getProject()).thenReturn(project2);
    when(build2.getResult()).thenReturn(Result.SUCCESS);
    instance.started(event, build2);

    instance.completed(event, build);
    instance.completed(event, build2);

    MemoryImprint memoryImprint = instance.getMemoryImprint(event);
    assertTrue(memoryImprint.wereAllBuildsSuccessful());
  }
  /**
   * Tests the isBuilding method of the class {@link BuildMemory}. With two events with started
   * builds in memory.
   */
  @Test
  public void testIsBuildingProjectTrue2() {
    PatchsetCreated event = Setup.createPatchsetCreated();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);

    BuildMemory instance = new BuildMemory();
    instance.started(event, build);

    PatchsetCreated event2 = Setup.createPatchsetCreated();
    event2.getChange().setNumber(event.getChange().getNumber() + 34);
    AbstractProject project2 = mock(AbstractProject.class);
    build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project2);
    instance.started(event2, build);

    AbstractProject project3 = mock(AbstractProject.class);
    build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project3);
    instance.started(event2, build);

    assertTrue(instance.isBuilding(event, project));
    assertTrue(instance.isBuilding(event2, project2));
    assertTrue(instance.isBuilding(event2, project3));
  }
  /**
   * Tests the retriggered method of the class {@link BuildMemory}. With two started builds and the
   * one to be retriggered as completed already in memory.
   */
  @Test
  public void testRetriggeredExistingMemory() {
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);
    AbstractProject project2 = mock(AbstractProject.class);
    AbstractBuild build2 = mock(AbstractBuild.class);
    when(build2.getProject()).thenReturn(project2);
    AbstractProject project3 = mock(AbstractProject.class);
    AbstractBuild build3 = mock(AbstractBuild.class);
    when(build3.getProject()).thenReturn(project3);

    instance.started(event, build);
    instance.completed(event, build2);
    instance.started(event, build3);

    instance.retriggered(event, project2, null);
    MemoryImprint memory = instance.getMemoryImprint(event);
    assertNotNull(memory);
    assertEquals(3, memory.getEntries().length);

    MemoryImprint.Entry entry = null;
    for (MemoryImprint.Entry e : memory.getEntries()) {
      if (e.getProject().equals(project2)) {
        entry = e;
        break;
      }
    }
    assertNotNull(entry);
    assertFalse(entry.isBuildCompleted());
  }
  /** test. */
  @Test
  public void testGetBuildsStartedStats() {
    System.out.println("getBuildsStartedStats");

    BuildMemory instance = new BuildMemory();
    PatchsetCreated event = Setup.createPatchsetCreated();

    instance.triggered(event, mock(AbstractProject.class));

    AbstractBuild mock = mock(AbstractBuild.class);
    AbstractProject project = mock(AbstractProject.class);
    when(mock.getProject()).thenReturn(project);
    instance.started(event, mock);

    instance.triggered(event, mock(AbstractProject.class));

    mock = mock(AbstractBuild.class);
    project = mock(AbstractProject.class);
    when(mock.getProject()).thenReturn(project);
    instance.started(event, mock);

    BuildsStartedStats result = instance.getBuildsStartedStats(event);
    assertEquals(event, result.getEvent());
    // CS IGNORE MagicNumber FOR NEXT 3 LINES. REASON: mock.
    assertEquals(4, result.getTotalBuildsToStart());
    assertEquals(2, result.getStartedBuilds());
    assertEquals("(2/4)", result.toString());
  }
  /** test. */
  @Test
  public void testIsAllBuildsStartedBuildMemoryPatchSetKey() {
    System.out.println("isAllBuildsStarted");
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

    AbstractBuild mock = mock(AbstractBuild.class);
    AbstractProject project = mock(AbstractProject.class);
    when(mock.getProject()).thenReturn(project);
    instance.started(event, mock);

    mock = mock(AbstractBuild.class);
    project = mock(AbstractProject.class);
    when(mock.getProject()).thenReturn(project);
    instance.started(event, mock);

    boolean expResult = true;
    boolean result = instance.isAllBuildsStarted(event);
    assertEquals(expResult, result);
  }
  /**
   * Tests the isBuilding method of the class {@link BuildMemory}. With one started project in
   * memory.
   */
  @Test
  public void testIsBuildingProjectTrue() {
    PatchsetCreated event = Setup.createPatchsetCreated();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);

    BuildMemory instance = new BuildMemory();
    instance.started(event, build);
    assertTrue(instance.isBuilding(event, project));
  }
  /** Tests the isBuilding method of the class {@link BuildMemory}. With a forgotten build. */
  @Test
  public void testIsBuildingFalseWhenForgotten() {
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);
    PatchSetKey key = instance.started(event, build);
    instance.forget(key);
    assertFalse(instance.isBuilding(event));
  }
  /** test. */
  @Test
  public void testStarted() {
    System.out.println("started");
    PatchsetCreated event = Setup.createPatchsetCreated();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);

    BuildMemory instance = new BuildMemory();
    instance.started(event, build);
    assertTrue(instance.isAllBuildsStarted(event));
    assertFalse(instance.isAllBuildsCompleted(event));
  }
  /** test. */
  @Test
  public void testIsAllBuildsCompletedFalse() {
    System.out.println("isAllBuildsCompleted");
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

    AbstractProject project = mock(AbstractProject.class);
    AbstractBuild build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);
    instance.started(event, build);

    project = mock(AbstractProject.class);
    build = mock(AbstractBuild.class);
    when(build.getProject()).thenReturn(project);
    instance.completed(event, build);

    boolean expResult = false;
    boolean result = instance.isAllBuildsCompleted(event);
    assertEquals(expResult, result);
  }