diff --git a/src/main/java/com/github/switcherapi/client/ContextBuilder.java b/src/main/java/com/github/switcherapi/client/ContextBuilder.java index ef80c413..cc573abd 100644 --- a/src/main/java/com/github/switcherapi/client/ContextBuilder.java +++ b/src/main/java/com/github/switcherapi/client/ContextBuilder.java @@ -5,17 +5,16 @@ import java.util.Optional; -import static com.github.switcherapi.client.SwitcherProperties.DEFAULT_TIMEOUT_MS; -import static com.github.switcherapi.client.remote.Constants.DEFAULT_POOL_SIZE; +import static com.github.switcherapi.client.remote.Constants.*; public class ContextBuilder { private static ContextBuilder context; - private SwitcherProperties properties; + private SwitcherProperties switcherProperties; - private ContextBuilder() { - properties = new SwitcherProperties(); + private ContextBuilder(SwitcherProperties switcherProperties) { + this.switcherProperties = switcherProperties; } public static void preConfigure(SwitcherProperties switcherProperties) { @@ -40,17 +39,17 @@ public static ContextBuilder builder() { */ public static ContextBuilder builder(boolean init) { if (context == null || init) - context = new ContextBuilder(); + context = new ContextBuilder(new SwitcherPropertiesImpl()); return context; } void preBuild(SwitcherProperties properties) { - this.properties = properties; + this.switcherProperties = properties; } - + SwitcherProperties build() { - return this.properties; + return this.switcherProperties; } /** @@ -58,7 +57,7 @@ SwitcherProperties build() { * @return ContextBuilder */ public ContextBuilder contextLocation(String contextLocation) { - properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation); + switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation); return this; } @@ -67,7 +66,7 @@ public ContextBuilder contextLocation(String contextLocation) { * @return ContextBuilder */ public ContextBuilder url(String url) { - properties.setValue(ContextKey.URL, url); + switcherProperties.setValue(ContextKey.URL, url); return this; } @@ -76,7 +75,7 @@ public ContextBuilder url(String url) { * @return ContextBuilder */ public ContextBuilder apiKey(String apiKey) { - properties.setValue(ContextKey.APIKEY, apiKey); + switcherProperties.setValue(ContextKey.APIKEY, apiKey); return this; } @@ -85,7 +84,7 @@ public ContextBuilder apiKey(String apiKey) { * @return ContextBuilder */ public ContextBuilder domain(String domain) { - properties.setValue(ContextKey.DOMAIN, domain); + switcherProperties.setValue(ContextKey.DOMAIN, domain); return this; } @@ -94,7 +93,7 @@ public ContextBuilder domain(String domain) { * @return ContextBuilder */ public ContextBuilder component(String component) { - properties.setValue(ContextKey.COMPONENT, component); + switcherProperties.setValue(ContextKey.COMPONENT, component); return this; } @@ -103,7 +102,8 @@ public ContextBuilder component(String component) { * @return ContextBuilder */ public ContextBuilder environment(String environment) { - properties.setValue(ContextKey.ENVIRONMENT, properties.getValueDefault(environment, SwitcherProperties.DEFAULT_ENV)); + switcherProperties.setValue(ContextKey.ENVIRONMENT, + Optional.ofNullable(environment).orElse(DEFAULT_ENV)); return this; } @@ -112,7 +112,7 @@ public ContextBuilder environment(String environment) { * @return ContextBuilder */ public ContextBuilder snapshotLocation(String snapshotLocation) { - properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation); + switcherProperties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation); return this; } @@ -121,22 +121,11 @@ public ContextBuilder snapshotLocation(String snapshotLocation) { * @return ContextBuilder */ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) { - properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval); + switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval); if (snapshotAutoUpdateInterval != null) - properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); - - return this; - } + switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); - /** - * Java 8 only - * - * @param regexTimeout Time in ms given to Timed Match Worker used for local Regex (ReDoS safety mechanism) - 3000 default value - * @return ContextBuilder - */ - public ContextBuilder regexTimeout(int regexTimeout) { - properties.setValue(ContextKey.REGEX_TIMEOUT, regexTimeout); return this; } @@ -145,7 +134,7 @@ public ContextBuilder regexTimeout(int regexTimeout) { * @return ContextBuilder */ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) { - properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad); + switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad); return this; } @@ -154,7 +143,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) { * @return ContextBuilder */ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) { - properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation); + switcherProperties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation); return this; } @@ -163,10 +152,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) { * @return ContextBuilder */ public ContextBuilder silentMode(String retryAfter) { - properties.setValue(ContextKey.SILENT_MODE, retryAfter); + switcherProperties.setValue(ContextKey.SILENT_MODE, retryAfter); if (StringUtils.isNotBlank(retryAfter)) { - properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); + switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true); } return this; @@ -177,7 +166,7 @@ public ContextBuilder silentMode(String retryAfter) { * @return ContextBuilder */ public ContextBuilder local(boolean local) { - properties.setValue(ContextKey.LOCAL_MODE, local); + switcherProperties.setValue(ContextKey.LOCAL_MODE, local); return this; } @@ -186,7 +175,7 @@ public ContextBuilder local(boolean local) { * @return ContextBuilder */ public ContextBuilder truststorePath(String truststorePath) { - properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath); + switcherProperties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath); return this; } @@ -195,7 +184,7 @@ public ContextBuilder truststorePath(String truststorePath) { * @return ContextBuilder */ public ContextBuilder truststorePassword(String truststorePassword) { - properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword); + switcherProperties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword); return this; } @@ -204,8 +193,8 @@ public ContextBuilder truststorePassword(String truststorePassword) { * @return ContextBuilder */ public ContextBuilder timeoutMs(Integer timeoutMs) { - properties.setValue(ContextKey.TIMEOUT_MS, - Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT_MS)); + switcherProperties.setValue(ContextKey.TIMEOUT_MS, + Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT)); return this; } @@ -214,7 +203,7 @@ public ContextBuilder timeoutMs(Integer timeoutMs) { * @return ContextBuilder */ public ContextBuilder poolConnectionSize(Integer poolSize) { - properties.setValue(ContextKey.POOL_CONNECTION_SIZE, + switcherProperties.setValue(ContextKey.POOL_CONNECTION_SIZE, Optional.ofNullable(poolSize).orElse(DEFAULT_POOL_SIZE)); 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 7e6eb36d..7bcfbc69 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java @@ -93,7 +93,7 @@ public abstract class SwitcherContextBase extends SwitcherConfig { private static SnapshotWatcher watcherSnapshot; static { - switcherProperties = new SwitcherProperties(); + switcherProperties = new SwitcherPropertiesImpl(); } @Override @@ -175,13 +175,13 @@ public static void initializeClient() { private static SwitcherExecutor buildInstance() { final ClientWS clientWS = initRemotePoolExecutorService(); final SwitcherValidator validatorService = new ValidatorService(); - final ClientRemote clientRemote = new ClientRemoteService(clientWS); + final ClientRemote clientRemote = new ClientRemoteService(clientWS, switcherProperties); final ClientLocal clientLocal = new ClientLocalService(validatorService); if (contextBol(ContextKey.LOCAL_MODE)) { - return new SwitcherLocalService(clientRemote, clientLocal); + return new SwitcherLocalService(clientRemote, clientLocal, switcherProperties); } else { - return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal)); + return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, switcherProperties)); } } @@ -309,7 +309,7 @@ private static ClientWS initRemotePoolExecutorService() { return thread; }); - return ClientWSImpl.build(remotePoolExecutorService, timeoutMs); + return ClientWSImpl.build(switcherProperties, remotePoolExecutorService, timeoutMs); } /** @@ -447,6 +447,15 @@ public static Integer contextInt(ContextKey contextKey) { public static boolean contextBol(ContextKey contextKey) { return switcherProperties.getBoolean(contextKey); } + + /** + * Retrieve the Switcher Properties + * + * @return SwitcherProperties instance + */ + public static SwitcherProperties getSwitcherProperties() { + return switcherProperties; + } /** * Fluent builder to configure the Switcher Context diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java index 8cd26553..1f10e2d5 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java @@ -32,7 +32,13 @@ public abstract class SwitcherExecutor { private static final Map bypass = new HashMap<>(); + protected final SwitcherProperties switcherProperties; + protected Domain domain; + + protected SwitcherExecutor(final SwitcherProperties switcherProperties) { + this.switcherProperties = switcherProperties; + } /** * Execute criteria based on the Switcher configuration @@ -69,7 +75,7 @@ public abstract class SwitcherExecutor { public abstract long getSnapshotVersion(); protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain domain) { - final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); + final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT); SwitcherUtils.debug(logger, "verifying snapshot version - environment: {}", environment); return clientRemote.checkSnapshotVersion(domain.getVersion()); @@ -77,11 +83,11 @@ protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain d protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) throws SwitcherRemoteException, SwitcherSnapshotWriteException { - final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); + final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT); SwitcherUtils.debug(logger, "initializing snapshot from API - environment: {}", environment); final Snapshot snapshot = clientRemote.resolveSnapshot(); - final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); + final String snapshotLocation = switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION); if (snapshotLocation != null) { SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment); @@ -91,7 +97,7 @@ protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) } public boolean isLocalEnabled() { - return SwitcherContextBase.contextBol(ContextKey.LOCAL_MODE); + return switcherProperties.getBoolean(ContextKey.LOCAL_MODE); } /** @@ -140,6 +146,10 @@ public static Map getBypass() { return bypass; } + public SwitcherProperties getSwitcherProperties() { + return switcherProperties; + } + public Domain getDomain() { return domain; } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java index 102f1c79..739f9547 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java @@ -1,108 +1,53 @@ package com.github.switcherapi.client; -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; -import static com.github.switcherapi.client.remote.Constants.DEFAULT_POOL_SIZE; - /** * The configuration definition object contains all necessary SDK properties to - * control the API client behaviors, access and snapshot location. - * + * control the API client state. + * * @author Roger Floriano (petruki) */ -public class SwitcherProperties { - - public static final String DEFAULT_ENV = "default"; - - public static final Integer DEFAULT_REGEX_TIMEOUT = 3000; - - public static final Integer DEFAULT_TIMEOUT_MS = 3000; - - private final Map properties = new HashMap<>(); - - public SwitcherProperties() { - setDefaults(); - } - - private void setDefaults() { - setValue(ContextKey.ENVIRONMENT, DEFAULT_ENV); - setValue(ContextKey.REGEX_TIMEOUT, DEFAULT_REGEX_TIMEOUT); - setValue(ContextKey.TIMEOUT_MS, DEFAULT_TIMEOUT_MS); - setValue(ContextKey.POOL_CONNECTION_SIZE, DEFAULT_POOL_SIZE); - setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false); - setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false); - setValue(ContextKey.LOCAL_MODE, false); - } - - 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, getValueDefault(SwitcherUtils.resolveProperties(ContextKey.ENVIRONMENT.getParam(), prop), DEFAULT_ENV)); - setValue(ContextKey.SNAPSHOT_LOCATION, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_LOCATION.getParam(), prop)); - setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_SKIP_VALIDATION.getParam(), prop), false)); - setValue(ContextKey.SNAPSHOT_AUTO_LOAD, getBoolDefault(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(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false)); - setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT)); - 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, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.TIMEOUT_MS.getParam(), prop), DEFAULT_TIMEOUT_MS)); - setValue(ContextKey.POOL_CONNECTION_SIZE, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.POOL_CONNECTION_SIZE.getParam(), prop), DEFAULT_POOL_SIZE)); - } - - public String getValue(ContextKey contextKey) { - return getValue(contextKey, String.class); - } - - public boolean getBoolean(ContextKey contextKey) { - return getValue(contextKey, Boolean.class); - } - - public Integer getInt(ContextKey contextKey) { - return getValue(contextKey, Integer.class); - } - - private T getValue(ContextKey contextKey, Class type) { - try { - return type.cast(properties.get(contextKey.getParam())); - } catch (ClassCastException e) { - throw new SwitcherContextException(e.getMessage()); - } - } - - public void setValue(ContextKey contextKey, Object value) { - properties.put(contextKey.getParam(), value); - } - - public String getValueDefault(String value, String defaultValue) { - return StringUtils.defaultIfBlank(value, defaultValue); - } - - public Integer getIntDefault(String value, Integer defaultValue) { - if (StringUtils.isNotBlank(value)) { - return Integer.parseInt(value); - } - - return defaultValue; - } - - public Boolean getBoolDefault(String value, Boolean defaultValue) { - if (StringUtils.isNotBlank(value)) { - return Boolean.parseBoolean(value); - } - - return defaultValue; - } - +public interface SwitcherProperties { + + /** + * Load properties into map + * + * @param prop The properties object + */ + void loadFromProperties(Properties prop); + + /** + * Get a value (string) from the properties map + * + * @param contextKey The context key + * @return The value + */ + String getValue(ContextKey contextKey); + + /** + * Get a value (integer) from the properties map + * + * @param contextKey The context key + * @return The value + */ + Integer getInt(ContextKey contextKey); + + /** + * Get a value (boolean) from the properties map + * + * @param contextKey The context key + * @return The value + */ + boolean getBoolean(ContextKey contextKey); + + /** + * Set a value into the properties map + * + * @param contextKey The context key + * @param value The value + */ + void setValue(ContextKey contextKey, Object value); } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherPropertiesImpl.java b/src/main/java/com/github/switcherapi/client/SwitcherPropertiesImpl.java new file mode 100644 index 00000000..1cbf40c2 --- /dev/null +++ b/src/main/java/com/github/switcherapi/client/SwitcherPropertiesImpl.java @@ -0,0 +1,97 @@ +package com.github.switcherapi.client; + +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; + +import static com.github.switcherapi.client.remote.Constants.*; + +public class SwitcherPropertiesImpl implements SwitcherProperties { + + private final Map properties = new HashMap<>(); + + public SwitcherPropertiesImpl() { + setValue(ContextKey.ENVIRONMENT, DEFAULT_ENV); + setValue(ContextKey.REGEX_TIMEOUT, DEFAULT_REGEX_TIMEOUT); + setValue(ContextKey.TIMEOUT_MS, DEFAULT_TIMEOUT); + setValue(ContextKey.POOL_CONNECTION_SIZE, DEFAULT_POOL_SIZE); + setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false); + setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false); + setValue(ContextKey.LOCAL_MODE, false); + } + + @Override + 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, getValueDefault(SwitcherUtils.resolveProperties(ContextKey.ENVIRONMENT.getParam(), prop), DEFAULT_ENV)); + setValue(ContextKey.SNAPSHOT_LOCATION, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_LOCATION.getParam(), prop)); + setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_SKIP_VALIDATION.getParam(), prop), false)); + setValue(ContextKey.SNAPSHOT_AUTO_LOAD, getBoolDefault(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(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false)); + setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT)); + 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, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.TIMEOUT_MS.getParam(), prop), DEFAULT_TIMEOUT)); + setValue(ContextKey.POOL_CONNECTION_SIZE, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.POOL_CONNECTION_SIZE.getParam(), prop), DEFAULT_POOL_SIZE)); + } + + @Override + public String getValue(ContextKey contextKey) { + return getValue(contextKey, String.class); + } + + @Override + public boolean getBoolean(ContextKey contextKey) { + return getValue(contextKey, Boolean.class); + } + + @Override + public Integer getInt(ContextKey contextKey) { + return getValue(contextKey, Integer.class); + } + + @Override + public void setValue(ContextKey contextKey, Object value) { + properties.put(contextKey.getParam(), value); + } + + private T getValue(ContextKey contextKey, Class type) { + try { + return type.cast(properties.get(contextKey.getParam())); + } catch (ClassCastException e) { + throw new SwitcherContextException(e.getMessage()); + } + } + + private String getValueDefault(String value, String defaultValue) { + return StringUtils.defaultIfBlank(value, defaultValue); + } + + private Integer getIntDefault(String value, Integer defaultValue) { + if (StringUtils.isNotBlank(value)) { + return Integer.parseInt(value); + } + + return defaultValue; + } + + private Boolean getBoolDefault(String value, Boolean defaultValue) { + if (StringUtils.isNotBlank(value)) { + return Boolean.parseBoolean(value); + } + + return defaultValue; + } + +} diff --git a/src/main/java/com/github/switcherapi/client/remote/ClientWSBuilder.java b/src/main/java/com/github/switcherapi/client/remote/ClientWSBuilder.java index fe1d488b..4467bebe 100644 --- a/src/main/java/com/github/switcherapi/client/remote/ClientWSBuilder.java +++ b/src/main/java/com/github/switcherapi/client/remote/ClientWSBuilder.java @@ -1,6 +1,6 @@ package com.github.switcherapi.client.remote; -import com.github.switcherapi.client.SwitcherContextBase; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.model.ContextKey; import org.apache.commons.lang3.StringUtils; @@ -23,18 +23,18 @@ private ClientWSBuilder() { throw new IllegalStateException("Utility class"); } - public static HttpClient.Builder builder(final ExecutorService executorService) { - if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.TRUSTSTORE_PATH))) { - return builderSSL(executorService); + public static HttpClient.Builder builder(final ExecutorService executorService, final SwitcherProperties switcherProperties) { + if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.TRUSTSTORE_PATH))) { + return builderSSL(executorService, switcherProperties); } return HttpClient.newBuilder().executor(executorService); } - private static HttpClient.Builder builderSSL(final ExecutorService executorService) { - try (InputStream readStream = new FileInputStream(SwitcherContextBase.contextStr(ContextKey.TRUSTSTORE_PATH))) { + private static HttpClient.Builder builderSSL(final ExecutorService executorService, final SwitcherProperties switcherProperties) { + try (InputStream readStream = new FileInputStream(switcherProperties.getValue(ContextKey.TRUSTSTORE_PATH))) { final KeyStore trustStore = KeyStore.getInstance(KEYSTORE_TYPE); - trustStore.load(readStream, SwitcherContextBase.contextStr(ContextKey.TRUSTSTORE_PASSWORD).toCharArray()); + trustStore.load(readStream, switcherProperties.getValue(ContextKey.TRUSTSTORE_PASSWORD).toCharArray()); final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(trustStore); diff --git a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java index 8dd1083c..78507ea4 100644 --- a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java +++ b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java @@ -1,6 +1,6 @@ package com.github.switcherapi.client.remote; -import com.github.switcherapi.client.SwitcherContextBase; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.exception.SwitcherRemoteException; import com.github.switcherapi.client.model.ContextKey; import com.github.switcherapi.client.model.Switcher; @@ -28,29 +28,32 @@ * @since 2019-12-24 */ public class ClientWSImpl implements ClientWS { - + + private final SwitcherProperties switcherProperties; + private final HttpClient client; private final int timeoutMs; private final Gson gson = new Gson(); - public ClientWSImpl(HttpClient client, int timeoutMs) { + public ClientWSImpl(SwitcherProperties switcherProperties, HttpClient client, int timeoutMs) { + this.switcherProperties = switcherProperties; this.timeoutMs = timeoutMs; this.client = client; } - public static ClientWS build(final ExecutorService executorService, int timeoutMs) { - final HttpClient httpClient = ClientWSBuilder.builder(executorService) + public static ClientWS build(SwitcherProperties switcherProperties, ExecutorService executorService, int timeoutMs) { + final HttpClient httpClient = ClientWSBuilder.builder(executorService, switcherProperties) .connectTimeout(Duration.ofMillis(timeoutMs)) .build(); - return new ClientWSImpl(httpClient, timeoutMs); + return new ClientWSImpl(switcherProperties, httpClient, timeoutMs); } @Override public CriteriaResponse executeCriteriaService(final Switcher switcher, final String token) { - final String url = SwitcherContextBase.contextStr(ContextKey.URL); + final String url = switcherProperties.getValue(ContextKey.URL); try { final URI uri = new URI(url) @@ -83,20 +86,20 @@ public CriteriaResponse executeCriteriaService(final Switcher switcher, final St @Override public Optional auth() { final AuthRequest authRequest = new AuthRequest(); - authRequest.setDomain(SwitcherContextBase.contextStr(ContextKey.DOMAIN)); - authRequest.setComponent(SwitcherContextBase.contextStr(ContextKey.COMPONENT)); - authRequest.setEnvironment(SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT)); + authRequest.setDomain(switcherProperties.getValue(ContextKey.DOMAIN)); + authRequest.setComponent(switcherProperties.getValue(ContextKey.COMPONENT)); + authRequest.setEnvironment(switcherProperties.getValue(ContextKey.ENVIRONMENT)); - final String url = SwitcherContextBase.contextStr(ContextKey.URL); + final String url = switcherProperties.getValue(ContextKey.URL); try { final HttpResponse response = client.send(HttpRequest.newBuilder() .uri(URI.create(String.format(AUTH_URL, url))) - .headers(HEADER_APIKEY, SwitcherContextBase.contextStr(ContextKey.APIKEY), + .headers(HEADER_APIKEY, switcherProperties.getValue(ContextKey.APIKEY), CONTENT_TYPE[0], CONTENT_TYPE[1]) .timeout(Duration.ofMillis(timeoutMs)) - .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(authRequest))) - .build(), HttpResponse.BodyHandlers.ofString()); + .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(authRequest)) + ).build(), HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) { throw new SwitcherRemoteException(url, response.statusCode()); @@ -110,7 +113,7 @@ public Optional auth() { @Override public Snapshot resolveSnapshot(final String token) { - final String url = SwitcherContextBase.contextStr(ContextKey.URL); + final String url = switcherProperties.getValue(ContextKey.URL); try { final HttpResponse response = client.send(HttpRequest.newBuilder() @@ -119,9 +122,9 @@ public Snapshot resolveSnapshot(final String token) { CONTENT_TYPE[0], CONTENT_TYPE[1]) .timeout(Duration.ofMillis(timeoutMs)) .POST(HttpRequest.BodyPublishers.ofString(String.format(QUERY, - SwitcherContextBase.contextStr(ContextKey.DOMAIN), - SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT), - SwitcherContextBase.contextStr(ContextKey.COMPONENT))) + switcherProperties.getValue(ContextKey.DOMAIN), + switcherProperties.getValue(ContextKey.ENVIRONMENT), + switcherProperties.getValue(ContextKey.COMPONENT))) ).build(), HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) { @@ -136,7 +139,7 @@ public Snapshot resolveSnapshot(final String token) { @Override public SnapshotVersionResponse checkSnapshotVersion(final long version, final String token) { - final String url = SwitcherContextBase.contextStr(ContextKey.URL); + final String url = switcherProperties.getValue(ContextKey.URL); try { final HttpResponse response = client.send(HttpRequest.newBuilder() @@ -160,7 +163,7 @@ public SnapshotVersionResponse checkSnapshotVersion(final long version, final St public boolean isAlive() { try { HttpResponse response = client.send(HttpRequest.newBuilder() - .uri(URI.create(String.format(CHECK_URL, SwitcherContextBase.contextStr(ContextKey.URL)))) + .uri(URI.create(String.format(CHECK_URL, switcherProperties.getValue(ContextKey.URL)))) .timeout(Duration.ofMillis(timeoutMs)) .GET().build(), HttpResponse.BodyHandlers.ofString()); @@ -175,7 +178,7 @@ public boolean isAlive() { @Override public SwitchersCheck checkSwitchers(Set switchers, final String token) { - final String url = SwitcherContextBase.contextStr(ContextKey.URL); + final String url = switcherProperties.getValue(ContextKey.URL); try { final HttpResponse response = client.send(HttpRequest.newBuilder() diff --git a/src/main/java/com/github/switcherapi/client/remote/Constants.java b/src/main/java/com/github/switcherapi/client/remote/Constants.java index 676a5956..df0a3f4f 100644 --- a/src/main/java/com/github/switcherapi/client/remote/Constants.java +++ b/src/main/java/com/github/switcherapi/client/remote/Constants.java @@ -2,6 +2,8 @@ public final class Constants { + public static final String DEFAULT_ENV = "default"; + public static final Integer DEFAULT_REGEX_TIMEOUT = 3000; public static final int DEFAULT_TIMEOUT = 3000; public static final int DEFAULT_POOL_SIZE = 2; public static final String HEADER_AUTHORIZATION = "Authorization"; diff --git a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java index bdbf4f56..8e98a52c 100644 --- a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java +++ b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java @@ -1,7 +1,7 @@ package com.github.switcherapi.client.service.local; -import com.github.switcherapi.client.SwitcherContextBase; import com.github.switcherapi.client.SwitcherExecutor; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.exception.*; import com.github.switcherapi.client.model.ContextKey; import com.github.switcherapi.client.model.Switcher; @@ -30,7 +30,8 @@ public class SwitcherLocalService extends SwitcherExecutor { private final ClientLocal clientLocal; - public SwitcherLocalService(ClientRemote clientRemote, ClientLocal clientLocal) { + public SwitcherLocalService(ClientRemote clientRemote, ClientLocal clientLocal, SwitcherProperties switcherProperties) { + super(switcherProperties); this.clientRemote = clientRemote; this.clientLocal = clientLocal; this.init(); @@ -42,9 +43,9 @@ public SwitcherLocalService(ClientRemote clientRemote, ClientLocal clientLocal) * @throws SwitcherSnapshotLoadException in case it was not possible to load snapshot automatically */ public void init() { - final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); - final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); - final boolean snapshotAutoload = SwitcherContextBase.contextBol(ContextKey.SNAPSHOT_AUTO_LOAD); + final String snapshotLocation = switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION); + final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT); + final boolean snapshotAutoload = switcherProperties.getBoolean(ContextKey.SNAPSHOT_AUTO_LOAD); if (StringUtils.isBlank(snapshotLocation) && snapshotAutoload) { this.domain = this.initializeSnapshotFromAPI(this.clientRemote); @@ -68,8 +69,8 @@ public void init() { * @return true if valid change */ public boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler) { - final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); - final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); + final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT); + final String snapshotLocation = switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION); try { if (snapshotFile.equals(String.format("%s.json", environment))) { diff --git a/src/main/java/com/github/switcherapi/client/service/remote/ClientRemoteService.java b/src/main/java/com/github/switcherapi/client/service/remote/ClientRemoteService.java index 6b88d723..a9899887 100644 --- a/src/main/java/com/github/switcherapi/client/service/remote/ClientRemoteService.java +++ b/src/main/java/com/github/switcherapi/client/service/remote/ClientRemoteService.java @@ -1,6 +1,6 @@ package com.github.switcherapi.client.service.remote; -import com.github.switcherapi.client.SwitcherContextBase; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException; import com.github.switcherapi.client.exception.SwitcherRemoteException; @@ -24,6 +24,8 @@ * @since 2019-12-24 */ public class ClientRemoteService implements ClientRemote { + + private final SwitcherProperties switcherProperties; private final ClientWS clientWs; @@ -33,8 +35,9 @@ private enum TokenStatus { VALID, INVALID, SILENT } - public ClientRemoteService(ClientWS clientWs) { + public ClientRemoteService(ClientWS clientWs, SwitcherProperties switcherProperties) { this.clientWs = clientWs; + this.switcherProperties = switcherProperties; } @Override @@ -97,7 +100,7 @@ private void auth(TokenStatus tokenStatus) { } if (tokenStatus == TokenStatus.SILENT) { - throw new SwitcherRemoteException(SwitcherContextBase.contextStr(ContextKey.URL)); + throw new SwitcherRemoteException(switcherProperties.getValue(ContextKey.URL)); } } @@ -120,8 +123,8 @@ private TokenStatus isTokenValid() throws SwitcherRemoteException, } private void setSilentModeExpiration() throws SwitcherInvalidDateTimeArgumentException { - if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) { - final String addValue = SwitcherContextBase.contextStr(ContextKey.SILENT_MODE); + if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.SILENT_MODE))) { + final String addValue = switcherProperties.getValue(ContextKey.SILENT_MODE); final AuthResponse response = new AuthResponse(); response.setToken(ContextKey.SILENT_MODE.getParam()); diff --git a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java index 685a0cf5..a4282281 100644 --- a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java +++ b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java @@ -1,6 +1,5 @@ package com.github.switcherapi.client.service.remote; -import com.github.switcherapi.client.SwitcherContextBase; import com.github.switcherapi.client.SwitcherExecutor; import com.github.switcherapi.client.exception.SwitcherRemoteException; import com.github.switcherapi.client.exception.SwitchersValidationException; @@ -29,6 +28,7 @@ public class SwitcherRemoteService extends SwitcherExecutor { private final ClientRemote clientRemote; public SwitcherRemoteService(ClientRemote clientRemote, SwitcherExecutor switcherExecutor) { + super(switcherExecutor.getSwitcherProperties()); this.clientRemote = clientRemote; this.switcherLocal = switcherExecutor; } @@ -50,7 +50,7 @@ public CriteriaResponse executeCriteria(final Switcher switcher) { private CriteriaResponse tryExecuteLocalCriteria(final Switcher switcher, final SwitcherRemoteException e) { - if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) { + if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.SILENT_MODE))) { final CriteriaResponse response = this.switcherLocal.executeCriteria(switcher); SwitcherUtils.debug(logger, "[Silent] response: {}", response); @@ -69,7 +69,7 @@ private CriteriaResponse tryExecuteLocalCriteria(final Switcher switcher, @Override public boolean checkSnapshotVersion() { - if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION)) + if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION)) && this.switcherLocal.getDomain() != null) { return super.checkSnapshotVersion(this.clientRemote, this.switcherLocal.getDomain()); } diff --git a/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderDefaultsTest.java b/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderDefaultsTest.java index 0d15c515..c499aae5 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderDefaultsTest.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherContextBuilderDefaultsTest.java @@ -4,6 +4,7 @@ import com.github.switcherapi.client.model.ContextKey; import org.junit.jupiter.api.Test; +import static com.github.switcherapi.client.remote.Constants.*; import static org.junit.jupiter.api.Assertions.assertEquals; class SwitcherContextBuilderDefaultsTest { @@ -14,7 +15,7 @@ void shouldLoadDefault_environment() { SwitchersBase.configure(ContextBuilder.builder(true).environment(null)); //test - assertEquals(SwitcherProperties.DEFAULT_ENV, SwitchersBase.contextStr(ContextKey.ENVIRONMENT)); + assertEquals(DEFAULT_ENV, SwitchersBase.contextStr(ContextKey.ENVIRONMENT)); } @Test @@ -23,9 +24,9 @@ void shouldLoadDefaults() { SwitchersBase.configure(ContextBuilder.builder(true)); //test - assertEquals(SwitcherProperties.DEFAULT_REGEX_TIMEOUT, SwitchersBase.contextInt(ContextKey.REGEX_TIMEOUT)); - assertEquals(SwitcherProperties.DEFAULT_TIMEOUT_MS, SwitchersBase.contextInt(ContextKey.TIMEOUT_MS)); - assertEquals(SwitcherProperties.DEFAULT_ENV, SwitchersBase.contextStr(ContextKey.ENVIRONMENT)); + assertEquals(DEFAULT_REGEX_TIMEOUT, SwitchersBase.contextInt(ContextKey.REGEX_TIMEOUT)); + assertEquals(DEFAULT_TIMEOUT, SwitchersBase.contextInt(ContextKey.TIMEOUT_MS)); + assertEquals(DEFAULT_ENV, SwitchersBase.contextStr(ContextKey.ENVIRONMENT)); } } diff --git a/src/test/java/com/github/switcherapi/client/SwitcherLocal3Test.java b/src/test/java/com/github/switcherapi/client/SwitcherLocal3Test.java index b08d9595..2b62ad6b 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherLocal3Test.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherLocal3Test.java @@ -92,9 +92,12 @@ void localShouldCheckSwitchers() { switchers.add(Switchers.USECASE17); switchers.add(Switchers.USECASE16); - ClientWS clientWS = ClientWSImpl.build(executorService, DEFAULT_TIMEOUT); + SwitcherProperties switcherProperties = SwitcherContext.getSwitcherProperties(); + ClientWS clientWS = ClientWSImpl.build(switcherProperties, executorService, DEFAULT_TIMEOUT); SwitcherValidator validatorService = new ValidatorService(); - SwitcherLocalService switcherLocal = new SwitcherLocalService(new ClientRemoteService(clientWS), new ClientLocalService(validatorService)); + SwitcherLocalService switcherLocal = new SwitcherLocalService( + new ClientRemoteService(clientWS, switcherProperties), + new ClientLocalService(validatorService), switcherProperties); switcherLocal.init(); //test @@ -107,9 +110,12 @@ void localShouldCheckSwitchers_notFound() { Set notFound = new HashSet<>(); notFound.add("NOT_FOUND_1"); - ClientWS clientWS = ClientWSImpl.build(executorService, DEFAULT_TIMEOUT); + SwitcherProperties switcherProperties = SwitcherContext.getSwitcherProperties(); + ClientWS clientWS = ClientWSImpl.build(switcherProperties, executorService, DEFAULT_TIMEOUT); SwitcherValidator validatorService = new ValidatorService(); - SwitcherLocalService switcherLocal = new SwitcherLocalService(new ClientRemoteService(clientWS), new ClientLocalService(validatorService)); + SwitcherLocalService switcherLocal = new SwitcherLocalService( + new ClientRemoteService(clientWS, switcherProperties), + new ClientLocalService(validatorService), switcherProperties); switcherLocal.init(); //test diff --git a/src/test/java/com/github/switcherapi/client/remote/ClientRemoteTest.java b/src/test/java/com/github/switcherapi/client/remote/ClientRemoteTest.java index 6dd02391..7114428e 100644 --- a/src/test/java/com/github/switcherapi/client/remote/ClientRemoteTest.java +++ b/src/test/java/com/github/switcherapi/client/remote/ClientRemoteTest.java @@ -2,6 +2,7 @@ import com.github.switcherapi.Switchers; import com.github.switcherapi.client.ContextBuilder; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.model.Switcher; import com.github.switcherapi.client.model.criteria.SwitchersCheck; import com.github.switcherapi.client.model.response.CriteriaResponse; @@ -54,7 +55,8 @@ static void tearDown() throws IOException { @BeforeEach void resetSwitcherContextState() { - clientRemote = new ClientRemoteService(ClientWSImpl.build(executorService, DEFAULT_TIMEOUT)); + SwitcherProperties switcherProperties = Switchers.getSwitcherProperties(); + clientRemote = new ClientRemoteService(ClientWSImpl.build(switcherProperties, executorService, DEFAULT_TIMEOUT), switcherProperties); ((QueueDispatcher) mockBackEnd.getDispatcher()).clear(); Switchers.configure(ContextBuilder.builder()); @@ -69,7 +71,7 @@ void shouldExecuteCriteria() { SwitcherValidator validatorService = new ValidatorService(); ClientLocal clientLocal = new ClientLocalService(validatorService); - Switcher switcher = new Switcher("KEY", new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal))); + Switcher switcher = new Switcher("KEY", new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, Switchers.getSwitcherProperties()))); //test CriteriaResponse actual = clientRemote.executeCriteria(switcher); diff --git a/src/test/java/com/github/switcherapi/client/remote/ClientWSBuilderTest.java b/src/test/java/com/github/switcherapi/client/remote/ClientWSBuilderTest.java index bb8bdb81..0e2a97c8 100644 --- a/src/test/java/com/github/switcherapi/client/remote/ClientWSBuilderTest.java +++ b/src/test/java/com/github/switcherapi/client/remote/ClientWSBuilderTest.java @@ -2,6 +2,7 @@ import com.github.switcherapi.client.ContextBuilder; import com.github.switcherapi.client.SwitcherContextBase; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.exception.SwitcherException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -36,8 +37,10 @@ void shouldCreateClientBuilder() { .truststorePath("") .truststorePassword("")); + SwitcherProperties properties = SwitcherContextBase.getSwitcherProperties(); + // test - HttpClient.Builder clientBuilder = ClientWSBuilder.builder(executorService); + HttpClient.Builder clientBuilder = ClientWSBuilder.builder(executorService, properties); assertNotNull(clientBuilder); SSLContext sslContext = clientBuilder.build().sslContext(); @@ -55,8 +58,10 @@ void shouldCreateClientBuilderSSL() { .truststorePath(truststorePath) .truststorePassword("changeit")); + SwitcherProperties properties = SwitcherContextBase.getSwitcherProperties(); + // test - HttpClient.Builder clientBuilder = ClientWSBuilder.builder(executorService); + HttpClient.Builder clientBuilder = ClientWSBuilder.builder(executorService, properties); assertNotNull(clientBuilder); SSLContext sslContext = clientBuilder.build().sslContext(); @@ -74,8 +79,10 @@ void shouldNotCreateClientBuilderSSL_invalidKeystorePassword() { .truststorePath(truststorePath) .truststorePassword("INVALID")); + SwitcherProperties properties = SwitcherContextBase.getSwitcherProperties(); + // test - assertThrows(SwitcherException.class, () -> ClientWSBuilder.builder(executorService)); + assertThrows(SwitcherException.class, () -> ClientWSBuilder.builder(executorService, properties)); } @Test @@ -85,7 +92,9 @@ void shouldNotCreateClientBuilderSSL_invalidKeystorePath() { .truststorePath("INVALID") .truststorePassword("changeit")); + SwitcherProperties properties = SwitcherContextBase.getSwitcherProperties(); + // test - assertThrows(SwitcherException.class, () -> ClientWSBuilder.builder(executorService)); + assertThrows(SwitcherException.class, () -> ClientWSBuilder.builder(executorService, properties)); } } diff --git a/src/test/java/com/github/switcherapi/client/remote/ClientWSTest.java b/src/test/java/com/github/switcherapi/client/remote/ClientWSTest.java index 249c1d22..853b2a61 100644 --- a/src/test/java/com/github/switcherapi/client/remote/ClientWSTest.java +++ b/src/test/java/com/github/switcherapi/client/remote/ClientWSTest.java @@ -48,7 +48,7 @@ void resetSwitcherContextState() { @Test void shouldBeAlive() { // given - ClientWS clientWS = ClientWSImpl.build(executorService, DEFAULT_TIMEOUT); + ClientWS clientWS = ClientWSImpl.build(Switchers.getSwitcherProperties(), executorService, DEFAULT_TIMEOUT); givenResponse(generateTimeOut(100)); // test @@ -59,7 +59,7 @@ void shouldBeAlive() { @Test void shouldTimeOut() { // given - ClientWS clientWS = ClientWSImpl.build(executorService, DEFAULT_TIMEOUT); + ClientWS clientWS = ClientWSImpl.build(Switchers.getSwitcherProperties(), executorService, DEFAULT_TIMEOUT); givenResponse(generateTimeOut(3500)); // test @@ -69,7 +69,7 @@ void shouldTimeOut() { @Test void shouldExtendTimeOut() { - ClientWS clientWS = ClientWSImpl.build(executorService, 4000); + ClientWS clientWS = ClientWSImpl.build(Switchers.getSwitcherProperties(), executorService, 4000); givenResponse(generateTimeOut(3500)); // test diff --git a/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java index c5019a45..89ec83a9 100644 --- a/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java +++ b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; +import com.github.switcherapi.client.SwitcherProperties; import com.github.switcherapi.client.remote.ClientWS; import com.github.switcherapi.client.remote.ClientWSImpl; import com.github.switcherapi.client.service.SwitcherValidator; @@ -41,9 +42,12 @@ static void init() { .environment("default") .local(true)); - ClientWS clientWS = ClientWSImpl.build(executorService, DEFAULT_TIMEOUT); + SwitcherProperties properties = SwitchersBase.getSwitcherProperties(); + ClientWS clientWS = ClientWSImpl.build(properties, executorService, DEFAULT_TIMEOUT); SwitcherValidator validatorService = new ValidatorService(); - service = new SwitcherLocalService(new ClientRemoteService(clientWS), new ClientLocalService(validatorService)); + service = new SwitcherLocalService( + new ClientRemoteService(clientWS, properties), + new ClientLocalService(validatorService), properties); } @AfterAll