@Test public void shouldNotSetIdIfSynchronizingANewRefrigerator() throws Exception { Refrigerator refrigerator = new Refrigerator("serialNumberNew"); Refrigerator existingRefrigerator = new Refrigerator("serialNumber"); existingRefrigerator.setId(refrigeratorId); RefrigeratorReading refrigeratorReading = new RefrigeratorReading(refrigerator); DistributionRefrigerators distributionRefrigerators = new DistributionRefrigerators(facilityVisit, asList(refrigeratorReading)); distributionRefrigerators.setCreatedBy(createdBy); when(refrigeratorService.getAllBy(facilityId)).thenReturn(asList(existingRefrigerator)); service.save(facilityId, distributionRefrigerators); assertThat(refrigerator.getId(), is(nullValue())); assertThat(refrigerator.getFacilityId(), is(facilityId)); verify(refrigeratorService).save(refrigerator); }
public void save(Long facilityId, DistributionRefrigerators distributionRefrigerators) { List<RefrigeratorReading> readings = distributionRefrigerators.getReadings(); refrigeratorService.disableAllFor(facilityId); if (readings.size() == 0) { return; } List<Refrigerator> refrigeratorsForFacility = refrigeratorService.getAllBy(facilityId); for (RefrigeratorReading reading : readings) { final Refrigerator refrigerator = reading.getRefrigerator(); Refrigerator existingRefrigerator = (Refrigerator) CollectionUtils.find(refrigeratorsForFacility, new Predicate() { @Override public boolean evaluate(Object o) { return ((Refrigerator) o).getSerialNumber().equals(refrigerator.getSerialNumber()); } }); refrigerator.setEnabled(true); if (existingRefrigerator != null) { refrigerator.setId(existingRefrigerator.getId()); } else { refrigerator.setFacilityId(facilityId); } refrigeratorService.save(refrigerator); repository.saveReading(reading); } }
DistributionRefrigeratorsService { public void save(Long facilityId, DistributionRefrigerators distributionRefrigerators) { List<RefrigeratorReading> readings = distributionRefrigerators.getReadings(); refrigeratorService.disableAllFor(facilityId); if (readings.size() == 0) { return; } List<Refrigerator> refrigeratorsForFacility = refrigeratorService.getAllBy(facilityId); for (RefrigeratorReading reading : readings) { final Refrigerator refrigerator = reading.getRefrigerator(); Refrigerator existingRefrigerator = (Refrigerator) CollectionUtils.find(refrigeratorsForFacility, new Predicate() { @Override public boolean evaluate(Object o) { return ((Refrigerator) o).getSerialNumber().equals(refrigerator.getSerialNumber()); } }); refrigerator.setEnabled(true); if (existingRefrigerator != null) { refrigerator.setId(existingRefrigerator.getId()); } else { refrigerator.setFacilityId(facilityId); } refrigeratorService.save(refrigerator); repository.saveReading(reading); } } }
DistributionRefrigeratorsService { public void save(Long facilityId, DistributionRefrigerators distributionRefrigerators) { List<RefrigeratorReading> readings = distributionRefrigerators.getReadings(); refrigeratorService.disableAllFor(facilityId); if (readings.size() == 0) { return; } List<Refrigerator> refrigeratorsForFacility = refrigeratorService.getAllBy(facilityId); for (RefrigeratorReading reading : readings) { final Refrigerator refrigerator = reading.getRefrigerator(); Refrigerator existingRefrigerator = (Refrigerator) CollectionUtils.find(refrigeratorsForFacility, new Predicate() { @Override public boolean evaluate(Object o) { return ((Refrigerator) o).getSerialNumber().equals(refrigerator.getSerialNumber()); } }); refrigerator.setEnabled(true); if (existingRefrigerator != null) { refrigerator.setId(existingRefrigerator.getId()); } else { refrigerator.setFacilityId(facilityId); } refrigeratorService.save(refrigerator); repository.saveReading(reading); } }  }
DistributionRefrigeratorsService { public void save(Long facilityId, DistributionRefrigerators distributionRefrigerators) { List<RefrigeratorReading> readings = distributionRefrigerators.getReadings(); refrigeratorService.disableAllFor(facilityId); if (readings.size() == 0) { return; } List<Refrigerator> refrigeratorsForFacility = refrigeratorService.getAllBy(facilityId); for (RefrigeratorReading reading : readings) { final Refrigerator refrigerator = reading.getRefrigerator(); Refrigerator existingRefrigerator = (Refrigerator) CollectionUtils.find(refrigeratorsForFacility, new Predicate() { @Override public boolean evaluate(Object o) { return ((Refrigerator) o).getSerialNumber().equals(refrigerator.getSerialNumber()); } }); refrigerator.setEnabled(true); if (existingRefrigerator != null) { refrigerator.setId(existingRefrigerator.getId()); } else { refrigerator.setFacilityId(facilityId); } refrigeratorService.save(refrigerator); repository.saveReading(reading); } }  void save(Long facilityId, DistributionRefrigerators distributionRefrigerators); DistributionRefrigerators getBy(Long facilityVisitId); }
DistributionRefrigeratorsService { public void save(Long facilityId, DistributionRefrigerators distributionRefrigerators) { List<RefrigeratorReading> readings = distributionRefrigerators.getReadings(); refrigeratorService.disableAllFor(facilityId); if (readings.size() == 0) { return; } List<Refrigerator> refrigeratorsForFacility = refrigeratorService.getAllBy(facilityId); for (RefrigeratorReading reading : readings) { final Refrigerator refrigerator = reading.getRefrigerator(); Refrigerator existingRefrigerator = (Refrigerator) CollectionUtils.find(refrigeratorsForFacility, new Predicate() { @Override public boolean evaluate(Object o) { return ((Refrigerator) o).getSerialNumber().equals(refrigerator.getSerialNumber()); } }); refrigerator.setEnabled(true); if (existingRefrigerator != null) { refrigerator.setId(existingRefrigerator.getId()); } else { refrigerator.setFacilityId(facilityId); } refrigeratorService.save(refrigerator); repository.saveReading(reading); } }  void save(Long facilityId, DistributionRefrigerators distributionRefrigerators); DistributionRefrigerators getBy(Long facilityVisitId);  }