Skip to content

Commit

Permalink
task#60 core improvement to improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGib committed Nov 13, 2024
1 parent dbfe4ed commit 9736d8c
Show file tree
Hide file tree
Showing 96 changed files with 2,254 additions and 2,121 deletions.
6 changes: 5 additions & 1 deletion application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@
<target>21</target>
<release>21</release>
<annotationProcessorPaths>
<!-- Your existing annotation processor(s)... -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-panache-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
import com.cdx.bas.domain.bank.account.type.AccountType;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.*;
import java.util.HashSet;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@Entity
@Table(schema = "basapp", name = "bank_accounts", uniqueConstraints = @UniqueConstraint(columnNames = "account_id"))
public class BankAccountEntity extends PanacheEntityBase {
Expand All @@ -32,53 +41,4 @@ public class BankAccountEntity extends PanacheEntityBase {
@OneToMany(mappedBy = "emitterBankAccountEntity", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@OrderBy("date")
private Set<TransactionEntity> issuedTransactions = new HashSet<>();


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public AccountType getType() {
return type;
}

public void setType(AccountType type) {
this.type = type;
}

public BigDecimal getBalance() {
return balance;
}

public void setBalance(BigDecimal balance) {
this.balance = balance;
}

public Set<CustomerEntity> getCustomers() {
return customers;
}

public void setCustomers(Set<CustomerEntity> customers) {
this.customers = customers;
}

public Set<TransactionEntity> getIssuedTransactions() {
return issuedTransactions;
}

public void setIssuedTransactions(Set<TransactionEntity> issuedTransactions) {
this.issuedTransactions = issuedTransactions;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BankAccountEntity that = (BankAccountEntity) o;
return Objects.equals(id, that.id) && type == that.type && Objects.equals(balance, that.balance) && Objects.equals(customers, that.customers) && Objects.equals(issuedTransactions, that.issuedTransactions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
import com.cdx.bas.domain.bank.account.BankAccountServicePort;
import com.cdx.bas.domain.bank.account.validation.BankAccountValidator;
import com.cdx.bas.domain.bank.transaction.Transaction;
import com.cdx.bas.domain.bank.transaction.TransactionException;
import com.cdx.bas.domain.bank.transaction.TransactionServicePort;
import com.cdx.bas.domain.currency.rate.ExchangeRateUtils;
import com.cdx.bas.domain.money.Money;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import static com.cdx.bas.domain.message.CommonMessages.*;
import static com.cdx.bas.domain.message.MessageFormatter.format;
import static com.cdx.bas.domain.money.AmountUtils.isNotPositive;

@ApplicationScoped
public class BankAccountServiceImpl implements BankAccountServicePort {
Expand Down Expand Up @@ -54,6 +49,11 @@ public List<BankAccount> getAll() {
@Override
@Transactional
public BankAccount findBankAccount(Long bankAccountId) {
if (bankAccountId == null) {
throw new BankAccountException(format(BANK_ACCOUNT_CONTEXT, SEARCHING_ACTION, FAILED_STATUS,
Optional.of(MISSING_ID_CAUSE), List.of(BANK_ACCOUNT_ID_DETAIL + "null")));
}

return bankAccountRepository.findById(bankAccountId)
.orElseThrow(() -> new BankAccountException(format(BANK_ACCOUNT_CONTEXT, SEARCHING_ACTION, FAILED_STATUS,
Optional.of(NOT_FOUND_CAUSE), List.of(BANK_ACCOUNT_ID_DETAIL + bankAccountId))));
Expand Down Expand Up @@ -85,38 +85,4 @@ public BankAccount updateBankAccount(BankAccount bankAccount) throws BankAccount
logger.debug(format(BANK_ACCOUNT_CONTEXT, UPDATE_ACTION, SUCCESS_STATUS));
return updatedBankAccount;
}

@Override
public void creditAmountToAccounts(Transaction transaction, BankAccount emitterBankAccount, BankAccount receiverBankAccount) {
BigDecimal euroAmount = ExchangeRateUtils.getEuroAmountFrom(transaction.getCurrency(), transaction.getAmount());
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(CREDIT_TRANSACTION_CONTEXT, CREDIT_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
List.of(TRANSACTION_ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
}
emitterBankAccount.getBalance().minus(Money.of(euroAmount));
receiverBankAccount.getBalance().plus(Money.of(euroAmount));
}

@Override
public void depositAmountToAccount(Transaction transaction, BankAccount emitterBankAccount) {
BigDecimal euroAmount = ExchangeRateUtils.getEuroAmountFrom(transaction.getCurrency(), transaction.getAmount());
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(DEBIT_TRANSACTION_CONTEXT, DEBIT_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
List.of(TRANSACTION_ID_DETAIL + transaction.getId(), EURO_AMOUNT_DETAIL + euroAmount)));
}
emitterBankAccount.getBalance().plus(Money.of(euroAmount));
}

@Override
public void withdrawAmountToAccount(Transaction transaction, BankAccount emitterBankAccount) {
BigDecimal euroAmount = ExchangeRateUtils.getEuroAmountFrom(transaction.getCurrency(), transaction.getAmount());
if (isNotPositive(euroAmount)) {
throw new TransactionException(format(WITHDRAW_TRANSACTION_CONTEXT, WITHDRAW_ACTION, FAILED_STATUS,
Optional.of(SHOULD_HAVE_POSITIVE_VALUE_CAUSE),
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 @@ -9,55 +9,64 @@
import io.hypersistence.utils.hibernate.type.json.JsonType;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.Type;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@Entity
@Table(schema = "basapp", name = "customers", uniqueConstraints = @UniqueConstraint(columnNames = "customer_id"))
public class CustomerEntity extends PanacheEntityBase {

@Id
@Column(name = "customer_id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customers_customer_id_seq_gen")
@SequenceGenerator(name = "customers_customer_id_seq_gen", sequenceName = "customers_customer_id_seq", allocationSize = 1, initialValue = 1)
private Long id;

@Column(name = "first_name", nullable = false)
private String firstName;

@Column(name = "last_name", nullable = false)
private String lastName;

@Column(name = "gender", nullable = false)
@Convert(converter = GenderConverter.class)
private Gender gender;

@Column(name = "marital_status", nullable = false)
@Convert(converter = MaritalStatusConverter.class)
private MaritalStatus maritalStatus;

@Column(name = "birthday", nullable = false)
private LocalDate birthdate;

@Column(name = "country", nullable = false)
private String country;

@Column(name = "address", nullable = false)
private String address;

@Column(name = "city", nullable = false)
private String city;

@Column(name = "email", nullable = false)
private String email;

@Column(name = "phone_number", nullable = false)
private String phoneNumber;

@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "bank_accounts_customers", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "account_id"))
Expand All @@ -67,108 +76,4 @@ public class CustomerEntity extends PanacheEntityBase {
@Column(name = "metadata", columnDefinition = "jsonb")
@ColumnTransformer(write = "?::jsonb")
private String metadata;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
this.gender = gender;
}

public MaritalStatus getMaritalStatus() {
return maritalStatus;
}

public void setMaritalStatus(MaritalStatus maritalStatus) {
this.maritalStatus = maritalStatus;
}

public LocalDate getBirthdate() {
return birthdate;
}

public void setBirthdate(LocalDate birthdate) {
this.birthdate = birthdate;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public List<BankAccountEntity> getAccounts() {
return accounts;
}

public void setAccounts(List<BankAccountEntity> accounts) {
this.accounts = accounts;
}

public String getMetadata() {
return metadata;
}

public void setMetadata(String metadata) {
this.metadata = metadata;
}
}
Loading

0 comments on commit 9736d8c

Please sign in to comment.