From 7659f58776422a65e50067e730e8868010ba8069 Mon Sep 17 00:00:00 2001 From: Nico Schmitz Date: Tue, 16 Oct 2018 01:57:59 +0200 Subject: [PATCH] Fix tests --- .../automation/triggers/TriggerTime.java | 6 +- .../automation/triggers/TriggerAndTest.java | 58 -------- .../automation/triggers/TriggerBgTest.java | 30 ++-- .../triggers/TriggerConnectorTest.java | 131 ++++++++++++++++++ .../automation/triggers/TriggerOrTest.java | 58 -------- .../automation/triggers/TriggerTimeTest.java | 6 +- 6 files changed, 156 insertions(+), 133 deletions(-) delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAndTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnectorTest.java delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerOrTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java index b4aa3d711b..7c10c67a14 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java @@ -98,8 +98,12 @@ public class TriggerTime extends Trigger { long validTo; public TriggerTime() { + setAll(false); + } + + public void setAll(boolean value) { for(DayOfWeek day : DayOfWeek.values()) { - set(day, false); + set(day, value); } } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAndTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAndTest.java deleted file mode 100644 index 58b2a06331..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAndTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({}) -public class TriggerAndTest { - - @Test - public void doTests() { - TriggerAnd t = new TriggerAnd(); - TriggerAnd t2 = new TriggerAnd(); - TriggerAnd t3 = new TriggerAnd(); - - Assert.assertTrue(t.size() == 0); - - t.add(t2); - Assert.assertTrue(t.size() == 1); - Assert.assertEquals(t2, t.get(0)); - - t.add(t3); - Assert.assertTrue(t.size() == 2); - Assert.assertEquals(t2, t.get(0)); - Assert.assertEquals(t3, t.get(1)); - - Assert.assertTrue(t.remove(t2)); - Assert.assertTrue(t.size() == 1); - Assert.assertEquals(t3, t.get(0)); - - Assert.assertTrue(t.shouldRun()); - } - - String empty = "{\"data\":\"[]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAnd\"}"; - String oneItem = "{\"data\":\"[\\\"{\\\\\\\"data\\\\\\\":\\\\\\\"[]\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAnd\\\\\\\"}\\\"]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAnd\"}"; - - @Test - public void toJSONTest() { - TriggerAnd t = new TriggerAnd(); - Assert.assertEquals(empty, t.toJSON()); - t.add(new TriggerAnd()); - Assert.assertEquals(oneItem, t.toJSON()); - } - @Test - public void fromJSONTest() throws JSONException { - TriggerAnd t = new TriggerAnd(); - t.add(new TriggerAnd()); - - TriggerAnd t2 = (TriggerAnd) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(1, t2.size()); - Assert.assertTrue(t2.get(0) instanceof TriggerAnd); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java index fa6d03ed69..7c1f8235a6 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBgTest.java @@ -36,46 +36,46 @@ public class TriggerBgTest { public void shouldRunTest() { when(MainApp.getDbHelper().getBgreadingsDataFromTime(anyLong(), anyBoolean())).thenReturn(generateOneCurrentRecordBgData()); - TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.ISEQUAL); + TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.Comparator.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.ISEQUAL); + t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.Comparator.IS_EQUAL); Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.ISEQUALORGREATER); + t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.Comparator.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.ISEQUALORLOWER); + t = new TriggerBg().units(Constants.MGDL).threshold(214).comparator(Trigger.Comparator.IS_EQUAL_OR_LOWER); Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.ISEQUAL); + t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.Comparator.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.ISEQUALORLOWER); + t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.Comparator.IS_EQUAL_OR_LOWER); Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.ISEQUALORGREATER); + t = new TriggerBg().units(Constants.MGDL).threshold(215).comparator(Trigger.Comparator.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.ISEQUALORGREATER); + t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.Comparator.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.ISEQUALORLOWER); + t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.Comparator.IS_EQUAL_OR_LOWER); Assert.assertFalse(t.shouldRun()); when(MainApp.getDbHelper().getBgreadingsDataFromTime(anyLong(), anyBoolean())).thenReturn(new ArrayList<>()); - t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.ISEQUALORLOWER); + t = new TriggerBg().units(Constants.MGDL).threshold(213).comparator(Trigger.Comparator.IS_EQUAL_OR_LOWER); Assert.assertFalse(t.shouldRun()); - t = new TriggerBg().comparator(Trigger.ISNOTAVAILABLE); + t = new TriggerBg().comparator(Trigger.Comparator.IS_NOT_AVAILABLE); Assert.assertTrue(t.shouldRun()); } - String bgJson = "{\"data\":\"{\\\"comparator\\\":0,\\\"threshold\\\":4.1,\\\"units\\\":\\\"mmol\\\"}\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg\"}"; + String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"threshold\":4.1,\"units\":\"mmol\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg\"}"; @Test public void toJSONTest() { - TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.ISEQUAL); + TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.Comparator.IS_EQUAL); Assert.assertEquals(bgJson, t.toJSON()); } @Test public void fromJSONTest() throws JSONException { - TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.ISEQUAL); + TriggerBg t = new TriggerBg().units(Constants.MMOL).threshold(4.1d).comparator(Trigger.Comparator.IS_EQUAL); TriggerBg t2 = (TriggerBg) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(Trigger.ISEQUAL, t2.comparator); + Assert.assertEquals(Trigger.Comparator.IS_EQUAL, t2.comparator); Assert.assertEquals(4.1d, t2.threshold, 0.01d); Assert.assertEquals(Constants.MMOL, t2.units); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnectorTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnectorTest.java new file mode 100644 index 0000000000..86dbcb47fc --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnectorTest.java @@ -0,0 +1,131 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({}) +public class TriggerConnectorTest { + + private static class TriggerBoolean extends Trigger { + private boolean result; + + TriggerBoolean(boolean result) { + this.result = result; + } + + @Override + public boolean shouldRun() { + return result; + } + + @Override + String toJSON() { + return null; + } + + @Override + Trigger fromJSON(String data) { + return null; + } + + @Override + public int friendlyName() { + return 0; + } + + @Override + public String friendlyDescription() { + return null; + } + } + + @Test + public void testTriggerList() { + TriggerConnector t = new TriggerConnector(); + TriggerConnector t2 = new TriggerConnector(); + TriggerConnector t3 = new TriggerConnector(); + + Assert.assertTrue(t.size() == 0); + + t.add(t2); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t2, t.get(0)); + + t.add(t3); + Assert.assertTrue(t.size() == 2); + Assert.assertEquals(t2, t.get(0)); + Assert.assertEquals(t3, t.get(1)); + + Assert.assertTrue(t.remove(t2)); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t3, t.get(0)); + + Assert.assertTrue(t.shouldRun()); + } + + @Test + public void testListTriggerOR() { + TriggerConnector t = new TriggerConnector(TriggerConnector.Type.OR); + t.add(new TriggerBoolean(false)); + t.add(new TriggerBoolean(false)); + Assert.assertFalse(t.shouldRun()); + + t.add(new TriggerBoolean(true)); + t.add(new TriggerBoolean(false)); + Assert.assertTrue(t.shouldRun()); + } + + @Test + public void testListTriggerXOR() { + TriggerConnector t = new TriggerConnector(TriggerConnector.Type.XOR); + t.add(new TriggerBoolean(false)); + t.add(new TriggerBoolean(false)); + Assert.assertFalse(t.shouldRun()); + + t.add(new TriggerBoolean(true)); + t.add(new TriggerBoolean(false)); + Assert.assertTrue(t.shouldRun()); + + t.add(new TriggerBoolean(true)); + t.add(new TriggerBoolean(false)); + Assert.assertFalse(t.shouldRun()); + } + + @Test + public void testListTriggerAND() { + TriggerConnector t = new TriggerConnector(TriggerConnector.Type.AND); + t.add(new TriggerBoolean(true)); + t.add(new TriggerBoolean(true)); + Assert.assertTrue(t.shouldRun()); + + t.add(new TriggerBoolean(true)); + t.add(new TriggerBoolean(false)); + Assert.assertFalse(t.shouldRun()); + } + + String empty = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[]},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\"}"; + String oneItem = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[]},\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\\\"}\"]},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\"}"; + + @Test + public void toJSONTest() { + TriggerConnector t = new TriggerConnector(); + Assert.assertEquals(empty, t.toJSON()); + t.add(new TriggerConnector()); + Assert.assertEquals(oneItem, t.toJSON()); + } + @Test + public void fromJSONTest() throws JSONException { + TriggerConnector t = new TriggerConnector(); + t.add(new TriggerConnector()); + + TriggerConnector t2 = (TriggerConnector) Trigger.instantiate(new JSONObject(t.toJSON())); + Assert.assertEquals(1, t2.size()); + Assert.assertTrue(t2.get(0) instanceof TriggerConnector); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerOrTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerOrTest.java deleted file mode 100644 index 14ebc67183..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerOrTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.triggers; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({}) -public class TriggerOrTest { - - @Test - public void doTests() { - TriggerOr t = new TriggerOr(); - TriggerOr t2 = new TriggerOr(); - TriggerOr t3 = new TriggerOr(); - - Assert.assertTrue(t.size() == 0); - - t.add(t2); - Assert.assertTrue(t.size() == 1); - Assert.assertEquals(t2, t.get(0)); - - t.add(t3); - Assert.assertTrue(t.size() == 2); - Assert.assertEquals(t2, t.get(0)); - Assert.assertEquals(t3, t.get(1)); - - Assert.assertTrue(t.remove(t2)); - Assert.assertTrue(t.size() == 1); - Assert.assertEquals(t3, t.get(0)); - - Assert.assertFalse(t.shouldRun()); - } - - String empty = "{\"data\":\"[]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerOr\"}"; - String oneItem = "{\"data\":\"[\\\"{\\\\\\\"data\\\\\\\":\\\\\\\"[]\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerOr\\\\\\\"}\\\"]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerOr\"}"; - - @Test - public void toJSONTest() { - TriggerOr t = new TriggerOr(); - Assert.assertEquals(empty, t.toJSON()); - t.add(new TriggerOr()); - Assert.assertEquals(oneItem, t.toJSON()); - } - @Test - public void fromJSONTest() throws JSONException { - TriggerOr t = new TriggerOr(); - t.add(new TriggerOr()); - - TriggerOr t2 = (TriggerOr) Trigger.instantiate(new JSONObject(t.toJSON())); - Assert.assertEquals(1, t2.size()); - Assert.assertTrue(t2.get(0) instanceof TriggerOr); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java index 4aa94e2f8c..0f5205524d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeTest.java @@ -35,25 +35,29 @@ public class TriggerTimeTest { // scheduled 1 min before TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs()); Assert.assertTrue(t.shouldRun()); + // scheduled 1 min in the future t = new TriggerTime().runAt(now + T.mins(1).msecs()); Assert.assertFalse(t.shouldRun()); // limit by validTo t = new TriggerTime().recurring(true).hour(1).minute(34).validTo(1); + t.setAll(true); Assert.assertFalse(t.shouldRun()); // scheduled 1 min before t = new TriggerTime().recurring(true).hour(1).minute(34); + t.setAll(true); Assert.assertTrue(t.shouldRun()); // already run t = new TriggerTime().recurring(true).hour(1).minute(34).lastRun(now - 1); + t.setAll(true); Assert.assertFalse(t.shouldRun()); } - String timeJson = "{\"data\":\"{\\\"saturday\\\":true,\\\"runAt\\\":1514766840000,\\\"lastRun\\\":0,\\\"recurring\\\":false,\\\"thursday\\\":true,\\\"minute\\\":0,\\\"sunday\\\":true,\\\"tuesday\\\":true,\\\"hour\\\":0,\\\"wednesday\\\":true,\\\"friday\\\":true,\\\"monday\\\":true,\\\"validTo\\\":0}\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}"; + String timeJson = "{\"data\":{\"runAt\":1514766840000,\"THURSDAY\":false,\"lastRun\":0,\"SUNDAY\":false,\"recurring\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"minute\":0,\"WEDNESDAY\":false,\"MONDAY\":false,\"hour\":0,\"SATURDAY\":false,\"validTo\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}"; @Test public void toJSONTest() {