-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathDeveloperExperienceTest.java
93 lines (77 loc) · 3.36 KB
/
DeveloperExperienceTest.java
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
package dev.openfeature.sdk;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import dev.openfeature.sdk.fixtures.HookFixtures;
import java.util.Arrays;
class DeveloperExperienceTest implements HookFixtures {
transient String flagKey = "mykey";
@Test void noProviderSet() {
final String noOp = "no-op";
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(null);
Client client = api.getClient();
String retval = client.getStringValue(flagKey, noOp);
assertEquals(noOp, retval);
}
@Test void simpleBooleanFlag() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new NoOpProvider());
Client client = api.getClient();
Boolean retval = client.getBooleanValue(flagKey, false);
assertFalse(retval);
}
@Test void clientHooks() {
Hook<Boolean> exampleHook = mockBooleanHook();
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new NoOpProvider());
Client client = api.getClient();
client.addHooks(exampleHook);
Boolean retval = client.getBooleanValue(flagKey, false);
verify(exampleHook, times(1)).finallyAfter(any(), any());
assertFalse(retval);
}
@Test void evalHooks() {
Hook<Boolean> clientHook = mockBooleanHook();
Hook<Boolean> evalHook = mockBooleanHook();
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new NoOpProvider());
Client client = api.getClient();
client.addHooks(clientHook);
Boolean retval = client.getBooleanValue(flagKey, false, null,
FlagEvaluationOptions.builder().hook(evalHook).build());
verify(clientHook, times(1)).finallyAfter(any(), any());
verify(evalHook, times(1)).finallyAfter(any(), any());
assertFalse(retval);
}
/**
* As an application author, you probably know special things about your users. You can communicate these to the
* provider via {@link EvaluationContext}
*/
@Test void providingContext() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new NoOpProvider());
Client client = api.getClient();
EvaluationContext ctx = new EvaluationContext()
.add("int-val", 3)
.add("double-val", 4.0)
.add("str-val", "works")
.add("bool-val", false)
.add("value-val", Arrays.asList(new Value(2), new Value(4)));
Boolean retval = client.getBooleanValue(flagKey, false, ctx);
assertFalse(retval);
}
@Test void brokenProvider() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new AlwaysBrokenProvider());
Client client = api.getClient();
FlagEvaluationDetails<Boolean> retval = client.getBooleanDetails(flagKey, false);
assertEquals(ErrorCode.FLAG_NOT_FOUND, retval.getErrorCode());
assertEquals(TestConstants.BROKEN_MESSAGE, retval.getErrorMessage());
assertEquals(Reason.ERROR.toString(), retval.getReason());
assertFalse(retval.getValue());
}
}