-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from ADORSYS-GIS/146-implement-transaction-hi…
…story-retrieval-in-das-e5 chore(): implement transaction history retrieval in OBS
- Loading branch information
Showing
9 changed files
with
260 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/TransServiceApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.adorsys.webank.obs.service; | ||
|
||
import com.adorsys.webank.obs.dto.TransRequest; | ||
|
||
public interface TransServiceApi { | ||
String getTrans(String accountID); | ||
String getTrans(TransRequest transRequest, String accountCertificateJwt); | ||
} |
68 changes: 64 additions & 4 deletions
68
obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/TransServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,74 @@ | ||
package com.adorsys.webank.obs.serviceimpl; | ||
|
||
import com.adorsys.webank.obs.dto.TransRequest; | ||
import com.adorsys.webank.obs.security.JwtCertValidator; | ||
import com.adorsys.webank.obs.service.TransServiceApi; | ||
import de.adorsys.ledgers.postings.api.service.LedgerService; | ||
import de.adorsys.webank.bank.api.domain.BankAccountBO; | ||
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.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class TransServiceImpl implements TransServiceApi { | ||
|
||
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(String accountID) { | ||
// Mock implementation: Replace with actual logic to retrieve trans details | ||
return "Trans details for ID: " + accountID; | ||
public String getTrans(TransRequest transRequest, String accountCertificateJwt) { | ||
try { | ||
//Validate the JWT certificate | ||
boolean isValid = jwtCertValidator.validateJWT(accountCertificateJwt); | ||
if (!isValid) { | ||
return "Invalid certificate or JWT. Transaction retrieval failed."; | ||
} | ||
|
||
// Extract the account ID from the request | ||
String accountId = transRequest.getAccountID(); | ||
|
||
// Fetch the account details | ||
BankAccountBO bankAccount = bankAccountService.getAccountById(accountId); | ||
if (bankAccount == null) { | ||
return "Bank account not found for ID: " + accountId; | ||
} | ||
|
||
// Define the date range for transactions (default to last month) | ||
LocalDateTime dateFrom = LocalDateTime.now().minusMonths(1); | ||
LocalDateTime dateTo = LocalDateTime.now(); | ||
|
||
|
||
// Fetch the transactions using the ledger service | ||
List<TransactionDetailsBO> postingLines = bankAccountService.getTransactionsByDates(accountId, dateFrom, dateTo); | ||
|
||
// If no transactions found | ||
if (postingLines.isEmpty()) { | ||
return "No transactions found for the given account and date range."; | ||
} | ||
|
||
// Map the posting lines to a string of transaction details | ||
List<String> transactionDetails = postingLines.stream() | ||
.map(postingLine -> "Transaction ID: " + postingLine.getTransactionId() + ", Informations: " + postingLine.getAdditionalInformation() + ", Amount: " + postingLine.getTransactionAmount().getAmount()) | ||
.collect(Collectors.toList()); | ||
|
||
return String.join(", ", transactionDetails); | ||
|
||
} catch (Exception e) { | ||
return "An error occurred while processing the request: " + e.getMessage(); | ||
} | ||
} | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
...s-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/TransServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.adorsys.webank.obs.serviceimpl; | ||
|
||
import com.adorsys.webank.obs.dto.TransRequest; | ||
import com.adorsys.webank.obs.security.JwtCertValidator; | ||
import de.adorsys.webank.bank.api.domain.AmountBO; | ||
import de.adorsys.webank.bank.api.domain.BankAccountBO; | ||
import de.adorsys.webank.bank.api.domain.TransactionDetailsBO; | ||
import de.adorsys.webank.bank.api.service.BankAccountService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.Currency; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TransServiceImplTest { | ||
|
||
@Mock | ||
private BankAccountService bankAccountService; | ||
|
||
@Mock | ||
private JwtCertValidator jwtCertValidator; | ||
|
||
@InjectMocks | ||
private TransServiceImpl transService; | ||
|
||
private TransRequest transRequest; | ||
private String accountCertificateJwt; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transRequest = new TransRequest(); | ||
transRequest.setAccountID("12345"); | ||
// For this test we assume the date range is defined internally as one month ago to now. | ||
accountCertificateJwt = "valid-jwt"; | ||
} | ||
|
||
@Test | ||
void testGetTrans_SuccessfulTransactionRetrieval() { | ||
// Arrange | ||
when(jwtCertValidator.validateJWT(accountCertificateJwt)).thenReturn(true); | ||
|
||
// Return a non-null BankAccountBO (details not important for this test) | ||
BankAccountBO bankAccount = new BankAccountBO(); | ||
when(bankAccountService.getAccountById("12345")).thenReturn(bankAccount); | ||
|
||
// Create a dummy TransactionDetailsBO with an AmountBO value | ||
TransactionDetailsBO transaction = new TransactionDetailsBO(); | ||
transaction.setTransactionId("txn-001"); | ||
// 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); | ||
|
||
List<TransactionDetailsBO> transactions = List.of(transaction); | ||
when(bankAccountService.getTransactionsByDates(anyString(), any(LocalDateTime.class), any(LocalDateTime.class))) | ||
.thenReturn(transactions); | ||
|
||
// Act | ||
String result = transService.getTrans(transRequest, accountCertificateJwt); | ||
|
||
// Assert | ||
String expected = "Transaction ID: txn-001, Informations: null, Amount: 100.5"; | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testGetTrans_InvalidJWT() { | ||
// Arrange | ||
when(jwtCertValidator.validateJWT(accountCertificateJwt)).thenReturn(false); | ||
|
||
// Act | ||
String result = transService.getTrans(transRequest, accountCertificateJwt); | ||
|
||
// Assert | ||
assertEquals("Invalid certificate or JWT. Transaction retrieval failed.", result); | ||
} | ||
|
||
@Test | ||
void testGetTrans_AccountNotFound() { | ||
// Arrange | ||
when(jwtCertValidator.validateJWT(accountCertificateJwt)).thenReturn(true); | ||
when(bankAccountService.getAccountById("12345")).thenReturn(null); | ||
|
||
// Act | ||
String result = transService.getTrans(transRequest, accountCertificateJwt); | ||
|
||
// Assert | ||
assertEquals("Bank account not found for ID: 12345", result); | ||
} | ||
|
||
@Test | ||
void testGetTrans_NoTransactionsFound() { | ||
// Arrange | ||
when(jwtCertValidator.validateJWT(accountCertificateJwt)).thenReturn(true); | ||
when(bankAccountService.getAccountById("12345")).thenReturn(new BankAccountBO()); | ||
when(bankAccountService.getTransactionsByDates(anyString(), any(LocalDateTime.class), any(LocalDateTime.class))) | ||
.thenReturn(Collections.emptyList()); | ||
|
||
// Act | ||
String result = transService.getTrans(transRequest, accountCertificateJwt); | ||
|
||
// Assert | ||
assertEquals("No transactions found for the given account and date range.", result); | ||
} | ||
|
||
@Test | ||
void testGetTrans_ExceptionHandling() { | ||
// Arrange | ||
when(jwtCertValidator.validateJWT(accountCertificateJwt)).thenReturn(true); | ||
when(bankAccountService.getAccountById("12345")).thenThrow(new RuntimeException("Database error")); | ||
|
||
// Act | ||
String result = transService.getTrans(transRequest, accountCertificateJwt); | ||
|
||
// Assert | ||
assertEquals("An error occurred while processing the request: Database error", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters