Skip to content

Commit

Permalink
feat: add tests for the toggle monitor api
Browse files Browse the repository at this point in the history
Signed-off-by: vikhy-aws <[email protected]>
  • Loading branch information
vikhy-aws committed Jan 16, 2025
1 parent 314dc06 commit a40061d
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.apache.hc.core5.http.message.BasicHeader
import org.opensearch.alerting.ALERTING_BASE_URI
import org.opensearch.alerting.ALWAYS_RUN
import org.opensearch.alerting.ANOMALY_DETECTOR_INDEX
import org.opensearch.alerting.AlertingPlugin
import org.opensearch.alerting.AlertingRestTestCase
import org.opensearch.alerting.LEGACY_OPENDISTRO_ALERTING_BASE_URI
import org.opensearch.alerting.alerts.AlertIndices
Expand Down Expand Up @@ -1332,6 +1333,94 @@ class MonitorRestApiIT : AlertingRestTestCase() {
}
}

fun `test enable monitor`() {
val monitorId = createMonitor(randomQueryLevelMonitor(enabled = false, enabledTime = null), refresh = true).id
val response = client().makeRequest(
"PUT",
"${AlertingPlugin.MONITOR_BASE_URI}/$monitorId/enable",
emptyMap(),
null
)
val jsonResponse = createParser(XContentType.JSON.xContent(), response.entity.content).map()
val monitor = jsonResponse["Monitor"] as Map<*, *>
val enabled = monitor["enabled"]
// Monitor should be enabled
assertEquals(true, enabled)
}

fun `test disable monitor`() {
val monitorId = createMonitor(randomQueryLevelMonitor(enabled = true, enabledTime = Instant.now()), refresh = true).id
val response = client().makeRequest(
"PUT",
"${AlertingPlugin.MONITOR_BASE_URI}/$monitorId/disable",
emptyMap(),
null
)
val jsonResponse = createParser(XContentType.JSON.xContent(), response.entity.content).map()
val monitor = jsonResponse["Monitor"] as Map<*, *>
val enabled = monitor["enabled"]
val enabledTime = monitor["enabledTime"]
// Monitor should be disabled and the enabledTime should be null
assertEquals(false, enabled)
assertNull(enabledTime)
}

fun `test enable monitor when already enabled`() {
val monitorId = createMonitor(randomQueryLevelMonitor(enabled = true, enabledTime = Instant.now()), refresh = true).id
val exception = assertThrows(ResponseException::class.java) {
client().makeRequest(
"PUT",
"${AlertingPlugin.MONITOR_BASE_URI}/$monitorId/enable",
emptyMap(),
null
)
}
val errorResponse = createParser(XContentType.JSON.xContent(), exception.response.entity.content).map()
// Expected error
val expectedError = mapOf(
"error" to mapOf(
"root_cause" to listOf(
mapOf(
"type" to "status_exception",
"reason" to "Monitor $monitorId is already enabled"
)
),
"type" to "status_exception",
"reason" to "Monitor $monitorId is already enabled"
),
"status" to 400
)
assertEquals(expectedError, errorResponse)
}

fun `test disable monitor when already disabled`() {
val monitorId = createMonitor(randomQueryLevelMonitor(enabled = false, enabledTime = null), refresh = true).id
val exception = assertThrows(ResponseException::class.java) {
client().makeRequest(
"PUT",
"${AlertingPlugin.MONITOR_BASE_URI}/$monitorId/disable",
emptyMap(),
null
)
}
val errorResponse = createParser(XContentType.JSON.xContent(), exception.response.entity.content).map()
// Expected error
val expectedError = mapOf(
"error" to mapOf(
"root_cause" to listOf(
mapOf(
"type" to "status_exception",
"reason" to "Monitor $monitorId is already disabled"
)
),
"type" to "status_exception",
"reason" to "Monitor $monitorId is already disabled"
),
"status" to 400
)
assertEquals(expectedError, errorResponse)
}

/**
* This use case is needed by the frontend plugin for displaying alert counts on the Monitors list page.
* https://github.com/opensearch-project/alerting-dashboards-plugin/blob/main/server/services/MonitorService.js#L235
Expand Down

0 comments on commit a40061d

Please sign in to comment.