@Test public void shouldCreateOrder() { when(restaurantRepository.findById(AJANTA_ID)).thenReturn(Optional.of(AJANTA_RESTAURANT)); when(orderRepository.save(any(Order.class))).then(invocation -> { Order order = (Order) invocation.getArguments()[0]; order.setId(ORDER_ID); return order; }); Order order = orderService.createOrder(CONSUMER_ID, AJANTA_ID, OrderDetailsMother.DELIVERY_INFORMATION, CHICKEN_VINDALOO_MENU_ITEMS_AND_QUANTITIES); verify(orderRepository).save(same(order)); verify(orderAggregateEventPublisher).publish(order, Collections.singletonList(new OrderCreatedEvent(CHICKEN_VINDALOO_ORDER_DETAILS, OrderDetailsMother.DELIVERY_ADDRESS, RestaurantMother.AJANTA_RESTAURANT_NAME))); verify(sagaInstanceFactory).create(createOrderSaga, new CreateOrderSagaState(ORDER_ID, CHICKEN_VINDALOO_ORDER_DETAILS)); }
@Transactional public Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation, List<MenuItemIdAndQuantity> lineItems) { Restaurant restaurant = restaurantRepository.findById(restaurantId) .orElseThrow(() -> new RestaurantNotFoundException(restaurantId)); List<OrderLineItem> orderLineItems = makeOrderLineItems(lineItems, restaurant); ResultWithDomainEvents<Order, OrderDomainEvent> orderAndEvents = Order.createOrder(consumerId, restaurant, deliveryInformation, orderLineItems); Order order = orderAndEvents.result; orderRepository.save(order); orderAggregateEventPublisher.publish(order, orderAndEvents.events); OrderDetails orderDetails = new OrderDetails(consumerId, restaurantId, orderLineItems, order.getOrderTotal()); CreateOrderSagaState data = new CreateOrderSagaState(order.getId(), orderDetails); sagaInstanceFactory.create(createOrderSaga, data); meterRegistry.ifPresent(mr -> mr.counter("placed_orders").increment()); return order; }
OrderService { @Transactional public Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation, List<MenuItemIdAndQuantity> lineItems) { Restaurant restaurant = restaurantRepository.findById(restaurantId) .orElseThrow(() -> new RestaurantNotFoundException(restaurantId)); List<OrderLineItem> orderLineItems = makeOrderLineItems(lineItems, restaurant); ResultWithDomainEvents<Order, OrderDomainEvent> orderAndEvents = Order.createOrder(consumerId, restaurant, deliveryInformation, orderLineItems); Order order = orderAndEvents.result; orderRepository.save(order); orderAggregateEventPublisher.publish(order, orderAndEvents.events); OrderDetails orderDetails = new OrderDetails(consumerId, restaurantId, orderLineItems, order.getOrderTotal()); CreateOrderSagaState data = new CreateOrderSagaState(order.getId(), orderDetails); sagaInstanceFactory.create(createOrderSaga, data); meterRegistry.ifPresent(mr -> mr.counter("placed_orders").increment()); return order; } }
OrderService { @Transactional public Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation, List<MenuItemIdAndQuantity> lineItems) { Restaurant restaurant = restaurantRepository.findById(restaurantId) .orElseThrow(() -> new RestaurantNotFoundException(restaurantId)); List<OrderLineItem> orderLineItems = makeOrderLineItems(lineItems, restaurant); ResultWithDomainEvents<Order, OrderDomainEvent> orderAndEvents = Order.createOrder(consumerId, restaurant, deliveryInformation, orderLineItems); Order order = orderAndEvents.result; orderRepository.save(order); orderAggregateEventPublisher.publish(order, orderAndEvents.events); OrderDetails orderDetails = new OrderDetails(consumerId, restaurantId, orderLineItems, order.getOrderTotal()); CreateOrderSagaState data = new CreateOrderSagaState(order.getId(), orderDetails); sagaInstanceFactory.create(createOrderSaga, data); meterRegistry.ifPresent(mr -> mr.counter("placed_orders").increment()); return order; } OrderService(SagaInstanceFactory sagaInstanceFactory,
                      OrderRepository orderRepository,
                      DomainEventPublisher eventPublisher,
                      RestaurantRepository restaurantRepository,
                      CreateOrderSaga createOrderSaga,
                      CancelOrderSaga cancelOrderSaga,
                      ReviseOrderSaga reviseOrderSaga,
                      OrderDomainEventPublisher orderAggregateEventPublisher,
                      Optional<MeterRegistry> meterRegistry); }
OrderService { @Transactional public Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation, List<MenuItemIdAndQuantity> lineItems) { Restaurant restaurant = restaurantRepository.findById(restaurantId) .orElseThrow(() -> new RestaurantNotFoundException(restaurantId)); List<OrderLineItem> orderLineItems = makeOrderLineItems(lineItems, restaurant); ResultWithDomainEvents<Order, OrderDomainEvent> orderAndEvents = Order.createOrder(consumerId, restaurant, deliveryInformation, orderLineItems); Order order = orderAndEvents.result; orderRepository.save(order); orderAggregateEventPublisher.publish(order, orderAndEvents.events); OrderDetails orderDetails = new OrderDetails(consumerId, restaurantId, orderLineItems, order.getOrderTotal()); CreateOrderSagaState data = new CreateOrderSagaState(order.getId(), orderDetails); sagaInstanceFactory.create(createOrderSaga, data); meterRegistry.ifPresent(mr -> mr.counter("placed_orders").increment()); return order; } OrderService(SagaInstanceFactory sagaInstanceFactory,
                      OrderRepository orderRepository,
                      DomainEventPublisher eventPublisher,
                      RestaurantRepository restaurantRepository,
                      CreateOrderSaga createOrderSaga,
                      CancelOrderSaga cancelOrderSaga,
                      ReviseOrderSaga reviseOrderSaga,
                      OrderDomainEventPublisher orderAggregateEventPublisher,
                      Optional<MeterRegistry> meterRegistry); @Transactional Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation,
                           List<MenuItemIdAndQuantity> lineItems); Optional<Order> confirmChangeLineItemQuantity(Long orderId, OrderRevision orderRevision); void noteReversingAuthorization(Long orderId); @Transactional Order cancel(Long orderId); void approveOrder(long orderId); void rejectOrder(long orderId); void beginCancel(long orderId); void undoCancel(long orderId); void confirmCancelled(long orderId); @Transactional Order reviseOrder(long orderId, OrderRevision orderRevision); Optional<RevisedOrder> beginReviseOrder(long orderId, OrderRevision revision); void undoPendingRevision(long orderId); void confirmRevision(long orderId, OrderRevision revision); void createMenu(long id, String name, List<MenuItem> menuItems); void reviseMenu(long id, List<MenuItem> menuItems); }
OrderService { @Transactional public Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation, List<MenuItemIdAndQuantity> lineItems) { Restaurant restaurant = restaurantRepository.findById(restaurantId) .orElseThrow(() -> new RestaurantNotFoundException(restaurantId)); List<OrderLineItem> orderLineItems = makeOrderLineItems(lineItems, restaurant); ResultWithDomainEvents<Order, OrderDomainEvent> orderAndEvents = Order.createOrder(consumerId, restaurant, deliveryInformation, orderLineItems); Order order = orderAndEvents.result; orderRepository.save(order); orderAggregateEventPublisher.publish(order, orderAndEvents.events); OrderDetails orderDetails = new OrderDetails(consumerId, restaurantId, orderLineItems, order.getOrderTotal()); CreateOrderSagaState data = new CreateOrderSagaState(order.getId(), orderDetails); sagaInstanceFactory.create(createOrderSaga, data); meterRegistry.ifPresent(mr -> mr.counter("placed_orders").increment()); return order; } OrderService(SagaInstanceFactory sagaInstanceFactory,
                      OrderRepository orderRepository,
                      DomainEventPublisher eventPublisher,
                      RestaurantRepository restaurantRepository,
                      CreateOrderSaga createOrderSaga,
                      CancelOrderSaga cancelOrderSaga,
                      ReviseOrderSaga reviseOrderSaga,
                      OrderDomainEventPublisher orderAggregateEventPublisher,
                      Optional<MeterRegistry> meterRegistry); @Transactional Order createOrder(long consumerId, long restaurantId, DeliveryInformation deliveryInformation,
                           List<MenuItemIdAndQuantity> lineItems); Optional<Order> confirmChangeLineItemQuantity(Long orderId, OrderRevision orderRevision); void noteReversingAuthorization(Long orderId); @Transactional Order cancel(Long orderId); void approveOrder(long orderId); void rejectOrder(long orderId); void beginCancel(long orderId); void undoCancel(long orderId); void confirmCancelled(long orderId); @Transactional Order reviseOrder(long orderId, OrderRevision orderRevision); Optional<RevisedOrder> beginReviseOrder(long orderId, OrderRevision revision); void undoPendingRevision(long orderId); void confirmRevision(long orderId, OrderRevision revision); void createMenu(long id, String name, List<MenuItem> menuItems); void reviseMenu(long id, List<MenuItem> menuItems);  }