/**
   * 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 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);
  }
 @Override
 protected void setUp() throws Exception {
   sshKey = SshdServerMock.generateKeyPair();
   sshd = SshdServerMock.startServer();
   server = SshdServerMock.getInstance();
   server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
   server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
   server.returnCommandFor("gerrit approve.*", SshdServerMock.EofCommandMock.class);
   super.setUp();
 }
 /**
  * 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());
 }