|
1 | 1 | package javasdk;
|
2 | 2 |
|
| 3 | +import lombok.SneakyThrows; |
| 4 | +import org.junit.jupiter.api.AfterEach; |
| 5 | +import org.junit.jupiter.api.Disabled; |
3 | 6 | import org.junit.jupiter.api.Test;
|
4 | 7 |
|
5 |
| -import static org.junit.jupiter.api.Assertions.fail; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import static org.mockito.ArgumentMatchers.any; |
| 14 | +import static org.mockito.Mockito.*; |
6 | 15 |
|
7 | 16 | public class HookSpecTests {
|
| 17 | + @AfterEach |
| 18 | + void emptyApiHooks() { |
| 19 | + // it's a singleton. Don't pollute each test. |
| 20 | + OpenFeatureAPI.getInstance().clearHooks(); |
| 21 | + } |
| 22 | + |
8 | 23 | @Specification(spec="hooks", number="1.3", text="flag key, flag type, default value properties MUST be immutable. If the language does not support immutability, the hook MUST NOT modify these properties.")
|
9 | 24 | @Test void immutableValues() {
|
10 | 25 | try {
|
@@ -122,31 +137,170 @@ public class HookSpecTests {
|
122 | 137 | .build();
|
123 | 138 | }
|
124 | 139 |
|
125 |
| - @Specification(spec="hooks", number="1.4", text="The evaluation context MUST be mutable only within the before hook.") |
126 |
| - @Specification(spec="hooks", number="2.1", text="HookHints MUST be a map of objects.") |
127 |
| - @Specification(spec="hooks", number="2.2", text="Condition: HookHints MUST be immutable.") |
128 |
| - @Specification(spec="hooks", number="3.1", text="Hooks MUST specify at least one stage.") |
129 | 140 | @Specification(spec="hooks", number="3.2", text="The before stage MUST run before flag evaluation occurs. It accepts a hook context (required) and HookHints (optional) as parameters and returns either a HookContext or nothing.")
|
130 |
| - @Specification(spec="hooks", number="3.3", text="The after stage MUST run after flag evaluation occurs. It accepts a hook context (required), flag evaluation details (required) and HookHints (optional). It has no return value.") |
| 141 | + @Test void before_runs_ahead_of_evaluation() { |
| 142 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 143 | + api.setProvider(new AlwaysBrokenProvider()); |
| 144 | + Client client = api.getClient(); |
| 145 | + Hook<Boolean> evalHook = (Hook<Boolean>) mock(Hook.class); |
| 146 | + |
| 147 | + client.getBooleanValue("key", false, new EvaluationContext(), |
| 148 | + FlagEvaluationOptions.builder().hook(evalHook).build()); |
| 149 | + |
| 150 | + verify(evalHook, times(1)).before(any()); |
| 151 | + } |
| 152 | + |
| 153 | + @Specification(spec="hooks", number="5.1", text="Flag evalution options MUST contain a list of hooks to evaluate.") |
| 154 | + @Test void feo_has_hook_list() { |
| 155 | + FlagEvaluationOptions feo = FlagEvaluationOptions.builder() |
| 156 | + .build(); |
| 157 | + assertNotNull(feo.getHooks()); |
| 158 | + } |
| 159 | + |
| 160 | + @Specification(spec="hooks", number="4.3", text="If an error occurs in the finally hook, it MUST NOT trigger the error hook.") |
| 161 | + @Test void errors_in_finally() { |
| 162 | + Hook<Boolean> h = mock(Hook.class); |
| 163 | + doThrow(RuntimeException.class).when(h).finallyAfter(any()); |
| 164 | + |
| 165 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 166 | + api.setProvider(new NoOpProvider<>()); |
| 167 | + Client c= api.getClient(); |
| 168 | + |
| 169 | + assertThrows(RuntimeException.class, () -> c.getBooleanValue("key", false, null, FlagEvaluationOptions.builder().hook(h).build())); |
| 170 | + |
| 171 | + verify(h, times(1)).finallyAfter(any()); |
| 172 | + verify(h, times(0)).error(any(), any()); |
| 173 | + } |
| 174 | + |
131 | 175 | @Specification(spec="hooks", number="3.4", text="The error hook MUST run when errors are encountered in the before stage, the after stage or during flag evaluation. It accepts hook context (required), exception for what went wrong (required), and HookHints (optional). It has no return value.")
|
132 |
| - @Specification(spec="hooks", number="3.5", text="The finally hook MUST run after the before, after, and error stages. It accepts a hook context (required) and HookHints (optional). There is no return value.") |
133 |
| - @Specification(spec="hooks", number="3.6", text="Condition: If finally is a reserved word in the language, finallyAfter SHOULD be used.") |
134 |
| - @Specification(spec="hooks", number="4.1", text="The API, Client and invocation MUST have a method for registering hooks which accepts flag evaluation options") |
| 176 | + @Test void error_hook_run_during_non_finally_stage() { |
| 177 | + final boolean[] error_called = {false}; |
| 178 | + Hook h = mock(Hook.class); |
| 179 | + doThrow(RuntimeException.class).when(h).finallyAfter(any()); |
| 180 | + |
| 181 | + verify(h, times(0)).error(any(), any()); |
| 182 | + } |
135 | 183 | @Specification(spec="hooks", number="4.2", text="Hooks MUST be evaluated in the following order:" +
|
136 | 184 | "before: API, Client, Invocation" +
|
137 | 185 | "after: Invocation, Client, API" +
|
138 | 186 | "error (if applicable): Invocation, Client, API" +
|
139 | 187 | "finally: Invocation, Client, API")
|
140 |
| - @Specification(spec="hooks", number="4.3", text=" If an error occurs in the finally hook, it MUST NOT trigger the error hook.") |
| 188 | + @Test void eval_order() { |
| 189 | + List<String> evalOrder = new ArrayList<String>(); |
| 190 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 191 | + api.setProvider(new NoOpProvider()); |
| 192 | + api.registerHooks(new Hook() { |
| 193 | + @Override |
| 194 | + void before(HookContext ctx) { |
| 195 | + evalOrder.add("api before"); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + void after(HookContext ctx, FlagEvaluationDetails details) { |
| 200 | + evalOrder.add("api after"); |
| 201 | + throw new RuntimeException(); // trigger error flows. |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + void error(HookContext ctx, Exception error) { |
| 206 | + evalOrder.add("api error"); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + void finallyAfter(HookContext ctx) { |
| 211 | + evalOrder.add("api finally"); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + Client c = api.getClient(); |
| 216 | + c.registerHooks(new Hook() { |
| 217 | + @Override |
| 218 | + void before(HookContext ctx) { |
| 219 | + evalOrder.add("client before"); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + void after(HookContext ctx, FlagEvaluationDetails details) { |
| 224 | + evalOrder.add("client after"); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + void error(HookContext ctx, Exception error) { |
| 229 | + evalOrder.add("client error"); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + void finallyAfter(HookContext ctx) { |
| 234 | + evalOrder.add("client finally"); |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + c.getBooleanValue("key", false, null, FlagEvaluationOptions.builder() |
| 239 | + .hook(new Hook() { |
| 240 | + @Override |
| 241 | + void before(HookContext ctx) { |
| 242 | + evalOrder.add("invocation before"); |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + void after(HookContext ctx, FlagEvaluationDetails details) { |
| 247 | + evalOrder.add("invocation after"); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + void error(HookContext ctx, Exception error) { |
| 252 | + evalOrder.add("invocation error"); |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + void finallyAfter(HookContext ctx) { |
| 257 | + evalOrder.add("invocation finally"); |
| 258 | + } |
| 259 | + }) |
| 260 | + .build()); |
| 261 | + |
| 262 | + ArrayList<String> expectedOrder = new ArrayList<String>(); |
| 263 | + expectedOrder.addAll(Arrays.asList( |
| 264 | + "api before", "client before", "invocation before", |
| 265 | + "invocation after", "client after", "api after", |
| 266 | + "invocation error", "client error", "api error", |
| 267 | + "invocation finally", "client finally", "api finally")); |
| 268 | + assertEquals(expectedOrder, evalOrder); |
| 269 | + } |
| 270 | + |
| 271 | + @Specification(spec="hooks", number="1.4", text="The evaluation context MUST be mutable only within the before hook.") |
| 272 | + @Specification(spec="hooks", number="2.1", text="HookHints MUST be a map of objects.") |
| 273 | + @Specification(spec="hooks", number="2.2", text="Condition: HookHints MUST be immutable.") |
| 274 | + @Specification(spec="hooks", number="3.1", text="Hooks MUST specify at least one stage.") |
| 275 | + @Specification(spec="hooks", number="3.3", text="The after stage MUST run after flag evaluation occurs. It accepts a hook context (required), flag evaluation details (required) and HookHints (optional). It has no return value.") |
| 276 | + @Specification(spec="hooks", number="3.5", text="The finally hook MUST run after the before, after, and error stages. It accepts a hook context (required) and HookHints (optional). There is no return value.") |
| 277 | + @Specification(spec="hooks", number="4.1", text="The API, Client and invocation MUST have a method for registering hooks which accepts flag evaluation options") |
141 | 278 | @Specification(spec="hooks", number="4.4", text="If an error occurs in the before or after hooks, the error hooks MUST be invoked.")
|
142 | 279 | @Specification(spec="hooks", number="4.5", text="If an error occurs during the evaluation of before or after hooks, any remaining hooks in the before or after stages MUST NOT be invoked.")
|
143 | 280 | @Specification(spec="hooks", number="4.6", text="If an error is encountered in the error stage, it MUST NOT be returned to the user.")
|
144 |
| - @Specification(spec="hooks", number="5.1", text="Flag evalution options MUST contain a list of hooks to evaluate.") |
145 | 281 | @Specification(spec="hooks", number="5.2", text="Flag evaluation options MAY contain HookHints, a map of data to be provided to hook invocations.")
|
146 | 282 | @Specification(spec="hooks", number="5.3", text="HookHints MUST be passed to each hook through a parameter. It is merged into the object in the precedence order API -> Client -> Invocation (last wins).")
|
147 | 283 | @Specification(spec="hooks", number="5.4", text="The hook MUST NOT alter the HookHints object.")
|
148 | 284 | @Specification(spec="hooks", number="6.1", text="HookHints MUST passed between each hook.")
|
149 | 285 | void todo() {}
|
150 | 286 |
|
| 287 | + @SneakyThrows |
| 288 | + @Specification(spec="hooks", number="3.6", text="Condition: If finally is a reserved word in the language, finallyAfter SHOULD be used.") |
| 289 | + @Disabled |
| 290 | + @Test void doesnt_use_finally() { |
| 291 | +// Class [] carr = new Class[1]; |
| 292 | +// carr[0] = HookContext.class; |
| 293 | +// |
| 294 | +// try { |
| 295 | +// Hook.class.getMethod("finally", carr); |
| 296 | +// fail("Not possible. Finally is a reserved word."); |
| 297 | +// } catch (NoSuchMethodException e) { |
| 298 | +// // expected |
| 299 | +// } |
| 300 | + |
| 301 | + Hook.class.getMethod("finallyAfter", HookContext.class); |
| 302 | + |
| 303 | + } |
| 304 | + |
151 | 305 |
|
152 | 306 | }
|
0 commit comments