1919package org .apache .fineract .batch .service ;
2020
2121import static org .junit .jupiter .api .Assertions .assertEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertFalse ;
2223import static org .junit .jupiter .api .Assertions .assertTrue ;
2324import static org .mockito .ArgumentMatchers .any ;
2425import static org .mockito .ArgumentMatchers .anyString ;
26+ import static org .mockito .ArgumentMatchers .argThat ;
2527import static org .mockito .Mockito .doThrow ;
2628import static org .mockito .Mockito .mock ;
29+ import static org .mockito .Mockito .spy ;
2730import static org .mockito .Mockito .times ;
2831import static org .mockito .Mockito .verify ;
2932import static org .mockito .Mockito .when ;
5154import org .junit .jupiter .api .BeforeEach ;
5255import org .junit .jupiter .api .Test ;
5356import org .junit .jupiter .api .extension .ExtendWith ;
57+ import org .junit .jupiter .params .ParameterizedTest ;
58+ import org .junit .jupiter .params .provider .ValueSource ;
5459import org .mockito .InjectMocks ;
5560import org .mockito .Mock ;
5661import org .mockito .Mockito ;
@@ -86,8 +91,8 @@ class BatchApiServiceImplTest {
8691 @ InjectMocks
8792 private RetryConfigurationAssembler retryConfigurationAssembler ;
8893
89- private final ResolutionHelper resolutionHelper = Mockito . spy (new ResolutionHelper (new FromJsonHelper ()));
90- private final List <BatchRequestPreprocessor > batchPreprocessors = Mockito . spy (List .of ());
94+ private final ResolutionHelper resolutionHelper = spy (new ResolutionHelper (new FromJsonHelper ()));
95+ private final List <BatchRequestPreprocessor > batchPreprocessors = spy (List .of ());
9196
9297 @ InjectMocks
9398 private BatchApiServiceImpl batchApiService ;
@@ -96,8 +101,9 @@ class BatchApiServiceImplTest {
96101
97102 @ BeforeEach
98103 void setUp () {
99- batchApiService = new BatchApiServiceImpl (strategyProvider , resolutionHelper , transactionManager , errorHandler , List .of (),
100- batchPreprocessors , retryConfigurationAssembler );
104+ batchApiService = new BatchApiServiceImpl (strategyProvider , resolutionHelper , errorHandler , List .of (), batchPreprocessors ,
105+ retryConfigurationAssembler );
106+ batchApiService .setTransactionManager (transactionManager );
101107 batchApiService .setEntityManager (entityManager );
102108 request = new BatchRequest ();
103109 request .setRequestId (1L );
@@ -227,6 +233,43 @@ void testHandleBatchRequestsWithEnclosingTransactionReadOnly() {
227233 Mockito .verifyNoInteractions (entityManager );
228234 }
229235
236+ @ ParameterizedTest
237+ @ ValueSource (booleans = { true , false })
238+ void testCallInTransactionReadOnlyFlag (boolean isReadOnly ) {
239+ // Given
240+ ExtendedJpaTransactionManager extendedJpaTransactionManager = mock (ExtendedJpaTransactionManager .class );
241+
242+ // Create a transaction status with the correct read-only flag
243+ DefaultTransactionStatus transactionStatus = new DefaultTransactionStatus ("txn_name" , null , true , true , false , isReadOnly , false ,
244+ null );
245+
246+ // Mock getTransaction to return our status when the read-only flag matches
247+ when (extendedJpaTransactionManager .isReadOnlyConnection ()).thenReturn (isReadOnly );
248+ when (extendedJpaTransactionManager
249+ .getTransaction (argThat (definition -> definition != null && definition .isReadOnly () == isReadOnly )))
250+ .thenReturn (transactionStatus );
251+
252+ // Mock other required dependencies
253+ when (strategyProvider .getCommandStrategy (any ())).thenReturn (commandStrategy );
254+ when (commandStrategy .execute (any (), any ())).thenReturn (response );
255+
256+ batchApiService .setTransactionManager (extendedJpaTransactionManager );
257+
258+ // Set up a request that will trigger the read-only behavior we want to test
259+ BatchRequest testRequest = new BatchRequest ();
260+ testRequest .setRequestId (1L );
261+ testRequest .setMethod (isReadOnly ? "GET" : "POST" ); // Use GET for read-only, POST for read-write
262+ testRequest .setRelativeUrl ("/test/endpoint" );
263+
264+ // When
265+ List <BatchResponse > responses = batchApiService .handleBatchRequestsWithEnclosingTransaction (List .of (testRequest ), uriInfo );
266+
267+ // Then
268+ assertFalse (responses .isEmpty ());
269+ verify (extendedJpaTransactionManager )
270+ .getTransaction (argThat (definition -> definition != null && definition .isReadOnly () == isReadOnly ));
271+ }
272+
230273 private static final class RetryException extends RuntimeException {}
231274
232275}
0 commit comments