diff --git a/README.md b/README.md
index 27ae8db4..2c11de3c 100644
--- a/README.md
+++ b/README.md
@@ -70,7 +70,6 @@ switcher.domain -> Domain name
#optional
switcher.environment -> Environment name
switcher.offline -> true/false When offline, it will only use a local snapshot file
-switcher.snapshot.file -> Snapshot file path
switcher.snapshot.location -> Folder from where snapshots will be saved/read
switcher.snapshot.auto -> true/false Automated lookup for snapshot when loading the application
switcher.snapshot.skipvalidation -> true/false Skip snapshotValidation() that can be used for UT executions
@@ -183,8 +182,8 @@ switcher.throttle(1000).isItOn();
```
## Offline settings
-You can also force the Switcher library to work offline. In this case, the snapshot location must be set up, so the context can be re-built using the offline configuration.
-Or you can just configure the Client SDK using the properties file.
+You can also set the Switcher library to work offline. It will use a local snapshot file to retrieve the switchers configuration.
+This feature is useful for testing purposes or when you need to run your application without internet access.
```java
MyAppFeatures.configure(ContextBuilder.builder()
@@ -199,9 +198,8 @@ switcher.isItOn();
## Real-time snapshot updater
-Let the Switcher Client manage your application local snapshot file.
-
-In order to minimize roundtrips and unnecessary file parsing, try to use one of these features to improve the overall performance when accessing snapshots locally.
+Let the Switcher Client manage your application local snapshot.
+These features allow you to configure the SDK to automatically update the snapshot in the background.
1. This feature will update the in-memory Snapshot every time the file is modified.
@@ -210,17 +208,16 @@ MyAppFeatures.watchSnapshot();
MyAppFeatures.stopWatchingSnapshot();
```
-2. You can also perform snapshot update validation to verify if there are changes to be pulled. This will ensure that your application is running the most recent version of your remote configuration.
+2. You can also perform snapshot update validation to verify if there are changes to be pulled.
```java
MyAppFeatures.validateSnapshot();
```
3. Enable the Client SDK to execute Snapshot Auto Updates in the background using configuration. It basically encapsulates the validateSnapshot feature into a scheduled task managed by the SDK.
-It requires to set either snapshotFile or snapshotLocation.
```java
-// It will check and update the local/in-memory snapshot to the latest version
+// It will check and update the local/in-memory snapshot to the latest version every second
MyAppFeatures.configure(ContextBuilder.builder()
.snapshotAutoUpdateInterval("1s")
.snapshotLocation("/src/resources"));
diff --git a/pom.xml b/pom.xml
index 40c7e6a5..8a64b40e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,7 +8,7 @@
com.github.switcherapi
switcher-client
jar
- 2.0.3
+ 2.0.4-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 8909c71a..7c91a23a 100644
--- a/src/main/java/com/github/switcherapi/client/ContextBuilder.java
+++ b/src/main/java/com/github/switcherapi/client/ContextBuilder.java
@@ -70,13 +70,12 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
return this;
}
- public ContextBuilder snapshotFile(String snapshotFile) {
- properties.setSnapshotFile(snapshotFile);
- return this;
- }
-
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
properties.setSnapshotAutoUpdateInterval(snapshotAutoUpdateInterval);
+
+ if (snapshotAutoUpdateInterval != null)
+ properties.setSnapshotAutoLoad(true);
+
return this;
}
@@ -102,6 +101,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
public ContextBuilder silentMode(boolean silentMode) {
properties.setSilentMode(silentMode);
+
+ if (silentMode)
+ properties.setSnapshotAutoLoad(true);
+
return this;
}
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java b/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java
index a7f81f0e..5803075a 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherContextValidator.java
@@ -1,12 +1,9 @@
package com.github.switcherapi.client;
-import java.io.File;
-
+import com.github.switcherapi.client.exception.SwitcherContextException;
import com.github.switcherapi.client.model.ContextKey;
import org.apache.commons.lang3.StringUtils;
-import com.github.switcherapi.client.exception.SwitcherContextException;
-
/**
* Helper class to validate SwitcherProperties parameters
*
@@ -15,12 +12,7 @@
*/
class SwitcherContextValidator {
- private static final String SNAPSHOT_PATH_PATTERN = "%s/%s.json";
public static final String ERR_FORMAT = "Invalid parameter format for [%s]. Expected %s.";
- public static final String ERR_LOCATION_SNAPSHOT_FILE = "Snapshot locations not defined [add: switcher.snapshot.location or switcher.snapshot.file]";
- public static final String ERR_SNAPSHOT_FILE = "Snapshot file not defined [add: switcher.snapshot.file]";
- public static final String ERR_SNAPSHOT_LOCATION = "Snapshot location not defined [add: switcher.snapshot.location]";
- public static final String ERR_SNAPSHOT_AUTO_UPDATE = "Snapshot Update Interval requires [switcher.url, switcher.snapshot.file/location]";
public static final String ERR_RETRY = "Retry not defined [add: switcher.retry]";
public static final String ERR_URL = "URL not defined [add: switcher.url]";
public static final String ERR_API = "API Key not defined [add: switcher.apikey]";
@@ -43,75 +35,21 @@ public static void validate(final SwitcherProperties prop) {
if (!prop.isOfflineMode()) {
validateOnline(prop);
- } else {
- validateOffline(prop);
}
validateOptionals(prop);
}
- /**
- * Validate Offline settings
- *
- * @param prop Configured properties
- */
- public static void validateOffline(final SwitcherProperties prop) {
- final StringBuilder error = new StringBuilder();
-
- // No Snapshot File may require a Snapshot Location
- if (StringUtils.isBlank(prop.getSnapshotFile())) {
- error.append(ERR_LOCATION_SNAPSHOT_FILE);
- } else {
- final File file = new File(prop.getSnapshotFile());
- if (!file.exists()) {
- throw new SwitcherContextException(ERR_SNAPSHOT_FILE);
- } else {
- return;
- }
- }
-
- // No Snapshot Location may require Snapshot File
- if (StringUtils.isBlank(prop.getSnapshotLocation())) {
- if (!StringUtils.isBlank(error.toString())) {
- throw new SwitcherContextException(error.toString());
- }
-
- // Snapshot Autoload requires a valid Snapshot Location
- } else if (!prop.isSnapshotAutoLoad()) {
- final File folderPath = new File(prop.getSnapshotLocation());
- if (!folderPath.exists()) {
- throw new SwitcherContextException(ERR_SNAPSHOT_LOCATION);
- } else {
- final File snapshotFile = new File(
- String.format(SNAPSHOT_PATH_PATTERN, prop.getSnapshotLocation(), prop.getEnvironment()));
-
- if (!snapshotFile.exists()) {
- throw new SwitcherContextException(ERR_SNAPSHOT_LOCATION);
- }
- }
- }
- }
-
/**
* Validate optional context arguments
*
* @param prop Configured properties
*/
public static void validateOptionals(final SwitcherProperties prop) {
- if (prop.isSnapshotAutoLoad() && StringUtils.isBlank(prop.getSnapshotLocation())) {
- throw new SwitcherContextException(ERR_SNAPSHOT_LOCATION);
- }
-
if (prop.isSilentMode() && StringUtils.isBlank(prop.getRetryAfter())) {
throw new SwitcherContextException(ERR_RETRY);
}
- if (!StringUtils.isBlank(prop.getSnapshotAutoUpdateInterval()) &&
- (StringUtils.isBlank(StringUtils.defaultIfEmpty(prop.getSnapshotLocation(), StringUtils.EMPTY) +
- StringUtils.defaultIfEmpty(prop.getSnapshotFile(), StringUtils.EMPTY)))) {
- throw new SwitcherContextException(ERR_SNAPSHOT_AUTO_UPDATE);
- }
-
try {
Integer.parseInt(prop.getRegexTimeout());
} catch (NumberFormatException e) {
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
index 6eb632b4..2b30b3dd 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
@@ -94,9 +94,11 @@ protected Domain initializeSnapshotFromAPI() {
try {
final Snapshot snapshot = ClientRemoteService.getInstance().resolveSnapshot();
- SnapshotLoader.saveSnapshot(snapshot,
- SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION),
- environment);
+ final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
+
+ if (snapshotLocation != null) {
+ SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
+ }
return snapshot.getDomain();
} catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) {
diff --git a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java
index bb030f8b..c64dad6f 100644
--- a/src/main/java/com/github/switcherapi/client/SwitcherProperties.java
+++ b/src/main/java/com/github/switcherapi/client/SwitcherProperties.java
@@ -35,8 +35,6 @@ class SwitcherProperties {
private String snapshotLocation;
- private String snapshotFile;
-
private String snapshotAutoUpdateInterval;
private String retryAfter;
@@ -67,7 +65,6 @@ public void loadFromProperties(Properties prop) {
setDomain(SwitcherUtils.resolveProperties(ContextKey.DOMAIN.getParam(), prop));
setComponent(SwitcherUtils.resolveProperties(ContextKey.COMPONENT.getParam(), prop));
setEnvironment(SwitcherUtils.resolveProperties(ContextKey.ENVIRONMENT.getParam(), prop));
- setSnapshotFile(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_FILE.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)));
@@ -148,14 +145,6 @@ public void setSnapshotLocation(String snapshotLocation) {
this.snapshotLocation = snapshotLocation;
}
- public String getSnapshotFile() {
- return snapshotFile;
- }
-
- public void setSnapshotFile(String snapshotFile) {
- this.snapshotFile = snapshotFile;
- }
-
public String getSnapshotAutoUpdateInterval() {
return snapshotAutoUpdateInterval;
}
diff --git a/src/main/java/com/github/switcherapi/client/model/ContextKey.java b/src/main/java/com/github/switcherapi/client/model/ContextKey.java
index 3046614b..7f2888c8 100644
--- a/src/main/java/com/github/switcherapi/client/model/ContextKey.java
+++ b/src/main/java/com/github/switcherapi/client/model/ContextKey.java
@@ -33,11 +33,6 @@ public enum ContextKey {
*/
ENVIRONMENT("switcher.environment", "environment"),
- /**
- * (String) The absolute path of the snapshot file, including the file's name.
- */
- SNAPSHOT_FILE("switcher.snapshot.file", "snapshotFile"),
-
/**
* (String) Folder path where all snapshot files are located.
*/
diff --git a/src/main/java/com/github/switcherapi/client/remote/ClientWS.java b/src/main/java/com/github/switcherapi/client/remote/ClientWS.java
index 771ed9e7..f9abdf66 100644
--- a/src/main/java/com/github/switcherapi/client/remote/ClientWS.java
+++ b/src/main/java/com/github/switcherapi/client/remote/ClientWS.java
@@ -15,7 +15,8 @@
* @since 2019-12-24
*/
public interface ClientWS {
-
+
+ Integer DEFAULT_TIMEOUT_SECONDS = 3;
String HEADER_AUTHORIZATION = "Authorization";
String HEADER_APIKEY = "switcher-api-key";
String TOKEN_TEXT = "Bearer %s";
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 cd088d48..da463da9 100644
--- a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java
+++ b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java
@@ -20,6 +20,7 @@
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
/**
* @author Roger Floriano (petruki)
@@ -40,7 +41,9 @@ public class ClientWSImpl implements ClientWS {
private final Client client;
public ClientWSImpl() {
- this.client = ClientWSBuilder.builder().build();
+ this.client = ClientWSBuilder.builder()
+ .connectTimeout(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
+ .build();
}
@Override
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 f941ff3e..f51c001c 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
@@ -39,13 +39,12 @@ public SwitcherLocalService() {
* @throws SwitcherSnapshotLoadException in case it was not possible to load snapshot automatically
*/
public void init() {
- final String snapshotFile = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_FILE);
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
final boolean snapshotAutoload = SwitcherContextBase.contextBol(ContextKey.SNAPSHOT_AUTO_LOAD);
- if (StringUtils.isNotBlank(snapshotFile)) {
- this.domain = SnapshotLoader.loadSnapshot(snapshotFile);
+ if (StringUtils.isBlank(snapshotLocation) && snapshotAutoload) {
+ this.domain = this.initializeSnapshotFromAPI();
} else if (StringUtils.isNotBlank(snapshotLocation)) {
try {
this.domain = SnapshotLoader.loadSnapshot(snapshotLocation, environment);
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherApiMockTest.java b/src/test/java/com/github/switcherapi/client/SwitcherApiMockTest.java
index b3670ea9..66a83dba 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherApiMockTest.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherApiMockTest.java
@@ -35,17 +35,18 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SwitcherApiMockTest {
-
- private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
+
+ private static final String RESOURCES_PATH = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
+ private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
private static MockWebServer mockBackEnd;
@BeforeAll
static void setup() throws IOException {
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/not_accessible"));
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/new_folder/generated_on_new_folder.json"));
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/new_folder"));
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/generated_mock_default.json"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/not_accessible"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/new_folder/generated_on_new_folder.json"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/new_folder"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/generated_mock_default.json"));
mockBackEnd = new MockWebServer();
mockBackEnd.start();
@@ -62,9 +63,9 @@ static void tearDown() throws IOException {
//clean generated outputs
SwitcherContext.stopWatchingSnapshot();
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/new_folder/generated_on_new_folder.json"));
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/new_folder"));
- Files.deleteIfExists(Paths.get(SNAPSHOTS_LOCAL + "/generated_mock_default.json"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/new_folder/generated_on_new_folder.json"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/new_folder"));
+ Files.deleteIfExists(Paths.get(RESOURCES_PATH + "/generated_mock_default.json"));
}
@BeforeEach
@@ -148,7 +149,7 @@ private MockResponse generateCheckSnapshotVersionResponse(String status) {
private MockResponse generateSnapshotResponse() {
final Snapshot mockedSnapshot = new Snapshot();
final Criteria criteria = new Criteria();
- criteria.setDomain(SnapshotLoader.loadSnapshot(SNAPSHOTS_LOCAL + "/default.json"));
+ criteria.setDomain(SnapshotLoader.loadSnapshot(RESOURCES_PATH + "/default.json"));
mockedSnapshot.setData(criteria);
Gson gson = new Gson();
@@ -272,7 +273,7 @@ void shouldReturnTrue_silentMode() throws InterruptedException {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotLocation(SNAPSHOTS_LOCAL)
- .environment("snapshot_fixture1")
+ .environment("fixture1")
.silentMode(true)
.retryAfter("5s"));
@@ -345,7 +346,7 @@ void shouldValidateAndUpdateSnapshot() {
givenResponse(generateSnapshotResponse());
//test
- Switchers.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL));
+ Switchers.configure(ContextBuilder.builder().snapshotLocation(RESOURCES_PATH));
assertDoesNotThrow(() -> {
Switchers.initializeClient();
@@ -370,7 +371,7 @@ void shouldLookupForSnapshot() {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotAutoLoad(true)
- .snapshotLocation(SNAPSHOTS_LOCAL + "/new_folder")
+ .snapshotLocation(RESOURCES_PATH + "/new_folder")
.environment("generated_on_new_folder"));
//auth
@@ -388,7 +389,7 @@ void shouldNotLookupForSnapshot_serviceUnavailable() {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotAutoLoad(true)
- .snapshotLocation(SNAPSHOTS_LOCAL + "/new_folder")
+ .snapshotLocation(RESOURCES_PATH + "/new_folder")
.environment("generated_on_new_folder"));
//auth
@@ -406,7 +407,7 @@ void shouldLookupForSnapshot_whenNotAutoLoad() {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotAutoLoad(false)
- .snapshotLocation(SNAPSHOTS_LOCAL)
+ .snapshotLocation(RESOURCES_PATH)
.environment("generated_mock_default"));
Switchers.initializeClient();
@@ -427,7 +428,7 @@ void shouldValidateAndLoadSnapshot_whenOffline() {
Switchers.configure(ContextBuilder.builder()
.offlineMode(true)
.snapshotAutoLoad(false)
- .snapshotLocation(SNAPSHOTS_LOCAL)
+ .snapshotLocation(RESOURCES_PATH)
.environment("default"));
Switchers.initializeClient();
@@ -454,7 +455,7 @@ void shouldNotValidateAndLoadSnapshot_serviceUnavailable() {
Switchers.configure(ContextBuilder.builder()
.offlineMode(true)
.snapshotAutoLoad(false)
- .snapshotLocation(SNAPSHOTS_LOCAL)
+ .snapshotLocation(RESOURCES_PATH)
.environment("default"));
Switchers.initializeClient();
@@ -475,12 +476,12 @@ void shouldNotLookupForSnapshot_invalidLocation() {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotAutoLoad(true)
- .snapshotLocation(SNAPSHOTS_LOCAL + "/not_accessible"));
+ .snapshotLocation(RESOURCES_PATH + "/not_accessible"));
//test
assertDoesNotThrow(() -> {
try (final RandomAccessFile raFile =
- new RandomAccessFile(SNAPSHOTS_LOCAL + "/not_accessible", "rw")) {
+ new RandomAccessFile(RESOURCES_PATH + "/not_accessible", "rw")) {
//given an inaccessible folder
raFile.getChannel().lock();
@@ -503,12 +504,12 @@ void shouldNotLookupForSnapshot_invalidFolderLocation() {
//given
Switchers.configure(ContextBuilder.builder()
.snapshotAutoLoad(true)
- .snapshotLocation(SNAPSHOTS_LOCAL + "/not_accessible/folder"));
+ .snapshotLocation(RESOURCES_PATH + "/not_accessible/folder"));
//test
assertDoesNotThrow(() -> {
try (final RandomAccessFile raFile =
- new RandomAccessFile(SNAPSHOTS_LOCAL + "/not_accessible", "rw")) {
+ new RandomAccessFile(RESOURCES_PATH + "/not_accessible", "rw")) {
//given an inaccessible folder
raFile.getChannel().lock();
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java b/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java
index bb0069ac..221cabad 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java
@@ -18,9 +18,9 @@
class SwitcherBypassTest {
- private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
- private static final String FIXTURE1 = "/snapshot_fixture1.json";
- private static final String FIXTURE2 = "/snapshot_fixture2.json";
+ private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
+ private static final String FIXTURE1 = "fixture1";
+ private static final String FIXTURE2 = "fixture2";
@BeforeAll
static void setupContext() {
@@ -36,7 +36,7 @@ static void resetMock() {
@Test
void shouldReturnFalse_afterAssumingItsFalse() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE1));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE1));
SwitcherContext.initializeClient();
//test
@@ -50,7 +50,7 @@ void shouldReturnFalse_afterAssumingItsFalse() {
@Test
void shouldReturnTrue_afterAssumingItsTrue() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE2));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE2));
SwitcherContext.initializeClient();
Switcher switcher = getSwitcher(USECASE111);
@@ -63,7 +63,7 @@ void shouldReturnTrue_afterAssumingItsTrue() {
@Test
void shouldReturnTrue_afterForgettingItWasFalse() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE1));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE1));
SwitcherContext.initializeClient();
//test
@@ -80,7 +80,7 @@ void shouldReturnTrue_afterForgettingItWasFalse() {
@Test
void shouldReturnFalse_afterAssumingItsTrue() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE2));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE2));
SwitcherContext.initializeClient();
//test
@@ -98,7 +98,7 @@ void shouldReturnFalse_afterAssumingItsTrue() {
@SwitcherMock(key = USECASE111, result = false)
void shouldReturnFalse_usingParametrizedTest() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE2));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE2));
SwitcherContext.initializeClient();
//test
@@ -110,7 +110,7 @@ void shouldReturnFalse_usingParametrizedTest() {
@SwitcherMock(key = USECASE111, result = true)
void shouldReturnTrue_usingParametrizedTest() {
//given
- SwitcherContext.configure(ContextBuilder.builder().snapshotFile(SNAPSHOTS_LOCAL + FIXTURE2));
+ SwitcherContext.configure(ContextBuilder.builder().snapshotLocation(SNAPSHOTS_LOCAL).environment(FIXTURE2));
SwitcherContext.initializeClient();
//test
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherContextTest.java b/src/test/java/com/github/switcherapi/client/SwitcherContextTest.java
index cbbe7805..35383c91 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherContextTest.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherContextTest.java
@@ -3,12 +3,9 @@
import com.github.switcherapi.Switchers;
import com.github.switcherapi.client.exception.SwitcherContextException;
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
-import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import java.nio.file.Paths;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -74,33 +71,6 @@ void shouldThrowError_noComponent() {
CONTEXT_ERROR, SwitcherContextValidator.ERR_COMPONENT), ex.getMessage());
}
- @Test
- void shouldThrowErrorWhenAutoLoad_noValidLocation() {
- Switchers.configure(ContextBuilder.builder()
- .snapshotLocation(Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/invalid")
- .offlineMode(true)
- .snapshotAutoLoad(false));
-
- Exception ex = assertThrows(SwitcherContextException.class,
- Switchers::initializeClient);
-
- assertEquals(String.format(
- CONTEXT_ERROR, SwitcherContextValidator.ERR_SNAPSHOT_LOCATION), ex.getMessage());
- }
-
- @Test
- void shouldThrowErrorWhenAutoLoad_noLocation() {
- Switchers.configure(ContextBuilder.builder()
- .snapshotLocation(null)
- .snapshotAutoLoad(true));
-
- Exception ex = assertThrows(SwitcherContextException.class,
- Switchers::initializeClient);
-
- assertEquals(String.format(
- CONTEXT_ERROR, SwitcherContextValidator.ERR_SNAPSHOT_LOCATION), ex.getMessage());
- }
-
@Test
void shouldThrowErrorWhenSilentMode_noRetryTimer() {
Switchers.configure(ContextBuilder.builder()
@@ -143,20 +113,4 @@ void shouldThrowError_invalidRegexTimeoutFormat() {
ex.getMessage());
}
- @Test
- void shouldThrowError_invalidSnapshotUpdateInterval() {
- Switchers.configure(ContextBuilder.builder()
- .snapshotAutoUpdateInterval("2s")
- .url("http://localhost")
- .snapshotLocation(null)
- .snapshotFile(null));
-
- Exception ex = assertThrows(SwitcherContextException.class,
- Switchers::initializeClient);
-
- assertEquals(String.format(
- CONTEXT_ERROR, SwitcherContextValidator.ERR_SNAPSHOT_AUTO_UPDATE),
- ex.getMessage());
- }
-
}
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix1Test.java b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix1Test.java
index 1ebdab0d..b3f8d1fa 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix1Test.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix1Test.java
@@ -24,7 +24,7 @@
class SwitcherOfflineFix1Test {
- private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
+ private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
private final static String PAYLOAD_FIXTURE = new Gson().toJson(Product.getFixture());
@@ -33,7 +33,7 @@ static void setupContext() {
SwitcherContext.loadProperties();
SwitcherContext.configure(ContextBuilder.builder()
.snapshotLocation(SNAPSHOTS_LOCAL)
- .environment("snapshot_fixture1")
+ .environment("fixture1")
.offlineMode(true));
SwitcherContext.initializeClient();
@@ -42,7 +42,7 @@ static void setupContext() {
@Test
void offlineShouldValidateContext() {
assertEquals(SNAPSHOTS_LOCAL, SwitcherContext.contextStr(ContextKey.SNAPSHOT_LOCATION));
- assertEquals("snapshot_fixture1", SwitcherContext.contextStr(ContextKey.ENVIRONMENT));
+ assertEquals("fixture1", SwitcherContext.contextStr(ContextKey.ENVIRONMENT));
assertTrue(SwitcherContext.contextBol(ContextKey.OFFLINE_MODE));
}
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix2Test.java b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix2Test.java
index fd7abeca..f69a6db1 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix2Test.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix2Test.java
@@ -13,14 +13,14 @@
class SwitcherOfflineFix2Test {
- private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
+ private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
@BeforeAll
static void setupContext() {
SwitcherContext.loadProperties();
SwitcherContext.configure(ContextBuilder.builder()
.snapshotLocation(SNAPSHOTS_LOCAL)
- .environment("snapshot_fixture2")
+ .environment("fixture2")
.offlineMode(true));
SwitcherContext.initializeClient();
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix3Test.java b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix3Test.java
index 7e671293..6c321196 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix3Test.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherOfflineFix3Test.java
@@ -29,14 +29,14 @@
class SwitcherOfflineFix3Test {
- private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
+ private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
@BeforeAll
static void setupContext() {
SwitcherContext.loadProperties();
SwitcherContext.configure(ContextBuilder.builder()
.snapshotLocation(SNAPSHOTS_LOCAL)
- .environment("snapshot_fixture3")
+ .environment("fixture3")
.offlineMode(true));
SwitcherContext.initializeClient();
diff --git a/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java b/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java
index fdcff713..0aae3b08 100644
--- a/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java
+++ b/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java
@@ -107,10 +107,10 @@ private MockResponse generateCheckSnapshotVersionResponse(String status) {
*
* @return Generated mock /graphql response based on src/test/resources/default.json
*/
- private MockResponse generateSnapshotResponse() {
+ private MockResponse generateSnapshotResponse(String fromFile) {
final Snapshot mockedSnapshot = new Snapshot();
final Criteria criteria = new Criteria();
- criteria.setDomain(SnapshotLoader.loadSnapshot(SNAPSHOTS_LOCAL + "/default.json"));
+ criteria.setDomain(SnapshotLoader.loadSnapshot(SNAPSHOTS_LOCAL + "/" + fromFile));
mockedSnapshot.setData(criteria);
Gson gson = new Gson();
@@ -131,7 +131,7 @@ private void givenSnapshotUpdateResponse(boolean isUpdated) {
givenResponse(generateMockAuth());
//graphql
- givenResponse(generateSnapshotResponse());
+ givenResponse(generateSnapshotResponse("default.json"));
}
}
@@ -211,4 +211,33 @@ void shouldNotUpdateSnapshot_whenNoUpdateAvailable() throws InterruptedException
assertEquals(1, Switchers.getSnapshotVersion());
}
+ @Test
+ @Order(4)
+ void shouldUpdateSnapshot_online_inMemory() throws InterruptedException {
+ //given
+ //load snapshot in-memory
+ givenResponse(generateMockAuth()); //auth
+ givenResponse(generateSnapshotResponse("default_outdated.json")); //graphql
+
+ //update snapshot
+ givenSnapshotUpdateResponse(false);
+
+ //that
+ Switchers.configure(ContextBuilder.builder()
+ .url(String.format("http://localhost:%s", mockBackEnd.getPort()))
+ .snapshotLocation(null)
+ .environment("generated_mock_default_5")
+ .offlineMode(true)
+ .snapshotAutoLoad(true)
+ .snapshotAutoUpdateInterval("1m"));
+
+ Switchers.initializeClient();
+ assertEquals(1, Switchers.getSnapshotVersion());
+
+ //test
+ CountDownLatch waiter = new CountDownLatch(1);
+ waiter.await(2, TimeUnit.SECONDS);
+ assertEquals(2, Switchers.getSnapshotVersion());
+ }
+
}
diff --git a/src/test/java/com/github/switcherapi/client/utils/SwitcherUtilsTest.java b/src/test/java/com/github/switcherapi/client/utils/SwitcherUtilsTest.java
index a2b4df5c..815ae70e 100644
--- a/src/test/java/com/github/switcherapi/client/utils/SwitcherUtilsTest.java
+++ b/src/test/java/com/github/switcherapi/client/utils/SwitcherUtilsTest.java
@@ -39,25 +39,6 @@ void reloadProperties() {
SwitcherContext.loadProperties();
}
- @Test
- void shouldReturnError_snapshotNotFound() {
- SwitcherContext.configure(ContextBuilder.builder()
- .snapshotFile(SNAPSHOTS_LOCAL + "/UNKWNOW_SNAPSHOT_FILE.json"));
-
- assertThrows(SwitcherSnapshotLoadException.class ,
- SwitcherContext::initializeClient);
- }
-
- @Test
- void shouldReturnError_offlineSnapshotNotFound() {
- SwitcherContext.configure(ContextBuilder.builder()
- .snapshotFile(SNAPSHOTS_LOCAL + "/UNKWNOW_SNAPSHOT_FILE.json")
- .offlineMode(true));
-
- assertThrows(SwitcherContextException.class,
- SwitcherContext::initializeClient);
- }
-
@Test
void shouldReturnOk_offlineLocationFound() {
SwitcherContext.configure(ContextBuilder.builder()
@@ -67,40 +48,6 @@ void shouldReturnOk_offlineLocationFound() {
assertDoesNotThrow(SwitcherContext::initializeClient);
}
- @Test
- void shouldReturnError_offlineFolderLocationNotFound() {
- SwitcherContext.configure(ContextBuilder.builder()
- .snapshotFile(null)
- .snapshotLocation(SNAPSHOTS_LOCAL + "/UNKNOWN_LOCATION")
- .offlineMode(true));
-
- assertThrows(SwitcherContextException.class,
- SwitcherContext::initializeClient);
- }
-
- @Test
- void shouldReturnError_offlineFileLocationNotFound() {
- SwitcherContext.configure(ContextBuilder.builder()
- .snapshotFile(null)
- .environment("UNKNOWN_ENVIRONMENT")
- .snapshotLocation(SNAPSHOTS_LOCAL)
- .offlineMode(true));
-
- assertThrows(SwitcherContextException.class,
- SwitcherContext::initializeClient);
- }
-
- @Test
- void shouldReturnError_offlineNoLocationAndFileSpecified() {
- SwitcherContext.configure(ContextBuilder.builder()
- .snapshotLocation(null)
- .snapshotFile(null)
- .offlineMode(true));
-
- assertThrows(SwitcherContextException.class,
- SwitcherContext::initializeClient);
- }
-
@Test
void shouldReturnError_snapshotHasErrors() {
SwitcherContext.configure(ContextBuilder.builder()
diff --git a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java
index d3610b48..f954d331 100644
--- a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java
+++ b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java
@@ -30,8 +30,11 @@ public static void test() {
Switcher switcher = getSwitcher(MY_SWITCHER);
switcher.setShowReason(true);
- scheduler.scheduleAtFixedRate(() ->
- logger.info(switcher.isItOn()), 0, 10, TimeUnit.SECONDS);
+ scheduler.scheduleAtFixedRate(() -> {
+ long time = System.currentTimeMillis();
+ logger.info("Switcher is on: " + switcher.isItOn());
+ logger.info("Time elapsed: " + (System.currentTimeMillis() - time));
+ }, 0, 10, TimeUnit.SECONDS);
}
public static void main(String[] args) {
diff --git a/src/test/resources/snapshot_fixture1.json b/src/test/resources/snapshot/fixture1.json
similarity index 100%
rename from src/test/resources/snapshot_fixture1.json
rename to src/test/resources/snapshot/fixture1.json
diff --git a/src/test/resources/snapshot_fixture2.json b/src/test/resources/snapshot/fixture2.json
similarity index 100%
rename from src/test/resources/snapshot_fixture2.json
rename to src/test/resources/snapshot/fixture2.json
diff --git a/src/test/resources/snapshot_fixture3.json b/src/test/resources/snapshot/fixture3.json
similarity index 100%
rename from src/test/resources/snapshot_fixture3.json
rename to src/test/resources/snapshot/fixture3.json