Skip to content

Commit

Permalink
Notifications mail a la confirmation de la commande #20 - correction …
Browse files Browse the repository at this point in the history
…tests unitaires
  • Loading branch information
benjaminpochat committed Jul 29, 2024
1 parent b3b89c2 commit 3ee3ed8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface NotificationService<T> {

String getBodyTemplatePath();

default public void notify(T object) {
default void notify(T object) {
String recipient = getRecipient(object);
String subject = getSubject(object);
String body = getBody(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Object[] getTemplateValues(Order order) {
Object[] bodyTemplateValues = {
order.getId(),
viandeEnDirectConfiguration.getCustomerFrontendUrl(),
order.getItems().stream().mapToDouble(item -> item.getQuantity() * item.getUnitPrice()).sum(),
order.getItems().stream().mapToDouble(item -> item.getQuantity() * item.getUnitPrice() * item.getPackageLot().getNetWeight()).sum(),
order.getItems().stream().mapToDouble(item -> item.getQuantity() * item.getPackageLot().getNetWeight()).sum(),
sale.getDeliveryStart(),
sale.getDeliveryStop(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void should_send_a_mail_correctly() {
mailBodyCaptor.capture());
String mailBody = mailBodyCaptor.getValue();
String cleanMailBody = mailBody.replaceAll("( {4})*", "");
assertThat(cleanMailBody).isEqualTo(String.format("<meta charset=\"UTF-8\"><div>La commande n° 1 a été enregistrée sur <a href='%s'>ViandeEnDirect.eu</a>.</div><div><ul><li>Client :<ul><li>Nom : John Doe</li><li>Email : [email protected]</li><li>Téléphone : 0102030405</li></ul></li><li>Montant de la commande : 1000.0 €TTC</li><li>Quantité commandée : 30.0 kg</li></ul></div>",viandeEnDirectConfiguration.getProducerFrontendUrl()));
assertThat(cleanMailBody).isEqualTo(String.format("<meta charset=\"UTF-8\"><div>La commande n° %s a été enregistrée sur <a href='%s'>ViandeEnDirect.eu</a>.</div><div><ul><li>Client :<ul><li>Nom : John Doe</li><li>Email : [email protected]</li><li>Téléphone : 0102030405</li></ul></li><li>Montant de la commande : 1000.0 €TTC</li><li>Quantité commandée : 30.0 kg</li></ul></div>",order.getId(), viandeEnDirectConfiguration.getProducerFrontendUrl()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.stripe.exception.StripeException;
import eu.viandeendirect.domains.payment.StripeService;
import eu.viandeendirect.domains.production.PackageLotRepository;
import eu.viandeendirect.domains.sale.SaleRepository;
import eu.viandeendirect.model.*;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -16,9 +17,6 @@
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Optional;

import static eu.viandeendirect.model.OrderStatus.PAYMENT_ABORTED;
import static eu.viandeendirect.model.OrderStatus.PAYMENT_PENDING;
import static java.util.regex.Pattern.*;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -45,6 +43,9 @@ public class TestOrder_createOrderPayment {
@Autowired
private OrderRepository orderRepository;

@Autowired
private SaleRepository saleRepository;

@Test
void should_persist_order_in_database() throws StripeException {
// given
Expand All @@ -58,6 +59,7 @@ void should_persist_order_in_database() throws StripeException {
PackageLot honeyLotMielDeColza = orderTestService.getHoneyPackageLot(honeyProducer, "miel de colza", 5, 0);

Order order = orderTestService.createOrder(beefLotSteaksVache, beefLotCoteVeau, honeyLotMielDeSapin, honeyLotMielDeColza, customer);
saleRepository.save(order.getSale());

var stripePayment = new StripePayment();
stripePayment.setCheckoutSessionId("test_success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import com.stripe.exception.StripeException;
import com.stripe.model.Event;
import com.stripe.model.checkout.Session;
import eu.viandeendirect.domains.order.OrderRepository;
import eu.viandeendirect.domains.order.OrderService;
import eu.viandeendirect.domains.order.OrderTestService;
import eu.viandeendirect.domains.order.*;
import eu.viandeendirect.domains.sale.SaleRepository;
import eu.viandeendirect.model.*;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -43,17 +42,29 @@ public class TestStripeEventHandler_handleStripeAccountEvent {
@MockBean
private StripeService stripeService;

@MockBean
private OrderNotificationToCustomerService orderNotificationToCustomerService;

@MockBean
private OrderNotificationToProducerService orderNotificationToProducerService;

@Autowired
private OrderRepository orderRepository;

@Autowired
private SaleRepository saleRepository;

@Test
void should_change_order_status_after_payment_is_completed() throws StripeException {
// given
Order order = orderTestService.createOrder();
saleRepository.save(order.getSale());

var stripePayment = new StripePayment();
stripePayment.setCheckoutSessionId("stripe-id-success");
when(stripeService.createPayment(any())).thenReturn(stripePayment);
doNothing().when(orderNotificationToCustomerService).notify(any());
doNothing().when(orderNotificationToProducerService).notify(any());
orderService.createOrderPayment(order);

Session checkoutSession = new Session();
Expand All @@ -80,6 +91,7 @@ void should_change_order_status_after_payment_is_completed() throws StripeExcept
void should_change_order_status_after_payment_is_aborted() throws StripeException {
// given
Order order = orderTestService.createOrder();
saleRepository.save(order.getSale());

var stripePayment = new StripePayment();
stripePayment.setCheckoutSessionId("stripe-id-aborted");
Expand Down

0 comments on commit 3ee3ed8

Please sign in to comment.