19
19
import org .junit .jupiter .params .provider .EnumSource ;
20
20
21
21
class HookSupportTest implements HookFixtures {
22
+
23
+ private static final HookSupport hookSupport = new HookSupport ();
24
+
22
25
@ Test
23
26
@ DisplayName ("should merge EvaluationContexts on before hooks correctly" )
24
27
void shouldMergeEvaluationContextsOnBeforeHooksCorrectly () {
@@ -31,15 +34,16 @@ void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() {
31
34
when (hook1 .before (any (), any ())).thenReturn (Optional .of (evaluationContextWithValue ("bla" , "blubber" )));
32
35
when (hook2 .before (any (), any ())).thenReturn (Optional .of (evaluationContextWithValue ("foo" , "bar" )));
33
36
34
- HookSupport executor = new HookSupport (
37
+ var hookSupportData = new HookSupportData ();
38
+ hookSupportData .initialize (
35
39
Arrays .asList (hook1 , hook2 ),
36
40
getBaseHookContextForType (FlagValueType .STRING ),
37
41
baseContext ,
38
42
Collections .emptyMap ());
39
43
40
- executor .executeBeforeHooks ();
44
+ hookSupport .executeBeforeHooks (hookSupportData );
41
45
42
- EvaluationContext result = executor .getEvaluationContext ();
46
+ EvaluationContext result = hookSupportData .getEvaluationContext ();
43
47
44
48
assertThat (result .getValue ("bla" ).asString ()).isEqualTo ("blubber" );
45
49
assertThat (result .getValue ("foo" ).asString ()).isEqualTo ("bar" );
@@ -52,13 +56,14 @@ void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() {
52
56
void shouldAlwaysCallGenericHook (FlagValueType flagValueType ) {
53
57
Hook <?> genericHook = mockGenericHook ();
54
58
55
- HookSupport hookSupport = new HookSupport (
59
+ var hookSupportData = new HookSupportData ();
60
+ hookSupportData .initialize (
56
61
List .of (genericHook ),
57
62
getBaseHookContextForType (flagValueType ),
58
63
ImmutableContext .EMPTY ,
59
64
Collections .emptyMap ());
60
65
61
- callAllHooks (hookSupport );
66
+ callAllHooks (hookSupportData );
62
67
63
68
verify (genericHook ).before (any (), any ());
64
69
verify (genericHook ).after (any (), any (), any ());
@@ -71,22 +76,25 @@ void shouldAlwaysCallGenericHook(FlagValueType flagValueType) {
71
76
@ DisplayName ("should allow hooks to store and retrieve data across stages" )
72
77
void shouldPassDataAcrossStages (FlagValueType flagValueType ) {
73
78
var testHook = new TestHookWithData ();
74
- HookSupport hookSupport = new HookSupport (
79
+ var hookSupportData = new HookSupportData ();
80
+ hookSupportData .initialize (
75
81
List .of (testHook ),
76
82
getBaseHookContextForType (flagValueType ),
77
83
ImmutableContext .EMPTY ,
78
84
Collections .emptyMap ());
79
85
80
- hookSupport .executeBeforeHooks ();
86
+ hookSupport .executeBeforeHooks (hookSupportData );
81
87
assertHookData (testHook , "before" );
82
88
83
- hookSupport .executeAfterHooks (FlagEvaluationDetails .builder ().build ());
89
+ hookSupport .executeAfterHooks (
90
+ hookSupportData , FlagEvaluationDetails .builder ().build ());
84
91
assertHookData (testHook , "before" , "after" );
85
92
86
- hookSupport .executeAfterAllHooks (FlagEvaluationDetails .builder ().build ());
93
+ hookSupport .executeAfterAllHooks (
94
+ hookSupportData , FlagEvaluationDetails .builder ().build ());
87
95
assertHookData (testHook , "before" , "after" , "finallyAfter" );
88
96
89
- hookSupport .executeErrorHooks (mock (Exception .class ));
97
+ hookSupport .executeErrorHooks (hookSupportData , mock (Exception .class ));
90
98
assertHookData (testHook , "before" , "after" , "finallyAfter" , "error" );
91
99
}
92
100
@@ -97,23 +105,26 @@ void shouldIsolateDataBetweenHooks(FlagValueType flagValueType) {
97
105
var testHook1 = new TestHookWithData (1 );
98
106
var testHook2 = new TestHookWithData (2 );
99
107
100
- HookSupport hookSupport = new HookSupport (
108
+ var hookSupportData = new HookSupportData ();
109
+ hookSupportData .initialize (
101
110
List .of (testHook1 , testHook2 ),
102
111
getBaseHookContextForType (flagValueType ),
103
112
ImmutableContext .EMPTY ,
104
113
Collections .emptyMap ());
105
114
106
- callAllHooks (hookSupport );
115
+ callAllHooks (hookSupportData );
107
116
108
117
assertHookData (testHook1 , 1 , "before" , "after" , "finallyAfter" , "error" );
109
118
assertHookData (testHook2 , 2 , "before" , "after" , "finallyAfter" , "error" );
110
119
}
111
120
112
- private static void callAllHooks (HookSupport hookSupport ) {
113
- hookSupport .executeBeforeHooks ();
114
- hookSupport .executeAfterHooks (FlagEvaluationDetails .builder ().build ());
115
- hookSupport .executeAfterAllHooks (FlagEvaluationDetails .builder ().build ());
116
- hookSupport .executeErrorHooks (mock (Exception .class ));
121
+ private static void callAllHooks (HookSupportData hookSupportData ) {
122
+ hookSupport .executeBeforeHooks (hookSupportData );
123
+ hookSupport .executeAfterHooks (
124
+ hookSupportData , FlagEvaluationDetails .builder ().build ());
125
+ hookSupport .executeAfterAllHooks (
126
+ hookSupportData , FlagEvaluationDetails .builder ().build ());
127
+ hookSupport .executeErrorHooks (hookSupportData , mock (Exception .class ));
117
128
}
118
129
119
130
private static void assertHookData (TestHookWithData testHook , String ... expectedKeys ) {
0 commit comments