Skip to content

Commit

Permalink
Merge pull request #28 from ClementGib/task#58
Browse files Browse the repository at this point in the history
task#58: Cleaning BankAccount utils
  • Loading branch information
ClementGib authored Mar 16, 2024
2 parents b883845 + d925742 commit 5c92718
Show file tree
Hide file tree
Showing 16 changed files with 869 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.cdx.bas.application.bank.customer.CustomerEntity;
import com.cdx.bas.application.bank.customer.CustomerRepository;
import com.cdx.bas.application.mapper.DtoEntityMapper;
import com.cdx.bas.application.bank.transaction.TransactionEntity;
import com.cdx.bas.application.bank.transaction.TransactionTestUtils;
import com.cdx.bas.domain.bank.account.type.AccountType;
import com.cdx.bas.application.mapper.DtoEntityMapper;
import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.checking.CheckingBankAccount;
import com.cdx.bas.domain.bank.account.type.AccountType;
import com.cdx.bas.domain.bank.customer.Customer;
import com.cdx.bas.domain.bank.customer.gender.Gender;
import com.cdx.bas.domain.bank.customer.maritalstatus.MaritalStatus;
import com.cdx.bas.domain.money.Money;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.status.TransactionStatus;
import com.cdx.bas.domain.bank.transaction.type.TransactionType;
import com.cdx.bas.domain.money.Money;
import io.quarkus.test.InjectMock;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
Expand All @@ -26,6 +27,8 @@
import java.time.Month;
import java.util.*;

import static com.cdx.bas.domain.bank.transaction.status.TransactionStatus.ERROR;
import static com.cdx.bas.domain.bank.transaction.type.TransactionType.CREDIT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyLong;
Expand Down Expand Up @@ -58,56 +61,64 @@ public void toDto_shouldReturnNullDto_whenEntityIsNull() {

@Test
public void toEntity_shouldReturnNullEntity_whenDtoIsNull() {
// Act
BankAccountEntity entity = bankAccountMapper.toEntity(null);

// Assert
assertThat(entity).isNull();

verifyNoInteractions(customerMapper);
}

@Test
public void toDto_shouldReturnNullDto_whenEntityHasEmptyObject() {
// Act
BankAccount dto = bankAccountMapper.toDto(new BankAccountEntity());

// Assert
assertThat(dto).isNull();

verifyNoInteractions(customerMapper);
}

@Test
public void toDto_shouldMapAccountTypeOnly_whenEntityHasAccount_withOnlyAccountType() {
// Arrange
BankAccountEntity entity = new BankAccountEntity();
entity.setType(AccountType.CHECKING);

// Act
BankAccount dto = bankAccountMapper.toDto(entity);

// Assert
assertThat(dto.getId()).isNull();
assertThat(dto.getType()).isEqualTo(AccountType.CHECKING);
assertThat(dto.getBalance()).usingRecursiveComparison().isEqualTo(new Money(null));
assertThat(dto.getCustomersId()).isEmpty();
assertThat(dto.getIssuedTransactions()).isEmpty();

verifyNoInteractions(customerMapper);
}

@Test
public void toEntity_shouldMapNullValues_whenDtoHasAccount_withOnlyAccountTypeAndId() {
// Arrange
BankAccount bankAccount = new CheckingBankAccount();
bankAccount.setId(1L);

// Act
BankAccountEntity entity = bankAccountMapper.toEntity(bankAccount);

// Assert
assertThat(entity.getId()).isEqualTo(1L);
assertThat(entity.getType()).isEqualTo(AccountType.CHECKING);
assertThat(entity.getBalance()).isNull();
assertThat(entity.getCustomers()).isEmpty();
assertThat(entity.getIssuedTransactions()).isEmpty();

verifyNoInteractions(customerMapper);
}

@Test
public void toDto_shouldMapEveryFieldsOfDto_whenEntityHasValues() {
Instant date = Instant.now();
// Arrange
Instant timestamp = Instant.now();
BankAccountEntity entity = new BankAccountEntity();
entity.setId(10L);
entity.setType(AccountType.CHECKING);
Expand All @@ -117,18 +128,41 @@ public void toDto_shouldMapEveryFieldsOfDto_whenEntityHasValues() {
customers.add(customerEntity);
entity.setCustomers(customers);
Set<TransactionEntity> transactionEntities = new HashSet<>();
TransactionEntity transactionEntity1 = TransactionTestUtils.createTransactionEntity(2000L, date);
TransactionEntity transactionEntity1 = createTransactionEntity(2000L, timestamp);
transactionEntities.add(transactionEntity1);
TransactionEntity transactionEntity2 = TransactionTestUtils.createTransactionEntity(5000L, date);
TransactionEntity transactionEntity2 = createTransactionEntity(5000L, timestamp);
transactionEntities.add(transactionEntity2);
entity.setIssuedTransactions(transactionEntities);

Transaction transaction1 = TransactionTestUtils.createTransactionUtils(2000L, date);
Transaction transaction2 = TransactionTestUtils.createTransactionUtils(5000L, date);
Transaction transaction1 = Transaction.builder()
.id(2L)
.type(CREDIT)
.emitterAccountId(2000L)
.receiverAccountId(77L)
.amount(new BigDecimal("100"))
.currency("EUR")
.status(ERROR)
.date(timestamp)
.label("transaction test")
.build();
Transaction transaction2 = Transaction.builder()
.id(2L)
.type(CREDIT)
.emitterAccountId(5000L)
.receiverAccountId(77L)
.amount(new BigDecimal("100"))
.currency("EUR")
.status(ERROR)
.date(timestamp)
.label("transaction test")
.build();
when(transactionMapper.toDto(transactionEntity1)).thenReturn(transaction1);
when(transactionMapper.toDto(transactionEntity2)).thenReturn(transaction2);

// Act
BankAccount dto = bankAccountMapper.toDto(entity);

// Assert
assertThat(dto.getId()).isEqualTo(10L);
assertThat(dto.getType()).isEqualTo(AccountType.CHECKING);
assertThat(dto.getBalance()).usingRecursiveComparison().isEqualTo(new Money(new BigDecimal("1000")));
Expand All @@ -145,7 +179,8 @@ public void toDto_shouldMapEveryFieldsOfDto_whenEntityHasValues() {

@Test
public void toEntity_shouldThrowNoSuchElementException_whenCustomerIsNotFound() {
Instant date = Instant.now();
// Arrange
Instant timestamp = Instant.now();
BankAccount dto = new CheckingBankAccount();
dto.setId(10L);
dto.setType(AccountType.CHECKING);
Expand All @@ -155,18 +190,40 @@ public void toEntity_shouldThrowNoSuchElementException_whenCustomerIsNotFound()
customers.add(customer.getId());
dto.setCustomersId(customers);
Set<Transaction> transactions = new HashSet<>();
Transaction transaction1 = TransactionTestUtils.createTransactionUtils(2000L, date);
Transaction transaction1 = Transaction.builder()
.id(2L)
.emitterAccountId(2000L)
.receiverAccountId(77L)
.amount(new BigDecimal(100))
.type(TransactionType.CREDIT)
.status(TransactionStatus.ERROR)
.date(timestamp)
.label("transaction test")
.metadata(Map.of("amount_before", "0", "amount_after", "350"))
.build();
transactions.add(transaction1);
Transaction transaction2 = TransactionTestUtils.createTransactionUtils(5000L, date);
Transaction transaction2 = Transaction.builder()
.id(2L)
.emitterAccountId(5000L)
.receiverAccountId(77L)
.amount(new BigDecimal(100))
.type(TransactionType.CREDIT)
.status(TransactionStatus.ERROR)
.date(timestamp)
.label("transaction test")
.metadata(Map.of("amount_before", "0", "amount_after", "350"))
.build();
transactions.add(transaction2);
dto.setIssuedTransactions(transactions);

when(customerRepository.findById(anyLong())).thenReturn(Optional.empty());

try {
// Act
bankAccountMapper.toEntity(dto);
fail();
} catch (NoSuchElementException exception) {
// Assert
assertThat(exception.getMessage()).hasToString("Customer entity not found for id: 99");
}

Expand All @@ -177,7 +234,8 @@ public void toEntity_shouldThrowNoSuchElementException_whenCustomerIsNotFound()

@Test
public void toEntity_shouldMapEveryFieldsOfEntity_whenDtoHasValues() {
Instant date = Instant.now();
// Arrange
Instant timestamp = Instant.now();
BankAccount dto = new CheckingBankAccount();
dto.setId(10L);
dto.setType(AccountType.CHECKING);
Expand All @@ -187,23 +245,44 @@ public void toEntity_shouldMapEveryFieldsOfEntity_whenDtoHasValues() {
customers.add(customer.getId());
dto.setCustomersId(customers);
Set<Transaction> transactions = new HashSet<>();
Transaction transaction1 = TransactionTestUtils.createTransactionUtils(2000L, date);
Transaction transaction1 = Transaction.builder()
.id(2L)
.emitterAccountId(2000L)
.receiverAccountId(77L)
.amount(new BigDecimal(100))
.type(TransactionType.CREDIT)
.status(TransactionStatus.ERROR)
.date(timestamp)
.label("transaction test")
.metadata(Map.of("amount_before", "0", "amount_after", "350"))
.build();
transactions.add(transaction1);
Transaction transaction2 = TransactionTestUtils.createTransactionUtils(5000L, date);
Transaction transaction2 = Transaction.builder()
.id(2L)
.emitterAccountId(5000L)
.receiverAccountId(77L)
.amount(new BigDecimal(100))
.type(TransactionType.CREDIT)
.status(TransactionStatus.ERROR)
.date(timestamp)
.label("transaction test")
.metadata(Map.of("amount_before", "0", "amount_after", "350"))
.build();
transactions.add(transaction2);
dto.setIssuedTransactions(transactions);

CustomerEntity customerEntity = createCustomerEntityUtils();
when(customerRepository.findByIdOptional(anyLong())).thenReturn(Optional.of(customerEntity));
when(customerMapper.toEntity(customer)).thenReturn(customerEntity);
TransactionEntity transactionEntity1 = TransactionTestUtils.createTransactionEntity(2000L, date);
TransactionEntity transactionEntity1 = createTransactionEntity(2000L, timestamp);
when(transactionMapper.toEntity(transaction1)).thenReturn(transactionEntity1);
TransactionEntity transactionEntity2 = TransactionTestUtils.createTransactionEntity(5000L, date);
TransactionEntity transactionEntity2 = createTransactionEntity(5000L, timestamp);
when(transactionMapper.toEntity(transaction2)).thenReturn(transactionEntity2);


// Act
BankAccountEntity entity = bankAccountMapper.toEntity(dto);

// Assert
assertThat(entity.getId()).isEqualTo(10L);
assertThat(entity.getType()).isEqualTo(AccountType.CHECKING);
assertThat(entity.getBalance()).usingRecursiveComparison().isEqualTo(new BigDecimal("1000"));
Expand Down Expand Up @@ -250,4 +329,17 @@ private CustomerEntity createCustomerEntityUtils() {
customerEntity.setPhoneNumber("+33642645678");
return customerEntity;
}

private TransactionEntity createTransactionEntity(long id, Instant instantDate) {
TransactionEntity transactionEntity = new TransactionEntity();
transactionEntity.setId(id);
transactionEntity.setEmitterBankAccountEntity(null);
transactionEntity.setReceiverBankAccountEntity(null);
transactionEntity.setAmount(new BigDecimal("100"));
transactionEntity.setType(TransactionType.CREDIT);
transactionEntity.setStatus(TransactionStatus.ERROR);
transactionEntity.setDate(instantDate);
transactionEntity.setLabel("transaction test");
return transactionEntity;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.cdx.bas.application.bank.account;

import com.cdx.bas.application.mapper.DtoEntityMapper;
import com.cdx.bas.application.bank.transaction.TransactionTestUtils;
import com.cdx.bas.domain.bank.account.type.AccountType;
import com.cdx.bas.domain.bank.account.BankAccount;
import com.cdx.bas.domain.bank.account.checking.CheckingBankAccount;
import com.cdx.bas.domain.money.Money;
import com.cdx.bas.domain.bank.account.type.AccountType;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.status.TransactionStatus;
import com.cdx.bas.domain.bank.transaction.type.TransactionType;
import com.cdx.bas.domain.money.Money;
import io.quarkus.test.InjectMock;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
Expand Down Expand Up @@ -37,13 +38,17 @@ public class BankAccountRepositoryTest {
@Test
@Transactional
public void findById_shouldReturnBankAccount_whenAccountIsFound() {
// Arrange
long accountId = 1L;
Instant date = Instant.now();
BankAccount bankAccount = createBankAccountUtils(accountId, date);

when(bankAccountMapper.toDto(any())).thenReturn(bankAccount);

// Act
Optional<BankAccount> optionalBankAccount = bankAccountRepository.findById(accountId);


// Assert
assertThat(optionalBankAccount).contains(bankAccount);
verify(bankAccountMapper).toDto(any(BankAccountEntity.class));
verifyNoMoreInteractions(bankAccountMapper);
Expand All @@ -52,13 +57,15 @@ public void findById_shouldReturnBankAccount_whenAccountIsFound() {
@Test
@Transactional
public void findById_shouldReturnEmptyOptional_whenAccountIsNotFound() {
// Act
Optional<BankAccount> optionalBankAccount = bankAccountRepository.findById(99999L);


// Assert
assertThat(optionalBankAccount).isEmpty();
verifyNoInteractions(bankAccountMapper);
}

private BankAccount createBankAccountUtils(long accountId, Instant instantDate) {
private BankAccount createBankAccountUtils(long accountId, Instant timestamp) {
BankAccount bankAccount = new CheckingBankAccount();
bankAccount.setId(accountId);
bankAccount.setType(AccountType.CHECKING);
Expand All @@ -67,7 +74,17 @@ private BankAccount createBankAccountUtils(long accountId, Instant instantDate)
customersId.add(1L);
bankAccount.setCustomersId(customersId);
HashSet<Transaction> transactionHistory = new HashSet<>();
transactionHistory.add(TransactionTestUtils.createTransactionUtils(accountId, instantDate));
transactionHistory.add(Transaction.builder()
.id(2L)
.emitterAccountId(5000L)
.receiverAccountId(77L)
.amount(new BigDecimal(100))
.type(TransactionType.CREDIT)
.status(TransactionStatus.ERROR)
.date(timestamp)
.label("transaction test")
.metadata(Map.of("amount_before", "0", "amount_after", "350"))
.build());
bankAccount.setIssuedTransactions(transactionHistory);
return bankAccount;
}
Expand Down
Loading

0 comments on commit 5c92718

Please sign in to comment.