forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHookSupportTest.java
108 lines (98 loc) · 4.41 KB
/
HookSupportTest.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
package dev.openfeature.sdk;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
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.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 {
@Test
@DisplayName("should merge EvaluationContexts on before hooks correctly")
void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() {
Map<String, Value> attributes = new HashMap<>();
attributes.put("baseKey", new Value("baseValue"));
EvaluationContext baseContext = new ImmutableContext(attributes);
HookContext<String> hookContext = new HookContext<>(
"flagKey", FlagValueType.STRING, "defaultValue", baseContext, () -> "client", () -> "provider");
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")));
HookSupport hookSupport = new HookSupport();
EvaluationContext result = hookSupport.beforeHooks(
FlagValueType.STRING, hookContext, Arrays.asList(hook1, hook2), Collections.emptyMap());
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();
HookSupport hookSupport = new HookSupport();
EvaluationContext baseContext = new ImmutableContext();
IllegalStateException expectedException = new IllegalStateException("All fine, just a test");
HookContext<Object> hookContext = new HookContext<>(
"flagKey",
flagValueType,
createDefaultValue(flagValueType),
baseContext,
() -> "client",
() -> "provider");
hookSupport.beforeHooks(
flagValueType, hookContext, Collections.singletonList(genericHook), Collections.emptyMap());
hookSupport.afterHooks(
flagValueType,
hookContext,
FlagEvaluationDetails.builder().build(),
Collections.singletonList(genericHook),
Collections.emptyMap());
hookSupport.afterAllHooks(
flagValueType,
hookContext,
FlagEvaluationDetails.builder().build(),
Collections.singletonList(genericHook),
Collections.emptyMap());
hookSupport.errorHooks(
flagValueType,
hookContext,
expectedException,
Collections.singletonList(genericHook),
Collections.emptyMap());
verify(genericHook).before(any(), any());
verify(genericHook).after(any(), any(), any());
verify(genericHook).finallyAfter(any(), any(), any());
verify(genericHook).error(any(), any(), any());
}
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));
EvaluationContext baseContext = new ImmutableContext(attributes);
return baseContext;
}
}