Skip to content

Commit

Permalink
task#57: Add lombok, set Transaction as a @builder and adapt unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGib committed Feb 20, 2024
1 parent a84f528 commit ca1d18c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public class TransactionMapper implements DtoEntityMapper<Transaction, Transacti
ObjectMapper objectMapper;

public Transaction toDto(TransactionEntity entity) {
Transaction dto = new Transaction();
dto.setId(entity.getId());
Transaction dto = Transaction.builder()
.id(entity.getId())
.type(entity.getType())
.currency(entity.getCurrency())
.date(entity.getDate())
.label(entity.getLabel())
.build();

if (entity.getSenderBankAccountEntity() != null) {
dto.setSenderAccountId(entity.getSenderBankAccountEntity().getId());
Expand All @@ -45,11 +50,6 @@ public Transaction toDto(TransactionEntity entity) {
if (entity.getAmount() != null) {
dto.setAmount(entity.getAmount());
}
dto.setCurrency(entity.getCurrency());
dto.setType(entity.getType());
dto.setStatus(entity.getStatus());
dto.setDate(entity.getDate());
dto.setLabel(entity.getLabel());

try {
if (entity.getMetadata() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import com.cdx.bas.domain.customer.MaritalStatus;
import com.cdx.bas.domain.money.Money;
import com.cdx.bas.domain.transaction.Transaction;
import com.cdx.bas.domain.transaction.TransactionStatus;
import com.cdx.bas.domain.transaction.TransactionType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import io.quarkus.test.InjectMock;
Expand All @@ -30,6 +28,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.cdx.bas.domain.transaction.TransactionStatus.ERROR;
import static com.cdx.bas.domain.transaction.TransactionType.CREDIT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -250,18 +250,18 @@ private BankAccountEntity createBankAccountEntity(long id, Instant instantDate)
bankAccountEntity.setIssuedTransactions(transactionEntities);
return bankAccountEntity;
}
private Transaction createTransaction(long id, long accountId, Instant instantDate) {
Transaction transactionEntity = new Transaction();
transactionEntity.setId(id);
transactionEntity.setSenderAccountId(accountId);
transactionEntity.setReceiverAccountId(77L);
transactionEntity.setAmount(new BigDecimal(100));
transactionEntity.setType(TransactionType.CREDIT);
transactionEntity.setStatus(TransactionStatus.ERROR);
transactionEntity.setDate(instantDate);
transactionEntity.setLabel("transaction test");
return transactionEntity;

private Transaction createTransaction(long id, long senderAccountId, Instant instantDate) {
return Transaction.builder()
.id(id)
.type(CREDIT)
.senderAccountId(senderAccountId)
.receiverAccountId(77L)
.amount(new BigDecimal("100"))
.status(ERROR)
.date(instantDate)
.label("transaction test")
.build();
}

private TransactionEntity createTransactionEntity(long id, Instant instantDate) {
Expand All @@ -270,8 +270,8 @@ private TransactionEntity createTransactionEntity(long id, Instant instantDate)
transactionEntity.setSenderBankAccountEntity(null);
transactionEntity.setReceiverBankAccountEntity(null);
transactionEntity.setAmount(new BigDecimal("100"));
transactionEntity.setType(TransactionType.CREDIT);
transactionEntity.setStatus(TransactionStatus.ERROR);
transactionEntity.setType(CREDIT);
transactionEntity.setStatus(ERROR);
transactionEntity.setDate(instantDate);
transactionEntity.setLabel("transaction test");
return transactionEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ public void findById_shouldFindTransaction_whenIdIsFound() {
Instant expectedInstant = OffsetDateTime.of(
2024, 6, 6, 12, 0, 0, 0,
ZoneOffset.ofHours(1)).toInstant();
Optional<Transaction> expectedTransaction = Optional.of(new Transaction(1L, 1L, 2L,
new BigDecimal("1600.00"), "EUR", CREDIT, COMPLETED,
expectedInstant,
"transaction 1", Map.of("sender_amount_before", "2000", "receiver_amount_before", "0", "sender_amount_after", "400", "receiver_amount_after", "1600")));
Optional<Transaction> expectedTransaction = Optional.of(Transaction.builder()
.id(1L)
.senderAccountId(1L)
.receiverAccountId(2L)
.amount(new BigDecimal("1600.00"))
.currency("EUR")
.type(CREDIT)
.status(COMPLETED)
.date(expectedInstant)
.label("transaction 1")
.metadata(Map.of("sender_amount_before", "2000", "receiver_amount_before", "0", "sender_amount_after", "400", "receiver_amount_after", "1600"))
.build());
Optional<Transaction> actualTransaction = transactionRepository.findById(1);

// Assert
Expand All @@ -56,7 +64,20 @@ public void findUnprocessedTransactions_shouldFindEveryUnprocessedTransactions()
Queue<Transaction> actualUnprocessedTransactions = transactionRepository.findUnprocessedTransactions();

Queue<Transaction> expectedUnprocessedTransactions = new PriorityQueue<>();
expectedUnprocessedTransactions.add(new Transaction(5L, 2L, 1L, new BigDecimal("600.99"), "EUR", CREDIT, UNPROCESSED, Instant.parse("2024-11-06T17:00:00+00:00"), "transaction 5", new HashMap<>()));
expectedUnprocessedTransactions.add(Transaction.builder()
.id(5L)
.senderAccountId(2L)
.receiverAccountId(1L)
.amount(new BigDecimal("600.99"))
.currency("EUR")
.type(CREDIT)
.status(UNPROCESSED)
.date(Instant.parse("2024-11-06T17:00:00+00:00"))
.label("transaction 5")
.metadata(new HashMap<>())
.build());

//TODO
expectedUnprocessedTransactions.add(new Transaction(6L, 1L, 7L, new BigDecimal("2000.00"), "EUR", DEBIT, UNPROCESSED, Instant.parse("2024-11-06T17:30:00+00:00"), "transaction 6", new HashMap<>()));
expectedUnprocessedTransactions.add(new Transaction(7L, 3L, 1L, new BigDecimal("1000.00"), "EUR", CREDIT, UNPROCESSED, Instant.parse("2024-12-06T17:00:00+00:00"), "transaction 7", new HashMap<>()));
expectedUnprocessedTransactions.add(new Transaction(8L, 4L, 2L, new BigDecimal("300.80"), "EUR", DEBIT, UNPROCESSED, Instant.parse("2024-12-06T18:00:00+00:00"), "transaction 8", new HashMap<>()));
Expand Down
6 changes: 6 additions & 0 deletions domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
114 changes: 6 additions & 108 deletions domain/src/main/java/com/cdx/bas/domain/transaction/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.cdx.bas.domain.validator.ValidCurrency;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
@Builder
public class Transaction implements Comparable<Transaction> {

@NotNull(message = "id must not be null.")
Expand Down Expand Up @@ -43,115 +47,9 @@ public class Transaction implements Comparable<Transaction> {

private Map<String, String> metadata = new HashMap<>();

public Transaction() {
super();
}

public Transaction(long accountId, String currency, BigDecimal amount, TransactionType type) {
this.id = accountId;
this.amount = amount;
this.currency = currency;
this.type = type;
this.status = TransactionStatus.UNPROCESSED;
this.date = Instant.now();
}

public Transaction(Long id, Long senderAccountId, Long receiverAccountId, BigDecimal amount, String currency, TransactionType type, TransactionStatus status, Instant date, String label, Map<String, String> metadata) {
this.id = id;
this.senderAccountId = senderAccountId;
this.receiverAccountId = receiverAccountId;
this.amount = amount;
this.currency = currency;
this.type = type;
this.status = status;
this.date = date;
this.label = label;
this.metadata = metadata;
}

public Long getId() {
return id;
}

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

public Long getSenderAccountId() {
return senderAccountId;
}

public void setSenderAccountId(Long senderAccountId) {
this.senderAccountId = senderAccountId;
}

public Long getReceiverAccountId() {
return receiverAccountId;
}

public void setReceiverAccountId(Long receiverAccountId) {
this.receiverAccountId = receiverAccountId;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

public String getCurrency() {
return currency;
}

public void setCurrency(String currency) {
this.currency = currency;
}

public TransactionType getType() {
return type;
}

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

public TransactionStatus getStatus() {
return status;
}

public void setStatus(TransactionStatus status) {
this.status = status;
}

public Instant getDate() {
return date;
}

public void setDate(Instant date) {
this.date = date;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

@Override
public int compareTo(Transaction transactionToCompar) {
return this.getDate().compareTo(transactionToCompar.getDate());
public int compareTo(Transaction transactionToCompare) {
return this.getDate().compareTo(transactionToCompare.getDate());
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<jandex-maven-plugin.version>3.1.5</jandex-maven-plugin.version>
<compiler-plugin.version>3.11.0</compiler-plugin.version>
<assertj.version>3.25.3</assertj.version>
<lombok.version>1.18.30</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -92,6 +93,11 @@
<artifactId>quarkus-test-h2</artifactId>
<version>${quarkus.platform.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
Expand Down

0 comments on commit ca1d18c

Please sign in to comment.