1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.BooleanNode;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author hakonhall
*/
public class FlagsTest {
@Test
void testBoolean() {
final boolean defaultValue = false;
FlagSource source = mock(FlagSource.class);
BooleanFlag booleanFlag = Flags.defineFeatureFlag("id", defaultValue, List.of("owner"), "1970-01-01", "2100-01-01", "description",
"modification effect", FetchVector.Dimension.ZONE_ID, FetchVector.Dimension.HOSTNAME)
.with(FetchVector.Dimension.ZONE_ID, "a-zone")
.bindTo(source);
assertThat(booleanFlag.id().toString(), equalTo("id"));
when(source.fetch(eq(new FlagId("id")), any())).thenReturn(Optional.empty());
// default value without raw flag
assertThat(booleanFlag.value(), equalTo(defaultValue));
ArgumentCaptor<FetchVector> vector = ArgumentCaptor.forClass(FetchVector.class);
verify(source).fetch(any(), vector.capture());
// hostname is set by default
Optional<String> hostname = vector.getValue().getValue(FetchVector.Dimension.HOSTNAME);
assertTrue(hostname.isPresent());
assertFalse(hostname.get().isEmpty());
// zone is set because it was set on the unbound flag above
assertThat(vector.getValue().getValue(FetchVector.Dimension.ZONE_ID), is(Optional.of("a-zone")));
// application and node type are not set
assertThat(vector.getValue().getValue(FetchVector.Dimension.INSTANCE_ID), is(Optional.empty()));
assertThat(vector.getValue().getValue(FetchVector.Dimension.NODE_TYPE), is(Optional.empty()));
RawFlag rawFlag = mock(RawFlag.class);
when(source.fetch(eq(new FlagId("id")), any())).thenReturn(Optional.of(rawFlag));
when(rawFlag.asJsonNode()).thenReturn(BooleanNode.getTrue());
// raw flag deserializes to true
assertThat(booleanFlag.with(FetchVector.Dimension.INSTANCE_ID, "an-app").value(), equalTo(true));
verify(source, times(2)).fetch(any(), vector.capture());
// application was set on the (bound) flag.
assertThat(vector.getValue().getValue(FetchVector.Dimension.INSTANCE_ID), is(Optional.of("an-app")));
}
@Test
void testString() {
testGeneric(Flags.defineStringFlag("string-id", "default value", List.of("owner"), "1970-01-01", "2100-01-01", "description",
"modification effect", FetchVector.Dimension.ZONE_ID, FetchVector.Dimension.HOSTNAME),
"other value");
}
@Test
void testInt() {
testGeneric(Flags.defineIntFlag("int-id", 2, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 3);
}
@Test
void testLong() {
testGeneric(Flags.defineLongFlag("long-id", 1L, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 2L);
}
@Test
void testDouble() {
testGeneric(Flags.defineDoubleFlag("double-id", 3.142, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 2.718);
}
@Test
void testList() {
testGeneric(Flags.defineListFlag("list-id", List.of("a"), String.class, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), List.of("a", "b", "c"));
}
@Test
void testJacksonClass() {
ExampleJacksonClass defaultInstance = new ExampleJacksonClass();
ExampleJacksonClass instance = new ExampleJacksonClass();
instance.integer = -2;
instance.string = "foo";
testGeneric(Flags.defineJacksonFlag("jackson-id", defaultInstance, ExampleJacksonClass.class,
List.of("owner"), "1970-01-01", "2100-01-01", "description", "modification effect", FetchVector.Dimension.HOSTNAME),
instance);
testGeneric(Flags.defineListFlag("jackson-list-id", List.of(defaultInstance), ExampleJacksonClass.class, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"),
List.of(instance));
}
static <T> void testGeneric(UnboundFlag<T, ?, ?> unboundFlag, T value) {
FlagSource source = mock(FlagSource.class);
Flag<T, ?> flag = unboundFlag.bindTo(source);
when(source.fetch(any(), any()))
.thenReturn(Optional.empty())
.thenReturn(Optional.of(flag.serializer().serialize(value)));
assertThat(flag.boxedValue(), equalTo(unboundFlag.defaultValue()));
assertThat(flag.boxedValue(), equalTo(value));
assertTrue(Flags.getFlag(unboundFlag.id()).isPresent());
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class ExampleJacksonClass {
@JsonProperty("integer")
public int integer = 1;
@JsonProperty("string")
public String string = "2";
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExampleJacksonClass that = (ExampleJacksonClass) o;
return integer == that.integer &&
Objects.equals(string, that.string);
}
@Override
public int hashCode() {
return Objects.hash(integer, string);
}
}
}
|