From 4c0554f61aa3a665d7fa5d39cfd61c6114a50303 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 13 Mar 2019 19:35:48 +0100 Subject: [PATCH] SmsCommunicatorPluginTest 4 --- .../SmsCommunicatorPlugin.java | 16 +- app/src/test/java/info/AAPSMocker.java | 16 +- .../SmsCommunicatorPluginTest.java | 158 ++++++++++++++++-- 3 files changed, 172 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java index 8f6b437d06..a1adb1598f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java @@ -164,7 +164,7 @@ public class SmsCommunicatorPlugin extends PluginBase { case "LOOP": if (!remoteCommandsAllowed) sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2) + else if (splitted.length == 2 || splitted.length == 3) processLOOP(splitted, receivedSms); else sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); @@ -236,7 +236,7 @@ public class SmsCommunicatorPlugin extends PluginBase { messageToConfirm.action(splitted[0]); messageToConfirm = null; } else - sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand))); + sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand)); break; } } @@ -294,6 +294,8 @@ public class SmsCommunicatorPlugin extends PluginBase { sendSMS(new Sms(receivedSms.phoneNumber, reply)); } }); + } else { + sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisdisabled)); } receivedSms.processed = true; break; @@ -304,6 +306,8 @@ public class SmsCommunicatorPlugin extends PluginBase { loopPlugin.setPluginEnabled(PluginType.LOOP, true); sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loophasbeenenabled)); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); + } else { + sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisenabled)); } receivedSms.processed = true; break; @@ -326,18 +330,16 @@ public class SmsCommunicatorPlugin extends PluginBase { LoopPlugin.getPlugin().suspendTo(0); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_RESUME")); NSUpload.uploadOpenAPSOffline(0); - reply = MainApp.gs(R.string.smscommunicator_loopresumed); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed)); break; case "SUSPEND": int duration = 0; - if (splitted.length >= 3) + if (splitted.length == 3) duration = SafeParse.stringToInt(splitted[2]); duration = Math.max(0, duration); duration = Math.min(180, duration); if (duration == 0) { - reply = MainApp.gs(R.string.smscommunicator_wrongduration); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); + sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_wrongduration)); } else { String passCode = generatePasscode(); reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode); diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index e04576cdbc..27a2d2d6b4 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -9,6 +9,7 @@ import com.squareup.otto.Bus; import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; +import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import java.util.Locale; @@ -31,6 +32,7 @@ import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.treatments.TreatmentService; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.androidaps.utils.SP; @@ -38,6 +40,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -53,6 +56,8 @@ public class AAPSMocker { public static Intent intentSent = null; + public static CommandQueue queue; + public static void mockStrings() { Locale.setDefault(new Locale("en", "US")); @@ -114,6 +119,15 @@ public class AAPSMocker { when(MainApp.gs(R.string.loopsuspendedfor)).thenReturn("Suspended (%1$d m)"); when(MainApp.gs(R.string.smscommunicator_loopisdisabled)).thenReturn("Loop is disabled"); when(MainApp.gs(R.string.smscommunicator_loopisenabled)).thenReturn("Loop is enabled"); + when(MainApp.gs(R.string.wrongformat)).thenReturn("Wrong format"); + when(MainApp.gs(R.string.smscommunicator_loophasbeendisabled)).thenReturn("Loop has been disabled"); + when(MainApp.gs(R.string.smscommunicator_loophasbeenenabled)).thenReturn("Loop has been enabled"); + when(MainApp.gs(R.string.smscommunicator_tempbasalcanceled)).thenReturn("Temp basal canceled"); + when(MainApp.gs(R.string.smscommunicator_loopresumed)).thenReturn("Loop resumed"); + when(MainApp.gs(R.string.smscommunicator_wrongduration)).thenReturn("Wrong duration"); + when(MainApp.gs(R.string.smscommunicator_suspendreplywithcode)).thenReturn("To suspend loop for %1$d minutes reply with code %2$s"); + when(MainApp.gs(R.string.smscommunicator_loopsuspended)).thenReturn("Loop suspended"); + when(MainApp.gs(R.string.smscommunicator_unknowncommand)).thenReturn("Uknown command or wrong reply"); } public static MainApp mockMainApp() { @@ -171,7 +185,7 @@ public class AAPSMocker { } public static void mockCommandQueue() { - CommandQueue queue = mock(CommandQueue.class); + queue = mock(CommandQueue.class); when(ConfigBuilderPlugin.getPlugin().getCommandQueue()).thenReturn(queue); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.java index 7f11a57e2c..6a96acfb6d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.java @@ -6,6 +6,8 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -16,28 +18,42 @@ import java.util.List; import info.AAPSMocker; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.SP; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.powermock.api.mockito.PowerMockito.doAnswer; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({L.class, SP.class, MainApp.class, DateUtil.class, ProfileFunctions.class, TreatmentsPlugin.class, SmsManager.class, IobCobCalculatorPlugin.class}) +@PrepareForTest({ + L.class, SP.class, MainApp.class, DateUtil.class, ProfileFunctions.class, + TreatmentsPlugin.class, SmsManager.class, IobCobCalculatorPlugin.class, + CommandQueue.class, ConfigBuilderPlugin.class, NSUpload.class +}) public class SmsCommunicatorPluginTest { SmsCommunicatorPlugin smsCommunicatorPlugin; LoopPlugin loopPlugin; + boolean hasBeenRun = false; + @Test public void processSettingsTest() { // called from constructor @@ -62,17 +78,12 @@ public class SmsCommunicatorPluginTest { Assert.assertTrue(sms.ignored); Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "aText"); - // test remote control disabled - when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(false); + //UNKNOWN smsCommunicatorPlugin.messages = new ArrayList<>(); - sms = new Sms("1234", "LOOP STATUS"); + sms = new Sms("1234", "UNKNOWN"); smsCommunicatorPlugin.processSms(sms); - Assert.assertFalse(sms.ignored); - Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP STATUS"); - Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Remote command is not allowed")); - - // enable remote control for next tests - when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "UNKNOWN"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Uknown command or wrong reply"); //BG smsCommunicatorPlugin.messages = new ArrayList<>(); @@ -82,6 +93,16 @@ public class SmsCommunicatorPluginTest { Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("IOB:")); Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Last BG: 100")); + // LOOP : test remote control disabled + when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(false); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP STATUS"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP STATUS"); + Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Remote command is not allowed")); + when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true); + //LOOP STATUS : disabled when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false); smsCommunicatorPlugin.messages = new ArrayList<>(); @@ -109,6 +130,112 @@ public class SmsCommunicatorPluginTest { Assert.assertFalse(sms.ignored); Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP STATUS"); Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop is enabled"); + + //LOOP : wrong format + when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Wrong format"); + + //LOOP DISABLE : already disabled + when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP DISABLE"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP DISABLE"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop is disabled"); + + //LOOP DISABLE : from enabled + hasBeenRun = false; + when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true); + doAnswer((Answer) invocation -> { + hasBeenRun = true; + return null; + }).when(loopPlugin).setPluginEnabled(PluginType.LOOP, false); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP DISABLE"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP DISABLE"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop has been disabled Temp basal canceled"); + Assert.assertTrue(hasBeenRun); + + //LOOP ENABLE : already enabled + when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP ENABLE"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP ENABLE"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop is enabled"); + + //LOOP ENABLE : from disabled + hasBeenRun = false; + when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false); + doAnswer((Answer) invocation -> { + hasBeenRun = true; + return null; + }).when(loopPlugin).setPluginEnabled(PluginType.LOOP, true); + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP ENABLE"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP ENABLE"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop has been enabled"); + Assert.assertTrue(hasBeenRun); + + //LOOP RESUME : already enabled + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP RESUME"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP RESUME"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Loop resumed"); + + //LOOP SUSPEND 1 2: wrong format + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP SUSPEND 1 2"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP SUSPEND 1 2"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Wrong format"); + + //LOOP SUSPEND 0 : wrong duration + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP SUSPEND 0"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP SUSPEND 0"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Wrong duration"); + + //LOOP SUSPEND 100 : suspend for 100 min + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP SUSPEND 100"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP SUSPEND 100"); + Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To suspend loop for 100 minutes reply with code ")); + + //LOOP SUSPEND 200 : limit to 180 min + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP SUSPEND 200"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP SUSPEND 200"); + Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To suspend loop for 180 minutes reply with code ")); + + //LOOP BLABLA + smsCommunicatorPlugin.messages = new ArrayList<>(); + sms = new Sms("1234", "LOOP BLABLA"); + smsCommunicatorPlugin.processSms(sms); + Assert.assertFalse(sms.ignored); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "LOOP BLABLA"); + Assert.assertEquals(smsCommunicatorPlugin.messages.get(1).text, "Wrong format"); + } @Before @@ -122,6 +249,9 @@ public class SmsCommunicatorPluginTest { AAPSMocker.mockProfileFunctions(); AAPSMocker.mockTreatmentPlugin(); AAPSMocker.mockIobCobCalculatorPlugin(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockCommandQueue(); + AAPSMocker.mockNSUpload(); BgReading reading = new BgReading(); reading.value = 100; @@ -140,6 +270,14 @@ public class SmsCommunicatorPluginTest { loopPlugin = mock(LoopPlugin.class); when(MainApp.getSpecificPlugin(LoopPlugin.class)).thenReturn(loopPlugin); + + Mockito.doAnswer((Answer) invocation -> { + Callback callback = invocation.getArgument(1); + callback.result = new PumpEnactResult().success(true); + callback.run(); + return null; + }).when(AAPSMocker.queue).cancelTempBasal(anyBoolean(), any(Callback.class)); + } }