diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt index d726d561..322e4b30 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt @@ -1,6 +1,7 @@ package org.opensearch.commons.alerting.model import org.opensearch.common.CheckedFunction +import org.opensearch.commons.alerting.model.remote.monitors.RemoteMonitorTrigger import org.opensearch.commons.alerting.util.IndexUtils.Companion.MONITOR_MAX_INPUTS import org.opensearch.commons.alerting.util.IndexUtils.Companion.MONITOR_MAX_TRIGGERS import org.opensearch.commons.alerting.util.IndexUtils.Companion.NO_SCHEMA_VERSION @@ -188,8 +189,10 @@ data class Monitor( inputs.forEach { if (it is SearchInput) { out.writeEnum(Input.Type.SEARCH_INPUT) - } else { + } else if (it is DocLevelMonitorInput) { out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT) + } else { + out.writeEnum(Input.Type.REMOTE_DOC_LEVEL_MONITOR_INPUT) } it.writeTo(out) } @@ -199,6 +202,7 @@ data class Monitor( when (it) { is BucketLevelTrigger -> out.writeEnum(Trigger.Type.BUCKET_LEVEL_TRIGGER) is DocumentLevelTrigger -> out.writeEnum(Trigger.Type.DOCUMENT_LEVEL_TRIGGER) + is RemoteMonitorTrigger -> out.writeEnum(Trigger.Type.REMOTE_MONITOR_TRIGGER) else -> out.writeEnum(Trigger.Type.QUERY_LEVEL_TRIGGER) } it.writeTo(out) 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 a7866262..6aecb888 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt @@ -29,6 +29,7 @@ import org.opensearch.commons.alerting.randomThrottle import org.opensearch.commons.alerting.randomUser import org.opensearch.commons.alerting.randomUserEmpty import org.opensearch.commons.alerting.randomWorkflow +import org.opensearch.commons.alerting.util.IndexUtils import org.opensearch.commons.authuser.User import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput @@ -37,6 +38,7 @@ import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.test.OpenSearchTestCase import java.io.IOException import java.time.Instant +import java.time.temporal.ChronoUnit import kotlin.test.assertTrue class WriteableTests { @@ -370,6 +372,47 @@ class WriteableTests { Assert.assertEquals("Round tripping DocLevelMonitorInput failed", newDocLevelMonitorInput, docLevelMonitorInput) } + @Test + fun `test RemoteMonitor as stream`() { + val myMonitorInput = MyMonitorInput(1, "hello", MyMonitorInput(2, "world", null)) + var myObjOut = BytesStreamOutput() + myMonitorInput.writeTo(myObjOut) + val docLevelMonitorInput = DocLevelMonitorInput( + "test", + listOf("test"), + listOf(randomDocLevelQuery()) + ) + val remoteDocLevelMonitorInput = RemoteDocLevelMonitorInput(myObjOut.bytes(), docLevelMonitorInput) + + val myMonitorTrigger = MyMonitorTrigger(1, "hello", MyMonitorTrigger(2, "world", null)) + myObjOut = BytesStreamOutput() + myMonitorTrigger.writeTo(myObjOut) + val remoteMonitorTrigger = RemoteMonitorTrigger("id", "name", "1", listOf(), myObjOut.bytes()) + + val monitor = Monitor( + Monitor.NO_ID, + Monitor.NO_VERSION, + "hello", + true, + IntervalSchedule(1, ChronoUnit.MINUTES), + Instant.now(), + Instant.now(), + "remote_doc_level_monitor", + null, + IndexUtils.NO_SCHEMA_VERSION, + listOf(remoteDocLevelMonitorInput), + listOf(remoteMonitorTrigger), + mapOf() + ) + + val out = BytesStreamOutput() + monitor.writeTo(out) + + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newMonitor = Monitor(sin) + Assert.assertEquals("Round tripping RemoteMonitor failed", monitor, newMonitor) + } + @Test fun `test Comment object`() { val user = randomUser()