From 1c4902ccf6448f90a070585f1eb16c68a519c420 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 17 Nov 2024 22:55:25 -0800 Subject: [PATCH 1/2] Replaced reflection-based impl for validators, impproved SwitcherProperties --- pom.xml | 2 +- .../switcherapi/client/ContextBuilder.java | 37 +-- .../client/SwitcherContextBase.java | 12 +- .../client/SwitcherContextValidator.java | 14 +- .../client/SwitcherProperties.java | 240 +++++------------- .../client/service/ValidatorService.java | 34 +-- .../service/validators/DateValidator.java | 6 +- .../service/validators/NetworkValidator.java | 11 +- .../service/validators/NumericValidator.java | 13 +- .../service/validators/PayloadValidator.java | 12 +- .../service/validators/RegexValidator.java | 6 +- .../service/validators/TimeValidator.java | 6 +- .../client/service/validators/Validator.java | 3 + .../validators/ValidatorComponent.java | 16 -- .../service/validators/ValueValidator.java | 10 +- .../client/SwitcherContextBuilderTest.java | 8 + .../client/validator/CustomValidator.java | 7 +- .../validator/InvalidCustom2Validator.java | 18 -- .../validator/InvalidCustomValidator.java | 21 -- .../client/validator/ValidatorsTest.java | 18 +- 20 files changed, 166 insertions(+), 328 deletions(-) delete mode 100644 src/main/java/com/github/switcherapi/client/service/validators/ValidatorComponent.java delete mode 100644 src/test/java/com/github/switcherapi/client/validator/InvalidCustom2Validator.java delete mode 100644 src/test/java/com/github/switcherapi/client/validator/InvalidCustomValidator.java diff --git a/pom.xml b/pom.xml index 48bda661..fe6822d5 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.github.switcherapi switcher-client jar - 2.2.0 + 2.2.1-SNAPSHOT Switcher Client Switcher Client SDK for working with Switcher API diff --git a/src/main/java/com/github/switcherapi/client/ContextBuilder.java b/src/main/java/com/github/switcherapi/client/ContextBuilder.java index abcdfdfd..3799798c 100644 --- a/src/main/java/com/github/switcherapi/client/ContextBuilder.java +++ b/src/main/java/com/github/switcherapi/client/ContextBuilder.java @@ -1,5 +1,6 @@ package com.github.switcherapi.client; +import com.github.switcherapi.client.model.ContextKey; import org.apache.commons.lang3.StringUtils; public class ContextBuilder { @@ -52,7 +53,7 @@ SwitcherProperties build() { * @return ContextBuilder */ public ContextBuilder contextLocation(String contextLocation) { - properties.setContextLocation(contextLocation); + properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation); return this; } @@ -61,7 +62,7 @@ public ContextBuilder contextLocation(String contextLocation) { * @return ContextBuilder */ public ContextBuilder url(String url) { - properties.setUrl(url); + properties.setValue(ContextKey.URL, url); return this; } @@ -70,7 +71,7 @@ public ContextBuilder url(String url) { * @return ContextBuilder */ public ContextBuilder apiKey(String apiKey) { - properties.setApiKey(apiKey); + properties.setValue(ContextKey.APIKEY, apiKey); return this; } @@ -79,7 +80,7 @@ public ContextBuilder apiKey(String apiKey) { * @return ContextBuilder */ public ContextBuilder domain(String domain) { - properties.setDomain(domain); + properties.setValue(ContextKey.DOMAIN, domain); return this; } @@ -88,7 +89,7 @@ public ContextBuilder domain(String domain) { * @return ContextBuilder */ public ContextBuilder component(String component) { - properties.setComponent(component); + properties.setValue(ContextKey.COMPONENT, component); return this; } @@ -97,7 +98,7 @@ public ContextBuilder component(String component) { * @return ContextBuilder */ public ContextBuilder environment(String environment) { - properties.setEnvironment(environment); + properties.setValue(ContextKey.ENVIRONMENT, properties.getEnvironmentOrDefault(environment)); return this; } @@ -106,7 +107,7 @@ public ContextBuilder environment(String environment) { * @return ContextBuilder */ public ContextBuilder snapshotLocation(String snapshotLocation) { - properties.setSnapshotLocation(snapshotLocation); + properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation); return this; } @@ -115,10 +116,10 @@ public ContextBuilder snapshotLocation(String snapshotLocation) { * @return ContextBuilder */ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) { - properties.setSnapshotAutoUpdateInterval(snapshotAutoUpdateInterval); + properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval); if (snapshotAutoUpdateInterval != null) - properties.setSnapshotAutoLoad(true); + properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); return this; } @@ -130,7 +131,7 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv * @return ContextBuilder */ public ContextBuilder regexTimeout(String regexTimeout) { - properties.setRegexTimeout(regexTimeout); + properties.setValue(ContextKey.REGEX_TIMEOUT, properties.getRegexTimeoutOrDefault(regexTimeout)); return this; } @@ -139,7 +140,7 @@ public ContextBuilder regexTimeout(String regexTimeout) { * @return ContextBuilder */ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) { - properties.setSnapshotAutoLoad(snapshotAutoLoad); + properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad); return this; } @@ -148,7 +149,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) { * @return ContextBuilder */ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) { - properties.setSnapshotSkipValidation(snapshotSkipValidation); + properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation); return this; } @@ -157,10 +158,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) { * @return ContextBuilder */ public ContextBuilder silentMode(String retryAfter) { - properties.setSilentMode(retryAfter); + properties.setValue(ContextKey.SILENT_MODE, retryAfter); if (StringUtils.isNotBlank(retryAfter)) { - properties.setSnapshotAutoLoad(true); + properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); } return this; @@ -171,7 +172,7 @@ public ContextBuilder silentMode(String retryAfter) { * @return ContextBuilder */ public ContextBuilder local(boolean local) { - properties.setLocal(local); + properties.setValue(ContextKey.LOCAL_MODE, local); return this; } @@ -180,7 +181,7 @@ public ContextBuilder local(boolean local) { * @return ContextBuilder */ public ContextBuilder truststorePath(String truststorePath) { - properties.setTruststorePath(truststorePath); + properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath); return this; } @@ -189,7 +190,7 @@ public ContextBuilder truststorePath(String truststorePath) { * @return ContextBuilder */ public ContextBuilder truststorePassword(String truststorePassword) { - properties.setTruststorePassword(truststorePassword); + properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword); return this; } @@ -198,7 +199,7 @@ public ContextBuilder truststorePassword(String truststorePassword) { * @return ContextBuilder */ public ContextBuilder timeoutMs(String timeoutMs) { - properties.setTimeoutMs(timeoutMs); + properties.setValue(ContextKey.TIMEOUT_MS, properties.getTimeoutMsOrDefault(timeoutMs)); return this; } } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java index 58c56b73..33f5fd15 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java @@ -113,14 +113,14 @@ public static void initializeClient() { validateContext(); validateSwitcherKeys(); - if (switcherProperties.isLocal()) { + if (contextBol(ContextKey.LOCAL_MODE)) { instance = new SwitcherLocalService(); } else { instance = new SwitcherRemoteService(); } loadSwitchers(); - scheduleSnapshotAutoUpdate(switcherProperties.getSnapshotAutoUpdateInterval()); + scheduleSnapshotAutoUpdate(contextStr(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL)); ContextBuilder.preConfigure(switcherProperties); } @@ -143,7 +143,7 @@ private static void validateSwitcherKeys() { try { switcherKeys = new HashSet<>(); - final Class clazz = Class.forName(switcherProperties.getContextLocation()); + final Class clazz = Class.forName(contextStr(ContextKey.CONTEXT_LOCATION)); for (Field field : clazz.getFields()) { if (field.isAnnotationPresent(SwitcherKey.class)) { switcherKeys.add(field.getName()); @@ -275,7 +275,7 @@ public static Switcher getSwitcher(String key) { * @return true if snapshot was updated */ public static boolean validateSnapshot() { - if (switcherProperties.isSnapshotSkipValidation() || instance.checkSnapshotVersion()) { + if (contextBol(ContextKey.SNAPSHOT_SKIP_VALIDATION) || instance.checkSnapshotVersion()) { return false; } @@ -347,7 +347,7 @@ public static void checkSwitchers() { * @return Value configured for the context parameter */ public static String contextStr(ContextKey contextKey) { - return switcherProperties.getValue(contextKey, String.class); + return switcherProperties.getValue(contextKey); } /** @@ -357,7 +357,7 @@ public static String contextStr(ContextKey contextKey) { * @return Value configured for the context parameter */ public static boolean contextBol(ContextKey contextKey) { - return switcherProperties.getValue(contextKey, Boolean.class); + return switcherProperties.getBoolean(contextKey); } /** diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java b/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java index 42596119..f32915b0 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java @@ -28,11 +28,11 @@ private SwitcherContextValidator() {} * @throws SwitcherContextException if validation fails */ public static void validate(final SwitcherProperties prop) { - if (StringUtils.isBlank(prop.getContextLocation())) { + if (StringUtils.isBlank(prop.getValue(ContextKey.CONTEXT_LOCATION))) { throw new SwitcherContextException(ERR_CONTEXT); } - if (!prop.isLocal()) { + if (!prop.getBoolean(ContextKey.LOCAL_MODE)) { validateRemote(prop); } @@ -46,7 +46,7 @@ public static void validate(final SwitcherProperties prop) { */ public static void validateOptionals(final SwitcherProperties prop) { try { - Integer.parseInt(prop.getRegexTimeout()); + Integer.parseInt(prop.getValue(ContextKey.REGEX_TIMEOUT)); } catch (NumberFormatException e) { throw new SwitcherContextException( String.format(ERR_FORMAT, ContextKey.REGEX_TIMEOUT.getParam(), Integer.class)); @@ -59,19 +59,19 @@ public static void validateOptionals(final SwitcherProperties prop) { * @param prop Configured properties */ public static void validateRemote(final SwitcherProperties prop) { - if (StringUtils.isBlank(prop.getUrl())) { + if (StringUtils.isBlank(prop.getValue(ContextKey.URL))) { throw new SwitcherContextException(ERR_URL); } - if (StringUtils.isBlank(prop.getApiKey())) { + if (StringUtils.isBlank(prop.getValue(ContextKey.APIKEY))) { throw new SwitcherContextException(ERR_API); } - if (StringUtils.isBlank(prop.getDomain())) { + if (StringUtils.isBlank(prop.getValue(ContextKey.DOMAIN))) { throw new SwitcherContextException(ERR_DOMAIN); } - if (StringUtils.isBlank(prop.getComponent())) { + if (StringUtils.isBlank(prop.getValue(ContextKey.COMPONENT))) { throw new SwitcherContextException(ERR_COMPONENT); } } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java index 9700a5c5..5f0f7bc2 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java @@ -1,13 +1,13 @@ package com.github.switcherapi.client; -import java.lang.reflect.Field; -import java.util.Properties; - -import org.apache.commons.lang3.StringUtils; - import com.github.switcherapi.client.exception.SwitcherContextException; import com.github.switcherapi.client.model.ContextKey; import com.github.switcherapi.client.utils.SwitcherUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; /** * The configuration definition object contains all necessary SDK properties to @@ -23,210 +23,90 @@ public class SwitcherProperties { public static final String DEFAULT_TIMEOUT_MS = "3000"; - private String contextLocation; - - private String url; - - private String apiKey; - - private String domain; - - private String component; - - private String environment; - - private String snapshotLocation; - - private String snapshotAutoUpdateInterval; - - private String regexTimeout; - - private String silentMode; - - private boolean snapshotAutoLoad; - - private boolean snapshotSkipValidation; - - private boolean local; - - private String truststorePath; - - private String truststorePassword; - - private String timeoutMs; + private final Map properties = new HashMap<>(); public SwitcherProperties() { - this.environment = DEFAULT_ENV; - this.regexTimeout = DEFAULT_REGEX_TIMEOUT; - this.timeoutMs = DEFAULT_TIMEOUT_MS; - } - - public void loadFromProperties(Properties prop) { - setContextLocation(SwitcherUtils.resolveProperties(ContextKey.CONTEXT_LOCATION.getParam(), prop)); - setUrl(SwitcherUtils.resolveProperties(ContextKey.URL.getParam(), prop)); - setApiKey(SwitcherUtils.resolveProperties(ContextKey.APIKEY.getParam(), prop)); - setDomain(SwitcherUtils.resolveProperties(ContextKey.DOMAIN.getParam(), prop)); - setComponent(SwitcherUtils.resolveProperties(ContextKey.COMPONENT.getParam(), prop)); - setEnvironment(SwitcherUtils.resolveProperties(ContextKey.ENVIRONMENT.getParam(), prop)); - setSnapshotLocation(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_LOCATION.getParam(), prop)); - setSnapshotSkipValidation(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_SKIP_VALIDATION.getParam(), prop))); - setSnapshotAutoLoad(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_LOAD.getParam(), prop))); - setSnapshotAutoUpdateInterval(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop)); - setSilentMode(SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop)); - setLocal(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop))); - setRegexTimeout(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop)); - setTruststorePath(SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop)); - setTruststorePassword(SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop)); - setTimeoutMs(SwitcherUtils.resolveProperties(ContextKey.TIMEOUT_MS.getParam(), prop)); - } - - public T getValue(ContextKey contextKey, Class type) { - try { - final Field field = SwitcherProperties.class.getDeclaredField(contextKey.getPropField()); - return type.cast(field.get(this)); - } catch (Exception e) { - throw new SwitcherContextException(e.getMessage()); - } - } - - public String getContextLocation() { - return contextLocation; - } - - public void setContextLocation(String contextLocation) { - this.contextLocation = contextLocation; - } - - public String getUrl() { - return url; + setDefaults(); } - public void setUrl(String url) { - this.url = url; + private void setDefaults() { + setValue(ContextKey.ENVIRONMENT, DEFAULT_ENV); + setValue(ContextKey.REGEX_TIMEOUT, DEFAULT_REGEX_TIMEOUT); + setValue(ContextKey.TIMEOUT_MS, DEFAULT_TIMEOUT_MS); + setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false); + setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false); + setValue(ContextKey.LOCAL_MODE, false); } - - public String getApiKey() { - return apiKey; - } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - - public String getDomain() { - return domain; + + public void loadFromProperties(Properties prop) { + setValue(ContextKey.CONTEXT_LOCATION, SwitcherUtils.resolveProperties(ContextKey.CONTEXT_LOCATION.getParam(), prop)); + setValue(ContextKey.URL, SwitcherUtils.resolveProperties(ContextKey.URL.getParam(), prop)); + setValue(ContextKey.APIKEY, SwitcherUtils.resolveProperties(ContextKey.APIKEY.getParam(), prop)); + setValue(ContextKey.DOMAIN, SwitcherUtils.resolveProperties(ContextKey.DOMAIN.getParam(), prop)); + setValue(ContextKey.COMPONENT, SwitcherUtils.resolveProperties(ContextKey.COMPONENT.getParam(), prop)); + setValue(ContextKey.ENVIRONMENT, getEnvironmentOrDefault(SwitcherUtils.resolveProperties(ContextKey.ENVIRONMENT.getParam(), prop))); + setValue(ContextKey.SNAPSHOT_LOCATION, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_LOCATION.getParam(), prop)); + setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, getBoolDefault(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_SKIP_VALIDATION.getParam(), prop)), false)); + setValue(ContextKey.SNAPSHOT_AUTO_LOAD, getBoolDefault(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_LOAD.getParam(), prop)), false)); + setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop)); + setValue(ContextKey.SILENT_MODE, SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop)); + setValue(ContextKey.LOCAL_MODE, getBoolDefault(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop)), false)); + setValue(ContextKey.REGEX_TIMEOUT, getRegexTimeoutOrDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop))); + setValue(ContextKey.TRUSTSTORE_PATH, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop)); + setValue(ContextKey.TRUSTSTORE_PASSWORD, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop)); + setValue(ContextKey.TIMEOUT_MS, getTimeoutMsOrDefault(SwitcherUtils.resolveProperties(ContextKey.TIMEOUT_MS.getParam(), prop))); } - public void setDomain(String domain) { - this.domain = domain; + public String getValue(ContextKey contextKey) { + return getValue(contextKey, String.class); } - public String getComponent() { - return component; + public Boolean getBoolean(ContextKey contextKey) { + return getValue(contextKey, Boolean.class); } - public void setComponent(String component) { - this.component = component; + private T getValue(ContextKey contextKey, Class type) { + try { + return type.cast(properties.get(contextKey.getParam())); + } catch (ClassCastException e) { + throw new SwitcherContextException(e.getMessage()); + } } - public String getEnvironment() { - return environment; + public void setValue(ContextKey contextKey, Object value) { + properties.put(contextKey.getParam(), value); } - public void setEnvironment(String environment) { + public String getEnvironmentOrDefault(String environment) { if (StringUtils.isNotBlank(environment)) { - this.environment = environment; - } else { - this.environment = DEFAULT_ENV; + return environment; } - } - - public String getSnapshotLocation() { - return snapshotLocation; - } - - public void setSnapshotLocation(String snapshotLocation) { - this.snapshotLocation = snapshotLocation; - } - public String getSnapshotAutoUpdateInterval() { - return snapshotAutoUpdateInterval; + return DEFAULT_ENV; } - public void setSnapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) { - this.snapshotAutoUpdateInterval = snapshotAutoUpdateInterval; - } - - public String getRegexTimeout() { - return regexTimeout; - } - - public void setRegexTimeout(String regexTimeout) { + public String getRegexTimeoutOrDefault(String regexTimeout) { if (StringUtils.isNotBlank(regexTimeout)) { - this.regexTimeout = regexTimeout; - } else { - this.regexTimeout = DEFAULT_REGEX_TIMEOUT; + return regexTimeout; } - } - - public boolean isSnapshotAutoLoad() { - return snapshotAutoLoad; - } - - public void setSnapshotAutoLoad(boolean snapshotAutoLoad) { - this.snapshotAutoLoad = snapshotAutoLoad; - } - - public boolean isSnapshotSkipValidation() { - return snapshotSkipValidation; - } - - public void setSnapshotSkipValidation(boolean snapshotSkipValidation) { - this.snapshotSkipValidation = snapshotSkipValidation; - } - - public String getSilentMode() { - return silentMode; - } - - public void setSilentMode(String silentMode) { - this.silentMode = silentMode; - } - - public boolean isLocal() { - return local; - } - - public void setLocal(boolean local) { - this.local = local; - } - - public String getTruststorePath() { - return truststorePath; - } - - public void setTruststorePath(String truststorePath) { - this.truststorePath = truststorePath; - } - public String getTruststorePassword() { - return truststorePassword; + return DEFAULT_REGEX_TIMEOUT; } - public void setTruststorePassword(String truststorePassword) { - this.truststorePassword = truststorePassword; - } + public String getTimeoutMsOrDefault(String timeoutMs) { + if (StringUtils.isNotBlank(timeoutMs)) { + return timeoutMs; + } - public String getTimeoutMs() { - return timeoutMs; + return DEFAULT_TIMEOUT_MS; } - public void setTimeoutMs(String timeoutMs) { - if (StringUtils.isNotBlank(timeoutMs)) { - this.timeoutMs = timeoutMs; - } else { - this.timeoutMs = DEFAULT_TIMEOUT_MS; + public Boolean getBoolDefault(Boolean value, Boolean defaultValue) { + if (value != null) { + return value; } + + return defaultValue; } } diff --git a/src/main/java/com/github/switcherapi/client/service/ValidatorService.java b/src/main/java/com/github/switcherapi/client/service/ValidatorService.java index 05ab7ca7..a7af07c1 100644 --- a/src/main/java/com/github/switcherapi/client/service/ValidatorService.java +++ b/src/main/java/com/github/switcherapi/client/service/ValidatorService.java @@ -1,8 +1,6 @@ package com.github.switcherapi.client.service; -import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.exception.SwitcherInvalidStrategyException; -import com.github.switcherapi.client.exception.SwitcherInvalidValidatorException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; @@ -20,31 +18,17 @@ public ValidatorService() { } private void initializeValidators() { - registerValidator(DateValidator.class); - registerValidator(NetworkValidator.class); - registerValidator(NumericValidator.class); - registerValidator(PayloadValidator.class); - registerValidator(TimeValidator.class); - registerValidator(ValueValidator.class); - registerValidator(RegexValidator.class); + registerValidator(new DateValidator()); + registerValidator(new NetworkValidator()); + registerValidator(new NumericValidator()); + registerValidator(new PayloadValidator()); + registerValidator(new TimeValidator()); + registerValidator(new ValueValidator()); + registerValidator(new RegexValidator()); } - private StrategyValidator getStrategyValidator(Class validatorClass) { - if (!validatorClass.isAnnotationPresent(ValidatorComponent.class)) { - throw new SwitcherInvalidValidatorException(validatorClass.getName()); - } - - return validatorClass.getAnnotation(ValidatorComponent.class).type(); - } - - public void registerValidator(Class validatorClass) { - try { - validators.put(getStrategyValidator(validatorClass), validatorClass.getConstructor().newInstance()); - } catch (SwitcherInvalidValidatorException e) { - throw e; - } catch (Exception e) { - throw new SwitcherException(e.getMessage(), e); - } + public void registerValidator(Validator validator) { + validators.put(validator.getType(), validator); } public boolean execute(final Strategy strategy, final Entry switcherInput) diff --git a/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java index 939bd94b..06caa4f4 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java @@ -12,11 +12,15 @@ import java.text.ParseException; import java.util.Date; -@ValidatorComponent(type = StrategyValidator.DATE) public class DateValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + @Override + public StrategyValidator getType() { + return StrategyValidator.DATE; + } + @Override public boolean process(final Strategy strategy, final Entry switcherInput) throws SwitcherInvalidOperationException, SwitcherInvalidTimeFormat, SwitcherInvalidOperationInputException { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/NetworkValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/NetworkValidator.java index 4ccfae66..5b690251 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/NetworkValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/NetworkValidator.java @@ -1,17 +1,20 @@ package com.github.switcherapi.client.service.validators; -import org.apache.commons.net.util.SubnetUtils; -import org.apache.commons.net.util.SubnetUtils.SubnetInfo; - import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; +import org.apache.commons.net.util.SubnetUtils; +import org.apache.commons.net.util.SubnetUtils.SubnetInfo; -@ValidatorComponent(type = StrategyValidator.NETWORK) public class NetworkValidator extends Validator { public static final String CIDR_REGEX = "^(\\d{1,3}\\.){3}\\d{1,3}(/(\\d|[1-2]\\d|3[0-2]))"; + + @Override + public StrategyValidator getType() { + return StrategyValidator.NETWORK; + } @Override public boolean process(final Strategy strategy, final Entry switcherInput) { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/NumericValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/NumericValidator.java index 8333c651..e4545c31 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/NumericValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/NumericValidator.java @@ -1,17 +1,20 @@ package com.github.switcherapi.client.service.validators; -import java.util.Arrays; - -import org.apache.commons.lang3.math.NumberUtils; - import com.github.switcherapi.client.exception.SwitcherInvalidNumericFormat; import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; +import org.apache.commons.lang3.math.NumberUtils; + +import java.util.Arrays; -@ValidatorComponent(type = StrategyValidator.NUMERIC) public class NumericValidator extends Validator { + + @Override + public StrategyValidator getType() { + return StrategyValidator.NUMERIC; + } @Override public boolean process(final Strategy strategy, final Entry switcherInput) { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/PayloadValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/PayloadValidator.java index 90f8b7a9..4d058ad4 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/PayloadValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/PayloadValidator.java @@ -1,17 +1,21 @@ package com.github.switcherapi.client.service.validators; -import java.util.Arrays; -import java.util.Set; - import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; import com.github.switcherapi.client.utils.SwitcherUtils; -@ValidatorComponent(type = StrategyValidator.PAYLOAD) +import java.util.Arrays; +import java.util.Set; + public class PayloadValidator extends Validator { + @Override + public StrategyValidator getType() { + return StrategyValidator.PAYLOAD; + } + @Override public boolean process(Strategy strategy, Entry switcherInput) { switch (strategy.getEntryOperation()) { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/RegexValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/RegexValidator.java index 481394a2..bd23963d 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/RegexValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/RegexValidator.java @@ -7,11 +7,15 @@ import java.util.Arrays; -@ValidatorComponent(type = StrategyValidator.REGEX) public class RegexValidator extends Validator { private static final String DELIMITER_REGEX = "\\b%s\\b"; + @Override + public StrategyValidator getType() { + return StrategyValidator.REGEX; + } + @Override public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherInvalidOperationException { switch (strategy.getEntryOperation()) { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java index 61381c2b..8cd141b4 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java @@ -13,11 +13,15 @@ import java.text.SimpleDateFormat; import java.util.Date; -@ValidatorComponent(type = StrategyValidator.TIME) public class TimeValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + @Override + public StrategyValidator getType() { + return StrategyValidator.TIME; + } + @Override public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherInvalidOperationException, SwitcherInvalidTimeFormat, SwitcherInvalidOperationInputException { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/Validator.java b/src/main/java/com/github/switcherapi/client/service/validators/Validator.java index 8e601493..6fc76f46 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/Validator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/Validator.java @@ -1,5 +1,6 @@ package com.github.switcherapi.client.service.validators; +import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.utils.SwitcherUtils; import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.model.Entry; @@ -24,4 +25,6 @@ public boolean execute(final Strategy strategy, final Entry switcherInput) public abstract boolean process(final Strategy strategy, final Entry switcherInput) throws SwitcherException; + public abstract StrategyValidator getType(); + } diff --git a/src/main/java/com/github/switcherapi/client/service/validators/ValidatorComponent.java b/src/main/java/com/github/switcherapi/client/service/validators/ValidatorComponent.java deleted file mode 100644 index 415d62bd..00000000 --- a/src/main/java/com/github/switcherapi/client/service/validators/ValidatorComponent.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.github.switcherapi.client.service.validators; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import com.github.switcherapi.client.model.StrategyValidator; - -@Target({ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -public @interface ValidatorComponent { - - StrategyValidator type(); - -} diff --git a/src/main/java/com/github/switcherapi/client/service/validators/ValueValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/ValueValidator.java index 4346e34c..0adf6020 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/ValueValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/ValueValidator.java @@ -1,15 +1,19 @@ package com.github.switcherapi.client.service.validators; -import java.util.Arrays; - import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; -@ValidatorComponent(type = StrategyValidator.VALUE) +import java.util.Arrays; + public class ValueValidator extends Validator { + @Override + public StrategyValidator getType() { + return StrategyValidator.VALUE; + } + @Override public boolean process(Strategy strategy, Entry switcherInput) { switch (strategy.getEntryOperation()) { diff --git a/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderTest.java b/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderTest.java index 6d656521..51148e4d 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderTest.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderTest.java @@ -59,6 +59,14 @@ void shouldReturnError_snapshotNotLoaded() { @Test void shouldThrowError_wrongContextKeyTypeUsage() { + //given + SwitchersBase.configure(ContextBuilder.builder(true) + .contextLocation(SwitchersBase.class.getCanonicalName()) + .domain("switcher-domain") + .local(true)); + + SwitchersBase.initializeClient(); + assertThrows(SwitcherContextException.class, () -> SwitchersBase.contextBol(ContextKey.DOMAIN)); assertThrows(SwitcherContextException.class, () -> SwitchersBase.contextStr(ContextKey.LOCAL_MODE)); } diff --git a/src/test/java/com/github/switcherapi/client/validator/CustomValidator.java b/src/test/java/com/github/switcherapi/client/validator/CustomValidator.java index edabd7f1..16bfe02d 100644 --- a/src/test/java/com/github/switcherapi/client/validator/CustomValidator.java +++ b/src/test/java/com/github/switcherapi/client/validator/CustomValidator.java @@ -5,11 +5,14 @@ import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; import com.github.switcherapi.client.service.validators.Validator; -import com.github.switcherapi.client.service.validators.ValidatorComponent; -@ValidatorComponent(type = StrategyValidator.INVALID) public class CustomValidator extends Validator { + @Override + public StrategyValidator getType() { + return StrategyValidator.INVALID; + } + @Override public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherException { diff --git a/src/test/java/com/github/switcherapi/client/validator/InvalidCustom2Validator.java b/src/test/java/com/github/switcherapi/client/validator/InvalidCustom2Validator.java deleted file mode 100644 index 1ead84c7..00000000 --- a/src/test/java/com/github/switcherapi/client/validator/InvalidCustom2Validator.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.github.switcherapi.client.validator; - -import com.github.switcherapi.client.exception.SwitcherException; -import com.github.switcherapi.client.model.Entry; -import com.github.switcherapi.client.model.criteria.Strategy; -import com.github.switcherapi.client.service.validators.Validator; - -public class InvalidCustom2Validator extends Validator { - - private InvalidCustom2Validator() {} - - @Override - public boolean process(Strategy strategy, Entry switcherInput) - throws SwitcherException { - return true; - } - -} \ No newline at end of file diff --git a/src/test/java/com/github/switcherapi/client/validator/InvalidCustomValidator.java b/src/test/java/com/github/switcherapi/client/validator/InvalidCustomValidator.java deleted file mode 100644 index 28f8f75c..00000000 --- a/src/test/java/com/github/switcherapi/client/validator/InvalidCustomValidator.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.switcherapi.client.validator; - -import com.github.switcherapi.client.exception.SwitcherException; -import com.github.switcherapi.client.model.Entry; -import com.github.switcherapi.client.model.StrategyValidator; -import com.github.switcherapi.client.model.criteria.Strategy; -import com.github.switcherapi.client.service.validators.Validator; -import com.github.switcherapi.client.service.validators.ValidatorComponent; - -@ValidatorComponent(type = StrategyValidator.INVALID) -public class InvalidCustomValidator extends Validator { - - private InvalidCustomValidator() {} - - @Override - public boolean process(Strategy strategy, Entry switcherInput) - throws SwitcherException { - return true; - } - -} \ No newline at end of file diff --git a/src/test/java/com/github/switcherapi/client/validator/ValidatorsTest.java b/src/test/java/com/github/switcherapi/client/validator/ValidatorsTest.java index 27b909e9..c22431ea 100644 --- a/src/test/java/com/github/switcherapi/client/validator/ValidatorsTest.java +++ b/src/test/java/com/github/switcherapi/client/validator/ValidatorsTest.java @@ -1,11 +1,11 @@ package com.github.switcherapi.client.validator; -import com.github.switcherapi.client.exception.SwitcherInvalidValidatorException; import com.github.switcherapi.client.model.criteria.Strategy; import com.github.switcherapi.client.service.ValidatorService; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; class ValidatorsTest { @@ -13,23 +13,11 @@ class ValidatorsTest { @Test void shouldRegisterCustomValidator() { - assertDoesNotThrow(() -> service.registerValidator(CustomValidator.class)); + assertDoesNotThrow(() -> service.registerValidator(new CustomValidator())); Strategy strategy = new Strategy(); strategy.setStrategy("CUSTOM"); assertTrue(service.execute(strategy, null)); } - - @Test - void shouldNotRegisterCustomValidator() { - assertThrows(Exception.class, - () -> service.registerValidator(InvalidCustomValidator.class)); - } - - @Test - void shouldNotRegisterNotAnnotatedCustomValidator() { - assertThrows(SwitcherInvalidValidatorException.class, - () -> service.registerValidator(InvalidCustom2Validator.class)); - } } From a6ae6dc3a69e545c4c205d56c8588e0b8539cc1a Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 17 Nov 2024 23:01:17 -0800 Subject: [PATCH 2/2] chore: replaced with primitive --- .../java/com/github/switcherapi/client/SwitcherProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java index 5f0f7bc2..cd06d803 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java @@ -61,7 +61,7 @@ public String getValue(ContextKey contextKey) { return getValue(contextKey, String.class); } - public Boolean getBoolean(ContextKey contextKey) { + public boolean getBoolean(ContextKey contextKey) { return getValue(contextKey, Boolean.class); }