1
1
package dev .openfeature .sdk ;
2
2
3
- import dev .openfeature .sdk .exceptions .FlagNotFoundError ;
4
- import dev .openfeature .sdk .fixtures .HookFixtures ;
5
- import dev .openfeature .sdk .testutils .FeatureProviderTestUtils ;
6
- import dev .openfeature .sdk .testutils .TestEventsProvider ;
7
- import lombok .SneakyThrows ;
8
- import org .junit .jupiter .api .AfterEach ;
9
- import org .junit .jupiter .api .Test ;
10
- import org .mockito .ArgumentCaptor ;
11
- import org .mockito .InOrder ;
12
-
13
- import java .util .ArrayList ;
14
- import java .util .Arrays ;
15
- import java .util .Collections ;
16
- import java .util .HashMap ;
17
- import java .util .List ;
18
- import java .util .Map ;
19
- import java .util .Optional ;
20
-
21
3
import static org .assertj .core .api .Assertions .assertThat ;
22
4
import static org .assertj .core .api .Assertions .assertThatCode ;
23
5
import static org .assertj .core .api .Assertions .fail ;
34
16
import static org .mockito .Mockito .verify ;
35
17
import static org .mockito .Mockito .when ;
36
18
19
+ import dev .openfeature .sdk .exceptions .FlagNotFoundError ;
20
+ import dev .openfeature .sdk .fixtures .HookFixtures ;
21
+ import dev .openfeature .sdk .testutils .FeatureProviderTestUtils ;
22
+ import dev .openfeature .sdk .testutils .TestEventsProvider ;
23
+ import java .util .ArrayList ;
24
+ import java .util .Arrays ;
25
+ import java .util .Collections ;
26
+ import java .util .HashMap ;
27
+ import java .util .List ;
28
+ import java .util .Map ;
29
+ import java .util .Optional ;
30
+ import lombok .SneakyThrows ;
31
+ import org .junit .jupiter .api .AfterEach ;
32
+ import org .junit .jupiter .api .Test ;
33
+ import org .mockito .ArgumentCaptor ;
34
+ import org .mockito .InOrder ;
35
+
37
36
class HookSpecTest implements HookFixtures {
38
37
@ AfterEach
39
38
void emptyApiHooks () {
@@ -285,7 +284,10 @@ public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object>
285
284
}
286
285
287
286
@ Override
288
- public void finallyAfter (HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
287
+ public void finallyAfter (
288
+ HookContext <Boolean > ctx ,
289
+ FlagEvaluationDetails <Boolean > details ,
290
+ Map <String , Object > hints ) {
289
291
evalOrder .add ("provider finally" );
290
292
}
291
293
});
@@ -311,7 +313,8 @@ public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object>
311
313
}
312
314
313
315
@ Override
314
- public void finallyAfter (HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
316
+ public void finallyAfter (
317
+ HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
315
318
evalOrder .add ("api finally" );
316
319
}
317
320
});
@@ -336,7 +339,8 @@ public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object>
336
339
}
337
340
338
341
@ Override
339
- public void finallyAfter (HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
342
+ public void finallyAfter (
343
+ HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
340
344
evalOrder .add ("client finally" );
341
345
}
342
346
});
@@ -367,12 +371,15 @@ public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object>
367
371
evalOrder .add ("invocation error" );
368
372
}
369
373
370
- @ Override
371
- public void finallyAfter (HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
372
- evalOrder .add ("invocation finally" );
373
- }
374
- })
375
- .build ());
374
+ @ Override
375
+ public void finallyAfter (
376
+ HookContext <Boolean > ctx ,
377
+ FlagEvaluationDetails <Boolean > details ,
378
+ Map <String , Object > hints ) {
379
+ evalOrder .add ("invocation finally" );
380
+ }
381
+ })
382
+ .build ());
376
383
377
384
List <String > expectedOrder = Arrays .asList (
378
385
"api before" ,
@@ -408,10 +415,11 @@ void error_stops_before() {
408
415
api .setProviderAndWait (new AlwaysBrokenProvider ());
409
416
Client c = api .getClient ();
410
417
411
- c .getBooleanDetails ("key" , false , null , FlagEvaluationOptions .builder ()
412
- .hook (h2 )
413
- .hook (h )
414
- .build ());
418
+ c .getBooleanDetails (
419
+ "key" ,
420
+ false ,
421
+ null ,
422
+ FlagEvaluationOptions .builder ().hook (h2 ).hook (h ).build ());
415
423
verify (h , times (1 )).before (any (), any ());
416
424
verify (h2 , times (0 )).before (any (), any ());
417
425
}
@@ -472,8 +480,10 @@ public void error(HookContext<Boolean> ctx, Exception error, Map<String, Object>
472
480
}
473
481
474
482
@ Override
475
- public void finallyAfter (HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
476
- assertThatCode (() -> hints .put (hintKey , "changed value" )).isInstanceOf (UnsupportedOperationException .class );
483
+ public void finallyAfter (
484
+ HookContext <Boolean > ctx , FlagEvaluationDetails <Boolean > details , Map <String , Object > hints ) {
485
+ assertThatCode (() -> hints .put (hintKey , "changed value" ))
486
+ .isInstanceOf (UnsupportedOperationException .class );
477
487
}
478
488
};
479
489
@@ -569,8 +579,7 @@ void erroneous_flagResolution_setsAppropriateFieldsInFlagEvaluationDetails() {
569
579
flagKey ,
570
580
true ,
571
581
new ImmutableContext (),
572
- FlagEvaluationOptions .builder ().hook (hook ).build ()
573
- );
582
+ FlagEvaluationOptions .builder ().hook (hook ).build ());
574
583
575
584
ArgumentCaptor <FlagEvaluationDetails <Boolean >> captor = ArgumentCaptor .forClass (FlagEvaluationDetails .class );
576
585
verify (hook ).finallyAfter (any (), captor .capture (), any ());
@@ -582,7 +591,8 @@ void erroneous_flagResolution_setsAppropriateFieldsInFlagEvaluationDetails() {
582
591
assertThat (evaluationDetails .getReason ()).isEqualTo ("ERROR" );
583
592
assertThat (evaluationDetails .getVariant ()).isEqualTo ("Passed in default" );
584
593
assertThat (evaluationDetails .getFlagKey ()).isEqualTo (flagKey );
585
- assertThat (evaluationDetails .getFlagMetadata ()).isEqualTo (ImmutableMetadata .builder ().build ());
594
+ assertThat (evaluationDetails .getFlagMetadata ())
595
+ .isEqualTo (ImmutableMetadata .builder ().build ());
586
596
assertThat (evaluationDetails .getValue ()).isTrue ();
587
597
}
588
598
@@ -595,8 +605,7 @@ void successful_flagResolution_setsAppropriateFieldsInFlagEvaluationDetails() {
595
605
flagKey ,
596
606
true ,
597
607
new ImmutableContext (),
598
- FlagEvaluationOptions .builder ().hook (hook ).build ()
599
- );
608
+ FlagEvaluationOptions .builder ().hook (hook ).build ());
600
609
601
610
ArgumentCaptor <FlagEvaluationDetails <Boolean >> captor = ArgumentCaptor .forClass (FlagEvaluationDetails .class );
602
611
verify (hook ).finallyAfter (any (), captor .capture (), any ());
@@ -607,7 +616,8 @@ void successful_flagResolution_setsAppropriateFieldsInFlagEvaluationDetails() {
607
616
assertThat (evaluationDetails .getReason ()).isEqualTo ("DEFAULT" );
608
617
assertThat (evaluationDetails .getVariant ()).isEqualTo ("Passed in default" );
609
618
assertThat (evaluationDetails .getFlagKey ()).isEqualTo (flagKey );
610
- assertThat (evaluationDetails .getFlagMetadata ()).isEqualTo (ImmutableMetadata .builder ().build ());
619
+ assertThat (evaluationDetails .getFlagMetadata ())
620
+ .isEqualTo (ImmutableMetadata .builder ().build ());
611
621
assertThat (evaluationDetails .getValue ()).isTrue ();
612
622
}
613
623
@@ -772,7 +782,8 @@ void doesnt_use_finally() {
772
782
.as ("Not possible. Finally is a reserved word." )
773
783
.isInstanceOf (NoSuchMethodException .class );
774
784
775
- assertThatCode (() -> Hook .class .getMethod ("finallyAfter" , HookContext .class , FlagEvaluationDetails .class , Map .class ))
785
+ assertThatCode (() ->
786
+ Hook .class .getMethod ("finallyAfter" , HookContext .class , FlagEvaluationDetails .class , Map .class ))
776
787
.doesNotThrowAnyException ();
777
788
}
778
789
}
0 commit comments