Skip to content

Commit

Permalink
feature#25: Endpoint correctives and Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGib committed Mar 2, 2024
1 parent 31ca199 commit 72d06dd
Show file tree
Hide file tree
Showing 56 changed files with 736 additions and 418 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ quarkus dev -e

*A la racine du projet*
```bash
mvn process-test-resources
mvn test
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BankAccountEntity extends PanacheEntityBase {
@ManyToMany(mappedBy = "accounts", fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private List<CustomerEntity> customers = new ArrayList<>();

@OneToMany(mappedBy = "senderBankAccountEntity", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "emitterBankAccountEntity", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("date")
private Set<TransactionEntity> issuedTransactions = new HashSet<>();

Expand Down Expand Up @@ -76,7 +76,7 @@ public void setIssuedTransactions(Set<TransactionEntity> issuedTransactions) {

public void addTransaction(TransactionEntity transaction) {
this.issuedTransactions.add(transaction);
transaction.setSenderBankAccountEntity(this);
transaction.setEmitterBankAccountEntity(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cdx.bas.application.bank.customer.CustomerEntity;
import com.cdx.bas.application.bank.customer.CustomerRepository;
import com.cdx.bas.application.bank.transaction.TransactionMapper;
import com.cdx.bas.application.mapper.DtoEntityMapper;
import com.cdx.bas.application.bank.transaction.TransactionEntity;
import com.cdx.bas.domain.bank.account.BankAccount;
Expand All @@ -24,7 +25,7 @@ public class BankAccountMapper implements DtoEntityMapper<BankAccount, BankAccou
@Inject
BankAccountRepository bankAccountRepository;
@Inject
DtoEntityMapper<Transaction, TransactionEntity> transactionMapper;
TransactionMapper transactionMapper;

@Override
public BankAccount toDto(BankAccountEntity entity) {
Expand Down Expand Up @@ -73,7 +74,7 @@ public BankAccountEntity toEntity(BankAccount dto) {
Set<TransactionEntity> newIssuedTransactions = new HashSet<>();
for (Transaction issuedTransactionDto : dto.getIssuedTransactions()) {
TransactionEntity newIssuedTransactionEntity = transactionMapper.toEntity(issuedTransactionDto);
newIssuedTransactionEntity.setSenderBankAccountEntity(entity);
newIssuedTransactionEntity.setEmitterBankAccountEntity(entity);
newIssuedTransactions.add(newIssuedTransactionEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@ public class BankAccountRepository implements BankAccountPersistencePort, Panach
BankAccountMapper bankAccountMapper;

@Override
@Transactional(Transactional.TxType.MANDATORY)
@Transactional
public Set<BankAccount> getAll() {
return findAll(Sort.by("id")).stream()
.map(bankAccountEntity -> bankAccountMapper.toDto(bankAccountEntity))
.collect(Collectors.toSet());
}

@Override
@Transactional(Transactional.TxType.MANDATORY)
@Transactional
public Optional<BankAccount> findById(long id) {
return findByIdOptional(id).map(bankAccountMapper::toDto);
}

@Override
@Transactional(value = MANDATORY)
@Transactional
public BankAccount create(BankAccount bankAccount) {
getEntityManager().persist(bankAccountMapper.toEntity(bankAccount));
logger.info("BankAccount " + bankAccount.getId() + " created");
return bankAccount;
}
@Override
@Transactional(value = MANDATORY)
@Transactional
public BankAccount update(BankAccount bankAccount) {
bankAccount = bankAccountMapper.toDto(getEntityManager().merge(bankAccountMapper.toEntity(bankAccount)));
logger.info("BankAccount " + bankAccount.getId() + " updated");
return bankAccount;
}

@Override
@Transactional(value = MANDATORY)
@Transactional
public Optional<BankAccount> deleteById(long id) {
Optional<BankAccountEntity> entityOptional = findByIdOptional(id);
if (entityOptional.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,19 @@ public class BankAccountServiceImpl implements BankAccountServicePort {
TransactionServicePort transactionService;

@Override
@Transactional(Transactional.TxType.MANDATORY)
@Transactional
public Set<BankAccount> getAll() {
return bankAccountRepository.getAll();
}

@Override
@Transactional(Transactional.TxType.MANDATORY)
@Transactional
public BankAccount findBankAccount(Long bankAccountId){
return bankAccountRepository.findById(bankAccountId).orElse(null);
}

@Override
public void transferAmountBetweenAccounts(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) {
BigDecimal euroAmount = ExchangeRateUtils.getEuroAmountFrom(transaction.getCurrency(), transaction.getAmount());
if (euroAmount.signum() < 0) {
throw new TransactionException("Credit transaction " + transaction.getId() + " should have positive value, actual value: " + euroAmount);
}
emitterBankAccount.getBalance().minus(Money.of(euroAmount));
receiverBankAccount.getBalance().plus(Money.of(euroAmount));
logger.debug("add amount " + emitterBankAccount.getBalance() + " " + transaction.getCurrency()
+ " to bank account" + receiverBankAccount.getId() + " from bank account " + emitterBankAccount.getId());
}

@Override
@Transactional
public BankAccount addTransaction(Transaction transaction, BankAccount bankAccount) {
Optional<Transaction> optionalStoredTransaction = bankAccount.getIssuedTransactions().stream()
.filter(actualTransaction -> actualTransaction.getId().equals(transaction.getId()))
Expand All @@ -73,10 +62,22 @@ public BankAccount addTransaction(Transaction transaction, BankAccount bankAccou
}

@Override
@Transactional(Transactional.TxType.MANDATORY)
@Transactional
public BankAccount updateBankAccount(BankAccount bankAccount) throws BankAccountException {
logger.debug("update bank account" + bankAccount.getId());
bankAccountValidator.validateBankAccount(bankAccount);
return bankAccountRepository.update(bankAccount);
}

@Override
public void transferAmountBetweenAccounts(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) {
BigDecimal euroAmount = ExchangeRateUtils.getEuroAmountFrom(transaction.getCurrency(), transaction.getAmount());
if (euroAmount.signum() < 0) {
throw new TransactionException("Credit transaction " + transaction.getId() + " should have positive value, actual value: " + euroAmount);
}
emitterBankAccount.getBalance().minus(Money.of(euroAmount));
receiverBankAccount.getBalance().plus(Money.of(euroAmount));
logger.debug("add amount " + emitterBankAccount.getBalance() + " " + transaction.getCurrency()
+ " to bank account" + receiverBankAccount.getId() + " from bank account " + emitterBankAccount.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cdx.bas.application.bank.customer;

import com.cdx.bas.application.bank.account.BankAccountEntity;
import com.cdx.bas.application.bank.account.BankAccountMapper;
import com.cdx.bas.application.mapper.DtoEntityMapper;
import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.customer.Customer;
Expand All @@ -18,7 +19,7 @@
public class CustomerMapper implements DtoEntityMapper<Customer, CustomerEntity> {

@Inject
DtoEntityMapper<BankAccount, BankAccountEntity> bankAccountMapper;
BankAccountMapper bankAccountMapper;

@Inject
ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CustomerRepository implements CustomerPersistencePort, PanacheRepos
private static final Logger logger = Logger.getLogger(CustomerRepository.class);

@Inject
private DtoEntityMapper<Customer, CustomerEntity> customerMapper;
CustomerMapper customerMapper;

@Override
@Transactional(value = MANDATORY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import static com.cdx.bas.domain.bank.customer.gender.Gender.*;

@Converter
public class GenderConverter implements AttributeConverter<Gender, Character> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import static com.cdx.bas.domain.bank.customer.maritalstatus.MaritalStatus.*;

@Converter
public class MaritalStatusConverter implements AttributeConverter<MaritalStatus, Character> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class TransactionEntity extends PanacheEntityBase {
private Long id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "sender_account_id", nullable = false)
private BankAccountEntity senderBankAccountEntity;
@JoinColumn(name = "emitter_account_id", nullable = false)
private BankAccountEntity emitterBankAccountEntity;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "receiver_account_id", nullable = false)
Expand Down Expand Up @@ -66,12 +66,12 @@ public void setId(Long id) {
this.id = id;
}

public BankAccountEntity getSenderBankAccountEntity() {
return senderBankAccountEntity;
public BankAccountEntity getEmitterBankAccountEntity() {
return emitterBankAccountEntity;
}

public void setSenderBankAccountEntity(BankAccountEntity senderBankAccountEntity) {
this.senderBankAccountEntity = senderBankAccountEntity;
public void setEmitterBankAccountEntity(BankAccountEntity emitterBankAccountEntity) {
this.emitterBankAccountEntity = emitterBankAccountEntity;
}

public BankAccountEntity getReceiverBankAccountEntity() {
Expand Down Expand Up @@ -144,7 +144,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
TransactionEntity that = (TransactionEntity) o;
return Objects.equals(id, that.id)
&& Objects.equals(senderBankAccountEntity, that.senderBankAccountEntity)
&& Objects.equals(emitterBankAccountEntity, that.emitterBankAccountEntity)
&& Objects.equals(receiverBankAccountEntity, that.receiverBankAccountEntity)
&& Objects.equals(amount, that.amount)
&& Objects.equals(currency, that.currency)
Expand All @@ -156,6 +156,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(id, senderBankAccountEntity, receiverBankAccountEntity, amount, currency, type, status, date, label, metadata);
return Objects.hash(id, emitterBankAccountEntity, receiverBankAccountEntity, amount, currency, type, status, date, label, metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public Transaction toDto(TransactionEntity entity) {
.label(entity.getLabel())
.build();

if (entity.getSenderBankAccountEntity() != null) {
dto.setSenderAccountId(entity.getSenderBankAccountEntity().getId());
if (entity.getEmitterBankAccountEntity() != null) {
dto.setEmitterAccountId(entity.getEmitterBankAccountEntity().getId());
} else {
throw new NoSuchElementException("Transaction does not have sender bank account.");
throw new NoSuchElementException("Transaction does not have emitter bank account.");
}

if (entity.getReceiverBankAccountEntity() != null) {
Expand Down Expand Up @@ -73,13 +73,13 @@ public TransactionEntity toEntity(Transaction dto) {
entity.setId(dto.getId());
}

BankAccountEntity senderBankAccountEntity = bankAccountRepository.findByIdOptional(dto.getSenderAccountId())
.orElseThrow(() -> new NoSuchElementException("Transaction does not have sender bank account entity."));
BankAccountEntity emitterBankAccountEntity = bankAccountRepository.findByIdOptional(dto.getEmitterAccountId())
.orElseThrow(() -> new NoSuchElementException("Transaction does not have emitter bank account entity."));

BankAccountEntity receiverBankAccountEntity = bankAccountRepository.findByIdOptional(dto.getReceiverAccountId())
.orElseThrow(() -> new NoSuchElementException("Transaction does not have receiver bank account entity."));

entity.setSenderBankAccountEntity(senderBankAccountEntity);
entity.setEmitterBankAccountEntity(emitterBankAccountEntity);
entity.setReceiverBankAccountEntity(receiverBankAccountEntity);
entity.setAmount(dto.getAmount());
entity.setCurrency(dto.getCurrency());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import io.quarkus.panache.common.Sort;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.jboss.logging.Logger;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Queue;
Expand All @@ -24,20 +25,26 @@
* @author Clément Gibert
*
*/

@RequestScoped
public class TransactionRepository implements TransactionPersistencePort, PanacheRepositoryBase<TransactionEntity, Long> {

private static final Logger logger = Logger.getLogger(TransactionRepository.class);

@PersistenceContext
private EntityManager entityManager;

@Inject
TransactionMapper transactionMapper;

@Override
@Transactional
public Optional<Transaction> findById(long id) {
return findByIdOptional(id).map(transactionMapper::toDto);
}

@Override
@Transactional
public Set<Transaction> getAll() {
return findAll(Sort.by("status")).stream()
.map(transactionEntity -> transactionMapper.toDto(transactionEntity))
Expand All @@ -46,6 +53,7 @@ public Set<Transaction> getAll() {


@Override
@Transactional
public Set<Transaction> findAllByStatus(TransactionStatus transactionStatus) {
return findAll(Sort.by("status")).stream()
.filter(transaction -> transaction.getStatus().equals(transactionStatus))
Expand All @@ -54,6 +62,7 @@ public Set<Transaction> findAllByStatus(TransactionStatus transactionStatus) {
}

@Override
@Transactional
public Queue<Transaction> findUnprocessedTransactions() {
return find("#TransactionEntity.findUnprocessed",
Parameters.with("status", TransactionStatus.UNPROCESSED).map())
Expand All @@ -63,23 +72,23 @@ public Queue<Transaction> findUnprocessedTransactions() {
}

@Override
@Transactional(TxType.MANDATORY)
@Transactional
public Transaction create(Transaction transaction) {
getEntityManager().persist(transactionMapper.toEntity(transaction));
logger.info("Transaction " + transaction.getId() + " created");
entityManager.persist(transactionMapper.toEntity(transaction));
logger.info("Transaction from " + transaction.getEmitterAccountId() + " to " + transaction.getReceiverAccountId() + " created");
return transaction;
}

@Override
@Transactional(TxType.MANDATORY)
@Transactional
public Transaction update(Transaction transaction) {
getEntityManager().merge(transactionMapper.toEntity(transaction));
logger.info("Transaction " + transaction.getId() + " updated");
return transaction;
}

@Override
@Transactional(TxType.MANDATORY)
@Transactional
public Optional<Transaction> deleteById(long id) {
Optional<TransactionEntity> entityOptional = findByIdOptional(id);
if (entityOptional.isPresent()) {
Expand Down
Loading

0 comments on commit 72d06dd

Please sign in to comment.