From e097c088e259f4ff1c4de241e36fb2aa5b3ececc Mon Sep 17 00:00:00 2001 From: arielpetit Date: Fri, 7 Feb 2025 14:13:38 +0100 Subject: [PATCH 1/2] chore(): implemented transaction mock for particular account id --- .../obs/serviceimpl/ObsServiceImpl.java | 74 +++++++++++++++++-- .../obs/serviceimpl/TransServiceImpl.java | 25 +++++-- .../obs/serviceimpl/TransServiceImplTest.java | 12 ++- 3 files changed, 98 insertions(+), 13 deletions(-) diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java index 4b4a01c..b7a9fac 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java @@ -1,13 +1,18 @@ package com.adorsys.webank.obs.serviceimpl; import com.adorsys.webank.obs.dto.RegistrationRequest; +import com.adorsys.webank.obs.dto.TransRequest; import com.adorsys.webank.obs.security.JwtCertValidator; import com.adorsys.webank.obs.service.RegistrationServiceApi; +import de.adorsys.webank.bank.api.domain.AmountBO; +import de.adorsys.webank.bank.api.service.BankAccountTransactionService; import de.adorsys.webank.bank.api.service.util.BankAccountCertificateCreationService; import de.adorsys.webank.bank.api.domain.AccountTypeBO; import de.adorsys.webank.bank.api.domain.AccountUsageBO; import de.adorsys.webank.bank.api.domain.BankAccountBO; import de.adorsys.webank.bank.api.service.BankAccountService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -18,6 +23,7 @@ @Service public class ObsServiceImpl implements RegistrationServiceApi { + private static final Logger log = LoggerFactory.getLogger(ObsServiceImpl.class); @Autowired private BankAccountCertificateCreationService bankAccountCertificateCreationService; @@ -27,8 +33,13 @@ public class ObsServiceImpl implements RegistrationServiceApi { @Autowired private final JwtCertValidator jwtCertValidator; - public ObsServiceImpl(JwtCertValidator jwtCertValidator) { + @Autowired + private final BankAccountTransactionService bankAccountTransactionService; + + + public ObsServiceImpl(JwtCertValidator jwtCertValidator, BankAccountTransactionService bankAccountTransactionService) { this.jwtCertValidator = jwtCertValidator; + this.bankAccountTransactionService = bankAccountTransactionService; } @@ -78,15 +89,66 @@ public String registerAccount(RegistrationRequest registrationRequest, String ph // Call the service to create the account String createdAccountResult = bankAccountCertificateCreationService.registerNewBankAccount(registrationRequest.getPhoneNumber(), registrationRequest.getPublicKey(), bankAccountBO, UUID.randomUUID().toString(), "OBS"); - if (createdAccountResult != null) { - return "Bank account successfully created. Details: " + createdAccountResult; - } else { - return "Error creating account for phone number: " + registrationRequest.getPhoneNumber(); - } + // Split the string by newlines + String[] lines = createdAccountResult.split("\n"); + + // Access the account ID, which is in the third line (index 2) + String accountId = lines[2]; + + String deposit = makeTrans(accountId); + log.info("Created account with id: " + accountId); + + return "Bank account successfully created. Details: " + createdAccountResult; } catch (Exception e) { return "An error occurred while processing the request: " + e.getMessage(); } } + /** + * Makes a transaction (in this case, a deposit) into a particular account. + *

+ * The method: + * 1. Validates the provided JWT. + * 2. Retrieves the bank account using the account ID from the request. + * 3. Extracts the deposit details (amount, currency, record user) from the request. + * 4. Creates an AmountBO instance representing the deposit. + * 5. Calls the depositCash method on the BankAccountService. + */ + public String makeTrans(String accountId) { + try { + + // Fetch the account details. + BankAccountBO bankAccount = bankAccountService.getAccountById(accountId); + if (bankAccount == null) { + return "Bank account not found for ID: " + accountId; + } + + // Define multiple deposit values. + BigDecimal[] depositValues = { + new BigDecimal("1000.00"), + new BigDecimal("500.50"), + new BigDecimal("500.75"), + new BigDecimal("3000.00"), + new BigDecimal("1500.25") + }; + + Currency currency = Currency.getInstance("XAF"); + String recordUser = "Default name"; + + // Process each transaction. + for (BigDecimal depositValue : depositValues) { + AmountBO depositAmount = new AmountBO(currency, depositValue); + bankAccountTransactionService.depositCash(accountId, depositAmount, recordUser); + } + + return "5 transactions completed successfully for account " + accountId; + + } catch (Exception e) { + e.printStackTrace(); + return "An error occurred while processing the transactions: " + + (e.getMessage() != null ? e.getMessage() : e.toString()); + } + } + } diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java index 74bf565..6060164 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java @@ -8,6 +8,8 @@ import de.adorsys.webank.bank.api.domain.TransactionDetailsBO; import de.adorsys.webank.bank.api.service.BankAccountService; import de.adorsys.webank.bank.api.service.BankAccountTransactionService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -18,20 +20,22 @@ @Service public class TransServiceImpl implements TransServiceApi { + private static final Logger log = LoggerFactory.getLogger(TransServiceImpl.class); private final BankAccountService bankAccountService; private final JwtCertValidator jwtCertValidator; - @Autowired - public TransServiceImpl(BankAccountService bankAccountService, JwtCertValidator jwtCertValidator) { + public TransServiceImpl(BankAccountService bankAccountService, JwtCertValidator jwtCertValidator, BankAccountTransactionService bankAccountTransactionService, LedgerService ledgerService) { this.bankAccountService = bankAccountService; this.jwtCertValidator = jwtCertValidator; } + + @Override public String getTrans(TransRequest transRequest, String accountCertificateJwt) { try { - //Validate the JWT certificate + // Validate the JWT certificate boolean isValid = jwtCertValidator.validateJWT(accountCertificateJwt); if (!isValid) { return "Invalid certificate or JWT. Transaction retrieval failed."; @@ -59,12 +63,21 @@ public String getTrans(TransRequest transRequest, String accountCertificateJwt) return "No transactions found for the given account and date range."; } - // Map the posting lines to a string of transaction details + // Map the posting lines to a properly formatted JSON string List transactionDetails = postingLines.stream() - .map(postingLine -> "Transaction ID: " + postingLine.getTransactionId() + ", Informations: " + postingLine.getAdditionalInformation() + ", Amount: " + postingLine.getTransactionAmount().getAmount()) + .map(postingLine -> "{\n" + + " \"id\": \"" + postingLine.getTransactionId() + "\",\n" + + " \"date\": \"" + postingLine.getBookingDate().toString() + "\",\n" + + " \"amount\": \"" + postingLine.getTransactionAmount().getAmount() + "\",\n" + + " \"title\": \"" + "Deposit" + "\"\n" + + "}") .collect(Collectors.toList()); - return String.join(", ", transactionDetails); + log.info("Transaction details: " + transactionDetails.toString()); + + return "[\n" + String.join(",\n", transactionDetails) + "\n]"; + + } catch (Exception e) { return "An error occurred while processing the request: " + e.getMessage(); diff --git a/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/TransServiceImplTest.java b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/TransServiceImplTest.java index 2f095f4..d8d4337 100644 --- a/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/TransServiceImplTest.java +++ b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/TransServiceImplTest.java @@ -14,6 +14,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.math.BigDecimal; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Collections; import java.util.Currency; @@ -62,6 +63,7 @@ void testGetTrans_SuccessfulTransactionRetrieval() { // Create an AmountBO instance with EUR and an amount of 100.50 AmountBO amount = new AmountBO(Currency.getInstance("EUR"), BigDecimal.valueOf(100.50)); transaction.setTransactionAmount(amount); + transaction.setBookingDate(LocalDate.from(LocalDateTime.now())); // Make sure to set the booking date List transactions = List.of(transaction); when(bankAccountService.getTransactionsByDates(anyString(), any(LocalDateTime.class), any(LocalDateTime.class))) @@ -71,10 +73,18 @@ void testGetTrans_SuccessfulTransactionRetrieval() { String result = transService.getTrans(transRequest, accountCertificateJwt); // Assert - String expected = "Transaction ID: txn-001, Informations: null, Amount: 100.5"; + String expected = "[\n" + + "{\n" + + " \"id\": \"txn-001\",\n" + + " \"date\": \"" + transaction.getBookingDate().toString() + "\",\n" + + " \"amount\": \"" + transaction.getTransactionAmount().getAmount() + "\",\n" + + " \"title\": \"Deposit\"\n" + + "}\n" + + "]"; assertEquals(expected, result); } + @Test void testGetTrans_InvalidJWT() { // Arrange From b266a26695811e0dc4c5b0eb623f522b20505229 Mon Sep 17 00:00:00 2001 From: arielpetit Date: Fri, 7 Feb 2025 14:20:48 +0100 Subject: [PATCH 2/2] Fix() fixed pmd checks --- .../com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java | 3 +-- .../com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java index b7a9fac..264f8eb 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java @@ -96,7 +96,7 @@ public String registerAccount(RegistrationRequest registrationRequest, String ph String accountId = lines[2]; String deposit = makeTrans(accountId); - log.info("Created account with id: " + accountId); + log.info("Created account with id: " + accountId + " and deposit amount: " + deposit); return "Bank account successfully created. Details: " + createdAccountResult; } catch (Exception e) { @@ -144,7 +144,6 @@ public String makeTrans(String accountId) { return "5 transactions completed successfully for account " + accountId; } catch (Exception e) { - e.printStackTrace(); return "An error occurred while processing the transactions: " + (e.getMessage() != null ? e.getMessage() : e.toString()); } diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java index 6060164..12e2cf9 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java @@ -25,7 +25,7 @@ public class TransServiceImpl implements TransServiceApi { private final JwtCertValidator jwtCertValidator; @Autowired - public TransServiceImpl(BankAccountService bankAccountService, JwtCertValidator jwtCertValidator, BankAccountTransactionService bankAccountTransactionService, LedgerService ledgerService) { + public TransServiceImpl(BankAccountService bankAccountService, JwtCertValidator jwtCertValidator) { this.bankAccountService = bankAccountService; this.jwtCertValidator = jwtCertValidator; }