### Question:
LossService { public void saveLoss(Loss loss) { GenericDao<Loss> lossDao = new GenericDao<>(Loss.class, context); lossDao.create(loss); saveLossItems(loss.getLossItems()); }  void saveLoss(Loss loss); List<UtilizationValue> getLossesValues(Commodity commodity, Date startDate, Date endDate);  }### Answer:
@Test public void shouldSaveLossToDatabase() throws Exception { Commodity commodity = commodityService.all().get(0); LossItem lossItem = new LossItem(commodity); Loss loss = new Loss(); loss.addLossItem(lossItem); lossService.saveLoss(loss); List<Loss> losses = lossDao.queryForAll(); assertThat(losses.size(), is(1)); Loss lossFromDb = losses.get(0); assertThat(lossFromDb.getLossItemsCollection().size(), is(1)); }
@Test public void shouldUpdateCommodityStockOnHandWhenSavingLoss() throws Exception { Commodity commodity = commodityService.all().get(0); int stockOnHand = commodity.getStockOnHand(); System.out.println("Current Stock on Hand is " + stockOnHand); int expectedStockOnHand = stockOnHand - 1; Loss loss = new Loss(); loss.addLossItem(new LossItem(commodity, 1)); lossService.saveLoss(loss); commodity = commodityService.all().get(0); assertThat(commodity.getStockOnHand(), is(expectedStockOnHand)); }
@Test public void shouldUpdateSnapShotWhenLossItemIsSaved() throws Exception { Commodity commodity = commodityService.all().get(0); assertThat(snapshotGenericDao.queryForAll().size(), is(0)); int stockOnHand = commodity.getStockOnHand(); int expectedStockOnHand = stockOnHand - 1; Loss loss = new Loss(); loss.addLossItem(new LossItem(commodity, 1)); lossService.saveLoss(loss); commodity = commodityService.all().get(0); assertThat(commodity.getStockOnHand(), is(expectedStockOnHand)); assertThat(snapshotGenericDao.queryForAll().size(), is(2)); }