SmsCommunicatorPluginTest 1

This commit is contained in:
Milos Kozak 2019-03-12 22:58:04 +01:00
parent 3ebd7dcdb8
commit b24217491b
3 changed files with 130 additions and 12 deletions

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
@ -52,7 +51,6 @@ import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse;
import info.nightscout.androidaps.utils.T;
import info.nightscout.androidaps.utils.XdripCalibrations;
/**
@ -71,7 +69,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
return smsCommunicatorPlugin;
}
private List<String> allowedNumbers = new ArrayList<>();
List<String> allowedNumbers = new ArrayList<>();
private AuthRequest messageToConfirm = null;
@ -79,7 +77,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
ArrayList<Sms> messages = new ArrayList<>();
private SmsCommunicatorPlugin() {
SmsCommunicatorPlugin() {
super(new PluginDescription()
.mainType(PluginType.GENERAL)
.fragmentClass(SmsCommunicatorFragment.class.getName())
@ -118,7 +116,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
}
private boolean isAllowedNumber(String number) {
boolean isAllowedNumber(String number) {
for (String num : allowedNumbers) {
if (num.equals(number)) return true;
}
@ -139,7 +137,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
}
private void processSms(final Sms receivedSms) {
void processSms(final Sms receivedSms) {
if (!isEnabled(PluginType.GENERAL)) {
log.debug("Ignoring SMS. Plugin disabled.");
return;
@ -757,7 +755,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
passCode += Character.toString((char) (startChar2 + Math.random() * ('z' - 'a' + 1)));
int startChar3 = Math.random() > 0.5 ? 'a' : 'A';
passCode += Character.toString((char) (startChar3 + Math.random() * ('z' - 'a' + 1)));
passCode.replace('l','k').replace('I', 'J');
passCode.replace('l', 'k').replace('I', 'J');
return passCode;
}

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.ConstraintChecker;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.db.DatabaseHelper;
@ -24,11 +25,12 @@ import info.nightscout.androidaps.logging.L;
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.pump.danaR.DanaRPlugin;
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.plugins.pump.danaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.queue.CommandQueue;
import info.nightscout.androidaps.utils.SP;
@ -105,6 +107,9 @@ public class AAPSMocker {
when(MainApp.gs(R.string.profile_carbs_per_unit)).thenReturn("g/U");
when(MainApp.gs(R.string.profile_ins_units_per_hout)).thenReturn("U/h");
when(MainApp.gs(R.string.sms_wrongcode)).thenReturn("Wrong code. Command cancelled.");
when(MainApp.gs(R.string.sms_iob)).thenReturn("IOB:");
when(MainApp.gs(R.string.sms_lastbg)).thenReturn("Last BG:");
when(MainApp.gs(R.string.sms_minago)).thenReturn("%1$dmin ago");
}
public static MainApp mockMainApp() {
@ -144,7 +149,7 @@ public class AAPSMocker {
when(L.isEnabled(any())).thenReturn(true);
}
public static void mockNSUpload(){
public static void mockNSUpload() {
PowerMockito.mockStatic(NSUpload.class);
}
@ -170,12 +175,17 @@ public class AAPSMocker {
PowerMockito.mockStatic(TreatmentsPlugin.class);
TreatmentsPlugin treatmentsPlugin = PowerMockito.mock(TreatmentsPlugin.class);
when(TreatmentsPlugin.getPlugin()).thenReturn(treatmentsPlugin);
when(treatmentsPlugin.getLastCalculationTreatments()).thenReturn(new IobTotal(0));
when(treatmentsPlugin.getLastCalculationTempBasals()).thenReturn(new IobTotal(0));
return treatmentsPlugin;
}
public static void mockTreatmentService() throws Exception {
public static void mockTreatmentService() {
TreatmentService treatmentService = PowerMockito.mock(TreatmentService.class);
PowerMockito.whenNew(TreatmentService.class).withNoArguments().thenReturn(treatmentService);
try {
PowerMockito.whenNew(TreatmentService.class).withNoArguments().thenReturn(treatmentService);
} catch (Exception e) {
}
}
public static DanaRPlugin mockDanaRPlugin() {
@ -222,6 +232,13 @@ public class AAPSMocker {
PowerMockito.when(ProfileFunctions.getInstance()).thenReturn(profileFunctions);
profile = getValidProfile();
PowerMockito.when(ProfileFunctions.getInstance().getProfile()).thenReturn(profile);
PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL);
}
public static void mockIobCobCalculatorPlugin() {
PowerMockito.mockStatic(IobCobCalculatorPlugin.class);
IobCobCalculatorPlugin iobCobCalculatorPlugin = PowerMockito.mock(IobCobCalculatorPlugin.class);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin()).thenReturn(iobCobCalculatorPlugin);
}
private static MockedBus bus = new MockedBus();

View file

@ -0,0 +1,103 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.List;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
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({SmsCommunicatorPlugin.class, L.class, SP.class, MainApp.class, DateUtil.class, ProfileFunctions.class, TreatmentsPlugin.class, SmsManager.class, IobCobCalculatorPlugin.class})
public class SmsCommunicatorPluginTest {
SmsCommunicatorPlugin smsCommunicatorPlugin;
String sentNumber;
String sentText;
@Test
public void processSettingsTest() {
// called from constructor
Assert.assertEquals(smsCommunicatorPlugin.allowedNumbers.get(0), "1234");
Assert.assertEquals(smsCommunicatorPlugin.allowedNumbers.get(1), "5678");
Assert.assertEquals(smsCommunicatorPlugin.allowedNumbers.size(), 2);
}
@Test
public void isAllowedNumberTest() {
Assert.assertTrue(smsCommunicatorPlugin.isAllowedNumber("5678"));
Assert.assertFalse(smsCommunicatorPlugin.isAllowedNumber("56"));
}
@Test
public void processSmsTest() {
Sms sms;
// SMS from not allowed number should be ignored
sms = new Sms("12", "aText");
smsCommunicatorPlugin.processSms(sms);
Assert.assertTrue(sms.ignored);
Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "aText");
//BG
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BG");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals(smsCommunicatorPlugin.messages.get(0).text, "BG");
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("IOB:"));
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Last BG: 100"));
}
@Before
public void prepareTests() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
AAPSMocker.mockStrings();
AAPSMocker.mockBus();
AAPSMocker.mockProfileFunctions();
AAPSMocker.mockTreatmentPlugin();
AAPSMocker.mockIobCobCalculatorPlugin();
BgReading reading = new BgReading();
reading.value = 100;
List<BgReading> bgList = new ArrayList<>();
bgList.add(reading);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(bgList);
mockStatic(DateUtil.class);
mockStatic(SmsManager.class);
SmsManager smsManager = mock(SmsManager.class);
when(SmsManager.getDefault()).thenReturn(smsManager);
when(SP.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678");
smsCommunicatorPlugin = new SmsCommunicatorPlugin();
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true);
}
}