-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
190 additions
and
148 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
...stra/core/validations/CronExpression.java → .../core/validations/ScheduleValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package io.kestra.core.validations; | ||
|
||
import io.kestra.core.validations.validator.CronExpressionValidator; | ||
import io.kestra.core.validations.validator.ScheduleValidator; | ||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = CronExpressionValidator.class) | ||
public @interface CronExpression { | ||
String message() default "invalid cron expression ({validatedValue})"; | ||
@Constraint(validatedBy = ScheduleValidator.class) | ||
public @interface ScheduleValidation { | ||
String message() default "invalid cron expression ({validatedValue.cron})"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
38 changes: 0 additions & 38 deletions
38
core/src/main/java/io/kestra/core/validations/validator/CronExpressionValidator.java
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
core/src/main/java/io/kestra/core/validations/validator/ScheduleValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.kestra.core.validations.validator; | ||
|
||
import com.cronutils.model.Cron; | ||
import io.kestra.core.validations.ScheduleValidation; | ||
import io.kestra.plugin.core.trigger.Schedule; | ||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.validation.validator.constraints.ConstraintValidator; | ||
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
@Introspected | ||
public class ScheduleValidator implements ConstraintValidator<ScheduleValidation, Schedule> { | ||
@Override | ||
public boolean isValid( | ||
@Nullable Schedule value, | ||
@NonNull AnnotationValue<ScheduleValidation> annotationMetadata, | ||
@NonNull ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
if (value.getCron() != null) { // if null, the standard @NotNull will do its job | ||
try { | ||
Cron parsed = value.parseCron(); | ||
parsed.validate(); | ||
} catch (IllegalArgumentException e) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate( "invalid cron expression '" + value.getCron() + "': " + e.getMessage()) | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
core/src/test/java/io/kestra/core/validations/CronExpressionTest.java
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
core/src/test/java/io/kestra/core/validations/ScheduleTest.java
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
core/src/test/java/io/kestra/core/validations/ScheduleValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.kestra.core.validations; | ||
|
||
import io.kestra.core.junit.annotations.KestraTest; | ||
import org.junit.jupiter.api.Test; | ||
import io.kestra.plugin.core.trigger.Schedule; | ||
import io.kestra.core.models.validations.ModelValidator; | ||
import io.kestra.core.utils.IdUtils; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@KestraTest | ||
class ScheduleValidationTest { | ||
@Inject | ||
private ModelValidator modelValidator; | ||
|
||
@Test | ||
void cronValidation() throws Exception { | ||
Schedule build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.cron("* * * * *") | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isEmpty(), is(true)); | ||
|
||
build = Schedule.builder() | ||
.type(Schedule.class.getName()) | ||
.cron("$ome Inv@lid Cr0n") | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isPresent(), is(true)); | ||
assertThat(modelValidator.isValid(build).get().getMessage(), containsString("invalid cron expression")); | ||
} | ||
|
||
@Test | ||
void nicknameValidation() throws Exception { | ||
Schedule build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.cron("@hourly") | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
void withSecondsValidation() throws Exception { | ||
Schedule build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.withSeconds(true) | ||
.cron("* * * * * *") | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isEmpty(), is(true)); | ||
|
||
build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.cron("* * * * * *") | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isPresent(), is(true)); | ||
assertThat(modelValidator.isValid(build).get().getMessage(), containsString("invalid cron expression")); | ||
} | ||
|
||
@Test | ||
void lateMaximumDelayValidation() { | ||
Schedule build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.cron("* * * * *") | ||
.lateMaximumDelay(Duration.ofSeconds(10)) | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(build).isPresent(), is(false)); | ||
} | ||
|
||
@Test | ||
void intervalValidation() { | ||
Schedule build = Schedule.builder() | ||
.id(IdUtils.create()) | ||
.type(Schedule.class.getName()) | ||
.cron("* * * * *") | ||
.interval(Duration.ofSeconds(5)) | ||
.build(); | ||
|
||
|
||
assertThat(modelValidator.isValid(build).isPresent(), is(true)); | ||
assertThat(modelValidator.isValid(build).get().getMessage(), containsString("interval: must be null")); | ||
|
||
} | ||
} |
Oops, something went wrong.