@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));
  }
 private MaterialRevisions materialRevisions(String userWithHtmlCharacters) {
   MaterialRevisions revisions = new MaterialRevisions();
   revisions.addRevision(
       MaterialsMother.svnMaterial(),
       new Modification(userWithHtmlCharacters, "comment", "email", new Date(), "r1"));
   return revisions;
 }
  @Test
  public void shouldDelegateToSCMUpdaterToAddNewMaterial() throws Exception {
    PluggableSCMMaterial material = MaterialsMother.pluggableSCMMaterial();
    File file = new File("random");

    materialUpdater.addNewMaterialWithModifications(material, file);

    verify(scmMaterialUpdater).addNewMaterialWithModifications(material, file);
  }
  @Test
  public void shouldLoadPipelineAlongwithBuildCauseHavingMaterialPasswordsPopulated() {
    PipelineConfig pipelineConfig =
        PipelineConfigMother.pipelineConfig(
            "last",
            new StageConfig(
                new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one"))));
    pipelineConfig.materialConfigs().clear();
    SvnMaterial onDirOne =
        MaterialsMother.svnMaterial("google.com", "dirOne", "loser", "boozer", false, "**/*.html");
    P4Material onDirTwo =
        MaterialsMother.p4Material(
            "host:987654321", "zoozer", "secret", "through-the-window", true);
    onDirTwo.setFolder("dirTwo");
    pipelineConfig.addMaterialConfig(onDirOne.config());
    pipelineConfig.addMaterialConfig(onDirTwo.config());

    configHelper.addPipeline(pipelineConfig);

    Pipeline building = PipelineMother.building(pipelineConfig);
    Pipeline pipeline = dbHelper.savePipelineWithMaterials(building);

    final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId();
    Pipeline loadedPipeline =
        (Pipeline)
            transactionTemplate.execute(
                new TransactionCallback() {
                  public Object doInTransaction(TransactionStatus status) {
                    return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId);
                  }
                });

    MaterialRevisions revisions = loadedPipeline.getBuildCause().getMaterialRevisions();
    assertThat(
        ((SvnMaterial) revisions.findRevisionFor(onDirOne).getMaterial()).getPassword(),
        is("boozer"));
    assertThat(
        ((P4Material) revisions.findRevisionFor(onDirTwo).getMaterial()).getPassword(),
        is("secret"));
  }
 @Before
 public void setup() throws Exception {
   dbHelper.onSetUp();
   configHelper.onSetUp();
   configHelper.usingCruiseConfigDao(goConfigDao).initializeConfigFile();
   hgTestRepo = new HgTestRepo("hgTestRepo1");
   hgMaterial = MaterialsMother.hgMaterial(hgTestRepo.projectRepositoryUrl());
   hgMaterial.setFilter(new Filter(new IgnoredFiles("helper/**/*.*")));
   workingFolder = TestFileUtil.createTempFolder("workingFolder");
   outputStreamConsumer = inMemoryConsumer();
   mingleConfig =
       configHelper.addPipeline(
           "cruise", STAGE_NAME, this.hgMaterial.config(), "unit", "functional");
 }
  @Test
  public void shouldNotUpdateMaterialInstanceWhenConfigHas_NOT_Changed() throws Exception {
    PluggableSCMMaterial material = MaterialsMother.pluggableSCMMaterial();
    MaterialInstance materialInstance = material.createMaterialInstance();

    File file = new File("random");
    Modifications modifications = new Modifications();

    materialUpdater.insertLatestOrNewModifications(material, materialInstance, file, modifications);

    verify(materialRepository, never()).saveOrUpdate(Matchers.<MaterialInstance>any());
    verify(scmMaterialUpdater)
        .insertLatestOrNewModifications(material, materialInstance, file, modifications);
  }
 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;
 }
 private Pipeline createAndLoadModifyOneFilePipeline(PipelineConfig pipelineConfig) {
   MaterialConfigs expandedConfigs =
       materialExpansionService.expandMaterialConfigsForScheduling(
           pipelineConfig.materialConfigs());
   MaterialRevisions materialRevisions =
       ModificationsMother.modifyOneFile(
           MaterialsMother.createMaterialsFromMaterialConfigs(expandedConfigs));
   Pipeline building = PipelineMother.buildingWithRevisions(pipelineConfig, materialRevisions);
   Pipeline pipeline = dbHelper.savePipelineWithMaterials(building);
   final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId();
   return (Pipeline)
       transactionTemplate.execute(
           new TransactionCallback() {
             public Object doInTransaction(TransactionStatus status) {
               return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId);
             }
           });
 }
  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);
  }
  @Test
  public void shouldUpdateToNewMaterialInstanceWhenConfigHas_Changed() throws Exception {
    PluggableSCMMaterial material = MaterialsMother.pluggableSCMMaterial();
    MaterialInstance materialInstance = material.createMaterialInstance();
    materialInstance.setId(1);

    material
        .getScmConfig()
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("key2", false, "value2"));
    MaterialInstance newMaterialInstance = material.createMaterialInstance();
    newMaterialInstance.setId(1);
    File file = new File("random");

    Modifications modifications = new Modifications();
    when(materialRepository.find(anyLong())).thenReturn(materialInstance);

    materialUpdater.insertLatestOrNewModifications(material, materialInstance, file, modifications);

    verify(materialRepository).saveOrUpdate(newMaterialInstance);
    verify(scmMaterialUpdater)
        .insertLatestOrNewModifications(material, materialInstance, file, modifications);
  }