forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeveloperExperienceTest.java
119 lines (99 loc) · 4.64 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
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
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 java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import dev.openfeature.sdk.testutils.FeatureProviderTestUtils;
import org.junit.jupiter.api.Test;
import dev.openfeature.sdk.fixtures.HookFixtures;
class DeveloperExperienceTest implements HookFixtures {
transient String flagKey = "mykey";
@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 MutableContext}
*/
@Test void providingContext() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(new NoOpProvider());
Client client = api.getClient();
Map<String, Value> attributes = new HashMap<>();
List<Value> values = Arrays.asList(new Value(2), new Value(4));
attributes.put("int-val", new Value(3));
attributes.put("double-val", new Value(4.0));
attributes.put("str-val", new Value("works"));
attributes.put("bool-val", new Value(false));
attributes.put("value-val", new Value(values));
EvaluationContext ctx = new ImmutableContext(attributes);
Boolean retval = client.getBooleanValue(flagKey, false, ctx);
assertFalse(retval);
}
@Test void brokenProvider() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
FeatureProviderTestUtils.setFeatureProvider(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());
}
@Test
void providerLockedPerTransaction() {
class MutatingHook implements Hook {
@Override
// change the provider during a before hook - this should not impact the evaluation in progress
public Optional before(HookContext ctx, Map hints) {
FeatureProviderTestUtils.setFeatureProvider(new NoOpProvider());
return Optional.empty();
}
}
final String defaultValue = "string-value";
final OpenFeatureAPI api = OpenFeatureAPI.getInstance();
final Client client = api.getClient();
FeatureProviderTestUtils.setFeatureProvider(new DoSomethingProvider());
api.addHooks(new MutatingHook());
// if provider is changed during an evaluation transaction it should proceed with the original provider
String doSomethingValue = client.getStringValue("val", defaultValue);
assertEquals(new StringBuilder(defaultValue).reverse().toString(), doSomethingValue);
api.clearHooks();
// subsequent evaluations should now use new provider set by hook
String noOpValue = client.getStringValue("val", defaultValue);
assertEquals(noOpValue, defaultValue);
}
}