2020
2121import static org .apache .fineract .integrationtests .common .savings .SavingsAccountHelper .PAYMENT_TYPE_ID ;
2222import static org .apache .fineract .integrationtests .common .system .DatatableHelper .addDatatableColumn ;
23+ import static org .apache .http .HttpStatus .SC_CONFLICT ;
2324import static org .apache .http .HttpStatus .SC_FORBIDDEN ;
24- import static org .apache .http .HttpStatus .SC_LOCKED ;
2525import static org .apache .http .HttpStatus .SC_OK ;
2626import static org .hamcrest .Matchers .anyOf ;
2727import static org .hamcrest .Matchers .is ;
3131import static org .junit .jupiter .api .Assertions .assertTrue ;
3232
3333import com .fasterxml .jackson .core .JsonProcessingException ;
34+ import com .google .common .base .Strings ;
3435import com .google .gson .Gson ;
3536import io .restassured .builder .RequestSpecBuilder ;
3637import io .restassured .builder .ResponseSpecBuilder ;
@@ -85,6 +86,7 @@ public class SavingsAccountTransactionTest {
8586
8687 private ResponseSpecification responseSpec ;
8788 private ResponseSpecification concurrentResponseSpec ;
89+ private ResponseSpecification deadlockResponseSpec ;
8890 private RequestSpecification requestSpec ;
8991 private SavingsProductHelper savingsProductHelper ;
9092 private SavingsAccountHelper savingsAccountHelper ;
@@ -96,7 +98,8 @@ public void setup() {
9698 this .requestSpec = new RequestSpecBuilder ().setContentType (ContentType .JSON ).build ();
9799 this .requestSpec .header ("Authorization" , "Basic " + Utils .loginIntoServerAndGetBase64EncodedAuthenticationKey ());
98100 this .responseSpec = new ResponseSpecBuilder ().expectStatusCode (SC_OK ).build ();
99- this .concurrentResponseSpec = new ResponseSpecBuilder ().expectStatusCode (anyOf (is (SC_OK ), is (SC_LOCKED ))).build ();
101+ this .concurrentResponseSpec = new ResponseSpecBuilder ().expectStatusCode (anyOf (is (SC_OK ), is (SC_CONFLICT ))).build ();
102+ this .deadlockResponseSpec = new ResponseSpecBuilder ().expectStatusCode (anyOf (is (SC_OK ), is (SC_CONFLICT ), is (SC_FORBIDDEN ))).build ();
100103 this .savingsAccountHelper = new SavingsAccountHelper (this .requestSpec , this .responseSpec );
101104 this .savingsProductHelper = new SavingsProductHelper ();
102105 this .datatableHelper = new DatatableHelper (this .requestSpec , this .responseSpec );
@@ -190,7 +193,7 @@ public void testConcurrentSavingsBatchTransactions() {
190193
191194 SavingsAccountHelper batchWithTransactionHelper = new SavingsAccountHelper (requestSpec , concurrentResponseSpec );
192195 SavingsAccountHelper batchWithoutTransactionHelper = new SavingsAccountHelper (requestSpec ,
193- new ResponseSpecBuilder ().expectStatusCode (anyOf (is (SC_OK ), is (SC_LOCKED ), is (SC_FORBIDDEN ))).build ());
196+ new ResponseSpecBuilder ().expectStatusCode (anyOf (is (SC_OK ), is (SC_CONFLICT ), is (SC_FORBIDDEN ))).build ());
194197 String transactionDate = SavingsAccountHelper .TRANSACTION_DATE ;
195198 String transactionAmount = "10" ;
196199 ExecutorService executor = Executors .newFixedThreadPool (30 );
@@ -223,7 +226,7 @@ public void testConcurrentSavingsBatchTransactions() {
223226 log .info ("\n Finished all threads" );
224227 }
225228
226- // @Test
229+ @ Test
227230 public void testDeadlockSavingsBatchTransactions () {
228231 final Integer clientID = ClientHelper .createClient (requestSpec , responseSpec );
229232 ClientHelper .verifyClientCreatedOnServer (requestSpec , responseSpec , clientID );
@@ -239,7 +242,7 @@ public void testDeadlockSavingsBatchTransactions() {
239242 savingsAccountHelper .approveSavings (savingsId2 );
240243 savingsAccountHelper .activateSavings (savingsId2 );
241244
242- SavingsAccountHelper batchWithTransactionHelper = new SavingsAccountHelper (requestSpec , concurrentResponseSpec );
245+ SavingsAccountHelper batchWithTransactionHelper = new SavingsAccountHelper (requestSpec , deadlockResponseSpec );
243246 String transactionDate = SavingsAccountHelper .TRANSACTION_DATE ;
244247 String transactionAmount = "10" ;
245248
@@ -293,11 +296,12 @@ private void performSavingsTransaction(Integer savingsId, String amount, LocalDa
293296
294297 assertEquals (transactionId , (Integer ) transaction .get ("id" ), "Check Savings " + transactionType + " Transaction" );
295298 LocalDate transactionDateFromResponse = extractLocalDate (transaction , "date" );
296- assertTrue (DateUtils .isEqual (transactionDate , transactionDateFromResponse ),
297- "Transaction Date check for Savings " + transactionType + " Transaction" );
298- LocalDate submittedOnDate = extractLocalDate (transaction , "submittedOnDate" );
299- assertTrue (DateUtils .isEqual (submittedOnDate , Utils .getLocalDateOfTenant ()),
300- "Submitted On Date check for Savings " + transactionType + " Transaction" );
299+ assertTrue (DateUtils .isEqual (transactionDate , transactionDateFromResponse ), "Transaction Date check for Savings " + transactionType
300+ + " Transaction. Expected: " + transactionDate + ", current: " + transactionDateFromResponse );
301+ LocalDate submittedOnDate = Utils .getLocalDateOfTenant ();
302+ LocalDate submittedOnDateFromResponse = extractLocalDate (transaction , "submittedOnDate" );
303+ assertTrue (DateUtils .isEqual (submittedOnDate , submittedOnDateFromResponse ), "Submitted On Date check for Savings " + transactionType
304+ + " Transaction. Expected: " + submittedOnDate + ", current: " + submittedOnDateFromResponse );
301305 }
302306
303307 private LocalDate extractLocalDate (HashMap transactionMap , String fieldName ) {
@@ -382,7 +386,7 @@ public void run() {
382386 if (enclosingTransaction ) {
383387 Integer statusCode1 = responses .get (0 ).getStatusCode ();
384388 assertNotNull (statusCode1 );
385- assertTrue (SC_OK == statusCode1 || SC_LOCKED == statusCode1 );
389+ assertTrue (SC_OK == statusCode1 || SC_CONFLICT == statusCode1 , "Status code: " + statusCode1 );
386390 if (SC_OK == statusCode1 ) {
387391 assertEquals (4 , responses .size ());
388392 Integer statusCode4 = responses .get (3 ).getStatusCode ();
@@ -395,11 +399,11 @@ public void run() {
395399 assertEquals (4 , responses .size ());
396400 Integer statusCode1 = responses .get (0 ).getStatusCode ();
397401 assertNotNull (statusCode1 );
398- assertTrue (SC_OK == statusCode1 || SC_LOCKED == statusCode1 );
402+ assertTrue (SC_OK == statusCode1 || SC_CONFLICT == statusCode1 , "Status code: " + statusCode1 );
399403 Integer statusCode4 = responses .get (3 ).getStatusCode ();
400404 assertNotNull (statusCode4 );
401- assertTrue (SC_OK == statusCode1 ? (SC_OK == statusCode4 || SC_LOCKED == statusCode4 )
402- : (SC_FORBIDDEN == statusCode4 || SC_LOCKED == statusCode4 ));
405+ assertTrue (SC_OK == statusCode1 ? (SC_OK == statusCode4 || SC_CONFLICT == statusCode4 )
406+ : (SC_FORBIDDEN == statusCode4 || SC_CONFLICT == statusCode4 ), "Status code: " + statusCode4 );
403407 }
404408 } else {
405409 String json = transactionData .getJson ();
@@ -415,12 +419,12 @@ public void run() {
415419 private static boolean checkConcurrentResponse (String response ) {
416420 assertNotNull (response );
417421 JsonPath res = JsonPath .from (response );
418- String statusCode = ( String ) res .get ("httpStatusCode" );
422+ String statusCode = res .get ("httpStatusCode" );
419423 if (statusCode == null ) {
420424 assertNotNull (res .get (CommonConstants .RESPONSE_RESOURCE_ID ));
421425 return true ;
422426 }
423- assertEquals (String .valueOf (SC_LOCKED ), statusCode );
427+ assertEquals (String .valueOf (SC_CONFLICT ), statusCode );
424428 return false ;
425429 }
426430 }
@@ -436,9 +440,14 @@ private void runDeadlockBatch(SavingsAccountHelper savingsHelper, Integer saving
436440 ResponseSpecification responseSpec = savingsHelper .getResponseSpec ();
437441 final List <BatchResponse > responses = BatchHelper .postBatchRequestsWithEnclosingTransaction (requestSpec , responseSpec , json );
438442 assertNotNull (responses );
439- Integer statusCode = responses .get (0 ).getStatusCode ();
443+ BatchResponse response1 = responses .get (0 );
444+ Integer statusCode = response1 .getStatusCode ();
445+ String msg = Strings .nullToEmpty (response1 .getBody ());
440446 assertNotNull (statusCode );
441- assertTrue (SC_OK == statusCode || SC_LOCKED == statusCode );
447+ assertTrue (
448+ SC_OK == statusCode || SC_CONFLICT == statusCode
449+ || (SC_FORBIDDEN == statusCode && msg .contains ("Cannot add or update a child row" )),
450+ "Status code: " + statusCode + ", message: " + msg );
442451 if (SC_OK == statusCode ) {
443452 assertEquals (4 , responses .size ());
444453 Integer statusCode4 = responses .get (3 ).getStatusCode ();
0 commit comments