AggregatingValidator implements AdmissionValidator<JobDescriptor> { @Override public Mono<Set<ValidationError>> validate(JobDescriptor jobDescriptor) { return Mono.zip( getMonos(jobDescriptor, timeout, validators), objects -> Arrays.stream(objects) .map(objectSet -> (Set<ValidationError>) objectSet) .flatMap(Set::stream) .collect(Collectors.toSet())) .defaultIfEmpty(Collections.emptySet()); } @Inject  AggregatingValidator(
            TitusValidatorConfiguration configuration,
            Registry registry,
            Collection<? extends AdmissionValidator<JobDescriptor>> validators); @Override Mono<Set<ValidationError>> validate(JobDescriptor jobDescriptor); @Override ValidationError.Type getErrorType(); static String getTimeoutMsg(Duration timeout);  }
@Test public void validateHardPassPass() { AdmissionValidator pass0 = new PassJobValidator(); AdmissionValidator pass1 = new PassJobValidator(); AggregatingValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(pass0, pass1) ); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNext(Collections.emptySet()) .verifyComplete(); }
@Test public void validateHardFailFail() { AdmissionValidator fail0 = new FailJobValidator(ValidationError.Type.HARD); AdmissionValidator fail1 = new FailJobValidator(ValidationError.Type.HARD); AggregatingValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(fail0, fail1)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 2) .verifyComplete(); Set<ValidationError> errors = mono.block(); validateFailErrors(errors); validateErrorType(errors, ValidationError.Type.HARD); }
@Test public void validateHardPassFail() { AdmissionValidator pass = new PassJobValidator(); AdmissionValidator fail = new FailJobValidator(ValidationError.Type.HARD); AggregatingValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(pass, fail)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 1) .verifyComplete(); Set<ValidationError> errors = mono.block(); validateFailErrors(errors); validateErrorType(errors, ValidationError.Type.HARD); }
@Test public void validateHardPassTimeout() { AdmissionValidator pass = new PassJobValidator(); AdmissionValidator never = new NeverJobValidator(ValidationError.Type.HARD); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(pass, never)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 1) .verifyComplete(); Set<ValidationError> errors = mono.block(); validateTimeoutErrors(errors); validateErrorType(errors, ValidationError.Type.HARD); }
@Test public void validateHardPassFailTimeout() { AdmissionValidator pass = new PassJobValidator(); AdmissionValidator fail = new FailJobValidator(); AdmissionValidator never = new NeverJobValidator(); AdmissionValidator parallelValidator = new AggregatingValidator( configuration, registry, Arrays.asList(pass, fail, never)); Mono<Set<ValidationError>> mono = parallelValidator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 2) .verifyComplete(); Set<ValidationError> errors = mono.block(); validateErrorType(errors, ValidationError.Type.HARD); Collection<ValidationError> failErrors = errors.stream() .filter(error -> error.getField().equals(FailJobValidator.ERR_FIELD)) .collect(Collectors.toList()); assertThat(failErrors).hasSize(1); validateFailErrors(failErrors); Collection<ValidationError> timeoutErrors = errors.stream() .filter(error -> error.getField().equals(NeverJobValidator.class.getSimpleName())) .collect(Collectors.toList()); assertThat(timeoutErrors).hasSize(1); validateTimeoutErrors(timeoutErrors); }
@Test public void validateSoftTimeout() { AdmissionValidator never = new NeverJobValidator(ValidationError.Type.SOFT); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(never)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 1) .verifyComplete(); Collection<ValidationError> errors = mono.block(); validateTimeoutErrors(errors); validateErrorType(errors, ValidationError.Type.SOFT); }
@Test public void validateSoftFailure() { AdmissionValidator fail = new FailJobValidator(ValidationError.Type.HARD); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(fail)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 1) .verifyComplete(); Collection<ValidationError> errors = mono.block(); validateFailErrors(errors); validateErrorType(errors, ValidationError.Type.HARD); }
@Test public void validateSoftPass() { AdmissionValidator pass = new PassJobValidator(); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(pass)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 0) .verifyComplete(); }
@Test public void validateHardSoftTimeout() { AdmissionValidator<JobDescriptor> never0 = new NeverJobValidator(ValidationError.Type.HARD); AdmissionValidator never1 = new NeverJobValidator(ValidationError.Type.SOFT); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(never0, never1)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 2) .verifyComplete(); Collection<ValidationError> errors = mono.block(); validateTimeoutErrors(errors); Collection<ValidationError> hardErrors = errors.stream() .filter(error -> error.getType().equals(ValidationError.Type.HARD)) .collect(Collectors.toList()); assertThat(hardErrors).hasSize(1); Collection<ValidationError> softErrors = errors.stream() .filter(error -> error.getType().equals(ValidationError.Type.SOFT)) .collect(Collectors.toList()); assertThat(softErrors).hasSize(1); }
@Test public void validateHardSoftPass() { AdmissionValidator pass0 = new PassJobValidator(); AdmissionValidator pass1 = new PassJobValidator(); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(pass0, pass1)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 0) .verifyComplete(); }
@Test public void validateHardSoftFail() { AdmissionValidator fail0 = new FailJobValidator(ValidationError.Type.HARD); AdmissionValidator fail1 = new FailJobValidator(ValidationError.Type.HARD); AdmissionValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(fail0, fail1)); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNextMatches(errors -> errors.size() == 2) .verifyComplete(); Collection<ValidationError> errors = mono.block(); validateFailErrors(errors); assertThat(errors).allMatch(error -> error.getType().equals(ValidationError.Type.HARD)); }
@Test public void validateIsProtectedAgainstEmpty() { AdmissionValidator<JobDescriptor> pass = new PassJobValidator(); AdmissionValidator<JobDescriptor> empty = new EmptyValidator(); AggregatingValidator validator = new AggregatingValidator( configuration, registry, Arrays.asList(empty, pass, empty) ); Mono<Set<ValidationError>> mono = validator.validate(MOCK_JOB); StepVerifier.create(mono) .expectNext(Collections.emptySet()) .verifyComplete(); }