Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/order coverage #9

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,38 @@ public class StartNewOrderUseCase {

public OrderDTO execute(
OrderGateway orderGateway, CustomerUseCase customerUseCase, String customerCpf) {
if (!CPF.isValid(customerCpf)) {
if (customerCpf != null && !CPF.isValid(customerCpf)) {
throw new InvalidCpfException(customerCpf);
}

Customer customer = findCustomerOrCreateAnonymous(customerUseCase, new CPF(customerCpf));
Order newOrder = Order.createFor(customer.getId());
return orderGateway.save(newOrder).toDTO();
if (customerCpf != null) {
Customer customer = findCustomer(customerUseCase, new CPF(customerCpf));
Order newOrder = Order.createFor(customer.getId());
return orderGateway.save(newOrder).toDTO();
} else {
Customer customer = CreateAnonymous(customerUseCase);
Order newOrder = Order.createFor(customer.getId());
return orderGateway.save(newOrder).toDTO();
}
}

public Customer findCustomerOrCreateAnonymous(
CustomerUseCase customerUseCase, CPF customerCpf) {
if (customerCpf != null) {
return customerUseCase.findCustomerByCpf(customerCpf.value());
public Customer findCustomer(CustomerUseCase customerUseCase, CPF customerCpf) {
return customerUseCase.findCustomerByCpf(customerCpf.value());
}

public Customer CreateAnonymous(CustomerUseCase customerUseCase) {

Customer anonymousCustomer = customerUseCase.findCustomerByCpf(Customer.ANONYMOUS_CPF);
if (anonymousCustomer != null) {
return anonymousCustomer;
} else {
Customer anonymousCustomer = customerUseCase.findCustomerByCpf(Customer.ANONYMOUS_CPF);
if (anonymousCustomer != null) {
return anonymousCustomer;
} else {
Customer newAnonymousCustomer =
Customer.create(
9999999999L,
"Anonymous",
Customer.ANONYMOUS_CPF,
"[email protected]");
return customerUseCase.save(newAnonymousCustomer);
}
Customer newAnonymousCustomer =
Customer.create(
9999999999L,
"Anonymous",
Customer.ANONYMOUS_CPF,
"[email protected]");
return customerUseCase.save(newAnonymousCustomer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ void shouldReturnOrderDTOWithCorrectId() {
assertThat(result.getOrderId()).isEqualTo(order.getId());
}

@Test
void shouldReturnOrderDTOWithoutCustomer() {
// Arrange
Order order = OrderHelper.createDefaultOrderStatus(DEFAULT_ORDERID, DEFAULT_ORDERSTATUS);

when(orderGateway.save(any(Order.class))).thenReturn(order);
when(customerUseCase.findCustomerByCpf(any(String.class)))
.thenReturn(CustomerHelper.createDefaultCustomerWithId(order.getCustomerId()));

// Act
OrderDTO result =
startNewOrderUseCase.execute(orderGateway, customerUseCase, DEFAULT_CUSTOMER_CPF);

// Assert
assertThat(result.getOrderId()).isEqualTo(order.getId());
}

@Test
void shouldReturnOrderDTOWithCorrectStatus() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ void shouldReturnOrderDTOWithCorrectCustomerName() throws Exception {
assertThat(result.getCustomerId()).isEqualTo(order.getCustomerId());
}

@Test
void shouldReturnOrderDTOWithoutCPF() throws Exception {
// Arrange
Order order = OrderHelper.createDefaultOrderStatus(DEFAULT_ORDERID, DEFAULT_ORDERSTATUS);

when(orderGateway.save(any(Order.class))).thenReturn(order);
when(customerUseCase.findCustomerByCpf(any(String.class)))
.thenReturn(CustomerHelper.createDefaultCustomerWithId(order.getCustomerId()));

// Act
OrderDTO result = startNewOrderUseCase.execute(orderGateway, customerUseCase, null);

// Assert
assertThat(result.getCustomerId()).isEqualTo(order.getCustomerId());
}

@Test
void shouldReturnOrderDTOAnonymousCPF() throws Exception {
// Arrange
Order order = OrderHelper.createDefaultOrderStatus(DEFAULT_ORDERID, DEFAULT_ORDERSTATUS);

when(orderGateway.save(any(Order.class))).thenReturn(order);
when(customerUseCase.findCustomerByCpf(any(String.class)))
.thenReturn(CustomerHelper.createDefaultCustomerWithId(order.getCustomerId()));

// Act
OrderDTO result = startNewOrderUseCase.execute(orderGateway, customerUseCase, null);

// Assert
assertThat(result.getCustomerId()).isEqualTo(order.getCustomerId());
}

@Test
void shouldReturnOrderDTOWithCorrectTotalPrice() throws Exception {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ void shouldInvokeStartNewOrderUseCase() throws Exception {
verify(getOrderUseCase, times(1)).execute(any(OrderGateway.class), eq(1L));
}

@Test
void shouldInvokeStartNewOrderUseCaseWithoutCustomer() throws Exception {
// Arrange
Long orderId = 1L;
OrderDTO orderDTO =
OrderHelper.createDefaultOrderDTOWithId(
1L, CustomerHelper.valid().getId(), ProductHelper.valid());
when(startNewOrderUseCase.execute(
any(OrderGateway.class), any(CustomerUseCase.class), eq(null)))
.thenReturn(orderDTO);

orderController.findById(orderId);

// Verify
verify(getOrderUseCase, times(1)).execute(any(OrderGateway.class), eq(1L));
}

@Test
void shouldInvokeAddProductToOrderUseCase() throws Exception {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,6 @@ public class OrderHelper {
private static final Long DEFAULT_PRODUCTID = 1L;
private static final Double DEFAULT_PRODUCT_PRICE = 10.50;

public static OrderDTO createOrderDTO(
Long id,
OrderStatus status,
Long customerId,
Collection<OrderItem> items,
Double totalPrice) {
return createOrderWithId(id, status, customerId, items, totalPrice).toDTO();
}

public static Order createOrderWithId(
Long id,
OrderStatus status,
Long customerId,
Collection<OrderItem> items,
Double totalPrice) {
return createOrderNew(id, status, customerId, items, totalPrice);
}

public static Order createOrderNew(
Long id,
OrderStatus status,
Long customerId,
Collection<OrderItem> items,
Double totalPrice) {
Order product = new Order(null, status, customerId, items, totalPrice);
product.setId(id);
return product;
}

public static OrderDTO createDefaultOrderDTOWithId(Long id, Long customerId, Long productId) {
return createDefaultOrderWithId(id, customerId, productId).toDTO();
}
Expand Down
Loading