@Override public void validate(ValidationContext validationContext) { this.validateGroupNameAndAddErrorsTo(this.configErrors); for(PipelineConfigs part : this.parts) { part.validate(validationContext); } verifyPipelineNameUniqueness(); }
@Override public boolean isValid(CruiseConfig preprocessedConfig) { preprocessedPipelineConfigs = pipelineConfigs; validateGroupName(pipelineConfigs.getGroup()); Authorization authorization = preprocessedPipelineConfigs.getAuthorization(); ConfigSaveValidationContext validationContext = ConfigSaveValidationContext.forChain(preprocessedConfig, preprocessedPipelineConfigs, authorization); preprocessedPipelineConfigs.validate(validationContext); boolean authorizationIsValid = true; if (authorization != null) { authorization.validateTree(new DelegatingValidationContext(validationContext) { @Override public boolean shouldNotCheckRole() { return false; } }); authorizationIsValid = authorization.getAllErrors().isEmpty(); } return preprocessedPipelineConfigs.errors().getAll().isEmpty() && authorizationIsValid; }
@Test public void validate_shouldMakeSureTheNameIsAppropriate() { PipelineConfigs group = createEmpty(); group.validate(null); assertThat(group.errors().on(BasicPipelineConfigs.GROUP), is("Invalid group name 'null'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); } @Test
@Test public void shouldValidateThatPipelineNameIsUnique() { PipelineConfig first = PipelineConfigMother.pipelineConfig("first"); PipelineConfig second = PipelineConfigMother.pipelineConfig("second"); PipelineConfigs group = createWithPipelines(first, second); PipelineConfig duplicate = PipelineConfigMother.pipelineConfig("first"); group.addWithoutValidation(duplicate); group.validate(null); assertThat(duplicate.errors().on(PipelineConfig.NAME), is("You have defined multiple pipelines called 'first'. Pipeline names are case-insensitive and must be unique.")); assertThat(first.errors().on(PipelineConfig.NAME), is("You have defined multiple pipelines called 'first'. Pipeline names are case-insensitive and must be unique.")); }
@Test public void validate_shouldMakeSureTheNameIsAppropriate_When2ConfigParts() { PipelineConfigs group = new MergePipelineConfigs(new BasicPipelineConfigs(),new BasicPipelineConfigs()); group.validate(null); assertThat(group.errors().on(BasicPipelineConfigs.GROUP), is("Invalid group name 'null'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
@Test public void shouldValidateThatPipelineNameIsUnique_When2ConfigParts() { PipelineConfig first = PipelineConfigMother.pipelineConfig("first"); PipelineConfig duplicate = PipelineConfigMother.pipelineConfig("first"); PipelineConfigs group = new MergePipelineConfigs( new BasicPipelineConfigs(first, PipelineConfigMother.pipelineConfig("second")), new BasicPipelineConfigs(duplicate, PipelineConfigMother.pipelineConfig("third"))); group.validate(null); assertThat(duplicate.errors().on(PipelineConfig.NAME), is("You have defined multiple pipelines called 'first'. Pipeline names are case-insensitive and must be unique.")); assertThat(first.errors().on(PipelineConfig.NAME), is("You have defined multiple pipelines called 'first'. Pipeline names are case-insensitive and must be unique.")); }
@Test public void shouldNotThrowExceptionIfNoRoleUsed() { StageConfig stage = new StageConfig(new CaseInsensitiveString("stage-foo"), new JobConfigs(new JobConfig(new CaseInsensitiveString("build-1"), new ResourceConfigs(), new ArtifactConfigs(), new Tasks(new ExecTask("ls", "-la", "work")) )) ); PipelineConfigs pipelinesConfig = new BasicPipelineConfigs("group", new Authorization(), new PipelineConfig(new CaseInsensitiveString("pipeline"), new MaterialConfigs(), stage)); CruiseConfig config = new BasicCruiseConfig(pipelinesConfig); config.server().security().addRole(new RoleConfig(new CaseInsensitiveString("role1"))); pipelinesConfig.validate(ConfigSaveValidationContext.forChain(config)); assertThat(pipelinesConfig.errors().isEmpty(), is(true)); }