### Question:
PriceEngineImpl implements PriceEngine { @Override public void computePrice(Order order) { if (CollectionUtils.isEmpty(order.getItems())){ throw new IllegalStateException("Order items list is empty "+order); } Double price = 0.0; for (OrderItem orderItem : order.getItems()){ SKU sku = entityManager.find(SKU.class,(orderItem).getSkuId()); price += (sku.getPrice()*(orderItem).getQuantity()); orderItem.setPrice(sku.getPrice()); } final Double fixedDeliveryFee = orderConfiguration.getFixedDeliveryFee(); price = applyEligibleDiscounts(order, price); if (fixedDeliveryFee != null){ price += fixedDeliveryFee; order.setDeliveryFee(fixedDeliveryFee); } order.setPrice(price); } PriceEngineImpl();  PriceEngineImpl(EntityManager entityManager, OrderConfiguration orderConfiguration,
                           OrderFinder orderFinder, DiscountFinder discountFinder); @Override void computePrice(Order order);  }### Answer:
@Test public void computePrice_ShouldThrowEx_whenOrderHasNoItems() { Order order = new Order(); try { orderPriceEngine.computePrice(order); fail("Should have thrown ex"); } catch (IllegalStateException e) { } }
@Test public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantityWithFixedDeliveryFee() { SKU sku = new SKU(); sku.setPrice(10.0); SKU sku2 = new SKU(); sku2.setPrice(20.0); when(entityManager.find(SKU.class, 1L)).thenReturn(sku); when(entityManager.find(SKU.class, 2L)).thenReturn(sku2); when(orderConfiguration.getFixedDeliveryFee()).thenReturn(11.0); Order order = new Order(); Set<OrderItem> items = new HashSet<>(); items.add(new OrderItem(1L, 1L, 1)); items.add(new OrderItem(2L, 2L, 2)); order.setItems(items); orderPriceEngine.computePrice(order); verify(entityManager).find(SKU.class, 1L); verify(entityManager).find(SKU.class, 2L); verify(orderConfiguration).getFixedDeliveryFee(); assertThat(order.getPrice()).isEqualTo(61.0); }
@Test public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantity() { SKU sku = new SKU(); sku.setPrice(10.0); SKU sku2 = new SKU(); sku2.setPrice(20.0); when(entityManager.find(SKU.class, 1L)).thenReturn(sku); when(entityManager.find(SKU.class, 2L)).thenReturn(sku2); when(orderConfiguration.getFixedDeliveryFee()).thenReturn(null); Order order = new Order(); Set<OrderItem> items = new HashSet<>(); items.add(new OrderItem(1L, 1L, 1)); items.add(new OrderItem(2L, 2L, 2)); order.setItems(items); orderPriceEngine.computePrice(order); verify(entityManager).find(SKU.class, 1L); verify(entityManager).find(SKU.class, 2L); verify(orderConfiguration).getFixedDeliveryFee(); assertThat(order.getPrice()).isEqualTo(50.0); for (OrderItem item : order.getItems()) { if (item.getSkuId().equals(1L)) assertThat(item.getPrice()).isEqualTo(10.0); else assertThat(item.getPrice()).isEqualTo(20.0); } }