### Question:
DoorObstructionGenerator extends AlertGenerator { @Override protected void handleModelChanged(SubsystemContext<PlaceMonitorSubsystemModel> context, Model model, AlertScratchPad scratchPad) { if(!MOTORIZED_DOOR.apply(model)) { return; } Map<String, String> errs = DeviceAdvancedModel.getErrors(model, ImmutableMap.of()); if(errs.containsKey(ERR_KEY)) { scratchPad.putAlert(SmartHomeAlerts.create( SmartHomeAlert.ALERTTYPE_DEV_ERR_GARAGEDOOR_OBSTRUCTION, SmartHomeAlert.SEVERITY_CRITICAL, model.getAddress(), SmartHomeAlerts.baseDeviceAttribues(model, prodCat), context.getPlaceId() )); } else { scratchPad.removeAlert(AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_ERR_GARAGEDOOR_OBSTRUCTION, model.getAddress())); } } @Inject  DoorObstructionGenerator(ProductCatalogManager prodCat); @Override void onStarted(SubsystemContext<PlaceMonitorSubsystemModel> context, AlertScratchPad scratch); static final String ERR_KEY; }### Answer:
@Test public void testHandleModelChangedAddsAlert() { replay(); door.setAttribute(DeviceAdvancedCapability.ATTR_ERRORS, ImmutableMap.of(DoorObstructionGenerator.ERR_KEY, "obstructed")); generator.handleModelChanged(context, door, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_ERR_GARAGEDOOR_OBSTRUCTION, door.getAddress()); assertScratchPadHasAlert(key); SmartHomeAlert expected = createAlert(); assertAlert(expected, scratchPad.getAlert(key)); }
@Test public void testHandleModelChangedClearsAlert() { replay(); scratchPad.putAlert(createAlert()); generator.handleModelChanged(context, door, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_ERR_GARAGEDOOR_OBSTRUCTION, door.getAddress()); assertScratchPadNoAlert(key); }
@Test public void testHandleModelChangedIgnoresNonDoor() { replay(); lock.setAttribute(DeviceAdvancedCapability.ATTR_ERRORS, ImmutableMap.of(DoorObstructionGenerator.ERR_KEY, "obstructed")); generator.handleModelChanged(context, door, scratchPad); String key = AlertKeys.key(SmartHomeAlert.ALERTTYPE_DEV_ERR_GARAGEDOOR_OBSTRUCTION, lock.getAddress()); assertScratchPadNoAlert(key); }