Commit 89f94d34 authored by Shouib's avatar Shouib

feat: complete foundational infrastructure

parent dd9f3c65
node_modules/
dist/
build/
target/
*.class
*.jar
.gradle/
build/
.idea/
*.log
.env*
data/
.DS_Store
Thumbs.db
*.tmp
*.swp
.vscode/
.idea/
package com.banking.models;
import java.math.BigDecimal;
public record Account(
String accountId,
BigDecimal balance,
long lastProcessedSeqKey
) {}
package com.banking.models;
import java.math.BigDecimal;
public record RequestDTO(
String accountId,
BigDecimal amount,
long sequenceKey,
boolean isRetry
) {}
package com.banking.models;
import java.math.BigDecimal;
public record ResponseDTO(
String status,
BigDecimal balance,
String message
) {}
package com.banking.models;
import java.math.BigDecimal;
import java.time.Instant;
public record Transaction(
Long id,
String accountId,
long sequenceKey,
BigDecimal amount,
String outcome,
BigDecimal balanceAfter,
String responseMessage,
Instant timestamp
) {}
package com.banking.persistence;
import com.banking.models.Account;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Optional;
public class AccountRepository {
private final Connection connection;
public AccountRepository(Connection connection) {
this.connection = connection;
}
public void createAccount(String accountId, BigDecimal initialBalance) throws SQLException {
String sql = "INSERT INTO ACCOUNT (account_id, balance, last_processed_seq_key) VALUES (?, ?, 0)";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, accountId);
pstmt.setBigDecimal(2, initialBalance);
pstmt.executeUpdate();
}
}
public Optional<Account> findById(String accountId) throws SQLException {
String sql = "SELECT account_id, balance, last_processed_seq_key FROM ACCOUNT WHERE account_id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, accountId);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return Optional.of(new Account(
rs.getString("account_id"),
rs.getBigDecimal("balance"),
rs.getLong("last_processed_seq_key")
));
}
}
}
return Optional.empty();
}
public Optional<Account> findByIdForUpdate(String accountId) throws SQLException {
String sql = "SELECT account_id, balance, last_processed_seq_key FROM ACCOUNT WHERE account_id = ? FOR UPDATE";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, accountId);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return Optional.of(new Account(
rs.getString("account_id"),
rs.getBigDecimal("balance"),
rs.getLong("last_processed_seq_key")
));
}
}
}
return Optional.empty();
}
public void updateBalance(String accountId, BigDecimal newBalance) throws SQLException {
String sql = "UPDATE ACCOUNT SET balance = ? WHERE account_id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setBigDecimal(1, newBalance);
pstmt.setString(2, accountId);
pstmt.executeUpdate();
}
}
public void updateSeqKey(String accountId, long newSeqKey) throws SQLException {
String sql = "UPDATE ACCOUNT SET last_processed_seq_key = ? WHERE account_id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setLong(1, newSeqKey);
pstmt.setString(2, accountId);
pstmt.executeUpdate();
}
}
}
package com.banking.persistence;
import com.banking.models.Transaction;
import java.sql.*;
import java.util.Optional;
import java.math.BigDecimal;
import java.time.Instant;
public class TransactionLedgerRepository {
private final Connection connection;
public TransactionLedgerRepository(Connection connection) {
this.connection = connection;
}
public Optional<Transaction> findByAccountAndSeqKey(String accountId, long sequenceKey) throws SQLException {
String sql = "SELECT * FROM transaction_ledger WHERE account_id = ? AND sequence_key = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, accountId);
pstmt.setLong(2, sequenceKey);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return Optional.of(mapRowToTransaction(rs));
}
}
}
return Optional.empty();
}
public void save(String accountId, long sequenceKey, BigDecimal amount, String outcome, BigDecimal balanceAfter, String responseMessage) throws SQLException {
String sql = "INSERT INTO transaction_ledger (account_id, sequence_key, amount, outcome, balance_after, response_message, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, accountId);
pstmt.setLong(2, sequenceKey);
pstmt.setBigDecimal(3, amount);
pstmt.setString(4, outcome);
pstmt.setBigDecimal(5, balanceAfter);
pstmt.setString(6, responseMessage);
pstmt.setTimestamp(7, Timestamp.from(Instant.now()));
pstmt.executeUpdate();
}
}
private Transaction mapRowToTransaction(ResultSet rs) throws SQLException {
return new Transaction(
rs.getLong("id"),
rs.getString("account_id"),
rs.getLong("sequence_key"),
rs.getBigDecimal("amount"),
rs.getString("outcome"),
rs.getBigDecimal("balance_after"),
rs.getString("response_message"),
rs.getTimestamp("timestamp").toInstant()
);
}
}
package com.banking.models;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import java.math.BigDecimal;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class ModelTest {
private final ObjectMapper mapper = new ObjectMapper();
@Test
void testAccountModel() {
Account account = new Account("ACC1", new BigDecimal("100.00"), 10L);
assertEquals("ACC1", account.accountId());
assertEquals(0, new BigDecimal("100.00").compareTo(account.balance()));
assertEquals(10L, account.lastProcessedSeqKey());
}
@Test
void testRequestDTOSerialization() throws Exception {
RequestDTO request = new RequestDTO("ACC1", new BigDecimal("20.00"), 101L, false);
String json = mapper.writeValueAsString(request);
RequestDTO deserialized = mapper.readValue(json, RequestDTO.class);
assertEquals("ACC1", deserialized.accountId());
assertEquals(0, new BigDecimal("20.00").compareTo(deserialized.amount()));
assertEquals(101L, deserialized.sequenceKey());
assertFalse(deserialized.isRetry());
}
@Test
void testResponseDTOSerialization() throws Exception {
ResponseDTO response = new ResponseDTO("SUCCESS", new BigDecimal("80.00"), "Success message");
String json = mapper.writeValueAsString(response);
ResponseDTO deserialized = mapper.readValue(json, ResponseDTO.class);
assertEquals("SUCCESS", deserialized.status());
assertEquals(0, new BigDecimal("80.00").compareTo(deserialized.balance()));
assertEquals("Success message", deserialized.message());
}
@Test
void testResponseDTOResponseWithNullBalance() throws Exception {
ResponseDTO response = new ResponseDTO("INVALID_ACCOUNT", null, "Account not found");
String json = mapper.writeValueAsString(response);
ResponseDTO deserialized = mapper.readValue(json, ResponseDTO.class);
assertEquals("INVALID_ACCOUNT", deserialized.status());
assertNull(deserialized.balance());
assertEquals("Account not found", deserialized.message());
}
@Test
void testBigDecimalPrecision() {
BigDecimal amount = new BigDecimal("100.0000000001");
RequestDTO request = new RequestDTO("ACC1", amount, 101L, false);
assertEquals(amount, request.amount());
}
@Test
void testTransactionModel() {
Transaction tx = new Transaction(
1L, "ACC1", 101L, new BigDecimal("20.00"),
"SUCCESS", new BigDecimal("80.00"), "Msg", java.time.Instant.now()
);
assertEquals("ACC1", tx.accountId());
assertEquals(0, new BigDecimal("20.00").compareTo(tx.amount()));
assertEquals("SUCCESS", tx.outcome());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment