-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for PaymentController in localeat/core
- Loading branch information
1 parent
48b629b
commit bc90df6
Showing
7 changed files
with
233 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/main/java/com/localeat/core/domains/payment/PaymentTransactionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.localeat.core.domains.payment; | ||
|
||
import com.localeat.core.domains.delivery.QuantitySoldForDeliveryService; | ||
import com.localeat.core.domains.order.Order; | ||
import com.localeat.core.domains.order.OrderService; | ||
import com.localeat.core.domains.security.Account; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static com.localeat.core.domains.order.OrderStatus.SUBMITTED; | ||
|
||
@Service | ||
abstract class PaymentTransactionService { | ||
|
||
@Autowired | ||
PaymentRepository paymentRepository; | ||
|
||
@Autowired | ||
private OrderService orderService; | ||
|
||
@Autowired | ||
private QuantitySoldForDeliveryService quantitySoldForDeliveryService; | ||
|
||
public Payment createPayment(Account account, Order order) { | ||
var payment = new Payment(); | ||
payment.setOrder(order); | ||
payment.setAmount(orderService.getTotalPrice(order)); | ||
payment.setStatus(PaymentStatus.PROCESSING); | ||
createPaymentTransaction(payment, account); | ||
return paymentRepository.save(payment); | ||
} | ||
|
||
abstract void createPaymentTransaction(Payment payment, Account account); | ||
|
||
public void updatePayment(String transactionId) { | ||
PaymentStatus paymentStatus = fetchPaymentStatus(transactionId.trim()); | ||
Payment payment = paymentRepository.findByTransactionId(transactionId.trim()); | ||
Order order = payment.getOrder(); | ||
payment.setStatus(paymentStatus); | ||
payment = paymentRepository.save(payment); | ||
if (order.getStatus().equals(SUBMITTED) && payment.isValidated()) { | ||
orderService.confirmOrder(payment.getOrder()); | ||
} else if (order.getStatus().equals(SUBMITTED) && payment.isFailed()) { | ||
orderService.abortOrder(payment.getOrder()); | ||
} | ||
quantitySoldForDeliveryService.updateQuantitySoldInBatches(order.getDelivery()); | ||
} | ||
|
||
abstract PaymentStatus fetchPaymentStatus(String transactionId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/src/test/java/com/localeat/core/domains/payment/MockPaymentTransactionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.localeat.core.domains.payment; | ||
|
||
import com.localeat.core.domains.security.Account; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static com.localeat.core.domains.payment.PaymentStatus.ABORTED; | ||
import static com.localeat.core.domains.payment.PaymentStatus.VALIDATED; | ||
|
||
@Service | ||
@Profile("test") | ||
public class MockPaymentTransactionService extends PaymentTransactionService{ | ||
|
||
@Override | ||
public void createPaymentTransaction(Payment payment, Account account) { | ||
payment.setTransactionId(String.valueOf(payment.getOrder().getOrderedItems().size())); | ||
} | ||
|
||
@Override | ||
public PaymentStatus fetchPaymentStatus(String transactionId) { | ||
return Integer.valueOf(transactionId) % 2 == 0 ? VALIDATED : ABORTED; | ||
} | ||
} |
Oops, something went wrong.