fix tests

This commit is contained in:
Milos Kozak 2021-09-06 11:27:27 +02:00
parent 379ed6dca6
commit 84343c39a4
4 changed files with 16 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger
import info.nightscout.androidaps.queue.Callback
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import javax.inject.Inject
@ -33,7 +34,7 @@ abstract class Action(val injector: HasAndroidInjector) {
open fun hasDialog(): Boolean = false
open fun toJSON(): String =
JSONObject().put("type", this.javaClass.name).toString()
JSONObject().put("type", this.javaClass.simpleName).toString()
open fun fromJSON(data: String): Action = this
@ -47,7 +48,7 @@ abstract class Action(val injector: HasAndroidInjector) {
fun instantiate(obj: JSONObject): Action? {
try {
val type = obj.getString("type")
val data = obj.getJSONObject("data")
val data = if (obj.has("data")) obj.getJSONArray("data") else JSONArray()
return when (type) {
ActionAlarm::class.java.name, // backward compatibility
ActionAlarm::class.java.simpleName -> ActionAlarm(injector).fromJSON(data.toString())

View file

@ -6,6 +6,7 @@ import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.interfaces.ConfigBuilder
import info.nightscout.androidaps.interfaces.Loop
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.automation.actions.Action
import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopEnable
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnectorTest
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDummy
@ -29,6 +30,9 @@ class AutomationEventTest : TestBase() {
if (it is AutomationEvent) {
it.aapsLogger = aapsLogger
}
if (it is Action) {
it.aapsLogger = aapsLogger
}
if (it is ActionLoopEnable) {
it.loopPlugin = loopPlugin
it.resourceHelper = resourceHelper
@ -48,7 +52,7 @@ class AutomationEventTest : TestBase() {
event.addAction(ActionLoopEnable(injector))
// export to json
val eventJsonExpected = "{\"autoRemove\":false,\"readOnly\":false,\"trigger\":\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[\\\"{\\\\\\\"data\\\\\\\":{\\\\\\\"connectorType\\\\\\\":\\\\\\\"AND\\\\\\\",\\\\\\\"triggerList\\\\\\\":[]},\\\\\\\"type\\\\\\\":\\\\\\\"TriggerConnector\\\\\\\"}\\\"]},\\\"type\\\":\\\"TriggerConnector\\\"}\",\"title\":\"Test\",\"systemAction\":false,\"actions\":[\"{\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopEnable\\\"}\"],\"enabled\":true}"
val eventJsonExpected = "{\"autoRemove\":false,\"readOnly\":false,\"trigger\":\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[\\\"{\\\\\\\"data\\\\\\\":{\\\\\\\"connectorType\\\\\\\":\\\\\\\"AND\\\\\\\",\\\\\\\"triggerList\\\\\\\":[]},\\\\\\\"type\\\\\\\":\\\\\\\"TriggerConnector\\\\\\\"}\\\"]},\\\"type\\\":\\\"TriggerConnector\\\"}\",\"title\":\"Test\",\"systemAction\":false,\"actions\":[\"{\\\"type\\\":\\\"ActionLoopEnable\\\"}\"],\"enabled\":true}"
Assert.assertEquals(eventJsonExpected, event.toJSON())
// clone
@ -64,7 +68,7 @@ class AutomationEventTest : TestBase() {
Assert.assertEquals(event.trigger.toJSON(), clone.trigger.toJSON())
// check action
Assert.assertEquals(1, clone.actions.size.toLong())
Assert.assertEquals(1, clone.actions.size)
Assert.assertFalse(event.actions === clone.actions) // not the same object reference
Assert.assertEquals(clone.toJSON(), clone.toJSON())
}

View file

@ -66,7 +66,7 @@ class ActionStopTempTargetTest : ActionsTestBase() {
}
@Test fun toJSONTest() {
Assert.assertEquals("{\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget\"}", sut.toJSON())
Assert.assertEquals("{\"type\":\"ActionStopTempTarget\"}", sut.toJSON())
}
@Test fun fromJSONTest() {

View file

@ -90,21 +90,25 @@ open class ActionsTestBase : TestBaseWithProfile() {
it.dateUtil = dateUtil
}
if (it is ActionProfileSwitchPercent) {
it.aapsLogger = aapsLogger
it.resourceHelper = resourceHelper
it.profileFunction = profileFunction
it.uel = uel
}
if (it is ActionNotification) {
it.aapsLogger = aapsLogger
it.resourceHelper = resourceHelper
it.rxBus = rxBus
}
if (it is ActionLoopSuspend) {
it.aapsLogger = aapsLogger
it.loopPlugin = loopPlugin
it.resourceHelper = resourceHelper
it.rxBus = rxBus
it.uel = uel
}
if (it is ActionLoopResume) {
it.aapsLogger = aapsLogger
it.loopPlugin = loopPlugin
it.resourceHelper = resourceHelper
it.configBuilder = configBuilder
@ -114,6 +118,7 @@ open class ActionsTestBase : TestBaseWithProfile() {
it.uel = uel
}
if (it is ActionLoopEnable) {
it.aapsLogger = aapsLogger
it.loopPlugin = loopPlugin
it.resourceHelper = resourceHelper
it.configBuilder = configBuilder
@ -121,6 +126,7 @@ open class ActionsTestBase : TestBaseWithProfile() {
it.uel = uel
}
if (it is ActionLoopDisable) {
it.aapsLogger = aapsLogger
it.loopPlugin = loopPlugin
it.resourceHelper = resourceHelper
it.configBuilder = configBuilder