@Test public void shouldThrowDataExceptionIfProductIsInvalid() throws Exception { setupStockData(); expectedException.expect(DataException.class); expectedException.expectMessage("error.product.unknown"); when(facilityRepository.getById(facilityId)).thenReturn(defaultFacility); when(productService.getByCode(productCode)).thenReturn(null); restStockCardService.adjustStock(facilityId, stockEventList, userId); }
public List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId) { if (!validFacility(facilityId)) { throw new DataException("error.facility.unknown"); } Map<String, StockCard> stockCardMap = new HashMap<>(); List<StockCardEntry> entries = new ArrayList<>(); for (StockEvent stockEvent : stockEventList) { String errorInStockEvent = validateStockEvent(stockEvent); if (errorInStockEvent != null) { throw new DataException(errorInStockEvent); } String productCode = stockEvent.getProductCode(); StockCard stockCard; if (stockCardMap.get(productCode) == null) { stockCard = stockCardService.getOrCreateStockCard(facilityId, productCode); stockCardMap.put(productCode, stockCard); } else { stockCard = stockCardMap.get(productCode); } StockCardEntry entry = createStockCardEntry(stockEvent, stockCard, userId); entries.add(entry); } stockCardService.addStockCardEntries(entries); return entries; }
RestStockCardService { public List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId) { if (!validFacility(facilityId)) { throw new DataException("error.facility.unknown"); } Map<String, StockCard> stockCardMap = new HashMap<>(); List<StockCardEntry> entries = new ArrayList<>(); for (StockEvent stockEvent : stockEventList) { String errorInStockEvent = validateStockEvent(stockEvent); if (errorInStockEvent != null) { throw new DataException(errorInStockEvent); } String productCode = stockEvent.getProductCode(); StockCard stockCard; if (stockCardMap.get(productCode) == null) { stockCard = stockCardService.getOrCreateStockCard(facilityId, productCode); stockCardMap.put(productCode, stockCard); } else { stockCard = stockCardMap.get(productCode); } StockCardEntry entry = createStockCardEntry(stockEvent, stockCard, userId); entries.add(entry); } stockCardService.addStockCardEntries(entries); return entries; } }
RestStockCardService { public List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId) { if (!validFacility(facilityId)) { throw new DataException("error.facility.unknown"); } Map<String, StockCard> stockCardMap = new HashMap<>(); List<StockCardEntry> entries = new ArrayList<>(); for (StockEvent stockEvent : stockEventList) { String errorInStockEvent = validateStockEvent(stockEvent); if (errorInStockEvent != null) { throw new DataException(errorInStockEvent); } String productCode = stockEvent.getProductCode(); StockCard stockCard; if (stockCardMap.get(productCode) == null) { stockCard = stockCardService.getOrCreateStockCard(facilityId, productCode); stockCardMap.put(productCode, stockCard); } else { stockCard = stockCardMap.get(productCode); } StockCardEntry entry = createStockCardEntry(stockEvent, stockCard, userId); entries.add(entry); } stockCardService.addStockCardEntries(entries); return entries; }  }
RestStockCardService { public List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId) { if (!validFacility(facilityId)) { throw new DataException("error.facility.unknown"); } Map<String, StockCard> stockCardMap = new HashMap<>(); List<StockCardEntry> entries = new ArrayList<>(); for (StockEvent stockEvent : stockEventList) { String errorInStockEvent = validateStockEvent(stockEvent); if (errorInStockEvent != null) { throw new DataException(errorInStockEvent); } String productCode = stockEvent.getProductCode(); StockCard stockCard; if (stockCardMap.get(productCode) == null) { stockCard = stockCardService.getOrCreateStockCard(facilityId, productCode); stockCardMap.put(productCode, stockCard); } else { stockCard = stockCardMap.get(productCode); } StockCardEntry entry = createStockCardEntry(stockEvent, stockCard, userId); entries.add(entry); } stockCardService.addStockCardEntries(entries); return entries; }  List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId); }
RestStockCardService { public List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId) { if (!validFacility(facilityId)) { throw new DataException("error.facility.unknown"); } Map<String, StockCard> stockCardMap = new HashMap<>(); List<StockCardEntry> entries = new ArrayList<>(); for (StockEvent stockEvent : stockEventList) { String errorInStockEvent = validateStockEvent(stockEvent); if (errorInStockEvent != null) { throw new DataException(errorInStockEvent); } String productCode = stockEvent.getProductCode(); StockCard stockCard; if (stockCardMap.get(productCode) == null) { stockCard = stockCardService.getOrCreateStockCard(facilityId, productCode); stockCardMap.put(productCode, stockCard); } else { stockCard = stockCardMap.get(productCode); } StockCardEntry entry = createStockCardEntry(stockEvent, stockCard, userId); entries.add(entry); } stockCardService.addStockCardEntries(entries); return entries; }  List<StockCardEntry> adjustStock(Long facilityId, List<StockEvent> stockEventList, Long userId);  }