From e7fcf758bfc564ef93cb88faff90dd6636d54fdf Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 13 Oct 2025 16:47:47 -0700 Subject: [PATCH] feat: changed flush to clean execution history too --- .../client/model/SwitcherBuilder.java | 10 ++-- .../client/model/SwitcherRequest.java | 3 -- .../SwitcherBasicCriteriaResponseTest.java | 3 +- .../switcherapi/client/SwitcherBasicTest.java | 3 +- .../client/SwitcherContextBuilderTest.java | 5 +- .../SwitcherContextRemoteExecutorTest.java | 5 +- .../switcherapi/client/SwitcherFail1Test.java | 3 +- .../switcherapi/client/SwitcherFail2Test.java | 3 +- .../client/SwitcherSilentModeTest.java | 3 +- .../client/SwitcherSnapshotLookupTest.java | 3 +- .../SwitcherSnapshotValidationFailTest.java | 3 +- .../SwitcherSnapshotValidationTest.java | 7 +-- .../client/SwitcherThrottleTest.java | 46 +++++++++---------- .../client/SwitcherValidateTest.java | 3 +- .../local/SwitcherLocalServiceTest.java | 3 +- .../utils/SnapshotWatcherWorkerTest.java | 4 +- .../client/utils/SwitcherUtilsTest.java | 3 +- 17 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java index 0e17344..70d325f 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java @@ -4,9 +4,7 @@ import com.switcherapi.client.exception.SwitcherContextException; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; /** * Builder class that simplifies how input are programmatically wrapped inside the Switcher. @@ -17,6 +15,8 @@ public abstract class SwitcherBuilder implements Switcher { protected final SwitcherProperties properties; + + protected final Map, SwitcherResult> historyExecution; protected long delay; @@ -32,16 +32,18 @@ public abstract class SwitcherBuilder implements Switcher { protected SwitcherBuilder(final SwitcherProperties properties) { this.properties = properties; + this.historyExecution = new HashMap<>(); this.entry = new ArrayList<>(); this.delay = 0; } /** - * Clear all entries previously added + * Clear all entries previously added and history of executions. * * @return {@link SwitcherBuilder} */ public SwitcherBuilder flush() { + this.historyExecution.clear(); this.entry.clear(); return this; } diff --git a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java index 8025488..b698970 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java @@ -23,8 +23,6 @@ public final class SwitcherRequest extends SwitcherBuilder { private final SwitcherExecutor switcherExecutor; private final String switcherKey; - - private final Map, SwitcherResult> historyExecution; private AsyncSwitcher asyncSwitcher; @@ -41,7 +39,6 @@ public SwitcherRequest(final String switcherKey, super(switcherProperties); this.switcherExecutor = switcherExecutor; this.switcherKey = switcherKey; - this.historyExecution = new HashMap<>(); } @Override diff --git a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java index 8e6e600..b71f862 100644 --- a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java @@ -15,6 +15,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherBasicCriteriaResponseTest extends MockWebServerHelper { @@ -31,7 +32,7 @@ static void setup() throws IOException { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherBasicTest.java b/src/test/java/com/switcherapi/client/SwitcherBasicTest.java index e73db94..ec6dba0 100644 --- a/src/test/java/com/switcherapi/client/SwitcherBasicTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherBasicTest.java @@ -11,6 +11,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -28,7 +29,7 @@ static void setup() throws IOException { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java b/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java index c7a46f1..dc5f2bb 100644 --- a/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java @@ -11,6 +11,7 @@ import static com.switcherapi.SwitchersBase.*; import static com.switcherapi.client.SwitcherContextValidator.ERR_LOCAL; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherContextBuilderTest { @@ -26,7 +27,7 @@ void shouldReturnSuccess() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client") - .environment("default") + .environment(DEFAULT_ENV) .snapshotLocation(SNAPSHOTS_LOCAL) .local(true)); @@ -46,7 +47,7 @@ void shouldReturnError_snapshotNotLoaded() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client") - .environment("default") + .environment(DEFAULT_ENV) .snapshotLocation(null) .local(true)); diff --git a/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java b/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java index 5b84301..66dfb78 100644 --- a/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java @@ -12,6 +12,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherContextRemoteExecutorTest extends MockWebServerHelper { @@ -41,7 +42,7 @@ void shouldConfigureRemotePoolSize() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client-pool-test") - .environment("default") + .environment(DEFAULT_ENV) .poolConnectionSize(1) .local(false)); @@ -72,7 +73,7 @@ void shouldConfigureRemoteTimeout() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client-timeout-test") - .environment("default") + .environment(DEFAULT_ENV) .timeoutMs(500) .local(false)); diff --git a/src/test/java/com/switcherapi/client/SwitcherFail1Test.java b/src/test/java/com/switcherapi/client/SwitcherFail1Test.java index 639b9a2..77e2a37 100644 --- a/src/test/java/com/switcherapi/client/SwitcherFail1Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherFail1Test.java @@ -14,6 +14,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherFail1Test extends MockWebServerHelper { @@ -43,7 +44,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherFail2Test.java b/src/test/java/com/switcherapi/client/SwitcherFail2Test.java index e85123a..f8cbe96 100644 --- a/src/test/java/com/switcherapi/client/SwitcherFail2Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherFail2Test.java @@ -13,6 +13,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -40,7 +41,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java b/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java index e1bd267..1b7d2a1 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertTrue; class SwitcherSilentModeTest extends MockWebServerHelper { @@ -41,7 +42,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java index 45aa5aa..07241d4 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java @@ -14,6 +14,7 @@ import java.nio.file.Files; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -52,7 +53,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java index 6fdd407..42e6b8c 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java @@ -12,6 +12,7 @@ import java.nio.file.Files; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -46,7 +47,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java index 10c9fd7..5900e94 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherSnapshotValidationTest extends MockWebServerHelper { @@ -43,7 +44,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); @@ -105,7 +106,7 @@ void shouldValidateAndLoadSnapshot_whenLocal() { .local(true) .snapshotAutoLoad(false) .snapshotLocation(RESOURCES_PATH) - .environment("default")); + .environment(DEFAULT_ENV)); Switchers.initializeClient(); @@ -129,7 +130,7 @@ void shouldNotValidateAndLoadSnapshot_serviceUnavailable() { .local(false) .snapshotAutoLoad(false) .snapshotLocation(RESOURCES_PATH) - .environment("default")); + .environment(DEFAULT_ENV)); Switchers.initializeClient(); diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java index f10ce77..25c2d73 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java @@ -1,27 +1,35 @@ package com.switcherapi.client; -import com.switcherapi.Switchers; +import com.switcherapi.SwitchersBase; import com.switcherapi.client.model.Switcher; import com.switcherapi.client.model.SwitcherBuilder; import com.switcherapi.fixture.CountDownHelper; import com.switcherapi.fixture.MockWebServerHelper; import mockwebserver3.QueueDispatcher; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class SwitcherThrottleTest extends MockWebServerHelper { @BeforeAll static void setup() throws IOException { MockWebServerHelper.setupMockServer(); - - Switchers.loadProperties(); - Switchers.configure(ContextBuilder.builder().url(String.format("http://localhost:%s", mockBackEnd.getPort()))); + + SwitchersBase.configure(ContextBuilder.builder(true) + .context(SwitchersBase.class.getName()) + .url(String.format("http://localhost:%s", mockBackEnd.getPort())) + .apiKey("TEST_API_KEY") + .domain("TEST_DOMAIN") + .component("TEST_COMPONENT") + .environment(DEFAULT_ENV)); } @AfterAll @@ -32,22 +40,11 @@ static void tearDown() { @BeforeEach void resetSwitcherContextState() { ((QueueDispatcher) mockBackEnd.getDispatcher()).clear(); - - Switchers.configure(ContextBuilder.builder() - .local(false) - .snapshotLocation(null) - .snapshotSkipValidation(false) - .environment("default") - .silentMode(null) - .snapshotAutoLoad(false) - .snapshotAutoUpdateInterval(null)); + SwitchersBase.initializeClient(); } @Test - @Order(1) void shouldReturnTrue_withThrottle() { - Switchers.initializeClient(); - // Initial remote call givenResponse(generateMockAuth(10)); //auth givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) @@ -57,8 +54,9 @@ void shouldReturnTrue_withThrottle() { givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) //test - Switcher switcher = Switchers - .getSwitcher(Switchers.REMOTE_KEY) + Switcher switcher = SwitchersBase + .getSwitcher(SwitchersBase.USECASE11) + .flush() .checkValue("value") .throttle(1000); @@ -71,10 +69,7 @@ void shouldReturnTrue_withThrottle() { } @Test - @Order(2) void shouldRetrieveNewResponse_whenStrategyInputChanged() { - Switchers.initializeClient(); - // Initial remote call givenResponse(generateMockAuth(10)); //auth givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) @@ -84,8 +79,9 @@ void shouldRetrieveNewResponse_whenStrategyInputChanged() { givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) //test - SwitcherBuilder switcher = Switchers - .getSwitcher(Switchers.REMOTE_KEY) + SwitcherBuilder switcher = SwitchersBase + .getSwitcher(SwitchersBase.USECASE11) + .flush() .throttle(1000); for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/switcherapi/client/SwitcherValidateTest.java b/src/test/java/com/switcherapi/client/SwitcherValidateTest.java index f227608..eac35c3 100644 --- a/src/test/java/com/switcherapi/client/SwitcherValidateTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherValidateTest.java @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.Set; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherValidateTest extends MockWebServerHelper { @@ -42,7 +43,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java b/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java index a6a55be..4fc8307 100644 --- a/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java +++ b/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java @@ -1,5 +1,6 @@ package com.switcherapi.client.service.local; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static com.switcherapi.client.remote.Constants.DEFAULT_TIMEOUT; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -39,7 +40,7 @@ static void init() { SwitchersBase.configure(ContextBuilder.builder() .context(SwitchersBase.class.getName()) .snapshotLocation(SNAPSHOTS_LOCAL) - .environment("default") + .environment(DEFAULT_ENV) .local(true)); SwitcherProperties properties = SwitchersBase.getSwitcherProperties(); diff --git a/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java b/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java index c17b40a..1d605ea 100644 --- a/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java +++ b/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; + class SnapshotWatcherWorkerTest extends SnapshotTest { @BeforeAll @@ -13,7 +15,7 @@ static void setupContext() { SwitchersBase.configure(ContextBuilder.builder(true) .context(SwitchersBase.class.getName()) .snapshotLocation(SNAPSHOTS_LOCAL) - .environment("default") + .environment(DEFAULT_ENV) .local(true)); SwitchersBase.initializeClient(); diff --git a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java index 3cf81bc..37717b5 100644 --- a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java +++ b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java @@ -22,6 +22,7 @@ import java.util.Properties; import java.util.stream.Stream; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherUtilsTest { @@ -116,7 +117,7 @@ void shouldReturnMillis(String time, long expectedValue) { */ static Stream envArguments() { return Stream.of( - Arguments.of("default", "default"), + Arguments.of(DEFAULT_ENV, DEFAULT_ENV), Arguments.of("${PORT:8080}", "8080"), Arguments.of("${SNAPSHOT_LOCAL:}", ""), Arguments.of("${ENVIRONMENT}", "staging")