From a3bb4c5eb337950720f3b562ccf125ac099089e0 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Tue, 30 Apr 2024 13:41:16 +0200 Subject: [PATCH] fix: properly validate variables with default values in @Scheduled field Signed-off-by: Fred Bricon --- .../psi/core/PropertiesManagerForJava.java | 4 +- .../hover/PropertiesHoverParticipant.java | 49 +++++----- .../scheduler/SchedulerErrorCodes.java | 3 +- .../quarkus/scheduler/SchedulerUtils.java | 43 +++++++-- .../java/QuarkusSchedulerASTVisitor.java | 22 ++--- .../QuarkusScheduledPropertiesProvider.java | 6 +- .../quarkus/scheduler/SchedulerUtilsTest.java | 91 +++++++++++++------ 7 files changed, 142 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java index c2a4ccd96..0a17397e5 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java @@ -232,7 +232,7 @@ private void collectDefinition(String uri, PsiFile typeRoot, PsiElement hyperlin hyperlinkedElement, hyperlinkedPosition); List definitions = IJavaDefinitionParticipant.EP_NAME.extensions() .filter(definition -> definition.isAdaptedForDefinition(context)) - .collect(Collectors.toList()); + .toList(); if (definitions.isEmpty()) { return; } @@ -290,7 +290,7 @@ private void collectDiagnostics(String uri, IPsiUtils utils, DocumentFormat docu JavaDiagnosticsContext context = new JavaDiagnosticsContext(uri, typeRoot, utils, module, documentFormat, settings); List definitions = IJavaDiagnosticsParticipant.EP_NAME.extensions() .filter(definition -> definition.isAdaptedForDiagnostics(context)) - .collect(Collectors.toList()); + .toList(); if (definitions.isEmpty()) { return; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/java/hover/PropertiesHoverParticipant.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/java/hover/PropertiesHoverParticipant.java index 5640967b7..44a6519b7 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/java/hover/PropertiesHoverParticipant.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/java/hover/PropertiesHoverParticipant.java @@ -176,14 +176,25 @@ public Hover collectHover(JavaHoverContext context) { return null; } + String defaultValue = null; // property references may be surrounded by curly braces in Java file if (propertyReplacer != null) { propertyKey = propertyReplacer.apply(propertyKey); + int colonIdx = propertyKey.indexOf(":"); + if (colonIdx > -1) { + defaultValue = propertyKey.substring(colonIdx+1); + propertyKey = propertyKey.substring(0,colonIdx); + } } + + String defaultAnnotationValue = getAnnotationMemberValue(annotation, defaultValueAnnotationMemberName); + PsiMicroProfileProject mpProject = PsiMicroProfileProjectManager.getInstance(javaProject.getProject()) .getMicroProfileProject(javaProject); List propertyInformation = getConfigPropertyInformation(propertyKey, - annotation, defaultValueAnnotationMemberName, typeRoot, mpProject, utils); + annotation, + defaultAnnotationValue == null? defaultValue : defaultAnnotationValue, + typeRoot, mpProject, utils); return new Hover(getDocumentation(propertyInformation, context.getDocumentFormat(), context.isSurroundEqualsWithSpaces()), propertyKeyRange); } @@ -211,34 +222,30 @@ protected boolean isAdaptableFor(PsiElement hoverElement) { /** * Returns all the config property information for the given property key. - * + *

* Includes the information for all the different profiles. * - * @param propertyKey the property key without the profile - * @param annotation the annotation that defines the - * config property - * @param defaultValueAnnotationMemberName the annotation member name for - * default value and null otherwise. - * @param project the project - * @param utils the JDT LS utilities. + * @param propertyKey the property key without the profile + * @param annotation the annotation that defines the + * config property + * @param defaultValue default value and null otherwise. + * @param project the project + * @param utils the JDT LS utilities. * @return the config property information for the given property key */ private static List getConfigPropertyInformation(String propertyKey, - PsiAnnotation annotation, String defaultValueAnnotationMemberName, PsiFile typeRoot, - PsiMicroProfileProject project, IPsiUtils utils) { + PsiAnnotation annotation, + String defaultValue, + PsiFile typeRoot, + PsiMicroProfileProject project, + IPsiUtils utils) { List infos = project.getPropertyInformations(propertyKey); - boolean defaultProfileDefined = false; - - for (MicroProfileConfigPropertyInformation info : infos) { - if (info.getPropertyNameWithProfile().equals(propertyKey)) { - defaultProfileDefined = true; - } - } + boolean defaultProfileDefined = infos.stream().anyMatch(info -> propertyKey.equals(info.getPropertyNameWithProfile())); - if (defaultValueAnnotationMemberName != null && !defaultProfileDefined) { + if (!defaultProfileDefined) { infos.add(new MicroProfileConfigPropertyInformation(propertyKey, - getAnnotationMemberValue(annotation, defaultValueAnnotationMemberName), utils.toUri(typeRoot), + defaultValue, utils.toUri(typeRoot), annotation.getContainingFile().getName())); } @@ -271,7 +278,7 @@ private static void buildDocumentation(List 0) { + if (!content.isEmpty()) { content.append(" \n"); } diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerErrorCodes.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerErrorCodes.java index dafd79819..76f106bef 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerErrorCodes.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerErrorCodes.java @@ -33,7 +33,8 @@ public enum SchedulerErrorCodes implements IJavaErrorCode { INVALID_CRON_YEAR("Year must be in the range [1970, 2099]"), INVALID_CRON_LENGTH("The cron expression must contain 6-7 parts, delimited by whitespace."), INVALID_DURATION_PARSE_PATTERN("Text cannot be parsed to a Duration."), - INVALID_CHAR_IN_EXPRESSION("Invalid char(s) in expression."), VALID_EXPRESSION("Expression is valid."); + INVALID_CHAR_IN_EXPRESSION("Invalid char(s) in expression."), + VALID_EXPRESSION("Expression is valid."); private final String errorMessage; diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerUtils.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerUtils.java index 1f35761b6..558ac5df8 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerUtils.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/SchedulerUtils.java @@ -15,6 +15,7 @@ import java.time.Duration; import java.time.format.DateTimeParseException; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -23,6 +24,10 @@ */ public class SchedulerUtils { + public static enum ValidationType { + cron, duration + } + // Patterns for each cron part of the cron member expression in order of // SchedulerErrorCode enum ordinal // Note: DAY_OF_MONTH and DAY_OF_WEEK are mutually exclusive @@ -57,7 +62,8 @@ public class SchedulerUtils { private static final Pattern TIME_PERIOD_PATTERN = Pattern.compile("\\d+[smhSMH]$"); - private static final Pattern ENV_PATTERN = Pattern.compile("^\\$?\\{[0-9a-zA-Z.:]*\\}$"); + // Define the regex pattern for environment variables with optional default values (allowing spaces in default values) and optional prefix + private static final Pattern ENV_PATTERN = Pattern.compile("^\\$?\\{([^\\{\\}:\\s=]+)(?:\\:([^\\{\\}=]*))?\\}$"); private static final Pattern LOOSE_ENV_PATTERN = Pattern.compile("^.*\\$?\\{.*\\}.*$"); @@ -69,7 +75,7 @@ private SchedulerUtils() { * error message if necessary * * @param cronString the cron member value - * @return the error fault for the cron string validation and null if valid + * @return the error fault for the cron string validation and null if valid */ public static SchedulerErrorCodes validateCronPattern(String cronString) { @@ -93,7 +99,7 @@ public static SchedulerErrorCodes validateCronPattern(String cronString) { * unit * * @param timePattern the member value for the time pattern - * @return the INVALID_DURATION_PARSE_PATTERN error code if invalid and null if + * @return the INVALID_DURATION_PARSE_PATTERN error code if invalid and null if * valid */ // \\d+[smhSMH] @@ -112,21 +118,38 @@ public static SchedulerErrorCodes validateDurationParse(String timePattern) { * Check if the member value env variable pattern is well formed * * @param memberValue the member value from expression - * @return INVALID_CHAR_IN_EXPRESSION if the env variable is malformed + * @param validationType the type of validation to perform on the default value + * @return a SchedulerErrorCodes if memberValue is invalid, + * null otherwise. */ - public static SchedulerErrorCodes matchEnvMember(String memberValue) { - return checkLooseEnvPattern(memberValue) ? (checkEnvPattern(memberValue) ? SchedulerErrorCodes.VALID_EXPRESSION - : SchedulerErrorCodes.INVALID_CHAR_IN_EXPRESSION) : null; + public static SchedulerErrorCodes matchEnvMember(String memberValue, ValidationType validationType) { + return checkLooseEnvPattern(memberValue) ? checkEnvPattern(memberValue, validationType) : null; } /** * Match the member to an env variable pattern * * @param memberValue the member value from expression - * @return true if pattern is well-formed env variable, false otherwise + * @param validationType the type of validation to perform on the default value + * @return SchedulerErrorCodes if the variable is invalid or the default value is not a valid cron expression, + * SchedulerErrorCodes.VALID_EXPRESSION otherwise. */ - private static boolean checkEnvPattern(String memberValue) { - return ENV_PATTERN.matcher(memberValue).matches(); + private static SchedulerErrorCodes checkEnvPattern(String memberValue, ValidationType validationType) { + Matcher matcher = ENV_PATTERN.matcher(memberValue); + if (matcher.matches()) { + String defaultValue = matcher.group(2); + if (defaultValue == null) { + return SchedulerErrorCodes.VALID_EXPRESSION; + } + SchedulerErrorCodes defaultValueValidation; + if (validationType == ValidationType.cron) { + defaultValueValidation = validateCronPattern(defaultValue); + } else { + defaultValueValidation = validateDurationParse(defaultValue); + } + return defaultValueValidation == null? SchedulerErrorCodes.VALID_EXPRESSION: defaultValueValidation; + } + return SchedulerErrorCodes.INVALID_CHAR_IN_EXPRESSION; } /** diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/java/QuarkusSchedulerASTVisitor.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/java/QuarkusSchedulerASTVisitor.java index 0de57578e..0af3efeeb 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/java/QuarkusSchedulerASTVisitor.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/java/QuarkusSchedulerASTVisitor.java @@ -68,9 +68,8 @@ public void visitMethod(PsiMethod node) { private void validateScheduledAnnotation(PsiMethod node, PsiAnnotation annotation) { PsiAnnotationMemberValue cronExpr = getAnnotationMemberValueExpression(annotation, QuarkusConstants.SCHEDULED_ANNOTATION_CRON); - if (cronExpr != null && cronExpr instanceof PsiLiteral && ((PsiLiteral) cronExpr).getValue() instanceof String) { - String cronValue = (String) ((PsiLiteral) cronExpr).getValue(); - if (!malformedEnvDiagnostic(cronExpr, cronValue)) { + if (cronExpr instanceof PsiLiteral cronExprLit && cronExprLit.getValue() instanceof String cronValue) { + if (!checkedEnvDiagnostic(cronExpr, cronValue, SchedulerUtils.ValidationType.cron)) { SchedulerErrorCodes cronPartFault = SchedulerUtils.validateCronPattern(cronValue); if (cronPartFault != null) { super.addDiagnostic(cronPartFault.getErrorMessage(), QuarkusConstants.QUARKUS_PREFIX, cronExpr, @@ -80,12 +79,12 @@ private void validateScheduledAnnotation(PsiMethod node, PsiAnnotation annotatio } PsiAnnotationMemberValue everyExpr = getAnnotationMemberValueExpression(annotation, QuarkusConstants.SCHEDULED_ANNOTATION_EVERY); - if (everyExpr != null && everyExpr instanceof PsiLiteral && ((PsiLiteral) everyExpr).getValue() instanceof String) { + if (everyExpr instanceof PsiLiteral everyExprLit && everyExprLit.getValue() instanceof String) { durationParseDiagnostics(everyExpr); } PsiAnnotationMemberValue delayedExpr = getAnnotationMemberValueExpression(annotation, QuarkusConstants.SCHEDULED_ANNOTATION_DELAYED); - if (delayedExpr != null && delayedExpr instanceof PsiLiteral && ((PsiLiteral) delayedExpr).getValue() instanceof String) { + if (delayedExpr instanceof PsiLiteral delayedExprLit && delayedExprLit.getValue() instanceof String) { durationParseDiagnostics(delayedExpr); } } @@ -97,7 +96,7 @@ private void validateScheduledAnnotation(PsiMethod node, PsiAnnotation annotatio */ private void durationParseDiagnostics(PsiAnnotationMemberValue expr) { String value = (String) ((PsiLiteral) expr).getValue(); - if (!malformedEnvDiagnostic(expr, value)) { + if (!checkedEnvDiagnostic(expr, value, SchedulerUtils.ValidationType.duration)) { SchedulerErrorCodes memberFault = SchedulerUtils.validateDurationParse(value); if (memberFault != null) { super.addDiagnostic(memberFault.getErrorMessage(), QuarkusConstants.QUARKUS_PREFIX, expr, memberFault, @@ -109,13 +108,14 @@ private void durationParseDiagnostics(PsiAnnotationMemberValue expr) { /** * Retrieve the SchedulerErrorCodes for env member value check * - * @param expr The expression retrieved from the annotation - * @param memberValue The member value from expression + * @param expr The expression retrieved from the annotation + * @param memberValue The member value from expression + * @param validationType */ - private boolean malformedEnvDiagnostic(PsiAnnotationMemberValue expr, String memberValue) { - SchedulerErrorCodes malformedEnvFault = SchedulerUtils.matchEnvMember(memberValue); + private boolean checkedEnvDiagnostic(PsiAnnotationMemberValue expr, String memberValue, SchedulerUtils.ValidationType validationType) { + SchedulerErrorCodes malformedEnvFault = SchedulerUtils.matchEnvMember(memberValue, validationType); if (malformedEnvFault != null) { - if (malformedEnvFault == SchedulerErrorCodes.INVALID_CHAR_IN_EXPRESSION) { + if (!SchedulerErrorCodes.VALID_EXPRESSION.equals(malformedEnvFault)) { super.addDiagnostic(malformedEnvFault.getErrorMessage(), QuarkusConstants.QUARKUS_PREFIX, expr, malformedEnvFault, DiagnosticSeverity.Warning); } diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/properties/QuarkusScheduledPropertiesProvider.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/properties/QuarkusScheduledPropertiesProvider.java index aa4fc5359..fb6dab183 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/properties/QuarkusScheduledPropertiesProvider.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/scheduler/properties/QuarkusScheduledPropertiesProvider.java @@ -38,7 +38,7 @@ public class QuarkusScheduledPropertiesProvider extends AbstractAnnotationTypeRe private static final String[] ANNOTATION_NAMES = { QuarkusConstants.SCHEDULED_ANNOTATION }; - private static Pattern PROP_PATTERN = Pattern.compile("\\{(.*)\\}"); + private static final Pattern PROP_PATTERN = Pattern.compile("\\{(.*)\\}"); @Override protected String[] getAnnotationNames() { @@ -62,6 +62,10 @@ protected void processAnnotation(PsiModifierListOwner javaElement, PsiAnnotation Matcher m = PROP_PATTERN.matcher(name); if (m.matches()) { name = m.group(1); + int colonIdx = name.indexOf(":"); + if (colonIdx > -1) {//remove default value + name = name.substring(0,colonIdx); + } addItemMetadata(collector, name, "java.lang.String", description, sourceType, null, sourceMethod, null, extensionName, binary); } diff --git a/src/test/java/com/redhat/microprofile/psi/quarkus/scheduler/SchedulerUtilsTest.java b/src/test/java/com/redhat/microprofile/psi/quarkus/scheduler/SchedulerUtilsTest.java index b273874ea..febb6c8a2 100644 --- a/src/test/java/com/redhat/microprofile/psi/quarkus/scheduler/SchedulerUtilsTest.java +++ b/src/test/java/com/redhat/microprofile/psi/quarkus/scheduler/SchedulerUtilsTest.java @@ -18,173 +18,204 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + public class SchedulerUtilsTest { @Test public void validCronPatternMinLengthTest() { String cronPattern = "* * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronPatternMaxLengthTest() { String cronPattern = "* * * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void invalidCronPatternUnderLengthTest() { String cronPattern = "* * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); } @Test public void invalidCronPatternOverLengthTest() { String cronPattern = "* * * * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); } @Test public void validCronPatternExclusionCharacterTest() { String cronPattern = "? ? ? ? ? ?"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronPatternCommaSeperatedTest() { String cronPattern = "0,1,59 0,1,59 0,1,23 1,2,31 1,2,12 1,2,7 1970,1971,2099"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronPatternCommaSeperatedAbbrevTest() { String cronPattern = "* * * * JAN,FEB,DEC MON,TUE,SUN"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void invalidCronPatternCommaSeperatedTest() { String cronPattern = "0,1,60 * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); } @Test public void validCronPatternRangeTest() { String cronPattern = "0-59 0-59 0-23 1-31 1-12 1-7 1970-2099"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronPatternRangeAbbrevTest() { String cronPattern = "* * * * JAN-DEC MON-SUN"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void invalidCronPatternRangeTest() { String cronPattern = "0-60 * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); } @Test public void cronMultipleInvalidTest() { String cronPattern = "60 60 24 32 13 ? 1969"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); } @Test public void cronFirstValidMultipleInvalidTest() { String cronPattern = "59 60 24 32 13 ? 1969"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_MINUTE, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_MINUTE, actual); } @Test public void validCronCaseMixingMonthAbbrevTest() { String cronPattern = "* * * * jAn *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronCaseMixingDayOfWeekAbbrevTest() { String cronPattern = "* * * * * mOn"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validCronIntervalTest() { String cronPattern = "*/5 * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void invalidCronIntervalTest() { String cronPattern = "*/s * * * * *"; SchedulerErrorCodes actual = SchedulerUtils.validateCronPattern(cronPattern); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); + assertEquals(SchedulerErrorCodes.INVALID_CRON_SECOND, actual); } @Test public void validDurationIntervalTest() { String durationPattern = "5m"; SchedulerErrorCodes actual = SchedulerUtils.validateDurationParse(durationPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void validDurationISO8601Test() { String durationPattern = "PT15M"; SchedulerErrorCodes actual = SchedulerUtils.validateDurationParse(durationPattern); - Assert.assertNull(actual); + assertNull(actual); } @Test public void valid$EnvMemberTest() { String envMember = "${some.expression}"; - SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember); - Assert.assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + } + + @Test + public void validDefaultCronValueTest() { + String envMember = "{quarkus.registry.cache.cron:*/10 * * * * ?}"; + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + } + + @Test + public void invalidDefaultCronValueTest() { + String envMember = "{quarkus.registry.cache.cron:*/10 * * ?}"; + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); + } + + @Test + public void validDefaultDurationValueTest() { + String envMember = "{some.duration:18m}"; + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.duration); + assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + } + + @Test + public void invalidDefaultDurationValueTest() { + String envMember = "{some.duration:18km}"; + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.duration); + assertEquals(SchedulerErrorCodes.INVALID_DURATION_PARSE_PATTERN, actual); } @Test public void validNo$EnvMemberTest() { String envMember = "{some.expression}"; - SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember); - Assert.assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); } @Test - public void validEnvMemberTestWithParamTest() { + public void validEnvMemberTestWithInvalidDefaultCronTest() { String envMember = "${some.expression:off}"; - SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember); - Assert.assertEquals(SchedulerErrorCodes.VALID_EXPRESSION, actual); + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.INVALID_CRON_LENGTH, actual); } @Test public void invalidEnvMemberTest() { String envMember = "$${some?expression}}"; - SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember); - Assert.assertEquals(SchedulerErrorCodes.INVALID_CHAR_IN_EXPRESSION, actual); + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember,SchedulerUtils.ValidationType.cron); + assertEquals(SchedulerErrorCodes.INVALID_CHAR_IN_EXPRESSION, actual); } @Test public void notEnvMemberTest() { String envMember = "nocurlybraceorenvvar"; - SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember); - Assert.assertNull(actual); + SchedulerErrorCodes actual = SchedulerUtils.matchEnvMember(envMember, SchedulerUtils.ValidationType.cron); + assertNull(actual); } } \ No newline at end of file