SmsCommunicatorPluginTest 10

This commit is contained in:
Milos Kozak 2019-03-21 21:00:42 +01:00
parent 30bfa1ca1f
commit a9992503f6
4 changed files with 146 additions and 13 deletions

View file

@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
@ -73,7 +72,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
AuthRequest messageToConfirm = null;
private Date lastRemoteBolusTime = new Date(0);
long lastRemoteBolusTime = 0;
ArrayList<Sms> messages = new ArrayList<>();
@ -211,9 +210,9 @@ public class SmsCommunicatorPlugin extends PluginBase {
case "BOLUS":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (DateUtil.now() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance)
else if (splitted.length == 2 && DateUtil.now() - lastRemoteBolusTime < Constants.remoteBolusMinDistance)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotebolusnotallowed));
else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended())
else if (splitted.length == 2 && ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended())
sendSMS(new Sms(receivedSms.phoneNumber, R.string.pumpsuspended));
else if (splitted.length == 2)
processBOLUS(splitted, receivedSms);
@ -684,7 +683,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), resultBolusDelivered);
if (pump != null)
reply += "\n" + pump.shortStatus(true);
lastRemoteBolusTime = new Date();
lastRemoteBolusTime = DateUtil.now();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
String reply = MainApp.gs(R.string.smscommunicator_bolusfailed);

View file

@ -293,10 +293,10 @@
<string name="smscommunicator_bolusreplywithcode">To deliver bolus %1$.2fU reply with code %2$s</string>
<string name="smscommunicator_calibrationreplywithcode">To send calibration %1$.2f reply with code %2$s</string>
<string name="smscommunicator_bolusfailed">Bolus failed</string>
<string name="bolusdelivered" formatted="false">Bolus %.2fU delivered successfully</string>
<string name="bolusrequested" formatted="false">Going to deliver %.2fU</string>
<string name="smscommunicator_bolusdelivered" formatted="false">Bolus %.2fU delivered successfully</string>
<string name="bolusdelivering" formatted="false">Delivering %.2fU</string>
<string name="bolusdelivered">Bolus %1$.2fU delivered successfully</string>
<string name="bolusrequested">Going to deliver %1$.2fU</string>
<string name="smscommunicator_bolusdelivered">Bolus %1$.2fU delivered successfully</string>
<string name="bolusdelivering">Delivering %1$.2fU</string>
<string name="smscommunicator_remotecommandsallowed">Allow remote commands via SMS</string>
<string name="glucosetype_finger">Finger</string>
<string name="glucosetype_sensor">Sensor</string>

View file

@ -141,6 +141,12 @@ public class AAPSMocker {
when(MainApp.gs(R.string.smscommunicator_extendedcanceled)).thenReturn("Extended bolus canceled");
when(MainApp.gs(R.string.smscommunicator_extendedreplywithcode)).thenReturn("To start extended bolus %1$.2fU for %2$d min reply with code %3$s");
when(MainApp.gs(R.string.smscommunicator_extendedset)).thenReturn("Extended bolus %1$.2fU for %2$d min started successfully");
when(MainApp.gs(R.string.smscommunicator_bolusreplywithcode)).thenReturn("To deliver bolus %1$.2fU reply with code %2$s");
when(MainApp.gs(R.string.smscommunicator_bolusdelivered)).thenReturn("Bolus %1$.2fU delivered successfully");
when(MainApp.gs(R.string.smscommunicator_remotebolusnotallowed)).thenReturn("Remote bolus not available. Try again later.");
when(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode)).thenReturn("To send calibration %1$.2f reply with code %2$s");
when(MainApp.gs(R.string.smscommunicator_calibrationsent)).thenReturn("Calibration sent. Receiving must be enabled in xDrip.");
when(MainApp.gs(R.string.pumpsuspended)).thenReturn("Pump suspended");
}
public static MainApp mockMainApp() {

View file

@ -16,13 +16,16 @@ import java.util.ArrayList;
import java.util.List;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
@ -36,6 +39,7 @@ 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 info.nightscout.androidaps.utils.XdripCalibrations;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
@ -52,15 +56,15 @@ import static org.powermock.api.mockito.PowerMockito.when;
L.class, SP.class, MainApp.class, DateUtil.class, ProfileFunctions.class,
TreatmentsPlugin.class, SmsManager.class, IobCobCalculatorPlugin.class,
CommandQueue.class, ConfigBuilderPlugin.class, NSUpload.class, ProfileInterface.class,
SimpleProfilePlugin.class
SimpleProfilePlugin.class, XdripCalibrations.class, VirtualPumpPlugin.class
})
public class SmsCommunicatorPluginTest {
SmsCommunicatorPlugin smsCommunicatorPlugin;
LoopPlugin loopPlugin;
private SmsCommunicatorPlugin smsCommunicatorPlugin;
private LoopPlugin loopPlugin;
boolean hasBeenRun = false;
private boolean hasBeenRun = false;
@Test
public void processSettingsTest() {
@ -81,6 +85,7 @@ public class SmsCommunicatorPluginTest {
Sms sms;
// SMS from not allowed number should be ignored
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("12", "aText");
smsCommunicatorPlugin.processSms(sms);
Assert.assertTrue(sms.ignored);
@ -562,7 +567,122 @@ public class SmsCommunicatorPluginTest {
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Extended bolus 1.00U for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
}
@Test
public void processBolusTest() {
Sms sms;
//BOLUS
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//BOLUS
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(1d));
when(DateUtil.now()).thenReturn(1000L);
//BOLUS 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote bolus not available. Try again later.", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(0d));
when(DateUtil.now()).thenReturn(Constants.remoteBolusMinDistance + 1002L);
//BOLUS 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//BOLUS a
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS a");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS a", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyExtendedBolusConstraints(any())).thenReturn(new Constraint<>(1d));
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(1d));
//BOLUS 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To deliver bolus 1.00U reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Bolus 1.00U delivered successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
//BOLUS 1 (Suspended pump)
smsCommunicatorPlugin.lastRemoteBolusTime = 0;
PumpInterface pump = mock(VirtualPumpPlugin.class);
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(pump);
when(pump.isSuspended()).thenReturn(true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Pump suspended", smsCommunicatorPlugin.messages.get(1).text);
when(pump.isSuspended()).thenReturn(false);
}
@Test
public void processCalTest() {
Sms sms;
//CAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//CAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//CAL 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(XdripCalibrations.sendIntent(any())).thenReturn(true);
//CAL 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To send calibration 1.00 reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Calibration sent. Receiving must be enabled in xDrip.", smsCommunicatorPlugin.messages.get(3).text);
}
@Before
@ -588,6 +708,7 @@ public class SmsCommunicatorPluginTest {
bgList.add(reading);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(bgList);
mockStatic(XdripCalibrations.class);
mockStatic(DateUtil.class);
mockStatic(SmsManager.class);
SmsManager smsManager = mock(SmsManager.class);
@ -621,6 +742,13 @@ public class SmsCommunicatorPluginTest {
return null;
}).when(AAPSMocker.queue).readStatus(anyString(), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(1);
callback.result = new PumpEnactResult().success(true).bolusDelivered(1);
callback.run();
return null;
}).when(AAPSMocker.queue).bolus(any(DetailedBolusInfo.class), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(4);
callback.result = new PumpEnactResult().success(true).isPercent(true).percent(invocation.getArgument(0)).duration(invocation.getArgument(1));