Skip to content

Commit 20f206a

Browse files
authored
Added switcher.check context configuration to validate Switchers during start (#345)
* Added switcher.check context configuration to validate Switchers during start * chore: removed unused env variable from master CI
1 parent 9d5eaf0 commit 20f206a

File tree

16 files changed

+107
-20
lines changed

16 files changed

+107
-20
lines changed

.github/workflows/master-2.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626

2727
- name: Build/Test & SonarCloud Scan
2828
run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }}
29-
env:
30-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3129

3230
build-test:
3331
name: Build & Test - JDK ${{ matrix.java }} on ${{ matrix.os }}

.github/workflows/master.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626

2727
- name: Build/Test & SonarCloud Scan
2828
run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }}
29-
env:
30-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3129

3230
build-test:
3331
name: Build & Test - JDK ${{ matrix.java }} on ${{ matrix.os }}

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ switcher.domain -> Domain name
7070
#optional
7171
switcher.environment -> Environment name
7272
switcher.local -> true/false When local, it will only use a local snapshot
73+
switcher.check -> true/false When true, it will check Switcher Keys
7374
switcher.relay.restrict -> true/false When true, it will check snapshot relay status
7475
switcher.snapshot.location -> Folder from where snapshots will be saved/read
7576
switcher.snapshot.auto -> true/false Automated lookup for snapshot when initializing the client
@@ -314,6 +315,16 @@ void testSwitchers() {
314315
}
315316
```
316317

318+
Alternatively, you can also set the Switcher Context configuration to check during the client initialization.
319+
320+
```java
321+
MyAppFeatures.configure(ContextBuilder.builder()
322+
...
323+
.checkSwitchers(true));
324+
325+
MyAppFeatures.initializeClient();
326+
```
327+
317328
#### SwitcherTest annotation - Requires JUnit 5 Jupiter
318329
Predefine Switchers result outside your test methods with the SwitcherTest annotation.
319330
</br>It encapsulates the test and makes sure that the Switcher returns to its original state after concluding the test.

src/main/java/com/github/switcherapi/client/ContextBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ public ContextBuilder local(boolean local) {
170170
return this;
171171
}
172172

173+
/**
174+
* @param checkSwitchers true/false When true, it will check switcher keys
175+
* @return ContextBuilder
176+
*/
177+
public ContextBuilder checkSwitchers(boolean checkSwitchers) {
178+
switcherProperties.setValue(ContextKey.CHECK_SWITCHERS, checkSwitchers);
179+
return this;
180+
}
181+
173182
/**
174183
* @param restrictRelay true/false When true, it will check snapshot relay status
175184
* @return ContextBuilder

src/main/java/com/github/switcherapi/client/SwitcherConfig.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ abstract class SwitcherConfig {
1111
protected String environment;
1212

1313
protected boolean local;
14+
protected boolean check;
1415
protected String silent;
1516
protected Integer timeout;
1617
protected Integer poolSize;
18+
protected RelayConfig relay;
1719
protected SnapshotConfig snapshot;
1820
protected TruststoreConfig truststore;
1921

2022
SwitcherConfig() {
23+
this.relay = new RelayConfig();
2124
this.snapshot = new SnapshotConfig();
2225
this.truststore = new TruststoreConfig();
2326
}
@@ -34,10 +37,15 @@ protected void updateSwitcherConfig(SwitcherProperties properties) {
3437
setComponent(properties.getValue(ContextKey.COMPONENT));
3538
setEnvironment(properties.getValue(ContextKey.ENVIRONMENT));
3639
setLocal(properties.getBoolean(ContextKey.LOCAL_MODE));
40+
setCheck(properties.getBoolean(ContextKey.CHECK_SWITCHERS));
3741
setSilent(properties.getValue(ContextKey.SILENT_MODE));
3842
setTimeout(properties.getInt(ContextKey.TIMEOUT_MS));
3943
setPoolSize(properties.getInt(ContextKey.POOL_CONNECTION_SIZE));
4044

45+
RelayConfig relayConfig = new RelayConfig();
46+
relayConfig.setRestrict(properties.getBoolean(ContextKey.RESTRICT_RELAY));
47+
setRelay(relayConfig);
48+
4149
SnapshotConfig snapshotConfig = new SnapshotConfig();
4250
snapshotConfig.setLocation(properties.getValue(ContextKey.SNAPSHOT_LOCATION));
4351
snapshotConfig.setAuto(properties.getBoolean(ContextKey.SNAPSHOT_AUTO_LOAD));
@@ -92,6 +100,10 @@ public void setLocal(boolean local) {
92100
this.local = local;
93101
}
94102

103+
public void setCheck(boolean check) {
104+
this.check = check;
105+
}
106+
95107
public void setSilent(String silent) {
96108
this.silent = silent;
97109
}
@@ -104,6 +116,9 @@ public void setPoolSize(Integer poolSize) {
104116
this.poolSize = poolSize;
105117
}
106118

119+
public void setRelay(RelayConfig relay) {
120+
this.relay = relay;
121+
}
107122
public void setSnapshot(SnapshotConfig snapshot) {
108123
this.snapshot = snapshot;
109124
}
@@ -112,6 +127,18 @@ public void setTruststore(TruststoreConfig truststore) {
112127
this.truststore = truststore;
113128
}
114129

130+
public static class RelayConfig {
131+
private boolean restrict;
132+
133+
public boolean isRestrict() {
134+
return restrict;
135+
}
136+
137+
public void setRestrict(boolean restrict) {
138+
this.restrict = restrict;
139+
}
140+
}
141+
115142
public static class SnapshotConfig {
116143
private String location;
117144
private boolean auto;

src/main/java/com/github/switcherapi/client/SwitcherContextBase.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ protected void configureClient() {
107107
.environment(environment)
108108
.component(component)
109109
.local(local)
110+
.checkSwitchers(check)
111+
.restrictRelay(relay.isRestrict())
110112
.silentMode(silent)
111113
.timeoutMs(timeout)
112114
.poolConnectionSize(poolSize)
@@ -180,7 +182,7 @@ public static void initializeClient() {
180182
validateContext();
181183
registerSwitcherKeys();
182184
switcherExecutor = buildInstance();
183-
185+
184186
loadSwitchers();
185187
scheduleSnapshotAutoUpdate(contextStr(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL));
186188
ContextBuilder.preConfigure(switcherProperties);
@@ -249,8 +251,14 @@ private static void registerSwitcherKey(Field[] fields) {
249251

250252
/**
251253
* Load Switcher instances into a map cache
254+
*
255+
* @throws SwitchersValidationException if "switcher.check" is enabled and one or more Switcher Keys are not found
252256
*/
253-
private static void loadSwitchers() {
257+
private static void loadSwitchers() throws SwitchersValidationException {
258+
if (contextBol(ContextKey.CHECK_SWITCHERS)) {
259+
checkSwitchers();
260+
}
261+
254262
if (Objects.isNull(switchers)) {
255263
switchers = new HashMap<>();
256264
}
@@ -446,7 +454,7 @@ public static void stopWatchingSnapshot() {
446454
*
447455
* @throws SwitchersValidationException when one or more Switcher Key is not found
448456
*/
449-
public static void checkSwitchers() {
457+
public static void checkSwitchers() throws SwitchersValidationException {
450458
switcherExecutor.checkSwitchers(switcherKeys);
451459
}
452460

src/main/java/com/github/switcherapi/client/SwitcherPropertiesImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public SwitcherPropertiesImpl() {
2323
setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false);
2424
setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false);
2525
setValue(ContextKey.LOCAL_MODE, false);
26+
setValue(ContextKey.CHECK_SWITCHERS, false);
2627
setValue(ContextKey.RESTRICT_RELAY, true);
2728
}
2829

@@ -40,6 +41,7 @@ public void loadFromProperties(Properties prop) {
4041
setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop));
4142
setValue(ContextKey.SILENT_MODE, SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop));
4243
setValue(ContextKey.LOCAL_MODE, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false));
44+
setValue(ContextKey.CHECK_SWITCHERS, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.CHECK_SWITCHERS.getParam(), prop), false));
4345
setValue(ContextKey.RESTRICT_RELAY, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.RESTRICT_RELAY.getParam(), prop), true));
4446
setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT));
4547
setValue(ContextKey.TRUSTSTORE_PATH, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public enum ContextKey {
6969
*/
7070
LOCAL_MODE("switcher.local"),
7171

72+
/**
73+
* (boolean) Defines if client will check the switchers before using them (default is false).
74+
*/
75+
CHECK_SWITCHERS("switcher.check"),
76+
7277
/**
7378
* (boolean) Defines if client will trigger local snapshot relay verification (default is true)
7479
*/

src/main/java/com/github/switcherapi/client/remote/dto/SwitchersCheck.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Set;
55

66
import com.github.switcherapi.client.remote.ClientWS;
7+
import com.google.gson.annotations.SerializedName;
78

89
/**
910
* Request/Response model to use with {@link ClientWS#checkSwitchers(Set, String)}
@@ -21,6 +22,7 @@ public class SwitchersCheck {
2122
/**
2223
* Response field
2324
*/
25+
@SerializedName("not_found")
2426
private String[] notFound;
2527

2628
public SwitchersCheck() {}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ static void setup() throws IOException {
2525
MockWebServerHelper.setupMockServer();
2626

2727
Switchers.loadProperties(); // Load default properties from resources
28-
Switchers.initializeClient(); // SwitcherContext requires preload before config override
2928
Switchers.configure(ContextBuilder.builder() // Override default properties
3029
.url(String.format("http://localhost:%s", mockBackEnd.getPort()))
3130
.local(false)

0 commit comments

Comments
 (0)