@Test public void shouldAddStockCardEntriesWithPositiveQuantityToStockCardIfReasonIsNotAdditive() throws Exception { setupStockData(); String stockAdjustmentReasonName = "Issue"; StockAdjustmentReason stockAdjustmentReason = new StockAdjustmentReason(); stockAdjustmentReason.setName(stockAdjustmentReasonName); stockAdjustmentReason.setAdditive(false); stockEvent1.setReasonName(stockAdjustmentReasonName); stockEvent2.setReasonName(stockAdjustmentReasonName); Long quantity1 = 1000L; Long quantity2 = 3000L; stockEvent1.setQuantity(quantity1); stockEvent2.setQuantity(quantity2); when(facilityRepository.getById(facilityId)).thenReturn(defaultFacility); when(productService.getByCode(productCode)).thenReturn(defaultProduct); when(stockAdjustmentReasonRepository.getAdjustmentReasonByName(stockAdjustmentReasonName)).thenReturn(stockAdjustmentReason); StockCard expectedStockCard = StockCard.createZeroedStockCard(defaultFacility, defaultProduct); when(stockCardService.getOrCreateStockCard(facilityId, productCode)).thenReturn(expectedStockCard); List<StockCardEntry> stockCardEntries = restStockCardService.adjustStock(facilityId, stockEventList, userId); assertThat(stockCardEntries.get(0).getQuantity(), is(-1000L)); assertThat(stockCardEntries.get(1).getQuantity(), is(-3000L)); }
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);  }