Skip to content

Commit

Permalink
feature control
Browse files Browse the repository at this point in the history
  • Loading branch information
olegood committed Jul 28, 2024
1 parent d93bcbf commit 7587af8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/vms/api/internal/FeatureController.java
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));
}
}
63 changes: 63 additions & 0 deletions src/test/java/vms/api/internal/FeatureControllerTest.java
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));
}
}

0 comments on commit 7587af8

Please sign in to comment.