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