Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/com/switcherapi/client/model/SwitcherBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,6 +15,8 @@
public abstract class SwitcherBuilder implements Switcher {

protected final SwitcherProperties properties;

protected final Map<List<Entry>, SwitcherResult> historyExecution;

protected long delay;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public final class SwitcherRequest extends SwitcherBuilder {
private final SwitcherExecutor switcherExecutor;

private final String switcherKey;

private final Map<List<Entry>, SwitcherResult> historyExecution;

private AsyncSwitcher asyncSwitcher;

Expand All @@ -41,7 +39,6 @@ public SwitcherRequest(final String switcherKey,
super(switcherProperties);
this.switcherExecutor = switcherExecutor;
this.switcherKey = switcherKey;
this.historyExecution = new HashMap<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));

Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherFail1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +44,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherFail2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,7 +41,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -41,7 +42,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,7 +53,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,7 +47,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +44,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down Expand Up @@ -105,7 +106,7 @@ void shouldValidateAndLoadSnapshot_whenLocal() {
.local(true)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
.environment(DEFAULT_ENV));

Switchers.initializeClient();

Expand All @@ -129,7 +130,7 @@ void shouldNotValidateAndLoadSnapshot_serviceUnavailable() {
.local(false)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
.environment(DEFAULT_ENV));

Switchers.initializeClient();

Expand Down
46 changes: 21 additions & 25 deletions src/test/java/com/switcherapi/client/SwitcherThrottleTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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);

Expand All @@ -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)
Expand All @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,7 +43,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading