Skip to content

Commit

Permalink
task#76: cleaning and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGib committed Nov 7, 2024
1 parent cd9d895 commit 8cb73e6
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public List<BankAccount> getAll() {
@Transactional
public BankAccount findBankAccount(Long bankAccountId) {
return bankAccountRepository.findById(bankAccountId)
.orElseThrow(() -> new BankAccountException(format(BANK_ACCOUNT_CONTEXT, UPDATE_ACTION, FAILED_STATUS,
Optional.of(NOT_FOUND_CAUSE), List.of(ID_DETAIL + bankAccountId))));
.orElseThrow(() -> new BankAccountException(format(BANK_ACCOUNT_CONTEXT, SEARCHING_ACTION, FAILED_STATUS,
Optional.of(NOT_FOUND_CAUSE), List.of(BANK_ACCOUNT_ID_DETAIL + bankAccountId))));
}

@Override
Expand Down Expand Up @@ -92,7 +92,7 @@ public void creditAmountToAccounts(Transaction transaction, BankAccount emitterB
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(CREDIT_TRANSACTION_CONTEXT, CREDIT_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
List.of(ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
List.of(TRANSACTION_ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
}
emitterBankAccount.getBalance().minus(Money.of(euroAmount));
receiverBankAccount.getBalance().plus(Money.of(euroAmount));
Expand All @@ -104,7 +104,7 @@ public void depositAmountToAccount(Transaction transaction, BankAccount emitterB
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(DEBIT_TRANSACTION_CONTEXT, DEBIT_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
List.of(ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
List.of(TRANSACTION_ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
}
emitterBankAccount.getBalance().plus(Money.of(euroAmount));
}
Expand All @@ -115,10 +115,8 @@ public void withdrawAmountToAccount(Transaction transaction, BankAccount emitter
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(WITHDRAW_TRANSACTION_CONTEXT, WITHDRAW_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
List.of(ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
List.of(TRANSACTION_ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
}
emitterBankAccount.getBalance().minus(Money.of(euroAmount));
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import org.jboss.logging.Logger;

import java.util.List;
Expand All @@ -20,13 +18,13 @@

/***
* persistence implementation for Customer entities
*
*
* @author Clément Gibert
*
*/
@RequestScoped
public class CustomerRepository implements CustomerPersistencePort, PanacheRepositoryBase<CustomerEntity, Long> {

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

private final CustomerMapper customerMapper;
Expand All @@ -43,35 +41,35 @@ public Set<Customer> getAll() {
.collect(Collectors.toSet());
}

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

@Override
public Customer create(Customer customer) {
@Override
public Customer create(Customer customer) {
persist(customerMapper.toEntity(customer));
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, CREATION_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + customer.getId())));
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, CREATION_ACTION, SUCCESS_STATUS, List.of(CUSTOMER_ID_DETAIL + customer.getId())));
return customer;
}
}

@Override
public Customer update(Customer customer) {
persist(customerMapper.toEntity(customer));
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + customer.getId())));
@Override
public Customer update(Customer customer) {
persist(customerMapper.toEntity(customer));
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS, List.of(CUSTOMER_ID_DETAIL + customer.getId())));
return customer;
}
}

@Override
public Optional<Customer> deleteById(long id) {
@Override
public Optional<Customer> deleteById(long id) {
Optional<CustomerEntity> entityOptional = findByIdOptional(id);
if (entityOptional.isPresent()) {
CustomerEntity entity = entityOptional.get();
delete(entity);
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, DELETION_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + id)));
logger.debug(MessageFormatter.format(CUSTOMER_CONTEXT, DELETION_ACTION, SUCCESS_STATUS, List.of(CUSTOMER_ID_DETAIL + id)));
return Optional.of(customerMapper.toDto(entity));
}
return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;

import static com.cdx.bas.domain.message.CommonMessages.*;
import static com.cdx.bas.domain.message.CommonMessages.ID_DETAIL;

@ApplicationScoped
public class CustomerServiceImpl implements CustomerServicePort {

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

CustomerPersistencePort customerPersistencePort;

@Inject
Expand All @@ -39,6 +34,6 @@ public Set<Customer> getAll() {
@Transactional
public Customer findCustomer(Long customerId) {
return customerPersistencePort.findById(customerId)
.orElseThrow(() -> new CustomerException(MessageFormatter.format(CUSTOMER_CONTEXT, SEARCHING_ACTION, NOT_FOUND_CAUSE, List.of(ID_DETAIL + customerId))));
.orElseThrow(() -> new CustomerException(MessageFormatter.format(CUSTOMER_CONTEXT, SEARCHING_ACTION, NOT_FOUND_CAUSE, List.of(CUSTOMER_ID_DETAIL + customerId))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
import java.util.stream.Collectors;

import static com.cdx.bas.domain.message.CommonMessages.*;
import static com.cdx.bas.domain.message.CommonMessages.ID_DETAIL;

/***
* persistence implementation for Transaction entities
*
*
* @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;

Expand Down Expand Up @@ -72,14 +71,14 @@ public Queue<Transaction> findUnprocessedTransactions() {
@Override
public void create(Transaction transaction) {
entityManager.persist(transactionMapper.toEntity(transaction));
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, CREATION_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + transaction.getId())));
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, CREATION_ACTION, SUCCESS_STATUS, List.of(TRANSACTION_ID_DETAIL + transaction.getId())));

}

@Override
public Transaction update(Transaction transaction) {
entityManager.merge(transactionMapper.toEntity(transaction));
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + transaction.getId())));
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS, List.of(TRANSACTION_ID_DETAIL + transaction.getId())));
return transaction;
}

Expand All @@ -89,7 +88,7 @@ public Optional<Transaction> deleteById(long id) {
if (entityOptional.isPresent()) {
TransactionEntity entity = entityOptional.get();
delete(entity);
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, DELETION_ACTION, SUCCESS_STATUS, List.of(ID_DETAIL + id)));
logger.debug(MessageFormatter.format(TRANSACTION_CONTEXT, DELETION_ACTION, SUCCESS_STATUS, List.of(TRANSACTION_ID_DETAIL + id)));
return Optional.of(transactionMapper.toDto(entity));
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.cdx.bas.domain.bank.transaction.*;
import com.cdx.bas.domain.bank.transaction.status.TransactionStatus;
import com.cdx.bas.domain.bank.transaction.type.TransactionTypeProcessingServicePort;
import com.cdx.bas.domain.bank.transaction.type.TransactionProcessorServicePort;
import com.cdx.bas.domain.bank.transaction.validation.validator.TransactionValidator;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand All @@ -23,12 +23,12 @@ public class TransactionServiceImpl implements TransactionServicePort {

private final TransactionValidator transactionValidator;

private final TransactionTypeProcessingServicePort transactionTypeProcessingService;
private final TransactionProcessorServicePort transactionTypeProcessingService;

@Inject
public TransactionServiceImpl(TransactionPersistencePort transactionRepository,
TransactionValidator transactionValidator,
TransactionTypeProcessingServicePort transactionTypeProcessingService) {
TransactionProcessorServicePort transactionTypeProcessingService) {
this.transactionRepository = transactionRepository;
this.transactionValidator = transactionValidator;
this.transactionTypeProcessingService = transactionTypeProcessingService;
Expand Down Expand Up @@ -81,7 +81,7 @@ public Transaction findTransaction(Long transactionId) {
public void processDigitalTransaction(Transaction digitalTransaction) {
if (CREDIT.equals(digitalTransaction.getType())) {
transactionTypeProcessingService.credit(digitalTransaction);
} else if (DEPOSIT.equals(digitalTransaction.getType())) {
} else if (DEBIT.equals(digitalTransaction.getType())) {
transactionTypeProcessingService.deposit(digitalTransaction);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cdx.bas.application.bank.transaction.type;

import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.BankAccountException;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;

public class CreditProcessorImpl implements DigitalTransactionProcessor {
@Override
public void processDigital(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) throws TransactionException, BankAccountException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cdx.bas.application.bank.transaction.type;

import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.BankAccountException;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;

public class DebitProcessorImpl implements DigitalTransactionProcessor{
@Override
public void processDigital(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) throws TransactionException, BankAccountException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cdx.bas.application.bank.transaction.type;

import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.BankAccountException;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;

public class DepositProcessorImpl implements PhysicalCashTransactionProcessor{
@Override
public void processPhysicalCash(Transaction transaction, BankAccount emitterBankAccount) throws TransactionException, BankAccountException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cdx.bas.application.bank.transaction.type;

import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.BankAccountException;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;

@FunctionalInterface
public interface DigitalTransactionProcessor {
void processDigital(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) throws TransactionException, BankAccountException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cdx.bas.application.bank.transaction.type;

import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.BankAccountException;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;

@FunctionalInterface
public interface PhysicalCashTransactionProcessor {
void processPhysicalCash(Transaction transaction, BankAccount emitterBankAccount) throws TransactionException, BankAccountException;
}
Loading

0 comments on commit 8cb73e6

Please sign in to comment.