diff --git a/.github/workflows/test-workflow.yml b/.github/workflows/test-workflow.yml index 30de39ef5..5dba154c3 100644 --- a/.github/workflows/test-workflow.yml +++ b/.github/workflows/test-workflow.yml @@ -29,6 +29,7 @@ jobs: with: java-version: ${{ matrix.java }} + - name: Build and run with Gradle run: ./gradlew build -Dopensearch.version=1.2.0-SNAPSHOT diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt index ae8653dbd..2780277a0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt @@ -75,7 +75,13 @@ class TransportAcknowledgeAlertAction @Inject constructor( val searchRequest = SearchRequest() .indices(AlertIndices.ALERT_INDEX) .routing(request.monitorId) - .source(SearchSourceBuilder().query(queryBuilder).version(true).seqNoAndPrimaryTerm(true)) + .source( + SearchSourceBuilder() + .query(queryBuilder) + .version(true) + .seqNoAndPrimaryTerm(true) + .size(request.alertIds.size) + ) client.search( searchRequest, diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt index 730b3b241..ccc23f894 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt @@ -569,6 +569,78 @@ class MonitorRestApiIT : AlertingRestTestCase() { assertFalse("Alert in state ${activeAlert.state} found in failed list", failedResponseList.contains(activeAlert.id)) } + fun `test acknowledging more than 10 alerts at once`() { + // GIVEN + putAlertMappings() // Required as we do not have a create alert API. + val monitor = createRandomMonitor(refresh = true) + val alertsToAcknowledge = (1..15).map { createAlert(randomAlert(monitor).copy(state = Alert.State.ACTIVE)) }.toTypedArray() + + // WHEN + val response = acknowledgeAlerts(monitor, *alertsToAcknowledge) + + // THEN + val responseMap = response.asMap() + val expectedAcknowledgedCount = alertsToAcknowledge.size + + val acknowledgedAlerts = responseMap["success"] as List + assertTrue("Expected $expectedAcknowledgedCount alerts to be acknowledged successfully.", acknowledgedAlerts.size == expectedAcknowledgedCount) + + val acknowledgedAlertsList = acknowledgedAlerts.toString() + alertsToAcknowledge.forEach { alert -> assertTrue("Alert with ID ${alert.id} not found in failed list.", acknowledgedAlertsList.contains(alert.id)) } + + val failedResponse = responseMap["failed"] as List + assertTrue("Expected 0 alerts to fail acknowledgment.", failedResponse.isEmpty()) + } + + fun `test acknowledging more than 10 alerts at once, including acknowledged alerts`() { + // GIVEN + putAlertMappings() // Required as we do not have a create alert API. + val monitor = createRandomMonitor(refresh = true) + val alertsGroup1 = (1..15).map { createAlert(randomAlert(monitor).copy(state = Alert.State.ACTIVE)) }.toTypedArray() + acknowledgeAlerts(monitor, *alertsGroup1) // Acknowledging the first array of alerts. + + val alertsGroup2 = (1..15).map { createAlert(randomAlert(monitor).copy(state = Alert.State.ACTIVE)) }.toTypedArray() + + val alertsToAcknowledge = arrayOf(*alertsGroup1, *alertsGroup2) // Creating an array of alerts that includes alerts that have been already acknowledged, and new alerts. + + // WHEN + val response = acknowledgeAlerts(monitor, *alertsToAcknowledge) + + // THEN + val responseMap = response.asMap() + val expectedAcknowledgedCount = alertsToAcknowledge.size - alertsGroup1.size + + val acknowledgedAlerts = responseMap["success"] as List + assertTrue("Expected $expectedAcknowledgedCount alerts to be acknowledged successfully.", acknowledgedAlerts.size == expectedAcknowledgedCount) + + val acknowledgedAlertsList = acknowledgedAlerts.toString() + alertsGroup2.forEach { alert -> assertTrue("Alert with ID ${alert.id} not found in failed list.", acknowledgedAlertsList.contains(alert.id)) } + alertsGroup1.forEach { alert -> assertFalse("Alert with ID ${alert.id} found in failed list.", acknowledgedAlertsList.contains(alert.id)) } + + val failedResponse = responseMap["failed"] as List + assertTrue("Expected ${alertsGroup1.size} alerts to fail acknowledgment.", failedResponse.size == alertsGroup1.size) + + val failedResponseList = failedResponse.toString() + alertsGroup1.forEach { alert -> assertTrue("Alert with ID ${alert.id} not found in failed list.", failedResponseList.contains(alert.id)) } + alertsGroup2.forEach { alert -> assertFalse("Alert with ID ${alert.id} found in failed list.", failedResponseList.contains(alert.id)) } + } + + @Throws(Exception::class) + fun `test acknowledging 0 alerts`() { + // GIVEN + putAlertMappings() // Required as we do not have a create alert API. + val monitor = createRandomMonitor(refresh = true) + val alertsToAcknowledge = arrayOf() + + // WHEN & THEN + try { + acknowledgeAlerts(monitor, *alertsToAcknowledge) + fail("Expected acknowledgeAlerts to throw an exception.") + } catch (e: ResponseException) { + assertEquals("Unexpected status", RestStatus.BAD_REQUEST, e.response.restStatus()) + } + } + fun `test get all alerts in all states`() { putAlertMappings() // Required as we do not have a create alert API. val monitor = createRandomMonitor(refresh = true) diff --git a/release-notes/opensearch-alerting.release-notes-1.1.0.0.md b/release-notes/opensearch-alerting.release-notes-1.1.0.0.md index 2ec826afd..59d83ea09 100644 --- a/release-notes/opensearch-alerting.release-notes-1.1.0.0.md +++ b/release-notes/opensearch-alerting.release-notes-1.1.0.0.md @@ -42,4 +42,4 @@ Compatible with OpenSearch 1.1.0 ### Refactoring -* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143)) \ No newline at end of file +* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143))