TriggerBg extends Trigger { @Override public synchronized boolean shouldRun() { GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); if (lastRun > DateUtil.now() - T.mins(5).msecs()) { if (L.isEnabled(L.AUTOMATION)) log.debug("NOT ready for execution: " + friendlyDescription()); return false; } if (glucoseStatus == null && comparator.getValue().equals(Comparator.Compare.IS_NOT_AVAILABLE)) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } if (glucoseStatus == null) { if (L.isEnabled(L.AUTOMATION)) log.debug("NOT ready for execution: " + friendlyDescription()); return false; } boolean doRun = comparator.getValue().check(glucoseStatus.glucose, Profile.toMgdl(bg.getValue(), bg.getUnits())); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } if (L.isEnabled(L.AUTOMATION)) log.debug("NOT ready for execution: " + friendlyDescription()); return false; } TriggerBg(); private  TriggerBg(TriggerBg triggerBg); double getValue(); Comparator getComparator(); String getUnits(); long getLastRun(); @Override synchronized boolean shouldRun(); @Override synchronized String toJSON(); @Override int friendlyName(); @Override String friendlyDescription(); @Override Optional<Integer> icon(); @Override Trigger duplicate(); @Override void generateDialog(LinearLayout root, FragmentManager fragmentManager);  }
@Test public void shouldRunTest() { when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateOneCurrentRecordBgData()); TriggerBg t = new TriggerBg().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(215).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); t = new TriggerBg().comparator(Comparator.Compare.IS_NOT_AVAILABLE); Assert.assertTrue(t.shouldRun()); t = new TriggerBg().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); }