1919import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemErrAndOutNormalized ;
2020import static org .assertj .core .api .Assertions .assertThat ;
2121import static org .assertj .core .api .Assertions .assertThatThrownBy ;
22- import static org .assertj .core .api .Assertions .catchThrowableOfType ;
22+ import static org .assertj .core .api .Assertions .catchThrowable ;
2323import static org .assertj .core .api .Assertions .entry ;
2424import static org .mockito .Mockito .doThrow ;
2525import static org .mockito .Mockito .inOrder ;
2929import static org .mockito .Mockito .verifyNoInteractions ;
3030import static org .mockito .Mockito .when ;
3131
32+ import java .util .Map ;
33+
3234import org .apache .pulsar .client .admin .PulsarAdmin ;
3335import org .apache .pulsar .client .admin .PulsarAdminException ;
3436import org .apache .pulsar .client .api .PulsarClientException ;
37+ import org .assertj .core .api .InstanceOfAssertFactories ;
3538import org .junit .jupiter .api .BeforeEach ;
3639import org .junit .jupiter .api .Nested ;
3740import org .junit .jupiter .api .Test ;
@@ -92,6 +95,15 @@ void setupSharedMocks() throws PulsarClientException, PulsarAdminException {
9295 when (source1 .type ()).thenReturn (FunctionType .SOURCE );
9396 }
9497
98+ @ SafeVarargs
99+ @ SuppressWarnings ("varargs" )
100+ private void assertThatPulsarFunctionExceptionFailedWith (Throwable thrown ,
101+ Map .Entry <? extends PulsarFunctionOperations <?>, ? extends Exception >... expectedFunctionAndErrors ) {
102+ assertThat (thrown ).isInstanceOf (PulsarFunctionException .class )
103+ .extracting ("failures" , InstanceOfAssertFactories .MAP )
104+ .containsExactly (expectedFunctionAndErrors );
105+ }
106+
95107 @ Nested
96108 class ProperCreateUpdateApiCalled {
97109
@@ -228,22 +240,21 @@ class WithFailFast {
228240 void firstProcessedFunctionFails () throws PulsarAdminException {
229241 var ex = new PulsarAdminException ("BOOM" );
230242 when (function1 .functionExists (pulsarAdmin )).thenThrow (ex );
231- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
232- PulsarFunctionException .class );
233- assertThat (thrown .getFailures ()).containsExactly (entry (function1 , ex ));
243+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
244+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (function1 , ex ));
234245 verify (function1 , never ()).create (pulsarAdmin );
235246 verify (function1 , never ()).update (pulsarAdmin );
236247 verifyNoInteractions (sink1 , source1 );
237248 assertThat (functionAdmin .getProcessedFunctions ()).isEmpty ();
238249 }
239250
251+ // PulsarFunctionOperations<?>, Exception
240252 @ Test
241253 void middleProcessedFunctionFails () throws PulsarAdminException {
242254 var ex = new PulsarAdminException ("BOOM" );
243255 when (sink1 .functionExists (pulsarAdmin )).thenThrow (ex );
244- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
245- PulsarFunctionException .class );
246- assertThat (thrown .getFailures ()).containsExactly (entry (sink1 , ex ));
256+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
257+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (sink1 , ex ));
247258 verify (function1 ).create (pulsarAdmin );
248259 verify (sink1 , never ()).create (pulsarAdmin );
249260 verify (sink1 , never ()).update (pulsarAdmin );
@@ -255,9 +266,8 @@ void middleProcessedFunctionFails() throws PulsarAdminException {
255266 void lastProcessedFunctionFails () throws PulsarAdminException {
256267 var ex = new PulsarAdminException ("BOOM" );
257268 when (source1 .functionExists (pulsarAdmin )).thenThrow (ex );
258- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
259- PulsarFunctionException .class );
260- assertThat (thrown .getFailures ()).containsExactly (entry (source1 , ex ));
269+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
270+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (source1 , ex ));
261271 verify (function1 ).create (pulsarAdmin );
262272 verify (sink1 ).create (pulsarAdmin );
263273 verify (source1 , never ()).create (pulsarAdmin );
@@ -282,9 +292,8 @@ void disableFailFastOnFunctionAdmin() {
282292 void firstProcessedFunctionFails () throws PulsarAdminException {
283293 var ex = new PulsarAdminException ("BOOM" );
284294 when (function1 .functionExists (pulsarAdmin )).thenThrow (ex );
285- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
286- PulsarFunctionException .class );
287- assertThat (thrown .getFailures ()).containsExactly (entry (function1 , ex ));
295+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
296+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (function1 , ex ));
288297 verify (function1 , never ()).create (pulsarAdmin );
289298 verify (function1 , never ()).update (pulsarAdmin );
290299 verify (sink1 ).create (pulsarAdmin );
@@ -296,9 +305,8 @@ void firstProcessedFunctionFails() throws PulsarAdminException {
296305 void middleProcessedFunctionFails () throws PulsarAdminException {
297306 var ex = new PulsarAdminException ("BOOM" );
298307 when (sink1 .functionExists (pulsarAdmin )).thenThrow (ex );
299- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
300- PulsarFunctionException .class );
301- assertThat (thrown .getFailures ()).containsExactly (entry (sink1 , ex ));
308+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
309+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (sink1 , ex ));
302310 verify (function1 ).create (pulsarAdmin );
303311 verify (sink1 , never ()).create (pulsarAdmin );
304312 verify (sink1 , never ()).update (pulsarAdmin );
@@ -310,9 +318,8 @@ void middleProcessedFunctionFails() throws PulsarAdminException {
310318 void lastProcessedFunctionFails () throws PulsarAdminException {
311319 var ex = new PulsarAdminException ("BOOM" );
312320 when (source1 .functionExists (pulsarAdmin )).thenThrow (ex );
313- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
314- PulsarFunctionException .class );
315- assertThat (thrown .getFailures ()).containsExactly (entry (source1 , ex ));
321+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
322+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (source1 , ex ));
316323 verify (function1 ).create (pulsarAdmin );
317324 verify (sink1 ).create (pulsarAdmin );
318325 verify (source1 , never ()).create (pulsarAdmin );
@@ -328,9 +335,8 @@ void allProcessedFunctionsFail() throws PulsarAdminException {
328335 when (function1 .functionExists (pulsarAdmin )).thenThrow (ex1 );
329336 when (sink1 .functionExists (pulsarAdmin )).thenThrow (ex2 );
330337 when (source1 .functionExists (pulsarAdmin )).thenThrow (ex3 );
331- var thrown = catchThrowableOfType (() -> functionAdmin .createOrUpdateUserDefinedFunctions (),
332- PulsarFunctionException .class );
333- assertThat (thrown .getFailures ()).containsExactly (entry (function1 , ex1 ), entry (sink1 , ex2 ),
338+ var thrown = catchThrowable (() -> functionAdmin .createOrUpdateUserDefinedFunctions ());
339+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (function1 , ex1 ), entry (sink1 , ex2 ),
334340 entry (source1 , ex3 ));
335341 verify (function1 , never ()).create (pulsarAdmin );
336342 verify (function1 , never ()).update (pulsarAdmin );
@@ -474,9 +480,8 @@ void createAdminClientFails() throws PulsarClientException {
474480 void firstProcessedFunctionFails () {
475481 var ex = new PulsarException ("BOOM" );
476482 doThrow (ex ).when (source1 ).stop (pulsarAdmin );
477- var thrown = catchThrowableOfType (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions (),
478- PulsarFunctionException .class );
479- assertThat (thrown .getFailures ()).containsExactly (entry (source1 , ex ));
483+ var thrown = catchThrowable (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions ());
484+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (source1 , ex ));
480485 verify (sink1 ).stop (pulsarAdmin );
481486 verify (function1 ).stop (pulsarAdmin );
482487 }
@@ -485,9 +490,8 @@ void firstProcessedFunctionFails() {
485490 void middleProcessedFunctionFails () {
486491 var ex = new PulsarException ("BOOM" );
487492 doThrow (ex ).when (sink1 ).stop (pulsarAdmin );
488- var thrown = catchThrowableOfType (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions (),
489- PulsarFunctionException .class );
490- assertThat (thrown .getFailures ()).containsExactly (entry (sink1 , ex ));
493+ var thrown = catchThrowable (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions ());
494+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (sink1 , ex ));
491495 verify (source1 ).stop (pulsarAdmin );
492496 verify (function1 ).stop (pulsarAdmin );
493497 }
@@ -496,9 +500,8 @@ void middleProcessedFunctionFails() {
496500 void lastProcessedFunctionFails () {
497501 var ex = new PulsarException ("BOOM" );
498502 doThrow (ex ).when (function1 ).stop (pulsarAdmin );
499- var thrown = catchThrowableOfType (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions (),
500- PulsarFunctionException .class );
501- assertThat (thrown .getFailures ()).containsExactly (entry (function1 , ex ));
503+ var thrown = catchThrowable (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions ());
504+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (function1 , ex ));
502505 verify (source1 ).stop (pulsarAdmin );
503506 verify (sink1 ).stop (pulsarAdmin );
504507 }
@@ -511,9 +514,8 @@ void allProcessedFunctionsFail() {
511514 doThrow (ex1 ).when (source1 ).stop (pulsarAdmin );
512515 doThrow (ex2 ).when (sink1 ).stop (pulsarAdmin );
513516 doThrow (ex3 ).when (function1 ).stop (pulsarAdmin );
514- var thrown = catchThrowableOfType (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions (),
515- PulsarFunctionException .class );
516- assertThat (thrown .getFailures ()).containsExactly (entry (source1 , ex1 ), entry (sink1 , ex2 ),
517+ var thrown = catchThrowable (() -> functionAdmin .enforceStopPolicyOnUserDefinedFunctions ());
518+ assertThatPulsarFunctionExceptionFailedWith (thrown , entry (source1 , ex1 ), entry (sink1 , ex2 ),
517519 entry (function1 , ex3 ));
518520 }
519521
0 commit comments