diff --git a/README.md b/README.md index baaeac7..2ac84f4 100644 --- a/README.md +++ b/README.md @@ -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** @@ -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' diff --git a/src/main/java/com/github/switcherapi/client/model/Switcher.java b/src/main/java/com/github/switcherapi/client/model/Switcher.java index 2abc007..3a4e896 100644 --- a/src/main/java/com/github/switcherapi/client/model/Switcher.java +++ b/src/main/java/com/github/switcherapi/client/model/Switcher.java @@ -13,28 +13,18 @@ *
  • Switcher execution
  • *
  • Switcher get input/output
  • * + * + * For example: + *
    + *  Switcher switcher = SwitcherContext
    + *  	.getSwitcher(MY_SWITCHER)
    + *  	.remote(true)
    + *  	.throttle(1000)
    + *  	.checkValue("value");
    + * 	
    */ public interface Switcher { - /** - * This method builds the Switcher object.
    - * Uses to isolate Switcher creation from the execution.
    - * - * For example: - *
    -	 * Switcher switcher = SwitcherContext
    -	 * 	.getSwitcher(MY_SWITCHER)
    -	 * 	.remote(true)
    -	 * 	.throttle(1000)
    -	 * 	.checkValue("value")
    -	 * 	.build();
    -	 * 
    - * - * @return instance of SwitcherInterface - * @see SwitcherRequest - */ - Switcher build(); - /** * Prepare the Switcher including a list of inputs necessary to run the criteria afterward. * @@ -107,4 +97,11 @@ public interface Switcher { */ List getEntry(); + /** + * Get the last execution result of the switcher. + * + * @return the last SwitcherResult, otherwise null if no executions were made + */ + SwitcherResult getLastExecutionResult(); + } diff --git a/src/main/java/com/github/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/github/switcherapi/client/model/SwitcherRequest.java index b2bf34d..2dba274 100644 --- a/src/main/java/com/github/switcherapi/client/model/SwitcherRequest.java +++ b/src/main/java/com/github/switcherapi/client/model/SwitcherRequest.java @@ -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) { @@ -136,6 +131,11 @@ public List getEntry() { return this.entry; } + @Override + public SwitcherResult getLastExecutionResult() { + return getFromHistory().orElse(null); + } + public boolean isBypassMetrics() { return bypassMetrics; } diff --git a/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java b/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java index e4733fa..57ba9b2 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherBypassTest.java @@ -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()); } @@ -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()); } @@ -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()); } @@ -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()); } @@ -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"); @@ -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"); diff --git a/src/test/java/com/github/switcherapi/client/SwitcherLocal1Test.java b/src/test/java/com/github/switcherapi/client/SwitcherLocal1Test.java index 1f20e9f..6cd4392 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherLocal1Test.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherLocal1Test.java @@ -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; @@ -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 diff --git a/src/test/java/com/github/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/github/switcherapi/client/SwitcherThrottleTest.java index fe0f7cb..19aca2e 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherThrottleTest.java @@ -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()); diff --git a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java index 9187976..8c2531e 100644 --- a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java +++ b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java @@ -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 {