/**
   * 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 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 testIsAllBuildsCompletedTrue() {
    System.out.println("isAllBuildsCompleted True");
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

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

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

    boolean expResult = true;
    boolean result = instance.isAllBuildsCompleted(event);
    assertEquals(expResult, result);
  }
 /**
  * Tests the isBuilding method of the class {@link BuildMemory}. With a completed build in memory.
  */
 @Test
 public void testIsBuildingProjectCompletedFalse() {
   PatchsetCreated event = Setup.createPatchsetCreated();
   BuildMemory instance = new BuildMemory();
   AbstractProject project = mock(AbstractProject.class);
   AbstractBuild build = mock(AbstractBuild.class);
   when(build.getProject()).thenReturn(project);
   when(build.getResult()).thenReturn(Result.UNSTABLE);
   instance.completed(event, build);
   assertFalse(instance.isBuilding(event, project));
 }
  /** test. */
  @Test
  public void testCompleted() {
    System.out.println("completed");
    PatchsetCreated event = Setup.createPatchsetCreated();

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

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

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

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

    instance.forget(event);
    assertNull(instance.getMemoryImprint(event));
  }
  /** test. */
  @Test
  public void testIsAllBuildsCompletedBuildMemoryPatchSetKeyFalse() {
    System.out.println("isAllBuildsCompleted True");
    PatchsetCreated event = Setup.createPatchsetCreated();
    BuildMemory instance = new BuildMemory();

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

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

    boolean expResult = false;
    PatchSetKey key =
        new PatchSetKey(event.getChange().getNumber(), event.getPatchSet().getNumber());
    boolean result = instance.isAllBuildsCompleted(key);
    assertEquals(expResult, result);
  }