This commit is contained in:
Milos Kozak 2020-02-29 00:12:13 +01:00
parent 1db651055b
commit a694edd170
6 changed files with 183 additions and 271 deletions

View file

@ -1,93 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.assertEquals;
/**
* Created by mike on 20.11.2017.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, L.class})
public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest {
@Test
public void runTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
// under 100% should last 30 min
DanaRS_Packet_APS_Basal_Set_Temporary_Basal packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal();
packet.setParams(0);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
//constructor with param
packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(10);
assertEquals(10, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(20, true, false);
assertEquals(20, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// over 100% should last 15 min
packet.setParams(150);
assertEquals(150, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test low hard limit
packet.setParams(-1);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// test high hard limit
packet.setParams(550);
assertEquals(500, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test setting 15 min
packet.setParams(50, true, false);
assertEquals(50, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test setting 30 min
packet.setParams(50, false, true);
assertEquals(50, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// over 200% set always 15 min
packet.setParams(250, false, true);
assertEquals(250, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test low hard limit
packet.setParams(-1, false, true);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// test high hard limit
packet.setParams(550, false, true);
assertEquals(500, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test message generation
packet.setParams(260, true, false);
byte[] generatedCode = packet.getRequestParams();
assertEquals(3, generatedCode.length);
assertEquals((byte) 4, generatedCode[0]);
assertEquals((byte) 1, generatedCode[1]);
assertEquals((byte) packet.PARAM15MIN, generatedCode[2]);
// test message decoding
packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0});
assertEquals(false, packet.failed);
packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1});
assertEquals(true, packet.failed);
assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", packet.getFriendlyName());
}
}

View file

@ -0,0 +1,54 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import info.TestBase
import info.nightscout.androidaps.logging.AAPSLogger
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest()
class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest : TestBase() {
@Mock lateinit var aapsLogger: AAPSLogger
@Test fun runTest() {
// under 100% should last 30 min
var packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, 0)
Assert.assertEquals(0, packet.temporaryBasalRatio)
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM30MIN, packet.temporaryBasalDuration)
//constructor with param
packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, 10)
Assert.assertEquals(10, packet.temporaryBasalRatio)
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM30MIN, packet.temporaryBasalDuration)
// over 100% should last 15 min
packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, 150)
Assert.assertEquals(150, packet.temporaryBasalRatio)
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM15MIN, packet.temporaryBasalDuration)
// test low hard limit
packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, -1)
Assert.assertEquals(0, packet.temporaryBasalRatio)
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM30MIN, packet.temporaryBasalDuration)
// test high hard limit
packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, 550)
Assert.assertEquals(500, packet.temporaryBasalRatio)
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM15MIN, packet.temporaryBasalDuration)
// test message generation
packet = DanaRS_Packet_APS_Basal_Set_Temporary_Basal(aapsLogger, 260)
val generatedCode = packet.requestParams
Assert.assertEquals(3, generatedCode.size.toLong())
Assert.assertEquals(4.toByte(), generatedCode[0])
Assert.assertEquals(1.toByte(), generatedCode[1])
Assert.assertEquals(DanaRS_Packet_APS_Basal_Set_Temporary_Basal.PARAM15MIN.toUByte(), generatedCode[2].toUByte())
// test message decoding
packet.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 0.toByte()))
Assert.assertEquals(false, packet.failed)
packet.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 1.toByte()))
Assert.assertEquals(true, packet.failed)
Assert.assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", packet.friendlyName)
}
}

View file

@ -1,106 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm;
import android.content.Context;
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.Calendar;
import java.util.GregorianCalendar;
import info.AAPSMocker;
import info.SPMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.treatments.TreatmentService;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* Created by Rumen on 31.07.2018.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({SP.class, MainApp.class, ConfigBuilderPlugin.class, Context.class, NSUpload.class, TreatmentsPlugin.class, TreatmentService.class, DatabaseHelper.class, DateUtil.class})
public class DanaRS_Packet_APS_History_EventsTest extends DanaRS_Packet_APS_History_Events {
@Test
public void runTest() {
DanaRS_Packet_APS_History_Events testPacket = new DanaRS_Packet_APS_History_Events(DateUtil.now());
// test getRequestedParams
byte[] returnedValues = testPacket.getRequestParams();
byte[] expectedValues = getCalender(DateUtil.now());
//year
assertEquals(expectedValues[0], returnedValues[0]);
//month
assertEquals(expectedValues[1], returnedValues[1]);
//day of month
assertEquals(expectedValues[2], returnedValues[2]);
// hour
assertEquals(expectedValues[3], returnedValues[3]);
// minute
assertEquals(expectedValues[4], returnedValues[4]);
// second
assertEquals(expectedValues[5], returnedValues[5]);
// test message decoding
testPacket.handleMessage(createArray(50, (byte) 0));
assertEquals(false, failed);
// testPacket.handleMessage(createArray(50, (byte) 1));
// assertEquals(true, done);
assertEquals("APS_HISTORY_EVENTS", getFriendlyName());
}
byte[] createArray(int length, byte fillWith) {
byte[] ret = new byte[length];
for (int i = 0; i < length; i++) {
ret[i] = fillWith;
}
return ret;
}
public byte[] getCalender(long from) {
GregorianCalendar cal = new GregorianCalendar();
if (from != 0)
cal.setTimeInMillis(from);
else
cal.set(2000, 0, 1, 0, 0, 0);
byte[] ret = new byte[6];
ret[0] = (byte) ((cal.get(Calendar.YEAR) - 1900 - 100) & 0xff);
ret[1] = (byte) ((cal.get(Calendar.MONTH) + 1) & 0xff);
ret[2] = (byte) ((cal.get(Calendar.DAY_OF_MONTH)) & 0xff);
ret[3] = (byte) ((cal.get(Calendar.HOUR_OF_DAY)) & 0xff);
ret[4] = (byte) ((cal.get(Calendar.MINUTE)) & 0xff);
ret[5] = (byte) ((cal.get(Calendar.SECOND)) & 0xff);
return ret;
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
SPMocker.prepareMock();
SP.putString("profile", AAPSMocker.getValidProfileStore().getData().toString());
AAPSMocker.mockConfigBuilder();
AAPSMocker.mockStrings();
PowerMockito.mockStatic(NSUpload.class);
AAPSMocker.mockDatabaseHelper();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(5456465445L);
}
}

View file

@ -0,0 +1,73 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import info.TestBase
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.util.*
@RunWith(PowerMockRunner::class)
@PrepareForTest(RxBusWrapper::class)
class DanaRS_Packet_APS_History_EventsTest : TestBase() {
@Mock lateinit var aapsLogger: AAPSLogger
@Mock lateinit var rxBus: RxBusWrapper
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var danaRSPlugin: DanaRSPlugin
@Test fun runTest() {
val now = DateUtil.now()
val testPacket = DanaRS_Packet_APS_History_Events(aapsLogger, rxBus, resourceHelper, activePlugin, danaRSPlugin, now)
// test getRequestedParams
val returnedValues = testPacket.requestParams
val expectedValues = getCalender(now)
//year
Assert.assertEquals(expectedValues[0], returnedValues[0])
//month
Assert.assertEquals(expectedValues[1], returnedValues[1])
//day of month
Assert.assertEquals(expectedValues[2], returnedValues[2])
// hour
Assert.assertEquals(expectedValues[3], returnedValues[3])
// minute
Assert.assertEquals(expectedValues[4], returnedValues[4])
// second
Assert.assertEquals(expectedValues[5], returnedValues[5])
// test message decoding
testPacket.handleMessage(createArray(50, 0.toByte()))
Assert.assertEquals(false, testPacket.failed)
Assert.assertEquals("APS_HISTORY_EVENTS", testPacket.friendlyName)
}
fun createArray(length: Int, fillWith: Byte): ByteArray {
val ret = ByteArray(length)
for (i in 0 until length) {
ret[i] = fillWith
}
return ret
}
fun getCalender(from: Long): ByteArray {
val cal = GregorianCalendar()
if (from != 0L) cal.timeInMillis = from else cal[2000, 0, 1, 0, 0] = 0
val ret = ByteArray(6)
ret[0] = (cal[Calendar.YEAR] - 1900 - 100 and 0xff).toByte()
ret[1] = (cal[Calendar.MONTH] + 1 and 0xff).toByte()
ret[2] = (cal[Calendar.DAY_OF_MONTH] and 0xff).toByte()
ret[3] = (cal[Calendar.HOUR_OF_DAY] and 0xff).toByte()
ret[4] = (cal[Calendar.MINUTE] and 0xff).toByte()
ret[5] = (cal[Calendar.SECOND] and 0xff).toByte()
return ret
}
}

View file

@ -1,72 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm;
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 info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* Created by Rumen on 31.07.2018.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, L.class, DateUtil.class})
public class DanaRS_Packet_APS_Set_Event_HistoryTest {
@Test
public void runTest() {
// test for negative carbs
DanaRS_Packet_APS_Set_Event_History historyTest = new DanaRS_Packet_APS_Set_Event_History(DanaRPump.CARBS, DateUtil.now(), -1, 0);
byte[] testparams = historyTest.getRequestParams();
assertEquals((byte) 0, testparams[8]);
// 5g carbs
historyTest = new DanaRS_Packet_APS_Set_Event_History(DanaRPump.CARBS, DateUtil.now(), 5, 0);
testparams = historyTest.getRequestParams();
assertEquals((byte) 5, testparams[8]);
// 150g carbs
historyTest = new DanaRS_Packet_APS_Set_Event_History(DanaRPump.CARBS, DateUtil.now(), 150, 0);
testparams = historyTest.getRequestParams();
assertEquals((byte) 150, testparams[8]);
// test low hard limit
// test high hard limit
// test message generation
historyTest = new DanaRS_Packet_APS_Set_Event_History(DanaRPump.CARBS, DateUtil.now(), 5, 0);
testparams = historyTest.getRequestParams();
assertEquals((byte) 5, testparams[8]);
assertEquals(11, testparams.length);
assertEquals((byte) DanaRPump.CARBS, testparams[0]);
// test message decoding
historyTest.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0});
assertEquals(false, historyTest.failed);
historyTest.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1});
assertEquals(true, historyTest.failed);
assertEquals("APS_SET_EVENT_HISTORY", historyTest.getFriendlyName());
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(5456465445L);
}
}

View file

@ -0,0 +1,56 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import info.AAPSMocker
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump
import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest()
class DanaRS_Packet_APS_Set_Event_HistoryTest {
@Mock lateinit var aapsLogger: AAPSLogger
@Mock lateinit var rxBus: RxBusWrapper
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var danaRSPlugin: DanaRSPlugin
@Test fun runTest() { // test for negative carbs
val now = DateUtil.now()
var historyTest = DanaRS_Packet_APS_Set_Event_History(aapsLogger, DanaRPump.CARBS, now, -1, 0)
var testparams = historyTest.requestParams
Assert.assertEquals(0.toByte(), testparams[8])
// 5g carbs
historyTest = DanaRS_Packet_APS_Set_Event_History(aapsLogger, DanaRPump.CARBS, now, 5, 0)
testparams = historyTest.requestParams
Assert.assertEquals(5.toByte(), testparams[8])
// 150g carbs
historyTest = DanaRS_Packet_APS_Set_Event_History(aapsLogger, DanaRPump.CARBS, now, 150, 0)
testparams = historyTest.requestParams
Assert.assertEquals(150.toByte(), testparams[8])
// test message generation
historyTest = DanaRS_Packet_APS_Set_Event_History(aapsLogger, DanaRPump.CARBS, now, 5, 0)
testparams = historyTest.requestParams
Assert.assertEquals(5.toByte(), testparams[8])
Assert.assertEquals(11, testparams.size)
Assert.assertEquals(DanaRPump.CARBS.toByte(), testparams[0])
// test message decoding
historyTest.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 0.toByte()))
Assert.assertEquals(false, historyTest.failed)
historyTest.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 1.toByte()))
Assert.assertEquals(true, historyTest.failed)
Assert.assertEquals("APS_SET_EVENT_HISTORY", historyTest.friendlyName)
}
}