-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package vms.api.internal; | ||
|
||
import static java.util.function.Predicate.not; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/internal/feature-control") | ||
public class FeatureController { | ||
|
||
private final Collection<String> features; | ||
|
||
FeatureController(Environment environment) { | ||
var values = environment.getProperty("FEATURE_CONTROL", "").split(","); | ||
features = Stream.of(values).map(String::trim).filter(not(String::isBlank)).collect(toSet()); | ||
} | ||
|
||
@GetMapping | ||
Collection<String> getAll() { | ||
return features; | ||
} | ||
|
||
@GetMapping("/{name}") | ||
Map<String, Boolean> getFeature(@PathVariable String name) { | ||
return Map.of(name, features.contains(name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package vms.api.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.env.Environment; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class FeatureControllerTest { | ||
|
||
@Mock Environment environment; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(environment.getProperty("FEATURE_CONTROL", "")).thenReturn("FooEarlyPreview,HideAppBar"); | ||
} | ||
|
||
@Test | ||
void shouldReturnNoFeaturesIfNoEnvironmentPropertySet() { | ||
// given | ||
when(environment.getProperty("FEATURE_CONTROL", "")).thenReturn(""); | ||
|
||
// when | ||
Collection<String> features = new FeatureController(environment).getAll(); | ||
|
||
// then | ||
assertThat(features).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldReturnFeaturesIfEnvironmentPropertySet() { | ||
// when | ||
Collection<String> features = new FeatureController(environment).getAll(); | ||
|
||
// then | ||
assertThat(features).containsOnly("FooEarlyPreview", "HideAppBar"); | ||
} | ||
|
||
@Test | ||
void shouldReturnFeatureDisabledIfNoFeaturePresent() { | ||
// when | ||
Map<String, Boolean> feature = new FeatureController(environment).getFeature("NewUI"); | ||
|
||
// then | ||
assertThat(feature).containsExactly(Map.entry("NewUI", false)); | ||
} | ||
|
||
@Test | ||
void shouldReturnFeatureEnabledIfFeaturePresent() { | ||
// when | ||
Map<String, Boolean> feature = new FeatureController(environment).getFeature("HideAppBar"); | ||
|
||
// then | ||
assertThat(feature).containsExactly(Map.entry("HideAppBar", true)); | ||
} | ||
} |