From 4af06620fb0ce944190c31c1fcaf51f89d6655e0 Mon Sep 17 00:00:00 2001 From: Lexsus Date: Fri, 29 May 2020 19:15:11 +0300 Subject: [PATCH] add tests for PUMP DISCONNECT sms command --- .../smsCommunicator/SmsCommunicatorPlugin.kt | 32 ++++++++------ .../SmsCommunicatorPluginTest.kt | 42 +++++++++++++++++++ 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 8ba5c5fb35..2cc357aee1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -480,18 +480,26 @@ class SmsCommunicatorPlugin @Inject constructor( }) } else if (splitted.size == 3) { if (splitted[1].equals("DISCONNECT", ignoreCase = true)) { - val minutes = SafeParse.stringToInt(splitted[2]) - val passCode = generatePasscode() - val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode), minutes, passCode) - receivedSms.processed = true - messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() { - override fun run() { - val profile = profileFunction.getProfile() - loopPlugin.disconnectPump(minutes, profile) - rxBus.send(EventRefreshOverview("SMS_PUMP_DISCONNECT")) - sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpdisconnected))) - } - }) + var duration = SafeParse.stringToInt(splitted[2]) + duration = Math.max(0, duration) + duration = Math.min(120, duration) + if (duration == 0) { + receivedSms.processed = true + sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_wrongduration))) + return + } else { + val passCode = generatePasscode() + val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode), duration, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + val profile = profileFunction.getProfile() + loopPlugin.disconnectPump(duration, profile) + rxBus.send(EventRefreshOverview("SMS_PUMP_DISCONNECT")) + sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpdisconnected))) + } + }) + } } else { sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) return diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 6af5afd171..e0d6a5b8ef 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -226,9 +226,12 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { `when`(resourceHelper.gs(R.string.smscommunicator_loopdisablereplywithcode)).thenReturn("To disable loop reply with code %1\$s") `when`(resourceHelper.gs(R.string.smscommunicator_loopenablereplywithcode)).thenReturn("To enable loop reply with code %1\$s") `when`(resourceHelper.gs(R.string.smscommunicator_loopresumereplywithcode)).thenReturn("To resume loop reply with code %1\$s") + `when`(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode)).thenReturn("To disconnect pump for %1d minutes reply with code %2\$s") `when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectwithcode)).thenReturn("To connect pump reply with code %1\$s") `when`(resourceHelper.gs(R.string.smscommunicator_reconnect)).thenReturn("Pump reconnected") `when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectfail)).thenReturn("Connection to pump failed") + `when`(resourceHelper.gs(R.string.smscommunicator_pumpdisconnected)).thenReturn("Pump disconnected") + } @Test @@ -547,6 +550,45 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) Assert.assertEquals("Pump reconnected", smsCommunicatorPlugin.messages[3].text) + //PUMP DISCONNECT 1 2: wrong format + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP DISCONNECT 1 2") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP DISCONNECT 1 2", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) + + //PUMP DISCONNECT 0 + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP DISCONNECT 0") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("Wrong duration", smsCommunicatorPlugin.messages[1].text) + + //PUMP DISCONNECT 30 + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP DISCONNECT 30") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP DISCONNECT 30", smsCommunicatorPlugin.messages[0].text) + Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To disconnect pump for")) + passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! + smsCommunicatorPlugin.processSms(Sms("1234", passCode)) + Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) + Assert.assertEquals("Pump disconnected", smsCommunicatorPlugin.messages[3].text) + + //PUMP DISCONNECT 30 + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP DISCONNECT 200") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP DISCONNECT 200", smsCommunicatorPlugin.messages[0].text) + Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To disconnect pump for")) + passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! + smsCommunicatorPlugin.processSms(Sms("1234", passCode)) + Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) + Assert.assertEquals("Pump disconnected", smsCommunicatorPlugin.messages[3].text) + //HELP smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "HELP")