@Transactional
public OrderDTO insert(OrderDTO dto) {
Order order = new Order();
order.setMoment(Instant.now());
order.setStatus(OrderStatus.WAITING_PAYMENT);
User user = userService.authenticated();
order.setClient(user);
for (OrderItemDTO itemDto : dto.getItems()) {
Product product = productRepository.getReferenceById(itemDto.getProductId());
OrderItem item = new OrderItem(order, product, itemDto.getQuantity(), product.getPrice());
order.getItems().add(item);
}
repository.save(order);
orderItemRepository.saveAll(order.getItems());
return new OrderDTO(order);
}
Mockaremos o UserService, o ProductRepository, OrderItemRepository e UserService com o @Mock, visto que tudo isso é usado no método acima.
Além disso, criar um existingProductId, nonExistingProductId e um Product utilizando a Factory.