forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookSupportTest.java
More file actions
254 lines (219 loc) · 10.1 KB
/
Copy pathHookSupportTest.java
File metadata and controls
254 lines (219 loc) · 10.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package dev.openfeature.sdk;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import dev.openfeature.sdk.fixtures.HookFixtures;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
class HookSupportTest implements HookFixtures {
private static final HookSupport hookSupport = new HookSupport();
@Test
@DisplayName("should merge EvaluationContexts on before hooks correctly")
void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() {
Map<String, Value> attributes = new HashMap<>();
attributes.put("baseKey", new Value("baseValue"));
EvaluationContext baseEvalContext = new ImmutableContext(attributes);
Hook<String> hook1 = mockStringHook();
Hook<String> hook2 = mockStringHook();
when(hook1.before(any(), any())).thenReturn(Optional.of(evaluationContextWithValue("bla", "blubber")));
when(hook2.before(any(), any())).thenReturn(Optional.of(evaluationContextWithValue("foo", "bar")));
var layered = new LayeredEvaluationContext(baseEvalContext, null, null, null);
var sharedContext = getBaseHookContextForType(FlagValueType.STRING);
var hookSupportData = new HookSupportData();
hookSupportData.evaluationContext = layered;
hookSupport.setHooks(
hookSupportData,
Collections.emptyList(),
Collections.emptyList(),
Arrays.asList(hook1, hook2),
Collections.emptyList(),
FlagValueType.STRING);
hookSupport.setHookContexts(hookSupportData, sharedContext, layered);
hookSupport.executeBeforeHooks(hookSupportData);
EvaluationContext result = hookSupportData.getEvaluationContext();
assertThat(result.getValue("bla").asString()).isEqualTo("blubber");
assertThat(result.getValue("foo").asString()).isEqualTo("bar");
assertThat(result.getValue("baseKey").asString()).isEqualTo("baseValue");
}
@ParameterizedTest
@EnumSource(value = FlagValueType.class)
@DisplayName("should always call generic hook")
void shouldAlwaysCallGenericHook(FlagValueType flagValueType) {
Hook<?> genericHook = mockGenericHook();
var hookSupportData = new HookSupportData();
hookSupport.setHooks(
hookSupportData,
List.of(genericHook),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
flagValueType);
callAllHooks(hookSupportData);
verify(genericHook).before(any(), any());
verify(genericHook).after(any(), any(), any());
verify(genericHook).finallyAfter(any(), any(), any());
verify(genericHook).error(any(), any(), any());
}
@ParameterizedTest
@EnumSource(value = FlagValueType.class)
@DisplayName("should allow hooks to store and retrieve data across stages")
void shouldPassDataAcrossStages(FlagValueType flagValueType) {
var testHook = new TestHookWithData();
var hookSupportData = new HookSupportData();
hookSupport.setHooks(
hookSupportData,
Collections.emptyList(),
List.of(testHook),
Collections.emptyList(),
Collections.emptyList(),
flagValueType);
hookSupport.setHookContexts(
hookSupportData,
getBaseHookContextForType(flagValueType),
new LayeredEvaluationContext(null, null, null, null));
hookSupport.executeBeforeHooks(hookSupportData);
assertHookData(testHook, "before");
hookSupport.executeAfterHooks(
hookSupportData, FlagEvaluationDetails.builder().build());
assertHookData(testHook, "before", "after");
hookSupport.executeAfterAllHooks(
hookSupportData, FlagEvaluationDetails.builder().build());
assertHookData(testHook, "before", "after", "finallyAfter");
hookSupport.executeErrorHooks(hookSupportData, mock(Exception.class));
assertHookData(testHook, "before", "after", "finallyAfter", "error");
}
@ParameterizedTest
@EnumSource(value = FlagValueType.class)
@DisplayName("should isolate data between different hook instances")
void shouldIsolateDataBetweenHooks(FlagValueType flagValueType) {
var testHook1 = new TestHookWithData(1);
var testHook2 = new TestHookWithData(2);
var hookSupportData = new HookSupportData();
hookSupport.setHooks(
hookSupportData,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
List.of(testHook1, testHook2),
flagValueType);
hookSupport.setHookContexts(
hookSupportData,
getBaseHookContextForType(flagValueType),
new LayeredEvaluationContext(null, null, null, null));
callAllHooks(hookSupportData);
assertHookData(testHook1, 1, "before", "after", "finallyAfter", "error");
assertHookData(testHook2, 2, "before", "after", "finallyAfter", "error");
}
@Test
@DisplayName("should place hooks in provider → options → client → API order")
void shouldOrderHooksBySource() {
Hook<?> providerHook = mockGenericHook();
Hook<?> optionHook = mockGenericHook();
Hook<?> clientHook = mockGenericHook();
Hook<?> apiHook = mockGenericHook();
var hookSupportData = new HookSupportData();
hookSupport.setHooks(
hookSupportData,
List.of(providerHook),
List.of(optionHook),
List.of(clientHook),
List.of(apiHook),
FlagValueType.STRING);
assertThat(hookSupportData.getHooks())
.extracting(Pair::getKey)
.containsExactly(providerHook, optionHook, clientHook, apiHook);
}
@Test
void hookThatReturnsTheGivenContext_doesNotResultInAStackOverflow() {
var hookSupportData = new HookSupportData();
var recursiveHook = new Hook() {
@Override
public Optional<EvaluationContext> before(HookContext ctx, Map hints) {
return Optional.of(ctx.getCtx());
}
};
var emptyHook = new Hook() {
@Override
public Optional<EvaluationContext> before(HookContext ctx, Map hints) {
return Optional.of(ImmutableContext.EMPTY);
}
};
var layeredEvaluationContext =
new LayeredEvaluationContext(evaluationContextWithValue("key", "value"), null, null, null);
hookSupportData.evaluationContext = layeredEvaluationContext;
hookSupport.setHooks(
hookSupportData,
Collections.emptyList(),
Collections.emptyList(),
List.of(recursiveHook, emptyHook),
Collections.emptyList(),
FlagValueType.STRING);
hookSupport.setHookContexts(
hookSupportData, getBaseHookContextForType(FlagValueType.STRING), layeredEvaluationContext);
callAllHooks(hookSupportData);
assertThatNoException().isThrownBy(layeredEvaluationContext::asObjectMap);
}
private static void callAllHooks(HookSupportData hookSupportData) {
hookSupport.executeBeforeHooks(hookSupportData);
hookSupport.executeAfterHooks(
hookSupportData, FlagEvaluationDetails.builder().build());
hookSupport.executeAfterAllHooks(
hookSupportData, FlagEvaluationDetails.builder().build());
hookSupport.executeErrorHooks(hookSupportData, mock(Exception.class));
}
private static void assertHookData(TestHookWithData testHook, String... expectedKeys) {
for (String expectedKey : expectedKeys) {
assertThat(testHook.hookData.get(expectedKey))
.withFailMessage("Expected key %s not present in hook data", expectedKey)
.isNotNull();
}
}
private static void assertHookData(TestHookWithData testHook, Object expectedValue, String... expectedKeys) {
for (String expectedKey : expectedKeys) {
assertThat(testHook.hookData.get(expectedKey))
.withFailMessage("Expected key '%s' not present in hook data", expectedKey)
.isNotNull();
assertThat(testHook.hookData.get(expectedKey))
.withFailMessage(
"Expected key '%s' not containing expected value. Expected '%s' but found '%s'",
expectedKey, expectedValue, testHook.hookData.get(expectedKey))
.isEqualTo(expectedValue);
}
}
private SharedHookContext getBaseHookContextForType(FlagValueType flagValueType) {
return new SharedHookContext<>(
"flagKey", flagValueType, () -> "client", () -> "provider", createDefaultValue(flagValueType));
}
private Object createDefaultValue(FlagValueType flagValueType) {
switch (flagValueType) {
case INTEGER:
return 1;
case BOOLEAN:
return true;
case STRING:
return "defaultValue";
case OBJECT:
return "object";
case DOUBLE:
return "double";
default:
throw new IllegalArgumentException();
}
}
private EvaluationContext evaluationContextWithValue(String key, String value) {
Map<String, Value> attributes = new HashMap<>();
attributes.put(key, new Value(value));
return new ImmutableContext(attributes);
}
}