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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ getSwitcher(FEATURE01)
.isItOn();
```

4. **Accessing the response history**
Switchers stores the last execution result from a given switcher key/entry.
4. **Accessing the last SwitcherResult**
Switchers stores the last execution result, which can be retrieved using the following operation.

```java
switcher.getHistoryExecution();
switcher.getLastExecutionResult();
```

5. **Throttling**
Expand Down Expand Up @@ -305,7 +305,7 @@ switcher.isItOn(); // Now, it's going to return the result retrieved from the AP
For more complex scenarios where you need to test features based on specific inputs, you can use test conditions.

```java
Switcher switcher = MyAppFeatures.getSwitcher(FEATURE01).checkValue("My value").build();
Switcher switcher = MyAppFeatures.getSwitcher(FEATURE01).checkValue("My value");

SwitcherBypass.assume(FEATURE01, true).when(StrategyValidator.VALUE, "My value");
switcher.isItOn(); // 'true'
Expand Down
35 changes: 16 additions & 19 deletions src/main/java/com/github/switcherapi/client/model/Switcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,18 @@
* <li>Switcher execution</li>
* <li>Switcher get input/output</li>
* </ul>
*
* For example:
* <pre>
* Switcher switcher = SwitcherContext
* .getSwitcher(MY_SWITCHER)
* .remote(true)
* .throttle(1000)
* .checkValue("value");
* </pre>
*/
public interface Switcher {

/**
* This method builds the Switcher object.<br>
* Uses to isolate Switcher creation from the execution.<br>
*
* For example:
* <pre>
* Switcher switcher = SwitcherContext
* .getSwitcher(MY_SWITCHER)
* .remote(true)
* .throttle(1000)
* .checkValue("value")
* .build();
* </pre>
*
* @return instance of SwitcherInterface
* @see SwitcherRequest
*/
Switcher build();

/**
* Prepare the Switcher including a list of inputs necessary to run the criteria afterward.
*
Expand Down Expand Up @@ -107,4 +97,11 @@ public interface Switcher {
*/
List<Entry> getEntry();

/**
* Get the last execution result of the switcher.
*
* @return the last SwitcherResult, otherwise null if no executions were made
*/
SwitcherResult getLastExecutionResult();

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public SwitcherRequest(final String switcherKey,
this.historyExecution = new HashSet<>();
this.entry = new ArrayList<>();
}

@Override
public SwitcherRequest build() {
return this;
}

@Override
public SwitcherRequest prepareEntry(final List<Entry> entry) {
Expand Down Expand Up @@ -136,6 +131,11 @@ public List<Entry> getEntry() {
return this.entry;
}

@Override
public SwitcherResult getLastExecutionResult() {
return getFromHistory().orElse(null);
}

public boolean isBypassMetrics() {
return bypassMetrics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void shouldReturnTrue_usingAnnotationAsTrueWhenValueMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1");
assertTrue(switcher.isItOn());
}

Expand All @@ -132,10 +132,10 @@ void shouldReturnTrue_usingAnnotationAsTrueWhenValueSetMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1");
assertTrue(switcher.isItOn());

switcher = getSwitcher(USECASE41).checkValue("Value2").build();
switcher = getSwitcher(USECASE41).checkValue("Value2");
assertTrue(switcher.isItOn());
}

Expand All @@ -150,7 +150,7 @@ void shouldReturnTrue_usingMultipleSwitchersAnnotationWhenValueMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1");
assertTrue(switcher.isItOn());
}

Expand All @@ -163,7 +163,7 @@ void shouldReturnFalse_usingAnnotationAsTrueWhenValueNotMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1");
assertFalse(switcher.isItOn());
}

Expand Down Expand Up @@ -251,7 +251,7 @@ void shouldReturnTrue_afterAssumingItsTrueWhenValueMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value1");

SwitcherBypass.assume(USECASE41, true)
.when(StrategyValidator.VALUE, "Value1");
Expand All @@ -266,7 +266,7 @@ void shouldReturnFalse_afterAssumingItsTrueWhenValueNotMatches() {
SwitcherContext.initializeClient();

//test
Switcher switcher = getSwitcher(USECASE41).checkValue("Value2").build();
Switcher switcher = getSwitcher(USECASE41).checkValue("Value2");

SwitcherBypass.assume(USECASE41, true)
.when(StrategyValidator.VALUE, "Value1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.github.switcherapi.client.exception.SwitcherInvalidNumericFormat;
import com.github.switcherapi.client.exception.SwitcherInvalidTimeFormat;
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
import com.github.switcherapi.client.model.*;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Entry;
import com.github.switcherapi.client.model.StrategyValidator;
import com.github.switcherapi.client.model.SwitcherRequest;
import com.github.switcherapi.fixture.Product;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -47,13 +50,23 @@ void localShouldValidateContext() {
@Test
void localShouldReturnTrue() {
SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE11, true);

assertNull(switcher.getLastExecutionResult());
assertTrue(switcher.isItOn());

// check result history
assertTrue(switcher.getLastExecutionResult().isItOn());
}

@Test
void localShouldReturnFalse() {
SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE12);

assertNull(switcher.getLastExecutionResult());
assertFalse(switcher.isItOn());

// check result history
assertFalse(switcher.getLastExecutionResult().isItOn());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ void shouldReturnTrue_withThrottle() {
//test
Switcher switcher = Switchers
.getSwitcher(Switchers.REMOTE_KEY)
.throttle(1000)
.build();
.throttle(1000);

for (int i = 0; i < 100; i++) {
assertTrue(switcher.isItOn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class ClientPlayground {
public static void test() {
new Features().configureClient();
Switcher switcher = getSwitcher(CLIENT_JAVA_FEATURE)
.bypassMetrics()
.build();
.bypassMetrics();

scheduler.scheduleAtFixedRate(() -> {
try {
Expand Down