@Test
  public void shouldLockPipelineWhenSchedulingIt() throws Exception {
    scheduleAndCompleteInitialPipelines();

    configHelper.lockPipeline("mingle");

    Material stubMaterial = new TestingMaterial();
    mingleConfig.setMaterialConfigs(new MaterialConfigs(stubMaterial.config()));

    assertThat(pipelineLockService.isLocked("mingle"), is(false));

    MaterialRevisions revisions = new MaterialRevisions();
    revisions.addRevision(
        stubMaterial,
        ((TestingMaterial) stubMaterial)
            .modificationsSince(null, null, subprocessExecutionContext));
    BuildCause buildCause = BuildCause.createWithModifications(revisions, "");
    dbHelper.saveMaterials(buildCause.getMaterialRevisions());
    Pipeline pipeline =
        instanceFactory.createPipelineInstance(
            mingleConfig,
            buildCause,
            new DefaultSchedulingContext(DEFAULT_APPROVED_BY),
            md5,
            new TimeProvider());
    pipelineService.save(pipeline);

    assertThat(pipelineLockService.isLocked("mingle"), is(true));
  }
  @Test
  public void shouldUnderstandChangedMaterial_forManual_triggerWithOptions_DoneWithANewRevision()
      throws Exception {
    SvnMaterial svn = SvnMaterial.createSvnMaterialWithMock(repository);

    MaterialRevisions revsAfterFoo = checkinFile(svn, "foo.c", svnRepository);

    String revisionForFingerPrint =
        revsAfterFoo.findRevisionForFingerPrint(svn.getFingerprint()).getRevision().getRevision();
    scheduleHelper.manuallySchedulePipelineWithRealMaterials(
        MINGLE_PIPELINE_NAME,
        new Username(new CaseInsensitiveString("loser")),
        m(
            MaterialsMother.createMaterialFromMaterialConfig(mingleConfig.materialConfigs().get(0))
                .getPipelineUniqueFingerprint(),
            revisionForFingerPrint));

    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), hasItem(MINGLE_PIPELINE_NAME));
    BuildCause bisectAfterBisectBuildCause =
        pipelineScheduleQueue.toBeScheduled().get(MINGLE_PIPELINE_NAME);
    for (MaterialRevision materialRevision : bisectAfterBisectBuildCause.getMaterialRevisions()) {
      assertThat(
          "material revision " + materialRevision + " should have been considered changed.",
          materialRevision.isChanged(),
          is(true));
    }
    assertThat(bisectAfterBisectBuildCause.getMaterialRevisions().getRevisions().size(), is(1));
  }
  @Test
  public void shouldForceStagePlanWithModificationsSinceLast() throws Exception {
    Pipeline completedMingle = scheduleAndCompleteInitialPipelines();
    pipelineDao.loadPipeline(completedMingle.getId());
    TestingMaterial testingMaterial = new TestingMaterial();
    mingleConfig.setMaterialConfigs(new MaterialConfigs(testingMaterial.config()));

    MaterialRevisions revisions = new MaterialRevisions();
    revisions.addRevision(
        testingMaterial,
        testingMaterial.modificationsSince(null, null, subprocessExecutionContext));
    BuildCause buildCause = BuildCause.createManualForced(revisions, Username.ANONYMOUS);
    dbHelper.saveMaterials(buildCause.getMaterialRevisions());
    Pipeline forcedPipeline =
        instanceFactory.createPipelineInstance(
            mingleConfig,
            buildCause,
            new DefaultSchedulingContext(DEFAULT_APPROVED_BY),
            md5,
            new TimeProvider());

    pipelineService.save(forcedPipeline);

    verifyMingleScheduledWithModifications();
  }
 private void verifyMingleScheduledWithModifications() {
   Pipeline scheduledPipeline =
       pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(mingleConfig.name()));
   BuildCause buildCause = scheduledPipeline.getBuildCause();
   assertThat(buildCause.getMaterialRevisions().totalNumberOfModifications(), is(3));
   JobInstance instance = scheduledPipeline.getFirstStage().getJobInstances().first();
   assertThat(instance.getState(), is(JobState.Scheduled));
 }
 private Pipeline tryToScheduleAPipeline() {
   BuildCause buildCause =
       BuildCause.createWithModifications(modifyOneFile(preCondition.pipelineConfig()), "");
   dbHelper.saveMaterials(buildCause.getMaterialRevisions());
   pipelineScheduleQueue.schedule(preCondition.pipelineName, buildCause);
   scheduleService.autoSchedulePipelinesFromRequestBuffer();
   return pipelineService.mostRecentFullPipelineByName(preCondition.pipelineName);
 }
  @Test
  public void shouldConsumeAllBuildCausesInServerHealth() throws Exception {
    pipelineScheduleQueue.schedule(
        "mingle", BuildCause.createManualForced(modifyOneFile(mingleConfig), Username.ANONYMOUS));
    pipelineScheduleQueue.schedule(
        "evolve", BuildCause.createManualForced(modifyOneFile(evolveConfig), Username.ANONYMOUS));

    scheduleService.autoSchedulePipelinesFromRequestBuffer();
    assertThat(pipelineScheduleQueue.toBeScheduled().size(), is(0));
  }
 private Pipeline scheduleWithFileChanges(PipelineConfig pipelineConfig) {
   BuildCause buildCause = BuildCause.createWithModifications(modifyOneFile(pipelineConfig), "");
   saveRevs(buildCause.getMaterialRevisions());
   return instanceFactory.createPipelineInstance(
       pipelineConfig,
       buildCause,
       new DefaultSchedulingContext(GoConstants.DEFAULT_APPROVED_BY),
       md5,
       new TimeProvider());
 }
  @Test
  public void shouldScheduleJobForAllAgentsWhenToBeRunOnAllAgents() throws Exception {
    configHelper.addAgent("localhost", "uuid1");
    configHelper.addAgent("localhost", "uuid2");
    configHelper.addAgent("localhost", "uuid3");
    configHelper.addAgentToEnvironment("dev", "uuid1");
    configHelper.setRunOnAllAgents(
        CaseInsensitiveString.str(evolveConfig.name()), STAGE_NAME, "unit", true);

    Material stubMaterial = new TestingMaterial();
    evolveConfig.setMaterialConfigs(new MaterialConfigs(stubMaterial.config()));
    MaterialRevisions revisions = new MaterialRevisions();
    revisions.addRevision(
        stubMaterial,
        ((TestingMaterial) stubMaterial)
            .modificationsSince(null, null, subprocessExecutionContext));
    BuildCause buildCause = BuildCause.createWithModifications(revisions, "");
    dbHelper.saveMaterials(buildCause.getMaterialRevisions());

    Pipeline pipeline =
        instanceFactory.createPipelineInstance(
            evolveConfig,
            buildCause,
            new DefaultSchedulingContext(
                DEFAULT_APPROVED_BY,
                environmentConfigService.agentsForPipeline(evolveConfig.name())),
            md5,
            new TimeProvider());
    pipelineService.save(pipeline);

    Stage instance =
        scheduleService.scheduleStage(
            pipeline,
            STAGE_NAME,
            "anyone",
            new ScheduleService.NewStageInstanceCreator(goConfigService),
            new ScheduleService.ExceptioningErrorHandler());
    JobInstances scheduledJobs = instance.getJobInstances();
    assertThat(
        scheduledJobs.toArray(),
        hasItemInArray(
            hasProperty(
                "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 1)))));
    assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid2"))));
    assertThat(
        scheduledJobs.toArray(),
        hasItemInArray(
            hasProperty(
                "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 2)))));
    assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid3"))));
    assertThat(scheduledJobs.size(), is(2));
  }
  @Test
  public void should_NOT_markAsChangedWhenMaterialIsReIntroducedWithSameRevisionsToPipeline()
      throws Exception {
    SvnMaterial svn1 = SvnMaterial.createSvnMaterialWithMock(repository);
    svn1.setFolder("another_repo");
    mingleConfig = configHelper.replaceMaterialForPipeline(MINGLE_PIPELINE_NAME, svn1.config());
    runAndPassWith(svn1, "foo.c", svnRepository);

    SvnTestRepo svn2Repository = new SvnTestRepo("testSvnRepo2");
    Subversion repository2 = new SvnCommand(null, svn2Repository.projectRepositoryUrl());
    SvnMaterial svn2 = SvnMaterial.createSvnMaterialWithMock(repository2);
    svn2.setFolder("boulder");

    checkinFile(svn2, "bar.c", svn2Repository);

    mingleConfig = configHelper.addMaterialToPipeline(MINGLE_PIPELINE_NAME, svn2.config());

    scheduleHelper.autoSchedulePipelinesWithRealMaterials(MINGLE_PIPELINE_NAME);

    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), hasItem(MINGLE_PIPELINE_NAME));
    BuildCause mingleBuildCause = pipelineScheduleQueue.toBeScheduled().get(MINGLE_PIPELINE_NAME);
    verifyChanged(svn2, mingleBuildCause, true);
    verifyChanged(
        svn1,
        mingleBuildCause,
        false); // this should not have changed, as foo.c was already built in the previous instance

    runAndPass(mingleBuildCause.getMaterialRevisions());

    mingleConfig = configHelper.replaceMaterialForPipeline(MINGLE_PIPELINE_NAME, svn1.config());
    scheduleHelper.autoSchedulePipelinesWithRealMaterials(MINGLE_PIPELINE_NAME);

    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), hasItem(MINGLE_PIPELINE_NAME));
    mingleBuildCause = pipelineScheduleQueue.toBeScheduled().get(MINGLE_PIPELINE_NAME);
    verifyChanged(
        svn1,
        mingleBuildCause,
        false); // this should not have changed, as foo.c was already built in the previous instance
    runAndPassWith(svn1, "baz.c", svnRepository);

    mingleConfig = configHelper.addMaterialToPipeline(MINGLE_PIPELINE_NAME, svn2.config());

    checkinFile(svn1, "quux.c", svnRepository);

    scheduleHelper.autoSchedulePipelinesWithRealMaterials(MINGLE_PIPELINE_NAME);

    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), hasItem(MINGLE_PIPELINE_NAME));
    mingleBuildCause = pipelineScheduleQueue.toBeScheduled().get(MINGLE_PIPELINE_NAME);
    verifyChanged(svn2, mingleBuildCause, false);
    verifyChanged(svn1, mingleBuildCause, true);
  }
  @Before
  public void setup() throws Exception {
    diskSpaceSimulator = new DiskSpaceSimulator();
    new HgTestRepo("testHgRepo");

    svnRepository = new SvnTestRepo("testSvnRepo");

    dbHelper.onSetUp();
    configHelper.onSetUp();
    configHelper.usingCruiseConfigDao(goConfigDao).initializeConfigFile();

    repository = new SvnCommand(null, svnRepository.projectRepositoryUrl());

    goParentPipelineConfig =
        configHelper.addPipeline(
            GO_PIPELINE_UPSTREAM,
            STAGE_NAME,
            new MaterialConfigs(new GitMaterialConfig("foo-bar")),
            "unit");

    goPipelineConfig = configHelper.addPipeline(GO_PIPELINE_NAME, STAGE_NAME, repository, "unit");

    svnMaterialRevs = new MaterialRevisions();
    SvnMaterial svnMaterial = SvnMaterial.createSvnMaterialWithMock(repository);
    svnMaterialRevs.addRevision(
        svnMaterial,
        svnMaterial.latestModification(
            null, new ServerSubprocessExecutionContext(goConfigService, new SystemEnvironment())));

    final MaterialRevisions materialRevisions = new MaterialRevisions();
    SvnMaterial anotherSvnMaterial = SvnMaterial.createSvnMaterialWithMock(repository);
    materialRevisions.addRevision(
        anotherSvnMaterial,
        anotherSvnMaterial.latestModification(null, subprocessExecutionContext));

    transactionTemplate.execute(
        new TransactionCallbackWithoutResult() {
          @Override
          protected void doInTransactionWithoutResult(TransactionStatus status) {
            materialRepository.save(svnMaterialRevs);
          }
        });

    BuildCause buildCause = BuildCause.createWithModifications(svnMaterialRevs, "");

    mingleConfig =
        configHelper.addPipeline(
            MINGLE_PIPELINE_NAME,
            STAGE_NAME,
            repository,
            new Filter(new IgnoredFiles("**/*.doc")),
            "unit",
            "functional");
    latestPipeline = PipelineMother.schedule(this.mingleConfig, buildCause);
    latestPipeline = pipelineDao.saveWithStages(latestPipeline);
    dbHelper.passStage(latestPipeline.getStages().first());
    pipelineScheduleQueue.clear();
  }
  private void prepareAPipelineWithHistory() throws SQLException {
    MaterialRevisions materialRevisions = new MaterialRevisions();
    List<Modification> modifications =
        this.hgMaterial.latestModification(workingFolder, subprocessExecutionContext);
    materialRevisions.addRevision(this.hgMaterial, modifications);
    BuildCause buildCause = BuildCause.createWithModifications(materialRevisions, "");

    latestPipeline = PipelineMother.schedule(mingleConfig, buildCause);
    latestPipeline = dbHelper.savePipelineWithStagesAndMaterials(latestPipeline);
    dbHelper.passStage(latestPipeline.getStages().first());
  }
  @Test
  public void shouldSchedulePipelineWithManualFirstStageWhenManuallyTriggered() throws Exception {
    configHelper.configureStageAsManualApproval(MINGLE_PIPELINE_NAME, STAGE_NAME);

    svnRepository.checkInOneFile("a.java");
    materialDatabaseUpdater.updateMaterial(svnRepository.material());

    final HashMap<String, String> revisions = new HashMap<String, String>();
    final HashMap<String, String> environmentVariables = new HashMap<String, String>();
    buildCauseProducer.manualProduceBuildCauseAndSave(
        MINGLE_PIPELINE_NAME,
        Username.ANONYMOUS,
        new ScheduleOptions(revisions, environmentVariables, new HashMap<String, String>()),
        new ServerHealthStateOperationResult());

    Map<String, BuildCause> afterLoad = scheduleHelper.waitForAnyScheduled(5);
    assertThat(afterLoad.keySet(), hasItem(MINGLE_PIPELINE_NAME));
    BuildCause cause = afterLoad.get(MINGLE_PIPELINE_NAME);
    assertThat(cause.getBuildCauseMessage(), containsString("Forced by anonymous"));
  }
 private void verifyChanged(Material material, BuildCause bc, final boolean changed) {
   MaterialRevision svn2MaterialRevision =
       bc.getMaterialRevisions().findRevisionForFingerPrint(material.getFingerprint());
   assertThat(
       "material revision "
           + svn2MaterialRevision
           + " was marked as"
           + (changed ? " not" : "")
           + " changed",
       svn2MaterialRevision.isChanged(),
       is(changed));
 }
 @Before
 public void setUp() {
   MaterialRevisions materialRevisions = multipleModifications();
   stage =
       StageMother.withOneScheduledBuild("stage", "job-that-will-fail", "job-that-will-pass", 1);
   modifications = BuildCause.createWithModifications(materialRevisions, "");
   pipeline = new Pipeline("pipeline", PipelineLabel.COUNT_TEMPLATE, modifications, stage);
   stage.setIdentifier(new StageIdentifier(pipeline, stage));
   for (JobInstance job : stage.getJobInstances()) {
     job.setIdentifier(new JobIdentifier(pipeline, stage, job));
   }
   pipeline.setId(PIPELINE_ID);
   pipeline.updateCounter(9);
 }
  @Test
  public void shouldEscapeBuildCauseMessage() throws Exception {
    String userWithHtmlCharacters = "<user>";
    pipeline.setBuildCause(
        BuildCause.createManualForced(
            materialRevisions(userWithHtmlCharacters),
            new Username(new CaseInsensitiveString(userWithHtmlCharacters))));
    StageJsonPresentationModel presenter =
        new StageJsonPresentationModel(pipeline, stage, null, new Agents());

    JsonTester jsonTester = new JsonTester(presenter.toJson());
    String expected = StringEscapeUtils.escapeHtml(userWithHtmlCharacters);
    jsonTester.shouldContain("{'buildCause':'Forced by " + expected + "'}");
  }
 public Pipeline newPipelineWithFirstStageScheduled(PipelineConfig config) throws SQLException {
   Pipeline pipeline =
       instanceFactory.createPipelineInstance(
           config,
           BuildCause.createManualForced(
               modifyOneFile(
                   MaterialsMother.createMaterialsFromMaterialConfigs(config.materialConfigs()),
                   ModificationsMother.nextRevision()),
               Username.ANONYMOUS),
           new DefaultSchedulingContext(GoConstants.DEFAULT_APPROVED_BY),
           md5,
           new TimeProvider());
   savePipelineWithStagesAndMaterials(pipeline);
   return pipeline;
 }
  @Test
  public void
      shouldUnderstandChangedMaterial_forCompatibleRevisionsBeingSelectedForChangedMaterials_whenTriggeringTheFirstTime()
          throws Exception {
    DependencyMaterialConfig mingleMaterialConfig =
        new DependencyMaterialConfig(
            new CaseInsensitiveString(MINGLE_PIPELINE_NAME), new CaseInsensitiveString(STAGE_NAME));
    String mingleDownstreamPipelineName = "down_of_mingle";
    SvnMaterial svn = SvnMaterial.createSvnMaterialWithMock(repository);

    runAndPassWith(svn, "foo.c", svnRepository);

    svnRepository.checkInOneFile("bar.c");
    materialDatabaseUpdater.updateMaterial(svn);

    configHelper.addPipeline(
        mingleDownstreamPipelineName,
        STAGE_NAME,
        new MaterialConfigs(svn.config(), mingleMaterialConfig),
        "unit");

    pipelineTimeline.update();
    scheduleHelper.autoSchedulePipelinesWithRealMaterials(mingleDownstreamPipelineName);

    assertThat(
        pipelineScheduleQueue.toBeScheduled().keySet(), hasItem(mingleDownstreamPipelineName));
    BuildCause downstreamBuildCause =
        pipelineScheduleQueue.toBeScheduled().get(mingleDownstreamPipelineName);
    for (MaterialRevision materialRevision : downstreamBuildCause.getMaterialRevisions()) {
      assertThat(
          "material revision " + materialRevision + " was marked as not changed",
          materialRevision.isChanged(),
          is(true));
    }
    assertThat(downstreamBuildCause.getMaterialRevisions().getRevisions().size(), is(2));
  }
 public Pipeline schedulePipelineWithAllStages(
     PipelineConfig pipelineConfig, BuildCause buildCause) {
   buildCause.assertMaterialsMatch(pipelineConfig.materialConfigs());
   DefaultSchedulingContext defaultSchedulingContext =
       new DefaultSchedulingContext(GoConstants.DEFAULT_APPROVED_BY);
   Stages stages = new Stages();
   Pipeline pipeline =
       instanceFactory.createPipelineInstance(
           pipelineConfig, buildCause, defaultSchedulingContext, md5, new TimeProvider());
   for (StageConfig stageConfig : pipelineConfig) {
     stages.add(
         instanceFactory.createStageInstance(
             stageConfig, defaultSchedulingContext, md5, new TimeProvider()));
   }
   pipeline.setStages(stages);
   return scheduleJobInstancesAndSavePipeline(pipeline);
 }
 @Test
 public void should_NOT_schedulePipeline_whenOneOfTheMaterialsHasNoModificationsPresent()
     throws Exception {
   Pipeline latestGoInstance =
       PipelineMother.schedule(
           goPipelineConfig,
           BuildCause.createManualForced(
               svnMaterialRevs, new Username(new CaseInsensitiveString("loser"))));
   latestGoInstance = pipelineDao.saveWithStages(latestGoInstance);
   dbHelper.passStage(latestGoInstance.getStages().first());
   configHelper.addMaterialToPipeline(
       GO_PIPELINE_NAME,
       new DependencyMaterialConfig(
           new CaseInsensitiveString(GO_PIPELINE_UPSTREAM),
           new CaseInsensitiveString(STAGE_NAME)));
   svnRepository.checkInOneFile("a.java");
   scheduleHelper.autoSchedulePipelinesWithRealMaterials(GO_PIPELINE_NAME);
   assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), not(hasItem(GO_PIPELINE_NAME)));
 }
  @Test
  public void shouldReturnJsonWithModifications() throws Exception {
    StageJsonPresentationModel presenter =
        new StageJsonPresentationModel(pipeline, stage, null, new Agents());
    Map json = presenter.toJson();

    JsonValue jsonValue = JsonUtils.from(json);
    JsonValue revision = jsonValue.getObject("materialRevisions", 0);
    // TODO: TRAINWRECK! WE should fix this when we re-do the JSON. We don't think this test will
    // last long in the new UI
    String expected =
        modifications
            .getMaterialRevisions()
            .getMaterialRevision(0)
            .getModifications()
            .get(0)
            .getRevision();
    assertThat(revision.getString("revision"), is(expected));
    assertThat(revision.getString("user"), is(ModificationsMother.MOD_USER_WITH_HTML_CHAR));
    assertThat(
        revision.getString("date"), is(DateUtils.formatISO8601(ModificationsMother.TODAY_CHECKIN)));
  }
  public Pipeline rescheduleTestPipeline(String pipelineName, String stageName, String userName)
      throws SQLException {
    String[] jobConfigNames = new String[] {};
    PipelineConfig pipelineConfig = configurePipeline(pipelineName, stageName, jobConfigNames);

    BuildCause buildCause =
        BuildCause.createManualForced(
            modifyOneFile(
                MaterialsMother.createMaterialsFromMaterialConfigs(
                    pipelineConfig.materialConfigs()),
                ModificationsMother.currentRevision()),
            Username.ANONYMOUS);

    Pipeline pipeline =
        instanceFactory.createPipelineInstance(
            pipelineConfig,
            buildCause,
            new DefaultSchedulingContext(GoConstants.DEFAULT_APPROVED_BY),
            md5,
            new TimeProvider());
    return savePipelineWithStagesAndMaterials(pipeline);
  }
  /**
   * (uppest/2/uppest-stage/1) |------------------> upper-peer ------- |
   * ...................................|............................................... | . | . [
   * uppest-stage ............................|...................... {bar.zip
   * uppest/upper-peer/downer} V . uppest uppest-stage-2 ------> upper ------> downer ------>
   * downest {foo.zip uppest/upper/downer} (uppest/1/uppest-stage-2/1) uppest-stage-3 ]
   *
   * <p>.... :: fetch artifact call ---> :: material dependency
   */
  @Test
  public void shouldCreateWork_withAncestorFetchArtifactCalls_resolvedToRelevantStage()
      throws Exception {
    configHelper.addPipeline("uppest", "uppest-stage");
    configHelper.addStageToPipeline("uppest", "uppest-stage-2");
    PipelineConfig uppest = configHelper.addStageToPipeline("uppest", "uppest-stage-3");

    configHelper.addPipeline("upper", "upper-stage");
    DependencyMaterial upper_sMaterial =
        new DependencyMaterial(
            new CaseInsensitiveString("uppest"), new CaseInsensitiveString("uppest-stage-2"));
    PipelineConfig upper =
        configHelper.setMaterialConfigForPipeline("upper", upper_sMaterial.config());

    configHelper.addPipeline("upper-peer", "upper-peer-stage");
    DependencyMaterial upperPeer_sMaterial =
        new DependencyMaterial(
            new CaseInsensitiveString("uppest"), new CaseInsensitiveString("uppest-stage"));
    PipelineConfig upperPeer =
        configHelper.setMaterialConfigForPipeline("upper-peer", upperPeer_sMaterial.config());

    configHelper.addPipeline("downer", "downer-stage");
    DependencyMaterial downer_sUpperMaterial =
        new DependencyMaterial(
            new CaseInsensitiveString("upper"), new CaseInsensitiveString("upper-stage"));
    configHelper.setMaterialConfigForPipeline("downer", downer_sUpperMaterial.config());
    DependencyMaterial downer_sUpperPeerMaterial =
        new DependencyMaterial(
            new CaseInsensitiveString("upper-peer"), new CaseInsensitiveString("upper-peer-stage"));
    PipelineConfig downer =
        configHelper.addMaterialToPipeline("downer", downer_sUpperPeerMaterial.config());

    configHelper.addPipeline("downest", "downest-stage");
    DependencyMaterial downest_sMaterial =
        new DependencyMaterial(
            new CaseInsensitiveString("downer"), new CaseInsensitiveString("downer-stage"));
    configHelper.setMaterialConfigForPipeline("downest", downest_sMaterial.config());
    Tasks allFetchTasks = new Tasks();
    allFetchTasks.add(
        new FetchTask(
            new CaseInsensitiveString("uppest/upper/downer"),
            new CaseInsensitiveString("uppest-stage"),
            new CaseInsensitiveString("unit"),
            "foo.zip",
            "bar"));
    allFetchTasks.add(
        new FetchTask(
            new CaseInsensitiveString("uppest/upper-peer/downer"),
            new CaseInsensitiveString("uppest-stage"),
            new CaseInsensitiveString("unit"),
            "bar.zip",
            "baz"));
    configHelper.replaceAllJobsInStage(
        "downest",
        "downest-stage",
        new JobConfig(
            new CaseInsensitiveString("fetcher"),
            new Resources("fetcher"),
            new ArtifactPlans(),
            allFetchTasks));
    PipelineConfig downest =
        goConfigService
            .getCurrentConfig()
            .pipelineConfigByName(new CaseInsensitiveString("downest"));

    DefaultSchedulingContext defaultSchedulingCtx =
        new DefaultSchedulingContext(DEFAULT_APPROVED_BY);
    Pipeline uppestInstanceForUpper =
        instanceFactory.createPipelineInstance(
            uppest, modifySomeFiles(uppest), defaultSchedulingCtx, md5, new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(uppestInstanceForUpper);
    dbHelper.passStage(uppestInstanceForUpper.findStage("uppest-stage"));
    Stage upper_sMaterialStage =
        dbHelper.scheduleStage(
            uppestInstanceForUpper, uppest.getStage(new CaseInsensitiveString("uppest-stage-2")));
    dbHelper.passStage(upper_sMaterialStage);

    Pipeline uppestInstanceForUpperPeer =
        instanceFactory.createPipelineInstance(
            uppest,
            modifySomeFiles(uppest),
            new DefaultSchedulingContext("super-hero"),
            md5,
            new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(uppestInstanceForUpperPeer);
    Stage upperPeer_sMaterialStage = uppestInstanceForUpperPeer.findStage("uppest-stage");
    dbHelper.passStage(upperPeer_sMaterialStage);

    Pipeline upperInstance =
        instanceFactory.createPipelineInstance(
            upper,
            buildCauseForDependency(upper_sMaterial, upper_sMaterialStage),
            defaultSchedulingCtx,
            md5,
            new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(upperInstance);
    Stage downer_sUpperMaterialStage = upperInstance.findStage("upper-stage");
    dbHelper.passStage(downer_sUpperMaterialStage);

    Pipeline upperPeerInstance =
        instanceFactory.createPipelineInstance(
            upperPeer,
            buildCauseForDependency(upperPeer_sMaterial, upperPeer_sMaterialStage),
            defaultSchedulingCtx,
            md5,
            new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(upperPeerInstance);
    Stage downer_sUpperPeerMaterialStage = upperPeerInstance.findStage("upper-peer-stage");
    dbHelper.passStage(downer_sUpperPeerMaterialStage);

    MaterialRevisions downer_sMaterialRevisions =
        new MaterialRevisions(
            materialRevisionForDownstream(downer_sUpperMaterial, downer_sUpperMaterialStage),
            materialRevisionForDownstream(
                downer_sUpperPeerMaterial, downer_sUpperPeerMaterialStage));

    Pipeline downerInstance =
        instanceFactory.createPipelineInstance(
            downer,
            BuildCause.createManualForced(downer_sMaterialRevisions, loserUser),
            defaultSchedulingCtx,
            md5,
            new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(downerInstance);
    Stage downest_sMaterialStage = downerInstance.findStage("downer-stage");
    dbHelper.passStage(downest_sMaterialStage);

    Pipeline downestInstance =
        instanceFactory.createPipelineInstance(
            downest,
            buildCauseForDependency(downest_sMaterial, downest_sMaterialStage),
            defaultSchedulingCtx,
            md5,
            new TimeProvider());
    dbHelper.savePipelineWithStagesAndMaterials(downestInstance);

    buildAssignmentService.onTimer();
    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("fetcher"));
    BuildWork work = (BuildWork) buildAssignmentService.assignWorkToAgent(agent(agentConfig));

    List<Builder> builders = work.getAssignment().getBuilders();
    FetchArtifactBuilder fooZipFetch = (FetchArtifactBuilder) builders.get(0);
    assertThat(fooZipFetch.artifactLocator(), is("uppest/1/uppest-stage/latest/unit/foo.zip"));
    FetchArtifactBuilder barZipFetch = (FetchArtifactBuilder) builders.get(1);
    assertThat(barZipFetch.artifactLocator(), is("uppest/2/uppest-stage/1/unit/bar.zip"));
  }
 private BuildCause saveMaterials(BuildCause buildCause) {
   dbHelper.saveMaterials(buildCause.getMaterialRevisions());
   return buildCause;
 }
 private void runAndPass(MaterialRevisions mingleRev) {
   BuildCause buildCause = BuildCause.createWithModifications(mingleRev, "boozer");
   latestPipeline = PipelineMother.schedule(mingleConfig, buildCause);
   latestPipeline = pipelineDao.saveWithStages(latestPipeline);
   dbHelper.passStage(latestPipeline.getStages().first());
 }
 private BuildCause buildCauseForDependency(DependencyMaterial material, Stage upstreamStage) {
   return BuildCause.createManualForced(
       new MaterialRevisions(materialRevisionForDownstream(material, upstreamStage)), loserUser);
 }