@@ -63,7 +63,7 @@ void shouldAlwaysCallGenericHook(FlagValueType flagValueType) {
6363 @ EnumSource (value = FlagValueType .class )
6464 @ DisplayName ("should allow hooks to store and retrieve data across stages" )
6565 void shouldPassDataAcrossStages (FlagValueType flagValueType ) {
66- var testHook = new HookDataHook ();
66+ var testHook = new TestHookWithData ();
6767 HookExecutor hookExecutor = HookExecutor .create (List .of (testHook ), getBaseHookContextForType (flagValueType ), ImmutableContext .EMPTY , Collections .emptyMap ());
6868
6969 hookExecutor .executeBeforeHooks ();
@@ -83,8 +83,8 @@ void shouldPassDataAcrossStages(FlagValueType flagValueType) {
8383 @ EnumSource (value = FlagValueType .class )
8484 @ DisplayName ("should isolate data between different hook instances" )
8585 void shouldIsolateDataBetweenHooks (FlagValueType flagValueType ) {
86- var testHook1 = new HookDataHook (1 );
87- var testHook2 = new HookDataHook (2 );
86+ var testHook1 = new TestHookWithData (1 );
87+ var testHook2 = new TestHookWithData (2 );
8888
8989 HookExecutor hookExecutor = HookExecutor .create (List .of (testHook1 , testHook2 ), getBaseHookContextForType (flagValueType ), ImmutableContext .EMPTY , Collections .emptyMap ());
9090
@@ -94,48 +94,22 @@ void shouldIsolateDataBetweenHooks(FlagValueType flagValueType) {
9494 assertHookData (testHook2 , 2 , "before" , "after" , "finallyAfter" , "error" );
9595 }
9696
97- @ ParameterizedTest
98- @ EnumSource (value = FlagValueType .class )
99- @ DisplayName ("should isolate data between the same hook executions" )
100- void shouldIsolateDataBetweenSameHookExecutions (FlagValueType flagValueType ) {
101- TestHookWithData testHook = new TestHookWithData ("test-key" , "value-1" );
102-
103- HookExecutor hookExecutor1 = HookExecutor .create (List .of (testHook ), getBaseHookContextForType (flagValueType ), ImmutableContext .EMPTY , Collections .emptyMap ());
104- HookExecutor hookExecutor2 = HookExecutor .create (List .of (testHook ), getBaseHookContextForType (flagValueType ), ImmutableContext .EMPTY , Collections .emptyMap ());
105-
106- // run hooks first time
107- callAllHooks (hookExecutor1 );
108- assertHookData (testHook , "value-1" );
109-
110- // re-run with different value, will throw if HookData contains already data
111- testHook .value = "value-2" ;
112- callAllHooks (hookExecutor2 );
113- assertHookData (testHook , "value-2" );
114- }
115-
11697 private static void callAllHooks (HookExecutor hookExecutor ) {
11798 hookExecutor .executeBeforeHooks ();
11899 hookExecutor .executeAfterHooks (FlagEvaluationDetails .builder ().build ());
119100 hookExecutor .executeAfterAllHooks (FlagEvaluationDetails .builder ().build ());
120101 hookExecutor .executeErrorHooks (mock (Exception .class ));
121102 }
122103
123- private static void assertHookData (TestHookWithData testHook1 , String expected ) {
124- assertThat (testHook1 .onBeforeValue ).isEqualTo (expected );
125- assertThat (testHook1 .onFinallyAfterValue ).isEqualTo (expected );
126- assertThat (testHook1 .onAfterValue ).isEqualTo (expected );
127- assertThat (testHook1 .onErrorValue ).isEqualTo (expected );
128- }
129-
130- private static void assertHookData (HookDataHook testHook , String ... expectedKeys ) {
104+ private static void assertHookData (TestHookWithData testHook , String ... expectedKeys ) {
131105 for (String expectedKey : expectedKeys ) {
132106 assertThat (testHook .hookData .get (expectedKey ))
133107 .withFailMessage ("Expected key %s not present in hook data" , expectedKey )
134108 .isNotNull ();
135109 }
136110 }
137111
138- private static void assertHookData (HookDataHook testHook , Object expectedValue , String ... expectedKeys ) {
112+ private static void assertHookData (TestHookWithData testHook , Object expectedValue , String ... expectedKeys ) {
139113 for (String expectedKey : expectedKeys ) {
140114 assertThat (testHook .hookData .get (expectedKey ))
141115 .withFailMessage ("Expected key '%s' not present in hook data" , expectedKey )
@@ -179,15 +153,15 @@ private EvaluationContext evaluationContextWithValue(String key, String value) {
179153 return new ImmutableContext (attributes );
180154 }
181155
182- private static class HookDataHook implements Hook {
156+ private static class TestHookWithData implements Hook {
183157 private final Object value ;
184158 HookData hookData = null ;
185159
186- public HookDataHook (Object value ) {
160+ public TestHookWithData (Object value ) {
187161 this .value = value ;
188162 }
189163
190- public HookDataHook () {
164+ public TestHookWithData () {
191165 this ("test" );
192166 }
193167
@@ -216,50 +190,4 @@ public void finallyAfter(HookContext ctx, FlagEvaluationDetails details, Map hin
216190 hookData = ctx .getHookData ();
217191 }
218192 }
219-
220- private class TestHookWithData implements Hook {
221-
222- private final String key ;
223- Object value ;
224-
225- Object onBeforeValue ;
226- Object onAfterValue ;
227- Object onErrorValue ;
228- Object onFinallyAfterValue ;
229-
230- TestHookWithData (Object value ) {
231- this ("test" , value );
232- }
233-
234- TestHookWithData (String key , Object value ) {
235- this .key = key ;
236- this .value = value ;
237- }
238-
239- @ Override
240- public Optional <EvaluationContext > before (HookContext ctx , Map hints ) {
241- var storedValue = ctx .getHookData ().get (key );
242- if (storedValue != null ) {
243- throw new Error ("Hook data isolation violated! Data is already set." );
244- }
245- ctx .getHookData ().set (key , value );
246- onBeforeValue = ctx .getHookData ().get (key );
247- return Optional .empty ();
248- }
249-
250- @ Override
251- public void after (HookContext ctx , FlagEvaluationDetails details , Map hints ) {
252- onAfterValue = ctx .getHookData ().get (key );
253- }
254-
255- @ Override
256- public void error (HookContext ctx , Exception error , Map hints ) {
257- onErrorValue = ctx .getHookData ().get (key );
258- }
259-
260- @ Override
261- public void finallyAfter (HookContext ctx , FlagEvaluationDetails details , Map hints ) {
262- onFinallyAfterValue = ctx .getHookData ().get (key );
263- }
264- }
265193}
0 commit comments