private MaterialRevision materialRevisionForDownstream(
     DependencyMaterial material, Stage upstreamStage) {
   StageIdentifier identifier = upstreamStage.getIdentifier();
   String rev = identifier.getStageLocator();
   String pipelineLabel = identifier.getPipelineLabel();
   return new MaterialRevision(
       material, new Modification(new Date(), rev, pipelineLabel, upstreamStage.getPipelineId()));
 }
  @Test
  public void shouldCancelBuildBelongingToNonExistentPipelineWhenCreatingWork() throws Exception {
    fixture.createPipelineWithFirstStageScheduled();
    Pipeline pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);

    ScheduledPipelineLoader scheduledPipelineLoader = mock(ScheduledPipelineLoader.class);
    when(scheduledPipelineLoader.pipelineWithPasswordAwareBuildCauseByBuildId(
            pipeline.getFirstStage().getJobInstances().first().getId()))
        .thenThrow(new PipelineNotFoundException("thrown by mockPipelineService"));

    GoConfigService mockGoConfigService = mock(GoConfigService.class);
    CruiseConfig config = configHelper.currentConfig();
    configHelper.removePipeline(fixture.pipelineName, config);
    when(mockGoConfigService.getCurrentConfig()).thenReturn(config);

    buildAssignmentService =
        new BuildAssignmentService(
            mockGoConfigService,
            jobInstanceService,
            scheduleService,
            agentService,
            environmentConfigService,
            timeProvider,
            transactionTemplate,
            scheduledPipelineLoader,
            pipelineService,
            builderFactory,
            agentRemoteHandler);
    buildAssignmentService.onTimer();

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-other-resource"));

    try {
      buildAssignmentService.assignWorkToAgent(agent(agentConfig));
      fail("should have thrown PipelineNotFoundException");
    } catch (PipelineNotFoundException e) {
      // ok
    }

    pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);

    JobInstance job = pipeline.getFirstStage().getJobInstances().first();
    assertThat(job.getState(), is(JobState.Completed));
    assertThat(job.getResult(), is(JobResult.Cancelled));
    Stage stage = stageDao.findStageWithIdentifier(job.getIdentifier().getStageIdentifier());
    assertThat(stage.getState(), is(StageState.Cancelled));
    assertThat(stage.getResult(), is(StageResult.Cancelled));
  }
 private long rerunJob(
     String jobName,
     PipelineConfig pipelineConfig,
     Pipeline previousSuccessfulBuildWithOlderPackageConfig) {
   Stage stage =
       instanceFactory.createStageForRerunOfJobs(
           previousSuccessfulBuildWithOlderPackageConfig.getFirstStage(),
           asList(jobName),
           new DefaultSchedulingContext(),
           pipelineConfig.getFirstStageConfig(),
           new TimeProvider(),
           configHelper.getGoConfigDao().md5OfConfigFile());
   stage = stageService.save(previousSuccessfulBuildWithOlderPackageConfig, stage);
   return stage.getFirstJob().getId();
 }
  @Test
  public void shouldCancelBuildBelongingToNonExistentPipeline() throws Exception {
    fixture.createPipelineWithFirstStageScheduled();
    buildAssignmentService.onTimer();

    configHelper.removePipeline(fixture.pipelineName);

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-other-resource"));

    assertThat(
        (NoWork) buildAssignmentService.assignWorkToAgent(agent(agentConfig)),
        Matchers.is(BuildAssignmentService.NO_WORK));
    Pipeline pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);
    JobInstance job = pipeline.getFirstStage().getJobInstances().first();
    assertThat(job.getState(), is(JobState.Completed));
    assertThat(job.getResult(), is(JobResult.Cancelled));
    Stage stage = stageDao.findStageWithIdentifier(job.getIdentifier().getStageIdentifier());
    assertThat(stage.getState(), is(StageState.Cancelled));
    assertThat(stage.getResult(), is(StageResult.Cancelled));
  }