From e0dd805e4e337eef899307eb2f8347654c8b3425 Mon Sep 17 00:00:00 2001 From: Surya Sashank Nistala Date: Mon, 19 Jun 2023 16:21:48 -0700 Subject: [PATCH] add tests for chained alert Signed-off-by: Surya Sashank Nistala --- .../alerting/model/ChainedAlertTrigger.kt | 11 ----------- .../opensearch/commons/alerting/AlertTests.kt | 10 ++++++++++ .../opensearch/commons/alerting/TestHelpers.kt | 18 +++++++++++++++++- .../commons/alerting/model/WriteableTests.kt | 11 +++++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/ChainedAlertTrigger.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/ChainedAlertTrigger.kt index 16c8cb20..debc4ca6 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/ChainedAlertTrigger.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/ChainedAlertTrigger.kt @@ -92,7 +92,6 @@ data class ChainedAlertTrigger( lateinit var name: String lateinit var severity: String lateinit var condition: Script - val queryIds: MutableList = mutableListOf() val actions: MutableList = mutableListOf() if (xcp.currentToken() != XContentParser.Token.START_OBJECT && xcp.currentToken() != XContentParser.Token.FIELD_NAME) { @@ -119,16 +118,6 @@ data class ChainedAlertTrigger( } xcp.nextToken() } - QUERY_IDS_FIELD -> { - XContentParserUtils.ensureExpectedToken( - XContentParser.Token.START_ARRAY, - xcp.currentToken(), - xcp - ) - while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { - queryIds.add(xcp.text()) - } - } ACTIONS_FIELD -> { XContentParserUtils.ensureExpectedToken( XContentParser.Token.START_ARRAY, diff --git a/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt index 23e47825..29b32bb5 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.opensearch.commons.alerting.model.Alert +import kotlin.test.assertTrue class AlertTests { @Test @@ -58,4 +59,13 @@ class AlertTests { val activeAlert = randomAlert().copy(state = Alert.State.ACTIVE) Assertions.assertFalse(activeAlert.isAcknowledged(), "Alert is acknowledged") } + + @Test + fun `test chained alert`() { + val workflow = randomWorkflow() + val trigger = randomChainedAlertTrigger() + val alert = randomChainedAlert(workflow = workflow, trigger = trigger) + assertEquals(alert.monitorId, "") + assertEquals(alert.id, "") + } } diff --git a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt index e4024054..6cf956e2 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt @@ -64,6 +64,7 @@ import org.opensearch.search.builder.SearchSourceBuilder import java.time.Instant import java.time.temporal.ChronoUnit import java.util.Random +import java.util.UUID const val ALL_ACCESS_ROLE = "all_access" @@ -171,7 +172,7 @@ fun randomWorkflow( enabled: Boolean = Random().nextBoolean(), enabledTime: Instant? = if (enabled) Instant.now().truncatedTo(ChronoUnit.MILLIS) else null, lastUpdateTime: Instant = Instant.now().truncatedTo(ChronoUnit.MILLIS), - triggers: List = (1..RandomNumbers.randomIntBetween(Random(), 0, 10)).map { randomChainedAlertTrigger() }, + triggers: List = listOf(randomChainedAlertTrigger()), ): Workflow { val delegates = mutableListOf() if (!monitorIds.isNullOrEmpty()) { @@ -523,6 +524,21 @@ fun randomAlert(monitor: Monitor = randomQueryLevelMonitor()): Alert { ) } +fun randomChainedAlert( + workflow: Workflow = randomWorkflow(), + trigger: ChainedAlertTrigger = randomChainedAlertTrigger(), +): Alert { + return Alert( + startTime = Instant.now(), + lastNotificationTime = Instant.now(), + state = Alert.State.ACTIVE, + errorMessage = "error", + executionId = UUID.randomUUID().toString(), + chainedAlertTrigger = trigger, + workflow = workflow + ) +} + fun randomActionExecutionResult( actionId: String = UUIDs.base64UUID(), lastExecutionTime: Instant = Instant.now().truncatedTo(ChronoUnit.MILLIS), diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt index 83ff4014..0c6c1b07 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt @@ -10,6 +10,7 @@ import org.opensearch.commons.alerting.model.action.Throttle import org.opensearch.commons.alerting.randomAction import org.opensearch.commons.alerting.randomActionExecutionPolicy import org.opensearch.commons.alerting.randomBucketLevelTrigger +import org.opensearch.commons.alerting.randomChainedAlertTrigger import org.opensearch.commons.alerting.randomDocumentLevelTrigger import org.opensearch.commons.alerting.randomQueryLevelMonitor import org.opensearch.commons.alerting.randomQueryLevelTrigger @@ -122,6 +123,16 @@ class WriteableTests { Assertions.assertEquals(trigger, newTrigger, "Round tripping DocumentLevelTrigger doesn't work") } + @Test + fun `test chained alert trigger as stream`() { + val trigger = randomChainedAlertTrigger() + val out = BytesStreamOutput() + trigger.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newTrigger = ChainedAlertTrigger.readFrom(sin) + Assertions.assertEquals(trigger, newTrigger, "Round tripping DocumentLevelTrigger doesn't work") + } + @Test fun `test searchinput as stream`() { val input = SearchInput(emptyList(), SearchSourceBuilder())