|
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 |
|
@@ -300,11 +302,30 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { |
300 | 302 | assertNotNull(result.getFlagMetadata()); |
301 | 303 | } |
302 | 304 |
|
303 | | - @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") |
304 | 305 | @Specification(number="3.2.2.1", text="The API MUST have a method for setting the global evaluation context.") |
| 306 | + @Test void api_context() { |
| 307 | + String contextKey = "some-key"; |
| 308 | + String contextValue = "some-value"; |
| 309 | + DoSomethingProvider provider = spy( new DoSomethingProvider()); |
| 310 | + FeatureProviderTestUtils.setFeatureProvider(provider); |
| 311 | + |
| 312 | + Map<String, Value> attributes = new HashMap<>(); |
| 313 | + attributes.put(contextKey, new Value(contextValue)); |
| 314 | + EvaluationContext apiCtx = new ImmutableContext(attributes); |
| 315 | + |
| 316 | + // set the global context |
| 317 | + api.setEvaluationContext(apiCtx); |
| 318 | + Client client = api.getClient(); |
| 319 | + client.getBooleanValue("any-flag", false); |
| 320 | + |
| 321 | + // assert that the value from the global context was passed to the provider |
| 322 | + verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> arg.getValue(contextKey).asString().equals(contextValue))); |
| 323 | + } |
| 324 | + |
| 325 | + @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") |
305 | 326 | @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.") |
306 | 327 | @Test void multi_layer_context_merges_correctly() { |
307 | | - DoSomethingProvider provider = new DoSomethingProvider(); |
| 328 | + DoSomethingProvider provider = spy(new DoSomethingProvider()); |
308 | 329 | FeatureProviderTestUtils.setFeatureProvider(provider); |
309 | 330 | TransactionContextPropagator transactionContextPropagator = new ThreadLocalTransactionContextPropagator(); |
310 | 331 | api.setTransactionContextPropagator(transactionContextPropagator); |
@@ -349,21 +370,21 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { |
349 | 370 | invocationAttributes.put("invocation", new Value("4")); |
350 | 371 | EvaluationContext invocationCtx = new ImmutableContext(invocationAttributes); |
351 | 372 |
|
352 | | - // dosomethingprovider inverts this value. |
353 | | - assertTrue(c.getBooleanValue("key", false, invocationCtx)); |
354 | | - |
355 | | - EvaluationContext merged = provider.getMergedContext(); |
356 | | - assertEquals("1", merged.getValue("api").asString()); |
357 | | - assertEquals("2", merged.getValue("transaction").asString()); |
358 | | - assertEquals("3", merged.getValue("client").asString()); |
359 | | - assertEquals("4", merged.getValue("invocation").asString()); |
360 | | - assertEquals("2", merged.getValue("common1").asString(), "transaction merge is incorrect"); |
361 | | - assertEquals("3", merged.getValue("common2").asString(), "api client merge is incorrect"); |
362 | | - assertEquals("4", merged.getValue("common3").asString(), "invocation merge is incorrect"); |
363 | | - assertEquals("3", merged.getValue("common4").asString(), "api client merge is incorrect"); |
364 | | - assertEquals("4", merged.getValue("common5").asString(), "invocation merge is incorrect"); |
365 | | - assertEquals("4", merged.getValue("common6").asString(), "invocation merge is incorrect"); |
366 | | - |
| 373 | + c.getBooleanValue("key", false, invocationCtx); |
| 374 | + |
| 375 | + // assert the connect overrides |
| 376 | + verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> { |
| 377 | + return arg.getValue("api").asString().equals("1") && |
| 378 | + arg.getValue("transaction").asString().equals("2") && |
| 379 | + arg.getValue("client").asString().equals("3") && |
| 380 | + arg.getValue("invocation").asString().equals("4") && |
| 381 | + arg.getValue("common1").asString().equals("2") && |
| 382 | + arg.getValue("common2").asString().equals("3") && |
| 383 | + arg.getValue("common3").asString().equals("4") && |
| 384 | + arg.getValue("common4").asString().equals("3") && |
| 385 | + arg.getValue("common5").asString().equals("4") && |
| 386 | + arg.getValue("common6").asString().equals("4"); |
| 387 | + })); |
367 | 388 | } |
368 | 389 |
|
369 | 390 | @Specification(number="3.3.1.1", text="The API SHOULD have a method for setting a transaction context propagator.") |
|
0 commit comments