Skip to content

Commit

Permalink
Merge pull request #41 from ClementGib/task#60
Browse files Browse the repository at this point in the history
task#60: Coverage and cleaning tests (IT and UT)
  • Loading branch information
ClementGib authored Nov 8, 2024
2 parents e2683f1 + 2045d65 commit 8ebeb06
Show file tree
Hide file tree
Showing 47 changed files with 726 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class BankAccountEntity extends PanacheEntityBase {
@Column(name = "balance", nullable = false)
private BigDecimal balance;

@ManyToMany(mappedBy = "accounts", fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@ManyToMany(mappedBy = "accounts", fetch = FetchType.LAZY)
private Set<CustomerEntity> customers = new HashSet<>();

@OneToMany(mappedBy = "emitterBankAccountEntity", cascade = CascadeType.MERGE)
@OneToMany(mappedBy = "emitterBankAccountEntity", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@OrderBy("date")
private Set<TransactionEntity> issuedTransactions = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public BankAccount toDto(BankAccountEntity entity) {
.map(CustomerEntity::getId)
.collect(Collectors.toSet()));

dto.setIssuedTransactions(entity.getIssuedTransactions().stream()
dto.setIssuedTransactions(entity.getIssuedTransactions()
.stream()
.map(transactionMapper::toDto)
.collect(Collectors.toSet()));

return dto;
}

@Override
public BankAccountEntity toEntity(BankAccount dto) {

if (dto == null || dto.getId() == null) {
if (dto == null) {
return null;
}

BankAccountEntity entity = bankAccountRepository.findByIdOptional(dto.getId()).orElse(new BankAccountEntity());
BankAccountEntity entity = new BankAccountEntity();
entity.setId(dto.getId());
entity.setType(dto.getType());

Expand All @@ -80,7 +80,6 @@ public BankAccountEntity toEntity(BankAccount dto) {
TransactionEntity newIssuedTransactionEntity = transactionMapper.toEntity(issuedTransactionDto);
newIssuedTransactions.add(newIssuedTransactionEntity);
}

entity.setIssuedTransactions(newIssuedTransactions);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.TransactionManager;
import jakarta.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,16 +32,13 @@ public class BankAccountRepository implements BankAccountPersistencePort, Panach

BankAccountMapper bankAccountMapper;

TransactionManager transactionManager;

EntityManager entityManager;
@PersistenceContext
private EntityManager entityManager;

@Inject
public BankAccountRepository(BankAccountMapper bankAccountMapper,
TransactionManager transactionManager,
EntityManager entityManager) {
this.bankAccountMapper = bankAccountMapper;
this.transactionManager = transactionManager;
this.entityManager = entityManager;
}

Expand All @@ -59,18 +56,16 @@ public Optional<BankAccount> findById(long id) {

@Override
public BankAccount create(BankAccount bankAccount) {
entityManager.persist(bankAccountMapper.toEntity(bankAccount));
BankAccountEntity entity = bankAccountMapper.toEntity(bankAccount);
persist(entity);
logger.debug(MessageFormatter.format(BANK_ACCOUNT_CONTEXT, CREATION_ACTION, SUCCESS_STATUS));
return bankAccount;
}

@Override
public BankAccount update(BankAccount bankAccount) {
try {
BankAccountEntity entity = bankAccountMapper.toEntity(bankAccount);
getEntityManager();
BankAccountEntity merge = entityManager.merge(entity);
bankAccount = bankAccountMapper.toDto(merge);
bankAccount = bankAccountMapper.toDto(entityManager.merge(bankAccountMapper.toEntity(bankAccount)));
logger.debug(MessageFormatter.format(BANK_ACCOUNT_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS));
return bankAccount;
} catch (UnsupportedOperationException exception) {
Expand All @@ -84,7 +79,8 @@ public Optional<BankAccount> deleteById(long id) {
if (entityOptional.isPresent()) {
BankAccountEntity entity = entityOptional.get();
delete(entity);
logger.debug(MessageFormatter.format(BANK_ACCOUNT_CONTEXT, DELETION_ACTION, SUCCESS_STATUS));
logger.debug(MessageFormatter.format(BANK_ACCOUNT_CONTEXT, DELETION_ACTION, SUCCESS_STATUS,
List.of(BANK_ACCOUNT_ID_DETAIL + id)));
return Optional.of(bankAccountMapper.toDto(entity));
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CustomerEntity extends PanacheEntityBase {
private String phoneNumber;

@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "bank_accounts_customers", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "account_id"))
private List<BankAccountEntity> accounts = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.jboss.logging.Logger;

import java.util.List;
Expand All @@ -27,11 +29,16 @@ public class CustomerRepository implements CustomerPersistencePort, PanacheRepos

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


private final CustomerMapper customerMapper;

@PersistenceContext
private EntityManager entityManager;

@Inject
public CustomerRepository(CustomerMapper customerMapper) {
public CustomerRepository(CustomerMapper customerMapper, EntityManager entityManager) {
this.customerMapper = customerMapper;
this.entityManager = entityManager;
}

@Override
Expand All @@ -55,9 +62,9 @@ public Customer create(Customer customer) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class GenderConverter implements AttributeConverter<Gender, Character> {

@Override
public Character convertToDatabaseColumn(Gender gender) {
if (gender == null) {
return null;
}

return switch (gender) {
case MALE -> strMale;
case FEMALE -> strFemale;
Expand All @@ -22,6 +26,10 @@ public Character convertToDatabaseColumn(Gender gender) {

@Override
public Gender convertToEntityAttribute(Character genderCode) {
if (genderCode == null) {
return null;
}

return switch (genderCode) {
case strMale -> MALE;
case strFemale -> FEMALE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class MaritalStatusConverter implements AttributeConverter<MaritalStatus,

@Override
public Character convertToDatabaseColumn(MaritalStatus maritalStatus) {
if (maritalStatus == null) {
return null; // Handle null maritalStatus by returning null
}
return switch (maritalStatus) {
case SINGLE -> strSingle;
case MARRIED -> strMarried;
Expand All @@ -24,6 +27,9 @@ public Character convertToDatabaseColumn(MaritalStatus maritalStatus) {

@Override
public MaritalStatus convertToEntityAttribute(Character maritalCode) {
if (maritalCode == null) {
return null; // Handle null maritalCode by returning null
}
return switch (maritalCode) {
case strSingle -> SINGLE;
case strMarried -> MARRIED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class TransactionEntity extends PanacheEntityBase {
@SequenceGenerator(name = "transactions_transaction_id_seq_gen", sequenceName = "transactions_transaction_id_seq", allocationSize = 1, initialValue = 1)
private Long id;

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

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@
public class TransactionRepository implements TransactionPersistencePort, PanacheRepositoryBase<TransactionEntity, Long> {

private static final Logger logger = Logger.getLogger(TransactionRepository.class);
public static final String STATUS = "status";

TransactionMapper transactionMapper;

@PersistenceContext
private EntityManager entityManager;

@Inject
TransactionMapper transactionMapper;
public TransactionRepository(TransactionMapper transactionMapper, EntityManager entityManager) {
this.transactionMapper = transactionMapper;
this.entityManager = entityManager;
}

@Override
public Optional<Transaction> findById(long id) {
Expand All @@ -44,7 +50,7 @@ public Optional<Transaction> findById(long id) {

@Override
public Set<Transaction> getAll() {
return findAll(Sort.by("status")).stream()
return findAll(Sort.by(STATUS)).stream()
.map(transactionEntity -> transactionMapper.toDto(transactionEntity))
.collect(Collectors.toSet());
}
Expand All @@ -53,7 +59,7 @@ public Set<Transaction> getAll() {
@Override
@Transactional
public Set<Transaction> findAllByStatus(TransactionStatus transactionStatus) {
return findAll(Sort.by("status")).stream()
return findAll(Sort.by(STATUS)).stream()
.filter(transaction -> transaction.getStatus().equals(transactionStatus))
.map(transaction -> transactionMapper.toDto(transaction))
.collect(Collectors.toSet());
Expand All @@ -62,7 +68,7 @@ public Set<Transaction> findAllByStatus(TransactionStatus transactionStatus) {
@Override
public Queue<Transaction> findUnprocessedTransactions() {
return find("#TransactionEntity.findUnprocessed",
Parameters.with("status", TransactionStatus.UNPROCESSED).map())
Parameters.with(STATUS, TransactionStatus.UNPROCESSED).map())
.list()
.stream().map(transactionMapper::toDto)
.collect(Collectors.toCollection(PriorityQueue::new));
Expand All @@ -77,9 +83,9 @@ public void create(Transaction transaction) {

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

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cdx.bas.application.mapper;

public enum MappingType {
CREATE, UPDATE, DELETE
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ public void processQueue() {
logger.debug(MessageFormatter.format(SCHEDULER_CONTEXT, ENDING_ACTION, DONE_STATUS));
}
}

public boolean isActivated() {
return activation;
}

public String getEvery() {
return every;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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.common.WithTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
Expand All @@ -35,7 +35,7 @@
import static org.mockito.Mockito.*;

@QuarkusTest
@QuarkusTestResource(H2DatabaseTestResource.class)
@WithTestResource(H2DatabaseTestResource.class)
public class BankAccountMapperTest {

@Inject
Expand Down
Loading

0 comments on commit 8ebeb06

Please sign in to comment.