TriggerBolusAgo extends Trigger { @Override public synchronized boolean shouldRun() { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime(false); if (lastBolusTime == 0) if (comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) return true; else return false; double minutesAgo = (double) (DateUtil.now() - lastBolusTime) / (60 * 1000); if (L.isEnabled(L.AUTOMATION)) log.debug("LastBolus min ago: " + minutesAgo); boolean doRun = comparator.getValue().check((minutesAgo), getValue()); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } return false; } TriggerBolusAgo(); private  TriggerBolusAgo(TriggerBolusAgo triggerBolusAgo); double getValue(); Comparator getComparator(); @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(DateUtil.now()).thenReturn(now); TriggerBolusAgo t = new TriggerBolusAgo().setValue(110).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); when(TreatmentsPlugin.getPlugin().getLastBolusTime(false)).thenReturn(now); when(DateUtil.now()).thenReturn(now + (10*60*1000)); t = new TriggerBolusAgo().setValue(110).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(110, t.getValue(), 0.01d); Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue()); Assert.assertFalse(t.shouldRun()); t = new TriggerBolusAgo().setValue(10).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(10, t.getValue(), 0.01d); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(420).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); t = new TriggerBolusAgo().setValue(2).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); when(TreatmentsPlugin.getPlugin().getLastBolusTime(false)).thenReturn(0l); t = new TriggerBolusAgo().comparator(Comparator.Compare.IS_NOT_AVAILABLE); Assert.assertTrue(t.shouldRun()); t = new TriggerBolusAgo().setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); }