|
11 | 11 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
12 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
13 | 13 | import static org.mockito.ArgumentMatchers.any;
|
| 14 | +import static org.mockito.ArgumentMatchers.argThat; |
14 | 15 | import static org.mockito.Mockito.mock;
|
| 16 | +import static org.mockito.Mockito.spy; |
15 | 17 | import static org.mockito.Mockito.times;
|
16 | 18 | import static org.mockito.Mockito.verify;
|
17 | 19 |
|
@@ -307,11 +309,30 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
|
307 | 309 | assertNotNull(result.getFlagMetadata());
|
308 | 310 | }
|
309 | 311 |
|
310 |
| - @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") |
311 | 312 | @Specification(number="3.2.2.1", text="The API MUST have a method for setting the global evaluation context.")
|
| 313 | + @Test void api_context() { |
| 314 | + String contextKey = "some-key"; |
| 315 | + String contextValue = "some-value"; |
| 316 | + DoSomethingProvider provider = spy( new DoSomethingProvider()); |
| 317 | + FeatureProviderTestUtils.setFeatureProvider(provider); |
| 318 | + |
| 319 | + Map<String, Value> attributes = new HashMap<>(); |
| 320 | + attributes.put(contextKey, new Value(contextValue)); |
| 321 | + EvaluationContext apiCtx = new ImmutableContext(attributes); |
| 322 | + |
| 323 | + // set the global context |
| 324 | + api.setEvaluationContext(apiCtx); |
| 325 | + Client client = api.getClient(); |
| 326 | + client.getBooleanValue("any-flag", false); |
| 327 | + |
| 328 | + // assert that the value from the global context was passed to the provider |
| 329 | + verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> arg.getValue(contextKey).asString().equals(contextValue))); |
| 330 | + } |
| 331 | + |
| 332 | + @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") |
312 | 333 | @Specification(number="3.2.3", text="Evaluation context MUST be merged in the order: API (global; lowest precedence) -> transaction -> client -> invocation -> before hooks (highest precedence), with duplicate values being overwritten.")
|
313 | 334 | @Test void multi_layer_context_merges_correctly() {
|
314 |
| - DoSomethingProvider provider = new DoSomethingProvider(); |
| 335 | + DoSomethingProvider provider = spy(new DoSomethingProvider()); |
315 | 336 | FeatureProviderTestUtils.setFeatureProvider(provider);
|
316 | 337 | TransactionContextPropagator transactionContextPropagator = new ThreadLocalTransactionContextPropagator();
|
317 | 338 | api.setTransactionContextPropagator(transactionContextPropagator);
|
@@ -356,21 +377,21 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
|
356 | 377 | invocationAttributes.put("invocation", new Value("4"));
|
357 | 378 | EvaluationContext invocationCtx = new ImmutableContext(invocationAttributes);
|
358 | 379 |
|
359 |
| - // dosomethingprovider inverts this value. |
360 |
| - assertTrue(c.getBooleanValue("key", false, invocationCtx)); |
361 |
| - |
362 |
| - EvaluationContext merged = provider.getMergedContext(); |
363 |
| - assertEquals("1", merged.getValue("api").asString()); |
364 |
| - assertEquals("2", merged.getValue("transaction").asString()); |
365 |
| - assertEquals("3", merged.getValue("client").asString()); |
366 |
| - assertEquals("4", merged.getValue("invocation").asString()); |
367 |
| - assertEquals("2", merged.getValue("common1").asString(), "transaction merge is incorrect"); |
368 |
| - assertEquals("3", merged.getValue("common2").asString(), "api client merge is incorrect"); |
369 |
| - assertEquals("4", merged.getValue("common3").asString(), "invocation merge is incorrect"); |
370 |
| - assertEquals("3", merged.getValue("common4").asString(), "api client merge is incorrect"); |
371 |
| - assertEquals("4", merged.getValue("common5").asString(), "invocation merge is incorrect"); |
372 |
| - assertEquals("4", merged.getValue("common6").asString(), "invocation merge is incorrect"); |
373 |
| - |
| 380 | + c.getBooleanValue("key", false, invocationCtx); |
| 381 | + |
| 382 | + // assert the connect overrides |
| 383 | + verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> { |
| 384 | + return arg.getValue("api").asString().equals("1") && |
| 385 | + arg.getValue("transaction").asString().equals("2") && |
| 386 | + arg.getValue("client").asString().equals("3") && |
| 387 | + arg.getValue("invocation").asString().equals("4") && |
| 388 | + arg.getValue("common1").asString().equals("2") && |
| 389 | + arg.getValue("common2").asString().equals("3") && |
| 390 | + arg.getValue("common3").asString().equals("4") && |
| 391 | + arg.getValue("common4").asString().equals("3") && |
| 392 | + arg.getValue("common5").asString().equals("4") && |
| 393 | + arg.getValue("common6").asString().equals("4"); |
| 394 | + })); |
374 | 395 | }
|
375 | 396 |
|
376 | 397 | @Specification(number="3.3.1.1", text="The API SHOULD have a method for setting a transaction context propagator.")
|
|
0 commit comments