Rewrites AutomationEventTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-04 20:36:12 -07:00
parent 584c014263
commit 8a4607549e

View file

@ -10,12 +10,13 @@ import app.aaps.plugins.automation.triggers.TriggerConnector
import app.aaps.plugins.automation.triggers.TriggerConnectorTest import app.aaps.plugins.automation.triggers.TriggerConnectorTest
import app.aaps.plugins.automation.triggers.TriggerDummy import app.aaps.plugins.automation.triggers.TriggerDummy
import app.aaps.shared.tests.TestBase import app.aaps.shared.tests.TestBase
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import org.json.JSONObject import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
import org.skyscreamer.jsonassert.JSONAssert
class AutomationEventTest : TestBase() { class AutomationEventTest : TestBase() {
@ -40,8 +41,7 @@ class AutomationEventTest : TestBase() {
} }
} }
@Test @Test fun testCloneEvent() {
fun testCloneEvent() {
// create test object // create test object
val event = AutomationEventObject(injector) val event = AutomationEventObject(injector)
event.title = "Test" event.title = "Test"
@ -51,33 +51,32 @@ class AutomationEventTest : TestBase() {
// export to json // export to json
val eventJsonExpected = val eventJsonExpected =
"{\"userAction\":false,\"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}" "{\"userAction\":false,\"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}"
Assertions.assertEquals(eventJsonExpected, event.toJSON()) JSONAssert.assertEquals(eventJsonExpected, event.toJSON(), true)
// clone // clone
val clone = AutomationEventObject(injector).fromJSON(eventJsonExpected, 1) val clone = AutomationEventObject(injector).fromJSON(eventJsonExpected, 1)
// check title // check title
Assertions.assertEquals(event.title, clone.title) assertThat(clone.title).isEqualTo(event.title)
// check trigger // check trigger
Assertions.assertNotNull(clone.trigger) assertThat(clone.trigger).isNotNull()
Assertions.assertFalse(event.trigger === clone.trigger) // not the same object reference assertThat(event.trigger).isNotSameInstanceAs(clone.trigger)
Assertions.assertEquals(event.trigger.javaClass, clone.trigger.javaClass) assertThat(event.trigger.javaClass).isNotInstanceOf(clone.trigger.javaClass)
Assertions.assertEquals(event.trigger.toJSON(), clone.trigger.toJSON()) JSONAssert.assertEquals(event.trigger.toJSON(), clone.trigger.toJSON(), true)
// check action // check action
Assertions.assertEquals(1, clone.actions.size) assertThat(clone.actions).hasSize(1)
Assertions.assertFalse(event.actions === clone.actions) // not the same object reference assertThat(event.actions).isNotSameInstanceAs(clone.actions)
Assertions.assertEquals(clone.toJSON(), clone.toJSON()) JSONAssert.assertEquals(clone.toJSON(), clone.toJSON(), true)
} }
@Test @Test fun hasStopProcessing() {
fun hasStopProcessing() {
val event = AutomationEventObject(injector) val event = AutomationEventObject(injector)
event.title = "Test" event.title = "Test"
event.trigger = TriggerDummy(injector).instantiate(JSONObject(TriggerConnectorTest().oneItem)) as TriggerConnector event.trigger = TriggerDummy(injector).instantiate(JSONObject(TriggerConnectorTest().oneItem)) as TriggerConnector
Assertions.assertFalse(event.hasStopProcessing()) assertThat(event.hasStopProcessing()).isFalse()
event.addAction(ActionStopProcessing(injector)) event.addAction(ActionStopProcessing(injector))
Assertions.assertTrue(event.hasStopProcessing()) assertThat(event.hasStopProcessing()).isTrue()
} }
} }