Skip to content

Commit 0bda797

Browse files
authored
Added switcher.defaultResult() to handle panic events (#298)
* bump pom version to 1.5.0-SNAPSHOT * Added switcher.defaultResult() to handle panic events (#297)
1 parent 5212f05 commit 0bda797

10 files changed

Lines changed: 108 additions & 23 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.github.switcherapi</groupId>
99
<artifactId>switcher-client</artifactId>
1010
<packaging>jar</packaging>
11-
<version>1.4.6</version>
11+
<version>1.5.0-SNAPSHOT</version>
1212

1313
<name>Switcher Client</name>
1414
<description>Switcher Client SDK for working with Switcher API</description>

src/main/java/com/github/switcherapi/client/model/SwitcherBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public abstract class SwitcherBuilder {
2323
protected boolean remote;
2424

2525
protected boolean bypassMetrics;
26+
27+
protected String defaultResult;
2628

2729
protected List<Entry> entry;
2830

@@ -58,6 +60,17 @@ public SwitcherBuilder remote(boolean remote) {
5860
this.remote = remote;
5961
return this;
6062
}
63+
64+
/**
65+
* Set the default result when client panics
66+
*
67+
* @param defaultResult true/false
68+
* @return {@link SwitcherBuilder}
69+
*/
70+
public SwitcherBuilder defaultResult(boolean defaultResult) {
71+
this.defaultResult = String.valueOf(defaultResult);
72+
return this;
73+
}
6174

6275
/**
6376
* Add a validation to the entry stack
@@ -219,4 +232,8 @@ public SwitcherBuilder bypassMetrics() {
219232
public boolean isRemote() {
220233
return remote;
221234
}
235+
236+
public String getDefaultResult() {
237+
return defaultResult;
238+
}
222239
}

src/main/java/com/github/switcherapi/client/model/response/CriteriaResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public CriteriaResponse buildFromSwitcher(Switcher switcher) {
3939
return this;
4040
}
4141

42+
public static CriteriaResponse buildFromDefault(Switcher switcher) {
43+
return new CriteriaResponse(true, "Default result", switcher);
44+
}
45+
4246
public boolean isItOn() {
4347
return result;
4448
}

src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.github.switcherapi.client.SwitcherContextBase;
44
import com.github.switcherapi.client.SwitcherExecutor;
5-
import com.github.switcherapi.client.exception.SwitcherContextException;
6-
import com.github.switcherapi.client.exception.SwitcherException;
7-
import com.github.switcherapi.client.exception.SwitcherSnapshotLoadException;
8-
import com.github.switcherapi.client.exception.SwitchersValidationException;
5+
import com.github.switcherapi.client.exception.*;
96
import com.github.switcherapi.client.model.ContextKey;
107
import com.github.switcherapi.client.model.Switcher;
118
import com.github.switcherapi.client.model.criteria.Domain;
@@ -71,12 +68,20 @@ public CriteriaResponse executeCriteria(final Switcher switcher) {
7168
SwitcherUtils.debug(logger, "switcher: {}", switcher);
7269

7370
CriteriaResponse response;
74-
if (switcher.isRemote()) {
75-
response = this.clientRemote.executeCriteria(switcher);
76-
SwitcherUtils.debug(logger, "[Remote] response: {}", response);
77-
} else {
78-
response = this.clientLocalService.executeCriteria(switcher, this.domain);
79-
SwitcherUtils.debug(logger, "[Local] response: {}", response);
71+
try {
72+
if (switcher.isRemote()) {
73+
response = this.clientRemote.executeCriteria(switcher);
74+
SwitcherUtils.debug(logger, "[Remote] response: {}", response);
75+
} else {
76+
response = this.clientLocalService.executeCriteria(switcher, this.domain);
77+
SwitcherUtils.debug(logger, "[Local] response: {}", response);
78+
}
79+
} catch (SwitcherKeyNotFoundException e) {
80+
if (StringUtils.isNotBlank(switcher.getDefaultResult())) {
81+
return CriteriaResponse.buildFromDefault(switcher);
82+
}
83+
84+
throw e;
8085
}
8186

8287
return response;

src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,27 @@ public CriteriaResponse executeCriteria(final Switcher switcher) {
4747
return response;
4848
} catch (final SwitcherRemoteException e) {
4949
logger.error("Failed to execute criteria - {}\nCause: {}", e.getMessage(), e.getCause());
50-
return executeSilentCriteria(switcher, e);
50+
return tryExecuteLocalCriteria(switcher, e);
5151
}
5252
}
5353

54-
private CriteriaResponse executeSilentCriteria(final Switcher switcher,
55-
final SwitcherRemoteException e) {
54+
private CriteriaResponse tryExecuteLocalCriteria(final Switcher switcher,
55+
final SwitcherRemoteException e) {
5656
if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) {
57-
CriteriaResponse response = this.switcherLocal.executeCriteria(switcher);
57+
final CriteriaResponse response = this.switcherLocal.executeCriteria(switcher);
5858
SwitcherUtils.debug(logger, "[Silent] response: {}", response);
59-
59+
6060
return response;
61-
} else {
62-
throw e;
6361
}
62+
63+
if (StringUtils.isNotBlank(switcher.getDefaultResult())) {
64+
final CriteriaResponse response = CriteriaResponse.buildFromDefault(switcher);
65+
SwitcherUtils.debug(logger, "[Default] response: {}", response);
66+
67+
return response;
68+
}
69+
70+
throw e;
6471
}
6572

6673
@Override

src/test/java/com/github/switcherapi/client/SwitcherFail1Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ void shouldReturnError_keyNotFound() {
6363
Switcher switcher = Switchers.getSwitcher(Switchers.REMOTE_KEY);
6464
assertThrows(SwitcherRemoteException.class, switcher::isItOn);
6565
}
66+
67+
@Test
68+
void shouldReturnSuccessDefaultResult_keyNotFound() {
69+
//auth
70+
givenResponse(generateMockAuth(10));
71+
72+
//criteria
73+
givenResponse(generateStatusResponse("404"));
74+
75+
Switcher switcher = Switchers.getSwitcher(Switchers.REMOTE_KEY);
76+
assertTrue(switcher.defaultResult(true).isItOn());
77+
}
6678

6779
@Test
6880
void shouldReturnError_unauthorizedAPIAccess() {

src/test/java/com/github/switcherapi/client/SwitcherLocal3Test.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.github.switcherapi.client;
22

3-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertThrows;
6-
73
import java.nio.file.Paths;
84
import java.util.Set;
95
import java.util.stream.Stream;
@@ -27,6 +23,8 @@
2723
import com.github.switcherapi.client.model.Switcher;
2824
import com.github.switcherapi.client.service.local.SwitcherLocalService;
2925

26+
import static org.junit.jupiter.api.Assertions.*;
27+
3028
class SwitcherLocal3Test {
3129

3230
private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources/snapshot";
@@ -101,4 +99,10 @@ void localShouldCheckSwitchers_notFound() {
10199
ex.getMessage());
102100
}
103101

102+
@Test
103+
void localShouldReturnTrue_defaultResult() {
104+
Switcher switcher = Switchers.getSwitcher(Switchers.NOT_FOUND_KEY);
105+
assertTrue(switcher.defaultResult(true).isItOn());
106+
}
107+
104108
}

src/test/java/com/github/switcherapi/fixture/MockWebServerHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected MockResponse generateCheckSnapshotVersionResponse(String status) {
9595
*/
9696
protected MockResponse generateStatusResponse(String code) {
9797
MockResponse.Builder builder = new MockResponse.Builder();
98-
builder.status(code);
98+
builder.setCode(Integer.parseInt(code));
9999
return builder.build();
100100
}
101101

src/test/java/com/github/switcherapi/playground/ClientPlayground.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public static void test() {
2424
.url("https://api.switcherapi.com")
2525
.apiKey("JDJiJDA4JEFweTZjSTR2bE9pUjNJOUYvRy9raC4vRS80Q2tzUnk1d3o1aXFmS2o5eWJmVW11cjR0ODNT")
2626
.domain("Playground")
27+
.local(true)
28+
.snapshotLocation("src/test/resources/snapshot/playground")
2729
.component("switcher-playground"));
2830

2931
initializeClient();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"data": {
3+
"domain": {
4+
"name": "Playground",
5+
"version": 0,
6+
"description": "My playground",
7+
"activated": true,
8+
"group": [
9+
{
10+
"name": "Experimental",
11+
"description": "Open feature flags for experimentation",
12+
"activated": true,
13+
"config": [
14+
{
15+
"key": "MY_SWITCHER",
16+
"description": "My first switcher",
17+
"activated": true,
18+
"strategies": [
19+
{
20+
"strategy": "VALUE_VALIDATION",
21+
"operation": "EXIST",
22+
"activated": false,
23+
"values": [
24+
"user_1"
25+
]
26+
}
27+
]
28+
}
29+
]
30+
}
31+
]
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)