@Test public void shouldThrowExceptionIfInvalidReplacedProductCode() throws Exception { Long productId = 2L; Long programId = 1L; ShipmentLineItem shipmentLineItem = spy(make(a(defaultShipmentLineItem, with(replacedProductCode, "P10"), with(orderId, 1L), with(quantityShipped, 20)))); when(requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), "P10")).thenReturn(null); when(requisitionService.getProgramId(shipmentLineItem.getOrderId())).thenReturn(programId); Product product = make(a(defaultProduct)); product.setId(productId); when(productService.getByCode("P123")).thenReturn(product); when(productService.getByCode("P10")).thenReturn(null); ProgramProduct programProduct = new ProgramProduct(); when(programproductService.getByProgramAndProductId(programId, productId)).thenReturn(programProduct); doNothing().when(shipmentLineItem).fillReferenceFields(programProduct); exException.expect(DataException.class); exException.expectMessage("error.unknown.product"); shipmentService.save(shipmentLineItem); }
public void save(ShipmentLineItem shipmentLineItem) { if (shipmentLineItem.getQuantityShipped() < 0) { throw new DataException("error.negative.shipped.quantity"); } RnrLineItem lineItem; if ((lineItem = requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), shipmentLineItem.getProductCode())) != null) { shipmentLineItem.fillReferenceFields(lineItem); } else { Product product = productService.getByCode(shipmentLineItem.getProductCode()); if (product == null) { throw new DataException("error.unknown.product"); } Long programId = requisitionService.getProgramId(shipmentLineItem.getOrderId()); ProgramProduct programProduct = programProductService.getByProgramAndProductId(programId, product.getId()); if (programProduct != null) { programProduct.setProduct(product); shipmentLineItem.fillReferenceFields(programProduct); } else { shipmentLineItem.fillReferenceFields(product); } } if (shipmentLineItem.getReplacedProductCode() != null) { if (productService.getByCode(shipmentLineItem.getReplacedProductCode()) == null) { throw new DataException("error.unknown.product"); } } shipmentRepository.save(shipmentLineItem); }
ShipmentService { public void save(ShipmentLineItem shipmentLineItem) { if (shipmentLineItem.getQuantityShipped() < 0) { throw new DataException("error.negative.shipped.quantity"); } RnrLineItem lineItem; if ((lineItem = requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), shipmentLineItem.getProductCode())) != null) { shipmentLineItem.fillReferenceFields(lineItem); } else { Product product = productService.getByCode(shipmentLineItem.getProductCode()); if (product == null) { throw new DataException("error.unknown.product"); } Long programId = requisitionService.getProgramId(shipmentLineItem.getOrderId()); ProgramProduct programProduct = programProductService.getByProgramAndProductId(programId, product.getId()); if (programProduct != null) { programProduct.setProduct(product); shipmentLineItem.fillReferenceFields(programProduct); } else { shipmentLineItem.fillReferenceFields(product); } } if (shipmentLineItem.getReplacedProductCode() != null) { if (productService.getByCode(shipmentLineItem.getReplacedProductCode()) == null) { throw new DataException("error.unknown.product"); } } shipmentRepository.save(shipmentLineItem); } }
ShipmentService { public void save(ShipmentLineItem shipmentLineItem) { if (shipmentLineItem.getQuantityShipped() < 0) { throw new DataException("error.negative.shipped.quantity"); } RnrLineItem lineItem; if ((lineItem = requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), shipmentLineItem.getProductCode())) != null) { shipmentLineItem.fillReferenceFields(lineItem); } else { Product product = productService.getByCode(shipmentLineItem.getProductCode()); if (product == null) { throw new DataException("error.unknown.product"); } Long programId = requisitionService.getProgramId(shipmentLineItem.getOrderId()); ProgramProduct programProduct = programProductService.getByProgramAndProductId(programId, product.getId()); if (programProduct != null) { programProduct.setProduct(product); shipmentLineItem.fillReferenceFields(programProduct); } else { shipmentLineItem.fillReferenceFields(product); } } if (shipmentLineItem.getReplacedProductCode() != null) { if (productService.getByCode(shipmentLineItem.getReplacedProductCode()) == null) { throw new DataException("error.unknown.product"); } } shipmentRepository.save(shipmentLineItem); }  }
ShipmentService { public void save(ShipmentLineItem shipmentLineItem) { if (shipmentLineItem.getQuantityShipped() < 0) { throw new DataException("error.negative.shipped.quantity"); } RnrLineItem lineItem; if ((lineItem = requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), shipmentLineItem.getProductCode())) != null) { shipmentLineItem.fillReferenceFields(lineItem); } else { Product product = productService.getByCode(shipmentLineItem.getProductCode()); if (product == null) { throw new DataException("error.unknown.product"); } Long programId = requisitionService.getProgramId(shipmentLineItem.getOrderId()); ProgramProduct programProduct = programProductService.getByProgramAndProductId(programId, product.getId()); if (programProduct != null) { programProduct.setProduct(product); shipmentLineItem.fillReferenceFields(programProduct); } else { shipmentLineItem.fillReferenceFields(product); } } if (shipmentLineItem.getReplacedProductCode() != null) { if (productService.getByCode(shipmentLineItem.getReplacedProductCode()) == null) { throw new DataException("error.unknown.product"); } } shipmentRepository.save(shipmentLineItem); }  void save(ShipmentLineItem shipmentLineItem); void insertShipmentFileInfo(ShipmentFileInfo shipmentFileInfo); List<ShipmentLineItem> getLineItems(long orderId); }
ShipmentService { public void save(ShipmentLineItem shipmentLineItem) { if (shipmentLineItem.getQuantityShipped() < 0) { throw new DataException("error.negative.shipped.quantity"); } RnrLineItem lineItem; if ((lineItem = requisitionService.getNonSkippedLineItem(shipmentLineItem.getOrderId(), shipmentLineItem.getProductCode())) != null) { shipmentLineItem.fillReferenceFields(lineItem); } else { Product product = productService.getByCode(shipmentLineItem.getProductCode()); if (product == null) { throw new DataException("error.unknown.product"); } Long programId = requisitionService.getProgramId(shipmentLineItem.getOrderId()); ProgramProduct programProduct = programProductService.getByProgramAndProductId(programId, product.getId()); if (programProduct != null) { programProduct.setProduct(product); shipmentLineItem.fillReferenceFields(programProduct); } else { shipmentLineItem.fillReferenceFields(product); } } if (shipmentLineItem.getReplacedProductCode() != null) { if (productService.getByCode(shipmentLineItem.getReplacedProductCode()) == null) { throw new DataException("error.unknown.product"); } } shipmentRepository.save(shipmentLineItem); }  void save(ShipmentLineItem shipmentLineItem); void insertShipmentFileInfo(ShipmentFileInfo shipmentFileInfo); List<ShipmentLineItem> getLineItems(long orderId);  }