|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import dev.openfeature.sdk.fixtures.HookFixtures; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Optional; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.EnumSource; |
| 20 | + |
| 21 | +class HookExecutorTest implements HookFixtures { |
| 22 | + @Test |
| 23 | + @DisplayName("should merge EvaluationContexts on before hooks correctly") |
| 24 | + void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() { |
| 25 | + Map<String, Value> attributes = new HashMap<>(); |
| 26 | + attributes.put("baseKey", new Value("baseValue")); |
| 27 | + EvaluationContext baseContext = new ImmutableContext(attributes); |
| 28 | + |
| 29 | + Hook<String> hook1 = mockStringHook(); |
| 30 | + Hook<String> hook2 = mockStringHook(); |
| 31 | + when(hook1.before(any(), any())).thenReturn(Optional.of(evaluationContextWithValue("bla", "blubber"))); |
| 32 | + when(hook2.before(any(), any())).thenReturn(Optional.of(evaluationContextWithValue("foo", "bar"))); |
| 33 | + |
| 34 | + HookExecutor executor = HookExecutor.create( |
| 35 | + Arrays.asList(hook1, hook2), getBaseHookContextForType(FlagValueType.STRING), baseContext, Collections.emptyMap()); |
| 36 | + |
| 37 | + executor.executeBeforeHooks(); |
| 38 | + |
| 39 | + EvaluationContext result = executor.getEvaluationContext(); |
| 40 | + |
| 41 | + assertThat(result.getValue("bla").asString()).isEqualTo("blubber"); |
| 42 | + assertThat(result.getValue("foo").asString()).isEqualTo("bar"); |
| 43 | + assertThat(result.getValue("baseKey").asString()).isEqualTo("baseValue"); |
| 44 | + } |
| 45 | + |
| 46 | + @ParameterizedTest |
| 47 | + @EnumSource(value = FlagValueType.class) |
| 48 | + @DisplayName("should always call generic hook") |
| 49 | + void shouldAlwaysCallGenericHook(FlagValueType flagValueType) { |
| 50 | + Hook<?> genericHook = mockGenericHook(); |
| 51 | + |
| 52 | + HookExecutor hookExecutor = HookExecutor.create(List.of(genericHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY, Collections.emptyMap()); |
| 53 | + |
| 54 | + callAllHooks(hookExecutor); |
| 55 | + |
| 56 | + verify(genericHook).before(any(), any()); |
| 57 | + verify(genericHook).after(any(), any(), any()); |
| 58 | + verify(genericHook).finallyAfter(any(), any(), any()); |
| 59 | + verify(genericHook).error(any(), any(), any()); |
| 60 | + } |
| 61 | + |
| 62 | + @ParameterizedTest |
| 63 | + @EnumSource(value = FlagValueType.class) |
| 64 | + @DisplayName("should allow hooks to store and retrieve data across stages") |
| 65 | + void shouldPassDataAcrossStages(FlagValueType flagValueType) { |
| 66 | + var testHook = new HookDataHook(); |
| 67 | + HookExecutor hookExecutor = HookExecutor.create(List.of(testHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY, Collections.emptyMap()); |
| 68 | + |
| 69 | + hookExecutor.executeBeforeHooks(); |
| 70 | + assertHookData(testHook, "before"); |
| 71 | + |
| 72 | + hookExecutor.executeAfterHooks(FlagEvaluationDetails.builder().build()); |
| 73 | + assertHookData(testHook, "before", "after"); |
| 74 | + |
| 75 | + hookExecutor.executeAfterAllHooks(FlagEvaluationDetails.builder().build()); |
| 76 | + assertHookData(testHook, "before", "after", "finallyAfter"); |
| 77 | + |
| 78 | + hookExecutor.executeErrorHooks(mock(Exception.class)); |
| 79 | + assertHookData(testHook, "before", "after", "finallyAfter", "error"); |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @EnumSource(value = FlagValueType.class) |
| 84 | + @DisplayName("should isolate data between different hook instances") |
| 85 | + void shouldIsolateDataBetweenHooks(FlagValueType flagValueType) { |
| 86 | + var testHook1 = new HookDataHook(1); |
| 87 | + var testHook2 = new HookDataHook(2); |
| 88 | + |
| 89 | + HookExecutor hookExecutor = HookExecutor.create(List.of(testHook1, testHook2), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY, Collections.emptyMap()); |
| 90 | + |
| 91 | + callAllHooks(hookExecutor); |
| 92 | + |
| 93 | + assertHookData(testHook1, 1, "before", "after", "finallyAfter", "error"); |
| 94 | + assertHookData(testHook2, 2, "before", "after", "finallyAfter", "error"); |
| 95 | + } |
| 96 | + |
| 97 | + @ParameterizedTest |
| 98 | + @EnumSource(value = FlagValueType.class) |
| 99 | + @DisplayName("should isolate data between the same hook executions") |
| 100 | + void shouldIsolateDataBetweenSameHookExecutions(FlagValueType flagValueType) { |
| 101 | + TestHookWithData testHook = new TestHookWithData("test-key", "value-1"); |
| 102 | + |
| 103 | + HookExecutor hookExecutor1 = HookExecutor.create(List.of(testHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY, Collections.emptyMap()); |
| 104 | + HookExecutor hookExecutor2 = HookExecutor.create(List.of(testHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY, Collections.emptyMap()); |
| 105 | + |
| 106 | + // run hooks first time |
| 107 | + callAllHooks(hookExecutor1); |
| 108 | + assertHookData(testHook, "value-1"); |
| 109 | + |
| 110 | + // re-run with different value, will throw if HookData contains already data |
| 111 | + testHook.value = "value-2"; |
| 112 | + callAllHooks(hookExecutor2); |
| 113 | + assertHookData(testHook, "value-2"); |
| 114 | + } |
| 115 | + |
| 116 | + private static void callAllHooks(HookExecutor hookExecutor) { |
| 117 | + hookExecutor.executeBeforeHooks(); |
| 118 | + hookExecutor.executeAfterHooks(FlagEvaluationDetails.builder().build()); |
| 119 | + hookExecutor.executeAfterAllHooks(FlagEvaluationDetails.builder().build()); |
| 120 | + hookExecutor.executeErrorHooks(mock(Exception.class)); |
| 121 | + } |
| 122 | + |
| 123 | + private static void assertHookData(TestHookWithData testHook1, String expected) { |
| 124 | + assertThat(testHook1.onBeforeValue).isEqualTo(expected); |
| 125 | + assertThat(testHook1.onFinallyAfterValue).isEqualTo(expected); |
| 126 | + assertThat(testHook1.onAfterValue).isEqualTo(expected); |
| 127 | + assertThat(testHook1.onErrorValue).isEqualTo(expected); |
| 128 | + } |
| 129 | + |
| 130 | + private static void assertHookData(HookDataHook testHook, String ... expectedKeys) { |
| 131 | + for (String expectedKey : expectedKeys) { |
| 132 | + assertThat(testHook.hookData.get(expectedKey)) |
| 133 | + .withFailMessage("Expected key %s not present in hook data", expectedKey) |
| 134 | + .isNotNull(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static void assertHookData(HookDataHook testHook, Object expectedValue, String ... expectedKeys) { |
| 139 | + for (String expectedKey : expectedKeys) { |
| 140 | + assertThat(testHook.hookData.get(expectedKey)) |
| 141 | + .withFailMessage("Expected key '%s' not present in hook data", expectedKey) |
| 142 | + .isNotNull(); |
| 143 | + assertThat(testHook.hookData.get(expectedKey)) |
| 144 | + .withFailMessage("Expected key '%s' not containing expected value. Expected '%s' but found '%s'", |
| 145 | + expectedKey, expectedValue, testHook.hookData.get(expectedKey)) |
| 146 | + .isEqualTo(expectedValue); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private SharedHookContext getBaseHookContextForType(FlagValueType flagValueType) { |
| 151 | + return new SharedHookContext<>( |
| 152 | + "flagKey", |
| 153 | + flagValueType, |
| 154 | + () -> "client", |
| 155 | + () -> "provider", |
| 156 | + createDefaultValue(flagValueType)); |
| 157 | + } |
| 158 | + |
| 159 | + private Object createDefaultValue(FlagValueType flagValueType) { |
| 160 | + switch (flagValueType) { |
| 161 | + case INTEGER: |
| 162 | + return 1; |
| 163 | + case BOOLEAN: |
| 164 | + return true; |
| 165 | + case STRING: |
| 166 | + return "defaultValue"; |
| 167 | + case OBJECT: |
| 168 | + return "object"; |
| 169 | + case DOUBLE: |
| 170 | + return "double"; |
| 171 | + default: |
| 172 | + throw new IllegalArgumentException(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private EvaluationContext evaluationContextWithValue(String key, String value) { |
| 177 | + Map<String, Value> attributes = new HashMap<>(); |
| 178 | + attributes.put(key, new Value(value)); |
| 179 | + return new ImmutableContext(attributes); |
| 180 | + } |
| 181 | + |
| 182 | + private static class HookDataHook implements Hook { |
| 183 | + private final Object value; |
| 184 | + HookData hookData = null; |
| 185 | + |
| 186 | + public HookDataHook(Object value) { |
| 187 | + this.value = value; |
| 188 | + } |
| 189 | + |
| 190 | + public HookDataHook() { |
| 191 | + this("test"); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public Optional<EvaluationContext> before(HookContext ctx, Map hints) { |
| 196 | + ctx.getHookData().set("before", value); |
| 197 | + hookData = ctx.getHookData(); |
| 198 | + return Optional.empty(); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) { |
| 203 | + ctx.getHookData().set("after", value); |
| 204 | + hookData = ctx.getHookData(); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void error(HookContext ctx, Exception error, Map hints) { |
| 209 | + ctx.getHookData().set("error", value); |
| 210 | + hookData = ctx.getHookData(); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void finallyAfter(HookContext ctx, FlagEvaluationDetails details, Map hints) { |
| 215 | + ctx.getHookData().set("finallyAfter", value); |
| 216 | + hookData = ctx.getHookData(); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private class TestHookWithData implements Hook { |
| 221 | + |
| 222 | + private final String key; |
| 223 | + Object value; |
| 224 | + |
| 225 | + Object onBeforeValue; |
| 226 | + Object onAfterValue; |
| 227 | + Object onErrorValue; |
| 228 | + Object onFinallyAfterValue; |
| 229 | + |
| 230 | + TestHookWithData(Object value) { |
| 231 | + this("test", value); |
| 232 | + } |
| 233 | + |
| 234 | + TestHookWithData(String key, Object value) { |
| 235 | + this.key = key; |
| 236 | + this.value = value; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public Optional<EvaluationContext> before(HookContext ctx, Map hints) { |
| 241 | + var storedValue = ctx.getHookData().get(key); |
| 242 | + if (storedValue != null) { |
| 243 | + throw new Error("Hook data isolation violated! Data is already set."); |
| 244 | + } |
| 245 | + ctx.getHookData().set(key, value); |
| 246 | + onBeforeValue = ctx.getHookData().get(key); |
| 247 | + return Optional.empty(); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) { |
| 252 | + onAfterValue = ctx.getHookData().get(key); |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public void error(HookContext ctx, Exception error, Map hints) { |
| 257 | + onErrorValue = ctx.getHookData().get(key); |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public void finallyAfter(HookContext ctx, FlagEvaluationDetails details, Map hints) { |
| 262 | + onFinallyAfterValue = ctx.getHookData().get(key); |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments