|
3 | 3 | import java.util.*;
|
4 | 4 |
|
5 | 5 | import com.google.common.collect.ImmutableMap;
|
| 6 | +import com.google.common.collect.Lists; |
6 | 7 | import dev.openfeature.javasdk.fixtures.HookFixtures;
|
7 | 8 | import lombok.SneakyThrows;
|
8 | 9 | import org.junit.jupiter.api.*;
|
@@ -166,15 +167,41 @@ void emptyApiHooks() {
|
166 | 167 | }
|
167 | 168 |
|
168 | 169 |
|
169 |
| - @Specification(number="4.4.1", text="The API, Client and invocation MUST have a method for registering hooks which accepts flag evaluation options") |
| 170 | + @Specification(number="4.4.1", text="The API, Client, Provider, and invocation MUST have a method for registering hooks.") |
170 | 171 | @Specification(number="4.3.5", text="The after stage MUST run after flag resolution occurs. It accepts a hook context (required), flag evaluation details (required) and hook hints (optional). It has no return value.")
|
171 |
| - @Specification(number="4.4.2", text="Hooks MUST be evaluated in the following order: - before: API, Client, Invocation - after: Invocation, Client, API - error (if applicable): Invocation, Client, API - finally: Invocation, Client, API") |
| 172 | + @Specification(number="4.4.2", text="Hooks MUST be evaluated in the following order: - before: API, Client, Invocation, Provider - after: Provider, Invocation, Client, API - error (if applicable): Provider, Invocation, Client, API - finally: Provider, Invocation, Client, API") |
172 | 173 | @Specification(number="4.3.6", text="The error hook MUST run when errors are encountered in the before stage, the after stage or during flag resolution. It accepts hook context (required), exception representing what went wrong (required), and hook hints (optional). It has no return value.")
|
173 | 174 | @Specification(number="4.3.7", text="The finally hook MUST run after the before, after, and error stages. It accepts a hook context (required) and hook hints (optional). There is no return value.")
|
174 | 175 | @Test void hook_eval_order() {
|
175 | 176 | List<String> evalOrder = new ArrayList<>();
|
176 | 177 | OpenFeatureAPI api = OpenFeatureAPI.getInstance();
|
177 |
| - api.setProvider(new NoOpProvider()); |
| 178 | + api.setProvider(new NoOpProvider() { |
| 179 | + public List<Hook> getProviderHooks() { |
| 180 | + return Lists.newArrayList(new BooleanHook() { |
| 181 | + |
| 182 | + @Override |
| 183 | + public Optional<EvaluationContext> before(HookContext<Boolean> ctx, Map<String, Object> hints) { |
| 184 | + evalOrder.add("provider before"); |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void after(HookContext<Boolean> ctx, FlagEvaluationDetails<Boolean> details, Map<String, Object> hints) { |
| 190 | + evalOrder.add("provider after"); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object> hints) { |
| 195 | + evalOrder.add("provider error"); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void finallyAfter(HookContext<Boolean> ctx, Map<String, Object> hints) { |
| 200 | + evalOrder.add("provider finally"); |
| 201 | + } |
| 202 | + }); |
| 203 | + } |
| 204 | + }); |
178 | 205 | api.addHooks(new BooleanHook() {
|
179 | 206 | @Override
|
180 | 207 | public Optional<EvaluationContext> before(HookContext<Boolean> ctx, Map<String, Object> hints) {
|
@@ -250,10 +277,10 @@ public void finallyAfter(HookContext<Boolean> ctx, Map<String, Object> hints) {
|
250 | 277 | .build());
|
251 | 278 |
|
252 | 279 | List<String> expectedOrder = Arrays.asList(
|
253 |
| - "api before", "client before", "invocation before", |
254 |
| - "invocation after", "client after", "api after", |
255 |
| - "invocation error", "client error", "api error", |
256 |
| - "invocation finally", "client finally", "api finally"); |
| 280 | + "api before", "client before", "invocation before", "provider before", |
| 281 | + "provider after", "invocation after", "client after", "api after", |
| 282 | + "provider error", "invocation error", "client error", "api error", |
| 283 | + "provider finally", "invocation finally", "client finally", "api finally"); |
257 | 284 | assertEquals(expectedOrder, evalOrder);
|
258 | 285 | }
|
259 | 286 |
|
|
0 commit comments