summaryrefslogtreecommitdiffstats
path: root/flags/src/test/java/com/yahoo
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-11-26 15:35:53 +0100
committerHåkon Hallingstad <hakon@oath.com>2018-11-26 15:35:53 +0100
commitc537b48e6bd2d752f2892a9899dd1d36ae58e82b (patch)
tree13a467d4929dc4f3efa7561588029803ed9d5f1c /flags/src/test/java/com/yahoo
parent62d08589d147c31119dd903ca198ba193a308c35 (diff)
Remove Optional classes
After review & new ideas: - All Optional classes have been removed: They must take a default argument to avoid having clients test whether the flag has been set or not, in order to allow local file flag source to undo a file flag setting from config server. - FeatureFlag is now backed by FlagSource::getString, and FlagSource::hasFeature has been removed. - FeatureFlag now allows default-true to facilitate default-enabled features
Diffstat (limited to 'flags/src/test/java/com/yahoo')
-rw-r--r--flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java53
-rw-r--r--flags/src/test/java/com/yahoo/vespa/flags/JacksonFlagTest.java6
2 files changed, 41 insertions, 18 deletions
diff --git a/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java b/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java
index e422057f5fe..4b7c8c0bbb3 100644
--- a/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java
+++ b/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java
@@ -9,41 +9,70 @@ import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class FileFlagSourceTest {
private final FileSystem fileSystem = TestFileSystem.create();
private final FileFlagSource source = new FileFlagSource(fileSystem);
+ private final FlagId id = new FlagId("foo");
@Test
- public void absentThenSet() throws IOException {
- FlagId id = new FlagId("foo");
+ public void testFeatureLikeFlags() throws IOException {
FeatureFlag featureFlag = new FeatureFlag(id, source);
+ FeatureFlag byDefaultTrue = featureFlag.defaultToTrue();
+
+ assertFalse(featureFlag.value());
+ assertTrue(byDefaultTrue.value());
+
+ writeFlag(id.toString(), "True\n");
+
+ assertTrue(featureFlag.value());
+ assertTrue(byDefaultTrue.value());
+
+ writeFlag(id.toString(), "False\n");
+
+ assertFalse(featureFlag.value());
+ assertFalse(byDefaultTrue.value());
+ }
+
+ @Test
+ public void testIntegerLikeFlags() throws IOException {
StringFlag stringFlag = new StringFlag(id, "default", source);
- OptionalStringFlag optionalStringFlag = new OptionalStringFlag(id, source);
IntFlag intFlag = new IntFlag(id, -1, source);
LongFlag longFlag = new LongFlag(id, -2L, source);
- assertFalse(source.hasFeature(id));
assertFalse(source.getString(id).isPresent());
- assertFalse(featureFlag.isSet());
assertEquals("default", stringFlag.value());
- assertFalse(optionalStringFlag.value().isPresent());
assertEquals(-1, intFlag.value());
assertEquals(-2L, longFlag.value());
- Path featurePath = fileSystem.getPath(FileFlagSource.FLAGS_DIRECTORY).resolve(id.toString());
- Files.createDirectories(featurePath.getParent());
- Files.write(featurePath, "1\n".getBytes());
+ writeFlag(id.toString(), "1\n");
- assertTrue(source.hasFeature(id));
assertTrue(source.getString(id).isPresent());
- assertTrue(featureFlag.isSet());
assertEquals("1\n", stringFlag.value());
- assertEquals("1\n", optionalStringFlag.value().get());
assertEquals(1, intFlag.value());
assertEquals(1L, longFlag.value());
}
+
+ @Test
+ public void parseFailure() throws IOException {
+ FeatureFlag featureFlag = new FeatureFlag(id, source);
+ writeFlag(featureFlag.id().toString(), "garbage");
+
+ try {
+ featureFlag.value();
+ } catch (IllegalArgumentException e) {
+ assertThat(e.getMessage(), containsString("garbage"));
+ }
+ }
+
+ private void writeFlag(String flagId, String value) throws IOException {
+ Path featurePath = fileSystem.getPath(FileFlagSource.FLAGS_DIRECTORY).resolve(flagId);
+ Files.createDirectories(featurePath.getParent());
+ Files.write(featurePath, value.getBytes());
+ }
} \ No newline at end of file
diff --git a/flags/src/test/java/com/yahoo/vespa/flags/JacksonFlagTest.java b/flags/src/test/java/com/yahoo/vespa/flags/JacksonFlagTest.java
index e4424d9886a..8bd486f5b47 100644
--- a/flags/src/test/java/com/yahoo/vespa/flags/JacksonFlagTest.java
+++ b/flags/src/test/java/com/yahoo/vespa/flags/JacksonFlagTest.java
@@ -9,8 +9,6 @@ import java.util.Objects;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -19,7 +17,6 @@ public class JacksonFlagTest {
private final ExampleJacksonClass defaultValue = new ExampleJacksonClass();
private final FlagSource source = mock(FlagSource.class);
private final JacksonFlag<ExampleJacksonClass> jacksonFlag = new JacksonFlag<>(id.toString(), ExampleJacksonClass.class, defaultValue, source);
- private final OptionalJacksonFlag<ExampleJacksonClass> optionalJacksonFlag = new OptionalJacksonFlag<>(id, ExampleJacksonClass.class, source);
@Test
public void unsetThenSet() {
@@ -28,7 +25,6 @@ public class JacksonFlagTest {
assertEquals(1, value.integer);
assertEquals("2", value.string);
assertEquals("3", value.dummy);
- assertFalse(optionalJacksonFlag.value().isPresent());
when(source.getString(id)).thenReturn(Optional.of("{\"integer\": 4, \"string\": \"foo\", \"stray\": 6}"));
value = jacksonFlag.value();
@@ -36,8 +32,6 @@ public class JacksonFlagTest {
assertEquals("foo", value.string);
assertEquals("3", value.dummy);
- assertTrue(optionalJacksonFlag.value().isPresent());
- value = optionalJacksonFlag.value().get();
assertEquals(4, value.integer);
assertEquals("foo", value.string);
assertEquals("3", value.dummy);