### Question:
DeviceLowBatteryGenerator extends AlertGenerator { @Override protected void handleModelChanged(SubsystemContext<PlaceMonitorSubsystemModel> context, Model model, AlertScratchPad scratchPad) { Map<String, Date> lowBatteries = context.model().getLowBatteryNotificationSent(); scratchPad.alerts(alert -> SmartHomeAlert.ALERTTYPE_DEV_WARN_BATTERY_LOW.equals(alert.getAlerttype())) .stream() .filter(alert -> !lowBatteries.containsKey(alert.getSubjectaddr())) .forEach(alert -> scratchPad.removeAlert(alert.getAlertkey())); lowBatteries.keySet().forEach(addr -> addAlert(context, scratchPad, addr)); } @Inject  DeviceLowBatteryGenerator(ProductCatalogManager prodCat); @Override void onStarted(SubsystemContext<PlaceMonitorSubsystemModel> context, AlertScratchPad scratch);  }### Answer:
@Test public void testLowBatteryAdded() { replay(); Map<String, Date> lowBattery = ImmutableMap.of(lock.getAddress().getRepresentation(), new Date()); model.setLowBatteryNotificationSent(lowBattery); generator.handleModelChanged(context, model, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_WARN_BATTERY_LOW, lock.getAddress()); assertScratchPadHasAlert(key); SmartHomeAlert expected = createAlert(); assertAlert(expected, scratchPad.getAlert(key)); }
@Test public void testLowBatteryCleared() { replay(); scratchPad.putAlert(createAlert()); model.setLowBatteryNotificationSent(ImmutableMap.of()); generator.handleModelChanged(context, model, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_WARN_BATTERY_LOW, lock.getAddress()); assertScratchPadNoAlert(key); }
@Test public void testLowBatteryModelMissingIgnored() { replay(); String addr = Address.platformDriverAddress(IrisUUID.randomUUID()).getRepresentation(); Map<String, Date> lowBattery = ImmutableMap.of(addr, new Date()); model.setLowBatteryNotificationSent(lowBattery); generator.handleModelChanged(context, model, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_WARN_BATTERY_LOW, addr); assertScratchPadNoAlert(key); }