TriggerAutosensValue extends Trigger { @Override public synchronized boolean shouldRun() { AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger"); if (autosensData == null) if (comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) return true; else return false; if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), getValue() / 100d); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } return false; } TriggerAutosensValue(); private  TriggerAutosensValue(TriggerAutosensValue triggerAutosensValue); 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() { Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d); when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData()); TriggerAutosensValue t = new TriggerAutosensValue().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 TriggerAutosensValue().setValue(100).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(100, t.getValue(), 0.01d); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(50).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(420).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); t = new TriggerAutosensValue().setValue(20).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(new AutosensData()); t = new TriggerAutosensValue().setValue(80).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(null); t = new TriggerAutosensValue().comparator(Comparator.Compare.IS_NOT_AVAILABLE); Assert.assertTrue(t.shouldRun()); t = new TriggerAutosensValue().setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); }