Skip to content

Commit 282438c

Browse files
authored
Added switcher.check context configuration to validate Switchers during start (#346)
* 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 * Fixed notFound serialization using Jackson lib
1 parent ae0cb71 commit 282438c

File tree

16 files changed

+172
-20
lines changed

16 files changed

+172
-20
lines changed

.github/workflows/master-2.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Master CI v2
2+
3+
on:
4+
push:
5+
branches: [master-2]
6+
pull_request:
7+
branches: [master-2]
8+
9+
jobs:
10+
build-scan:
11+
name: SonarCloud Scan
12+
runs-on: ubuntu-latest
13+
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Set up JDK 17
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: 17
24+
distribution: 'temurin'
25+
cache: maven
26+
27+
- name: Build/Test & SonarCloud Scan
28+
run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }}
29+
30+
build-test:
31+
name: Build & Test - JDK ${{ matrix.java }} on ${{ matrix.os }}
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
java: ['11', '17', '21']
36+
os: [ubuntu-latest, windows-latest]
37+
runs-on: ${{ matrix.os }}
38+
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"
39+
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- name: Setup Java
44+
uses: actions/setup-java@v4
45+
with:
46+
distribution: 'temurin'
47+
java-version: ${{ matrix.java }}
48+
49+
- name: Show Versions
50+
run: mvn -version
51+
52+
- name: Cache Maven packages
53+
uses: actions/cache@v4
54+
with:
55+
path: ~/.m2/repository
56+
key: ${{ runner.os }}-m2-${{ matrix.java }}
57+
restore-keys: ${{ runner.os }}-m2-
58+
59+
- name: Build/Test
60+
run: mvn -B clean package

.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
@@ -182,6 +182,15 @@ public ContextBuilder local(boolean local) {
182182
return this;
183183
}
184184

185+
/**
186+
* @param checkSwitchers true/false When true, it will check switcher keys
187+
* @return ContextBuilder
188+
*/
189+
public ContextBuilder checkSwitchers(boolean checkSwitchers) {
190+
switcherProperties.setValue(ContextKey.CHECK_SWITCHERS, checkSwitchers);
191+
return this;
192+
}
193+
185194
/**
186195
* @param restrictRelay true/false When true, it will check snapshot relay status
187196
* @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,14 +11,17 @@ 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 regexTimeout;
1718
protected Integer poolSize;
19+
protected RelayConfig relay;
1820
protected SnapshotConfig snapshot;
1921
protected TruststoreConfig truststore;
2022

2123
SwitcherConfig() {
24+
this.relay = new RelayConfig();
2225
this.snapshot = new SnapshotConfig();
2326
this.truststore = new TruststoreConfig();
2427
}
@@ -35,10 +38,15 @@ protected void updateSwitcherConfig(SwitcherProperties properties) {
3538
setComponent(properties.getValue(ContextKey.COMPONENT));
3639
setEnvironment(properties.getValue(ContextKey.ENVIRONMENT));
3740
setLocal(properties.getBoolean(ContextKey.LOCAL_MODE));
41+
setCheck(properties.getBoolean(ContextKey.CHECK_SWITCHERS));
3842
setSilent(properties.getValue(ContextKey.SILENT_MODE));
3943
setTimeout(properties.getInt(ContextKey.TIMEOUT_MS));
4044
setPoolSize(properties.getInt(ContextKey.POOL_CONNECTION_SIZE));
4145

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

104+
public void setCheck(boolean check) {
105+
this.check = check;
106+
}
107+
96108
public void setSilent(String silent) {
97109
this.silent = silent;
98110
}
@@ -109,6 +121,9 @@ public void setPoolSize(Integer poolSize) {
109121
this.poolSize = poolSize;
110122
}
111123

124+
public void setRelay(RelayConfig relay) {
125+
this.relay = relay;
126+
}
112127
public void setSnapshot(SnapshotConfig snapshot) {
113128
this.snapshot = snapshot;
114129
}
@@ -117,6 +132,18 @@ public void setTruststore(TruststoreConfig truststore) {
117132
this.truststore = truststore;
118133
}
119134

135+
public static class RelayConfig {
136+
private boolean restrict;
137+
138+
public boolean isRestrict() {
139+
return restrict;
140+
}
141+
142+
public void setRestrict(boolean restrict) {
143+
this.restrict = restrict;
144+
}
145+
}
146+
120147
public static class SnapshotConfig {
121148
private String location;
122149
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
.regexTimeout(regexTimeout)
112114
.timeoutMs(timeout)
@@ -181,7 +183,7 @@ public static void initializeClient() {
181183
validateContext();
182184
registerSwitcherKeys();
183185
switcherExecutor = buildInstance();
184-
186+
185187
loadSwitchers();
186188
scheduleSnapshotAutoUpdate(contextStr(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL));
187189
ContextBuilder.preConfigure(switcherProperties);
@@ -250,8 +252,14 @@ private static void registerSwitcherKey(Field[] fields) {
250252

251253
/**
252254
* Load Switcher instances into a map cache
255+
*
256+
* @throws SwitchersValidationException if "switcher.check" is enabled and one or more Switcher Keys are not found
253257
*/
254-
private static void loadSwitchers() {
258+
private static void loadSwitchers() throws SwitchersValidationException {
259+
if (contextBol(ContextKey.CHECK_SWITCHERS)) {
260+
checkSwitchers();
261+
}
262+
255263
if (Objects.isNull(switchers)) {
256264
switchers = new HashMap<>();
257265
}
@@ -447,7 +455,7 @@ public static void stopWatchingSnapshot() {
447455
*
448456
* @throws SwitchersValidationException when one or more Switcher Key is not found
449457
*/
450-
public static void checkSwitchers() {
458+
public static void checkSwitchers() throws SwitchersValidationException {
451459
switcherExecutor.checkSwitchers(switcherKeys);
452460
}
453461

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.github.switcherapi.client.remote.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.github.switcherapi.client.remote.ClientWS;
5+
36
import java.util.Arrays;
47
import java.util.Set;
58

6-
import com.github.switcherapi.client.remote.ClientWS;
7-
89
/**
910
* Request/Response model to use with {@link ClientWS#checkSwitchers(Set, String)}
1011
*
@@ -21,6 +22,7 @@ public class SwitchersCheck {
2122
/**
2223
* Response field
2324
*/
25+
@JsonProperty("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)