Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(): implemented transaction mock for particular account id #149

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,6 +23,7 @@
@Service
public class ObsServiceImpl implements RegistrationServiceApi {

private static final Logger log = LoggerFactory.getLogger(ObsServiceImpl.class);
@Autowired
private BankAccountCertificateCreationService bankAccountCertificateCreationService;

Expand All @@ -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;
}


Expand Down Expand Up @@ -78,15 +89,65 @@ 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 + " and deposit amount: " + deposit);

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.
* <p>
* 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) {
return "An error occurred while processing the transactions: "
+ (e.getMessage() != null ? e.getMessage() : e.toString());
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
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.";
Expand Down Expand Up @@ -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<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TransactionDetailsBO> transactions = List.of(transaction);
when(bankAccountService.getTransactionsByDates(anyString(), any(LocalDateTime.class), any(LocalDateTime.class)))
Expand All @@ -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
Expand Down