From 84343c39a4bebbf40b49345a42117640f1173fd7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 6 Sep 2021 11:27:27 +0200 Subject: [PATCH] fix tests --- .../plugins/general/automation/actions/Action.kt | 5 +++-- .../plugins/general/automation/AutomationEventTest.kt | 8 ++++++-- .../automation/actions/ActionStopTempTargetTest.kt | 2 +- .../plugins/general/automation/actions/ActionsTestBase.kt | 6 ++++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.kt index 53f8a32678..e2616f182a 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.kt @@ -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()) diff --git a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/AutomationEventTest.kt b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/AutomationEventTest.kt index c63aeb1356..4f2fb7cd6b 100644 --- a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/AutomationEventTest.kt +++ b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/AutomationEventTest.kt @@ -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()) } diff --git a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.kt b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.kt index a1e1139070..19e694a5d9 100644 --- a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.kt +++ b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTargetTest.kt @@ -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() { diff --git a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt index 2205947563..742e77cff1 100644 --- a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt +++ b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt @@ -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