private PackageRepository packageRepository(String pluginId) {
   PackageRepository packageRepository = new PackageRepository();
   packageRepository.setPluginConfiguration(new PluginConfiguration(pluginId, "1.0"));
   packageRepository.getConfiguration().add(ConfigurationPropertyMother.create("p1", false, "v1"));
   packageRepository.getConfiguration().add(ConfigurationPropertyMother.create("p2", true, "v2"));
   packageRepository.getConfiguration().add(ConfigurationPropertyMother.create("p3", true, "v3"));
   packageRepository.getConfiguration().add(ConfigurationPropertyMother.create("p4", false, "v4"));
   return packageRepository;
 }
 private PipelineConfig setupPipelineWithPackageMaterial(
     String pipelineName, String stageName, String jobName) {
   PackageMaterialConfig packageMaterialConfig = new PackageMaterialConfig("p-id");
   Configuration repoConfig =
       new Configuration(
           ConfigurationPropertyMother.create("repo-key1", false, "repo-k1-value"),
           ConfigurationPropertyMother.create("repo-key2", false, "repo-k2-value"));
   PackageRepository repository =
       PackageRepositoryMother.create("repo-id", "repo-name", "pluginid", "version", repoConfig);
   Configuration packageConfig =
       new Configuration(
           ConfigurationPropertyMother.create("package-key1", false, "package-key1-value"),
           ConfigurationPropertyMother.create("package-key2", false, "package-key2-value"));
   PackageDefinition packageDefinition =
       PackageDefinitionMother.create("p-id", "package-name", packageConfig, repository);
   packageMaterialConfig.setPackageDefinition(packageDefinition);
   repository.getPackages().add(packageDefinition);
   PipelineConfig pipelineConfig =
       PipelineConfigMother.pipelineConfig(
           pipelineName, stageName, new MaterialConfigs(packageMaterialConfig), jobName);
   configHelper.addPackageDefinition(packageMaterialConfig);
   configHelper.addPipeline(pipelineConfig);
   PackageConfigurations packageConfigurations = new PackageConfigurations();
   packageConfigurations.add(
       new PackageConfiguration("package-key1").with(PackageConfiguration.PART_OF_IDENTITY, true));
   packageConfigurations.add(
       new PackageConfiguration("package-key2")
           .with(PackageConfiguration.PART_OF_IDENTITY, false));
   PackageMetadataStore.getInstance()
       .addMetadataFor(packageMaterialConfig.getPluginId(), packageConfigurations);
   PackageConfigurations configuration = new PackageConfigurations();
   configuration.add(
       new PackageConfiguration("repo-key1").with(PackageConfiguration.PART_OF_IDENTITY, true));
   configuration.add(
       new PackageConfiguration("repo-key2").with(PackageConfiguration.PART_OF_IDENTITY, false));
   RepositoryMetadataStore.getInstance()
       .addMetadataFor(packageMaterialConfig.getPluginId(), configuration);
   return pipelineConfig;
 }
  @Test
  public void shouldValidateTask() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    PluggableTask task =
        new PluggableTask("abc", new PluginConfiguration("abc.def", "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("KEY1", "error message"));
    when(pluginManager.doOn(
            eq(Task.class), eq(task.getPluginConfiguration().getId()), any(ActionWithReturn.class)))
        .thenReturn(validationResult);

    pluggableTaskService.validate(task);
    assertThat(task.getConfiguration().getProperty("KEY1").errors().isEmpty(), is(false));
    assertThat(
        task.getConfiguration().getProperty("KEY1").errors().firstError(), is("error message"));
  }
  @Test
  public void shouldInvokePluginValidationsBeforeSavingPackageRepository() throws Exception {
    String pluginId = "yum";
    PackageRepository packageRepository = new PackageRepository();
    RepositoryMetadataStore.getInstance().addMetadataFor(pluginId, new PackageConfigurations());
    packageRepository.setPluginConfiguration(new PluginConfiguration(pluginId, "1.0"));
    packageRepository
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("url", false, "junk-url"));

    ArgumentCaptor<RepositoryConfiguration> packageConfigurationsArgumentCaptor =
        ArgumentCaptor.forClass(RepositoryConfiguration.class);
    ValidationResult expectedValidationResult = new ValidationResult();
    expectedValidationResult.addError(new ValidationError("url", "url format incorrect"));

    when(pluginManager.getPluginDescriptorFor(pluginId))
        .thenReturn(new GoPluginDescriptor("yum", "1.0", null, null, null, true));
    when(packageAsRepositoryExtension.isRepositoryConfigurationValid(
            eq(pluginId), packageConfigurationsArgumentCaptor.capture()))
        .thenReturn(expectedValidationResult);

    service =
        new PackageRepositoryService(
            pluginManager,
            packageAsRepositoryExtension,
            goConfigService,
            securityService,
            entityHashingService,
            mock(Localizer.class));
    service.performPluginValidationsFor(packageRepository);
    assertThat(
        packageRepository
            .getConfiguration()
            .get(0)
            .getConfigurationValue()
            .errors()
            .getAllOn("value"),
        is(Arrays.asList("url format incorrect")));
  }
  @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);
  }
  @Test
  public void shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository()
      throws Exception {
    // metadata setup
    String pluginId = "yum";

    PackageConfigurations repositoryConfiguration = new PackageConfigurations();
    repositoryConfiguration.add(
        new PackageConfiguration("required").with(PackageConfiguration.REQUIRED, true));
    repositoryConfiguration.add(
        new PackageConfiguration("required_secure")
            .with(PackageConfiguration.REQUIRED, true)
            .with(PackageConfiguration.SECURE, true));
    repositoryConfiguration.add(
        new PackageConfiguration("secure")
            .with(PackageConfiguration.SECURE, true)
            .with(PackageConfiguration.REQUIRED, false));
    repositoryConfiguration.add(new PackageConfiguration("not_required_not_secure"));
    RepositoryMetadataStore.getInstance().addMetadataFor(pluginId, repositoryConfiguration);

    PackageRepository packageRepository = new PackageRepository();
    packageRepository.setPluginConfiguration(new PluginConfiguration(pluginId, "1.0"));
    packageRepository
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("required", false, ""));
    packageRepository
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("required_secure", true, ""));
    packageRepository
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("secure", true, ""));
    packageRepository
        .getConfiguration()
        .add(ConfigurationPropertyMother.create("not_required_not_secure", false, ""));

    when(packageAsRepositoryExtension.isRepositoryConfigurationValid(
            eq(pluginId), any(RepositoryConfiguration.class)))
        .thenReturn(new ValidationResult());
    when(pluginManager.getPluginDescriptorFor(pluginId))
        .thenReturn(new GoPluginDescriptor(pluginId, "1.0", null, null, null, true));
    when(localizer.localize("MANDATORY_CONFIGURATION_FIELD")).thenReturn("mandatory field");

    service.performPluginValidationsFor(packageRepository);

    assertThat(
        packageRepository
            .getConfiguration()
            .get(0)
            .getConfigurationValue()
            .errors()
            .getAllOn("value"),
        is(Arrays.asList("mandatory field")));
    assertThat(
        packageRepository
            .getConfiguration()
            .get(1)
            .getEncryptedConfigurationValue()
            .errors()
            .getAllOn("value"),
        is(Arrays.asList("mandatory field")));
  }