Skip to content

Commit

Permalink
Feat/po 96: Add string variation for feature flags (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabahirfan authored Nov 30, 2023
1 parent 0cb8f6d commit ccdc117
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ public ResponseEntity<AppMode> updateMode(@RequestBody AppMode mode) {
return ResponseEntity.accepted().body(this.dynamicConfigService.updateAppMode(mode));
}

@GetMapping("/launchdarkly/{featureKey}")
public ResponseEntity<Boolean> getFeatureFlagValue(@PathVariable String featureKey) {
@GetMapping("/launchdarkly/bool/{featureKey}")
public ResponseEntity<Boolean> isFeatureEnabled(@PathVariable String featureKey) {
return ResponseEntity.ok(this.featureToggleService.isFeatureEnabled(featureKey));
}

@GetMapping("/launchdarkly/string/{featureKey}")
public ResponseEntity<String> getFeatureValue(@PathVariable String featureKey) {
return ResponseEntity.ok(this.featureToggleService.getFeatureValue(featureKey));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ public boolean isFeatureEnabled(String feature, LDUser user) {
return internalClient.boolVariation(feature, user, false);
}


public boolean isFeatureEnabled(String feature, LDUser user, boolean defaultValue) {
return internalClient.boolVariation(feature, user, defaultValue);
}

public String getFeatureValue(String feature, String defaultValue) {
return internalClient.stringVariation(feature, createLDUser().build(), defaultValue);
}

public LDUser.Builder createLDUser() {
return new LDUser.Builder("opal")
.custom("timestamp", String.valueOf(System.currentTimeMillis()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public boolean isFeatureEnabled(String feature) {
return this.featureToggleApi.isFeatureEnabled(feature);
}

public String getFeatureValue(String feature) {
return this.featureToggleApi.getFeatureValue(feature, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ void updateMode() {
}

@Test
void getFeatureFlagValue() {
void isFeatureEnabled() {
when(featureToggleService.isFeatureEnabled("my-feature")).thenReturn(true);

ResponseEntity<Boolean> response = controller.getFeatureFlagValue("my-feature");
ResponseEntity<Boolean> response = controller.isFeatureEnabled("my-feature");

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(true, response.getBody());
}

@Test
void getFeatureFlagValue() {
when(featureToggleService.getFeatureValue("my-feature")).thenReturn("value");

ResponseEntity<String> response = controller.getFeatureValue("my-feature");

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("value", response.getBody());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ void shouldReturnCorrectState_whenDefaultServiceUser(Boolean toggleState) {
verifyBoolVariationCalled(FAKE_FEATURE, List.of("timestamp", "environment"));
}

@ParameterizedTest
@ValueSource(strings = { "opal", "legacy"})
void shouldReturnCorrectStringValue_whenDefaultServiceUser(String toggleState) {
when(ldClient.stringVariation(eq(FAKE_FEATURE), any(LDUser.class), any()))
.thenReturn(toggleState);

assertThat(featureToggleApi.getFeatureValue(FAKE_FEATURE, null)).isEqualTo(toggleState);
}

private void givenToggle(String feature, boolean state) {
when(ldClient.boolVariation(eq(feature), any(LDUser.class), anyBoolean()))
.thenReturn(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -36,4 +37,13 @@ private void givenToggle(String feature, boolean state) {
when(featureToggleApi.isFeatureEnabled(eq(feature)))
.thenReturn(state);
}

@ParameterizedTest
@ValueSource(strings = { "opal", "legacy"})
void shouldReturnCorrectValue_whenMyFeatureValueInvoked(String toggleValue) {
when(featureToggleApi.getFeatureValue(eq("myFeature"), any()))
.thenReturn(toggleValue);

assertThat(featureToggleService.getFeatureValue("myFeature")).isEqualTo(toggleValue);
}
}

0 comments on commit ccdc117

Please sign in to comment.