Search in sources :

Example 11 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class ClusterProfilesServiceTest method shouldSetResultAsUnprocessableEntityWhenRulesAreViolated.

@Test
void shouldSetResultAsUnprocessableEntityWhenRulesAreViolated() {
    clusterProfile.addNewConfigurationWithValue("key", "{{SECRET:[config_id][key]}}", false);
    doThrow(new RulesViolationException("some rules violation message")).when(secretParamResolver).resolve(clusterProfile);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    clusterProfilesService.update(clusterProfile, new Username("Bob"), result);
    assertThat(result.httpCode()).isEqualTo(422);
    assertThat(result.message()).isEqualTo("Validations failed for clusterProfile 'prod_cluster'. Error(s): [some rules violation message]. Please correct and resubmit.");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Example 12 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class PackageDefinitionServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForUpdate.

@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForUpdate() {
    PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", new Configuration(), packageRepository);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    doThrow(new RulesViolationException("some rule violation message")).when(goConfigService).updateConfig(any(EntityConfigUpdateCommand.class), any(Username.class));
    service.createPackage(packageDefinition, packageRepository.getId(), new Username("user"), result);
    assertThat(result.isSuccessful()).isFalse();
    assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
    assertThat(result.message()).isEqualTo("Save failed. some rule violation message");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Configuration(com.thoughtworks.go.domain.config.Configuration) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) Username(com.thoughtworks.go.server.domain.Username) EntityConfigUpdateCommand(com.thoughtworks.go.config.commands.EntityConfigUpdateCommand) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Example 13 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class RulesService method validateSecretConfigReferences.

public boolean validateSecretConfigReferences(ScmMaterial scmMaterial) {
    List<CaseInsensitiveString> pipelines = goConfigService.pipelinesWithMaterial(scmMaterial.getFingerprint());
    Map<CaseInsensitiveString, StringBuilder> pipelinesWithErrors = new HashMap<>();
    pipelines.forEach(pipelineName -> {
        MaterialConfig materialConfig = goConfigService.findPipelineByName(pipelineName).materialConfigs().getByMaterialFingerPrint(scmMaterial.getFingerprint());
        PipelineConfigs group = goConfigService.findGroupByPipeline(pipelineName);
        ScmMaterialConfig scmMaterialConfig = (ScmMaterialConfig) materialConfig;
        SecretParams secretParams = SecretParams.parse(scmMaterialConfig.getPassword());
        secretParams.forEach(secretParam -> {
            String secretConfigId = secretParam.getSecretConfigId();
            SecretConfig secretConfig = goConfigService.getSecretConfigById(secretConfigId);
            if (secretConfig == null) {
                addError(pipelinesWithErrors, pipelineName, format("Pipeline '%s' is referring to none-existent secret config '%s'.", pipelineName, secretConfigId));
            } else if (!secretConfig.canRefer(group.getClass(), group.getGroup())) {
                addError(pipelinesWithErrors, pipelineName, format("Pipeline '%s' does not have permission to refer to secrets using secret config '%s'", pipelineName, secretConfigId));
            }
        });
    });
    if (!pipelinesWithErrors.isEmpty()) {
        LOGGER.debug("[Material Update] Failure: {}", errorString(pipelinesWithErrors));
    }
    if (pipelines.size() == pipelinesWithErrors.size()) {
        throw new RulesViolationException(errorString(pipelinesWithErrors));
    }
    return true;
}
Also used : HashMap(java.util.HashMap) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) ScmMaterialConfig(com.thoughtworks.go.config.materials.ScmMaterialConfig) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) ScmMaterialConfig(com.thoughtworks.go.config.materials.ScmMaterialConfig)

Example 14 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class PackageRepositoryServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForUpdate.

@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForUpdate() {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    doThrow(new RulesViolationException("some rule violation message")).when(goConfigService).updateConfig(any(EntityConfigUpdateCommand.class), any(Username.class));
    service.createPackageRepository(packageRepository("some-plugin"), new Username("user"), result);
    assertThat(result.isSuccessful()).isFalse();
    assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
    assertThat(result.message()).isEqualTo("Save failed. some rule violation message");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) EntityConfigUpdateCommand(com.thoughtworks.go.config.commands.EntityConfigUpdateCommand) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

RulesViolationException (com.thoughtworks.go.server.exceptions.RulesViolationException)14 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)8 Test (org.junit.jupiter.api.Test)7 SecretResolutionFailureException (com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)5 Username (com.thoughtworks.go.server.domain.Username)4 EntityConfigUpdateCommand (com.thoughtworks.go.config.commands.EntityConfigUpdateCommand)2 ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)2 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)2 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)2 Configuration (com.thoughtworks.go.domain.config.Configuration)2 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)2 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)2 Result (com.thoughtworks.go.plugin.api.response.Result)2 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)2 PackageMaterialTestHelper.assertPackageConfiguration (com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration)2 LocalizedOperationResult (com.thoughtworks.go.server.service.result.LocalizedOperationResult)2 HashMap (java.util.HashMap)2 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 ScmMaterialConfig (com.thoughtworks.go.config.materials.ScmMaterialConfig)1 AgentInstance (com.thoughtworks.go.domain.AgentInstance)1