/**
   * 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 to trigger a build with the same patch set twice, one is a manual event and the other a
   * normal. Expecting one build to be scheduled with two causes of different type..
   *
   * @throws Exception if so.
   */
  @LocalData
  public void testDoubleTriggeredBuildOfDifferentType() throws Exception {
    FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
    server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
    PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
    ManualPatchsetCreated mpc = new ManualPatchsetCreated();
    mpc.setChange(patchsetCreated.getChange());
    mpc.setPatchset(patchsetCreated.getPatchSet());
    mpc.setUploader(patchsetCreated.getUploader());
    mpc.setUserName("bobby");
    PluginImpl.getInstance().triggerEvent(Setup.createPatchsetCreated());
    PluginImpl.getInstance().triggerEvent(mpc);

    RunList<FreeStyleBuild> builds = waitForBuilds(project, 1, 5000);
    FreeStyleBuild build = builds.get(0);
    assertSame(Result.SUCCESS, build.getResult());
    assertEquals(1, builds.size());

    int count = 0;
    for (Cause cause : build.getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
        assertNotNull(((GerritCause) cause).getContext());
        assertNotNull(((GerritCause) cause).getContext().getThisBuild());
        assertNotNull(((GerritCause) cause).getEvent());
      }
    }
    assertEquals(2, count);
  }
  /**
   * Tests to trigger builds with two diffetent patch sets. Expecting two build to be scheduled with
   * one cause each.
   *
   * @throws Exception if so.
   */
  @LocalData
  public void testDoubleTriggeredBuildsOfDifferentChange() throws Exception {
    FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
    server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
    PluginImpl.getInstance().triggerEvent(Setup.createPatchsetCreated());
    PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
    patchsetCreated.getChange().setNumber("2000");
    PluginImpl.getInstance().triggerEvent(patchsetCreated);

    RunList<FreeStyleBuild> builds = waitForBuilds(project, 2, 5000);
    assertEquals(2, builds.size());
    assertSame(Result.SUCCESS, builds.get(0).getResult());
    assertSame(Result.SUCCESS, builds.get(1).getResult());

    int count = 0;
    for (Cause cause : builds.get(0).getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
      }
    }
    assertEquals(1, count);
    count = 0;
    for (Cause cause : builds.get(1).getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
        assertNotNull(((GerritCause) cause).getContext());
        assertNotNull(((GerritCause) cause).getEvent());
      }
    }
    assertEquals(1, count);
  }
 /** Tests the isBuilding method of the class {@link BuildMemory}. With an empty memory. */
 @Test
 public void testIsBuildingProjectFalse() {
   PatchsetCreated event = Setup.createPatchsetCreated();
   BuildMemory instance = new BuildMemory();
   AbstractProject project = mock(AbstractProject.class);
   assertFalse(instance.isBuilding(event, project));
 }
  /** 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());
  }
  /**
   * 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());
  }
  /**
   * Tests {@link TriggerContextConverter#marshal(Object,
   * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
   * com.thoughtworks.xstream.converters.MarshallingContext)}. With an empty list of "others".
   *
   * @throws Exception if so.
   */
  @Test
  public void testMarshalNoOthers() throws Exception {
    TriggeredItemEntity entity = new TriggeredItemEntity(100, "projectX");

    PatchsetCreated event = Setup.createPatchsetCreated();
    TriggerContext context = new TriggerContext(event);
    context.setThisBuild(entity);
    context.setOthers(new LinkedList<TriggeredItemEntity>());

    TestMarshalClass t =
        new TestMarshalClass(context, "Bobby", new TestMarshalClass(context, "SomeoneElse"));

    XStream xStream = new XStream2();
    xStream.registerConverter(new TriggerContextConverter());
    String xml = xStream.toXML(t);

    TestMarshalClass readT = (TestMarshalClass) xStream.fromXML(xml);

    assertNotNull(readT.getEntity());
    assertNotNull(readT.getEntity().getEvent());
    assertNotNull(readT.getEntity().getThisBuild());

    assertEquals("project", readT.getEntity().getEvent().getChange().getProject());
    assertEquals(100, readT.getEntity().getThisBuild().getBuildNumber().intValue());
    assertEquals("projectX", readT.getEntity().getThisBuild().getProjectId());

    assertSame(readT.getEntity(), readT.getTestClass().getEntity());
  }
  /**
   * 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());
  }
 /**
  * Same test logic as {@link #testBuildLatestPatchsetOnly()} except the trigger is configured to
  * not cancel the previous build.
  *
  * @throws Exception if so.
  */
 @LocalData
 public void testNotBuildLatestPatchsetOnly() throws Exception {
   ((Config) PluginImpl.getInstance().getConfig()).setGerritBuildCurrentPatchesOnly(false);
   FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
   project.getBuildersList().add(new SleepBuilder(2000));
   server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
   PatchsetCreated firstEvent = Setup.createPatchsetCreated();
   PluginImpl.getInstance().triggerEvent(firstEvent);
   AbstractBuild firstBuild = waitForBuildToStart(firstEvent, 2000);
   PatchsetCreated secondEvent = Setup.createPatchsetCreated();
   secondEvent.getPatchSet().setNumber("2");
   PluginImpl.getInstance().triggerEvent(secondEvent);
   RunList<FreeStyleBuild> builds = waitForBuilds(project, 2, 5000);
   assertEquals(2, builds.size());
   assertSame(Result.SUCCESS, firstBuild.getResult());
   assertSame(Result.SUCCESS, builds.getFirstBuild().getResult());
   assertSame(Result.SUCCESS, builds.getLastBuild().getResult());
 }
 /**
  * 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));
 }
 /**
  * Tests the retriggered method of the class {@link BuildMemory}. With no previous memory and null
  * list of "others".
  */
 @Test
 public void testRetriggeredNoMemoryOneProjectNullOthers() {
   PatchsetCreated event = Setup.createPatchsetCreated();
   BuildMemory instance = new BuildMemory();
   AbstractProject project = mock(AbstractProject.class);
   instance.retriggered(event, project, null);
   MemoryImprint memory = instance.getMemoryImprint(event);
   assertNotNull(memory);
   assertEquals(1, memory.getEntries().length);
   assertEquals(project, memory.getEntries()[0].getProject());
   assertFalse(memory.getEntries()[0].isBuildCompleted());
 }
  /** 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));
  }
  /**
   * 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));
  }
  /** test. */
  @Test
  public void testTriggered() {
    System.out.println("triggered");
    PatchsetCreated event = Setup.createPatchsetCreated();

    AbstractProject project = mock(AbstractProject.class);

    BuildMemory instance = new BuildMemory();
    instance.triggered(event, project);
    assertNotNull(instance.getMemoryImprint(event));
    assertFalse(instance.isAllBuildsStarted(event));
    assertFalse(instance.isAllBuildsCompleted(event));
  }
  /** 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 testGetMemoryImprint() {
    System.out.println("getMemoryImprint");

    BuildMemory instance = new BuildMemory();
    PatchsetCreated event = Setup.createPatchsetCreated();
    final AbstractProject project = mock(AbstractProject.class);
    instance.triggered(event, project);

    MemoryImprint result = instance.getMemoryImprint(event);
    assertNotNull(result);
    assertEquals(project, result.getEntries()[0].getProject());
    assertEquals(event, result.getEvent());
  }
  /** 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 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();
    PatchSetKey expResult = new PatchSetKey(event);
    PatchSetKey result = instance.started(event, build);
    assertEquals(expResult, result);
    assertTrue(instance.isAllBuildsStarted(result));
    assertFalse(instance.isAllBuildsCompleted(result));
  }
  /** 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;
    boolean result = instance.isAllBuildsCompleted(event);
    assertEquals(expResult, result);
  }
  /** 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);
  }
  /** test. */
  @Test
  public void testIsAllBuildsCompletedBuildMemoryPatchSetKeyTrue() {
    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;
    PatchSetKey key =
        new PatchSetKey(event.getChange().getNumber(), event.getPatchSet().getNumber());
    boolean result = instance.isAllBuildsCompleted(key);
    assertEquals(expResult, result);
  }
  /**
   * Tests {@link TriggerContextConverter#marshal(Object,
   * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
   * com.thoughtworks.xstream.converters.MarshallingContext)}. With list of "others" containing two
   * items.
   *
   * @throws Exception if so.
   */
  @Test
  public void testMarshalWithOthers() throws Exception {
    TriggeredItemEntity entity = new TriggeredItemEntity(100, "projectX");

    PatchsetCreated event = Setup.createPatchsetCreated();
    TriggerContext context = new TriggerContext(event);
    context.setThisBuild(entity);
    LinkedList<TriggeredItemEntity> otherBuilds = new LinkedList<TriggeredItemEntity>();
    otherBuilds.add(new TriggeredItemEntity(1, "projectY"));
    otherBuilds.add(new TriggeredItemEntity(12, "projectZ"));
    context.setOthers(otherBuilds);

    TestMarshalClass t =
        new TestMarshalClass(context, "Bobby", new TestMarshalClass(context, "SomeoneElse"));

    XStream xStream = new XStream2();
    xStream.registerConverter(new TriggerContextConverter());
    String xml = xStream.toXML(t);

    TestMarshalClass readT = (TestMarshalClass) xStream.fromXML(xml);

    assertNotNull(readT.getEntity());
    assertNotNull(readT.getEntity().getEvent());
    assertNotNull(readT.getEntity().getThisBuild());
    assertNotNull(readT.getEntity().getOthers());

    assertEquals(2, readT.getEntity().getOthers().size());

    TriggeredItemEntity other = readT.getEntity().getOthers().get(0);
    assertEquals(1, other.getBuildNumber().intValue());
    assertEquals("projectY", other.getProjectId());

    other = readT.getEntity().getOthers().get(1);
    assertEquals(12, other.getBuildNumber().intValue());
    assertEquals("projectZ", other.getProjectId());
  }
 /** Tests the isBuilding method of the class {@link BuildMemory}. With no memory. */
 @Test
 public void testIsBuildingFalse() {
   PatchsetCreated event = Setup.createPatchsetCreated();
   BuildMemory instance = new BuildMemory();
   assertFalse(instance.isBuilding(event));
 }