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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.switcherapi</groupId>
<artifactId>switcher-client</artifactId>
<packaging>jar</packaging>
<version>2.5.1</version>
<version>2.5.2-SNAPSHOT</version>

<name>Switcher Client</name>
<description>Switcher Client SDK for working with Switcher API</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public static SwitcherRequest getSwitcher(String key, boolean keepEntries) {

final SwitcherRequest switcher = switchers.get(key);
if (!keepEntries) {
switcher.resetEntry();
switcher.flush();
}

return switcher;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/switcherapi/client/model/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String toString() {
return String.format("Entry [strategy = %s, input = %s]",
strategy, input);
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -69,7 +69,7 @@ public boolean equals(Object obj) {

return this.input.equals(entry.getInput());
}
return true;
return false;
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/switcherapi/client/model/SwitcherBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ protected SwitcherBuilder(final SwitcherProperties properties) {
this.entry = new ArrayList<>();
this.delay = 0;
}

/**
* Clear all entries previously added
*
* @return {@link SwitcherBuilder}
*/
public SwitcherBuilder flush() {
this.entry.clear();
return this;
}

/**
* Skip API calls given a delay time
Expand Down Expand Up @@ -95,6 +105,7 @@ public SwitcherBuilder restrictRelay(boolean restrictRelay) {
*/
public SwitcherBuilder check(StrategyValidator strategy, String input) {
if (StringUtils.isNotBlank(input)) {
entry.removeIf(e -> e.getStrategy().equals(strategy.toString()));
entry.add(Entry.of(strategy, input));
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/com/switcherapi/client/model/SwitcherRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public SwitcherRequest(final String switcherKey,
this.switcherExecutor = switcherExecutor;
this.switcherKey = switcherKey;
this.historyExecution = new HashSet<>();
this.entry = new ArrayList<>();
}

@Override
Expand All @@ -54,13 +53,10 @@ public SwitcherRequest prepareEntry(final List<Entry> entry) {
@Override
public SwitcherRequest prepareEntry(final Entry entry, final boolean add) {
if (!add) {
this.entry.clear();
this.flush();
}

if (!this.entry.contains(entry)) {
this.entry.add(entry);
}

this.entry.add(entry);
return this;
}

Expand Down Expand Up @@ -133,10 +129,6 @@ public SwitcherResult getLastExecutionResult() {
public boolean isBypassMetrics() {
return bypassMetrics;
}

public void resetEntry() {
this.entry = new ArrayList<>();
}

private boolean canUseAsync() {
return super.delay > 0 && !this.historyExecution.isEmpty();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/switcherapi/client/utils/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.switcherapi.client.remote.dto.CriteriaRequest;
import com.switcherapi.client.remote.dto.CriteriaResponse;

import java.util.List;

public class Mapper {

private Mapper() {}
Expand All @@ -24,7 +26,11 @@ public static SwitcherResult mapFrom(final CriteriaResponse criteriaResponse,
switcherResult.setResult(criteriaResponse.getResult());
switcherResult.setReason(criteriaResponse.getReason());
switcherResult.setMetadata(criteriaResponse.getMetadata());
switcherResult.setEntry(switcherRequest.getEntry());

if (!switcherRequest.getEntry().isEmpty()) {
switcherResult.setEntry(List.copyOf(switcherRequest.getEntry()));
}

return switcherResult;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.switcherapi.client;

import com.switcherapi.Switchers;
import com.switcherapi.client.model.SwitcherBuilder;
import com.switcherapi.client.model.SwitcherRequest;
import com.switcherapi.client.model.SwitcherResult;
import com.switcherapi.fixture.MetadataErrorSample;
Expand Down Expand Up @@ -83,6 +84,23 @@ void shouldReturnCriteriaResponseWithInputs() {
assertEquals("Strategy VALUE_VALIDATION does not agree", response.getReason());
}

@Test
void shouldFlushStrategyInputs() {
SwitcherBuilder switcherBuilder = Switchers
.getSwitcher(Switchers.REMOTE_KEY)
.checkValue("value")
.checkNumeric("10");

assertEquals(2, switcherBuilder.getEntry().size());

//test
switcherBuilder
.flush()
.checkValue("anotherValue");

assertEquals(1, switcherBuilder.getEntry().size());
}

@Test
void shouldReturnCriteriaResponseWithMetadata() {
//auth
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/switcherapi/client/SwitcherThrottleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.switcherapi.Switchers;
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;
Expand Down Expand Up @@ -70,4 +71,28 @@ void shouldReturnTrue_withThrottle() {
assertFalse(switcher.isItOn());
}

@Test
void shouldRetrieveNewResponse_whenStrategyInputChanged() {
Switchers.initializeClient();

// Initial remote call
givenResponse(generateMockAuth(10)); //auth
givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached)
givenResponse(generateCriteriaResponse("false", false)); //criteria - async (cached)

// Throttle period - should use cache
givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background)

//test
SwitcherBuilder switcher = Switchers
.getSwitcher(Switchers.REMOTE_KEY)
.throttle(1000);

for (int i = 0; i < 100; i++) {
assertTrue(switcher.checkValue("value").isItOn());
}

assertFalse(switcher.checkValue("value_changed").isItOn());
}

}