Skip to content

Commit

Permalink
Added integration tests for rest delete action
Browse files Browse the repository at this point in the history
Signed-off-by: Stevan Buzejic <[email protected]>
  • Loading branch information
stevanbz committed Mar 13, 2023
1 parent 0f4f578 commit 13aa3e2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ class RestDeleteWorkflowAction : BaseRestHandler() {
log.debug("${request.method()} ${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}")

val workflowId = request.param("workflowID")
log.debug("${request.method()} ${AlertingPlugin.WORKFLOW_BASE_URI}/$workflowId")
val deleteDelegateMonitors = request.paramAsBoolean("deleteDelegateMonitors", false)
log.debug("${request.method()} ${request.uri()}")

val refreshPolicy =
WriteRequest.RefreshPolicy.parse(request.param(REFRESH, WriteRequest.RefreshPolicy.IMMEDIATE.value))
val deleteWorkflowRequest = DeleteWorkflowRequest(workflowId, true, refreshPolicy)
val deleteWorkflowRequest = DeleteWorkflowRequest(workflowId, deleteDelegateMonitors, refreshPolicy)

return RestChannelConsumer { channel ->
client.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.opensearch.alerting.resthandler

import org.opensearch.action.support.WriteRequest
import org.opensearch.alerting.AlertingPlugin
import org.opensearch.alerting.util.AlertingException
import org.opensearch.alerting.util.IF_PRIMARY_TERM
import org.opensearch.alerting.util.IF_SEQ_NO
import org.opensearch.alerting.util.REFRESH
Expand Down Expand Up @@ -53,7 +54,7 @@ class RestIndexWorkflowAction : BaseRestHandler() {
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer {
val id = request.param("workflowID", Workflow.NO_ID)
if (request.method() == RestRequest.Method.PUT && Workflow.NO_ID == id) {
throw IllegalArgumentException("Missing workflow ID")
throw AlertingException.wrap(IllegalArgumentException("Missing workflow ID"))
}

// Validate request by parsing JSON to Monitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class WorkflowMonitorIT : WorkflowSingleNodeTestCase() {

val monitorResponse2 = createMonitor(monitor2)!!

var workflow = randomWorkflowMonitor(
var workflow = randomWorkflow(
monitorIds = listOf(monitorResponse.id, monitorResponse2.id)
)
val workflowResponse = upsertWorkflow(workflow)!!
Expand Down Expand Up @@ -484,7 +484,7 @@ class WorkflowMonitorIT : WorkflowSingleNodeTestCase() {

var monitorResponse = createMonitor(monitor)!!

val workflowRequest = randomWorkflowMonitor(
val workflowRequest = randomWorkflow(
monitorIds = listOf(monitorResponse.id)
)
val workflowResponse = upsertWorkflow(workflowRequest)!!
Expand All @@ -494,7 +494,7 @@ class WorkflowMonitorIT : WorkflowSingleNodeTestCase() {
assertNotNull(getWorkflowResponse)
assertEquals(workflowId, getWorkflowResponse.id)

val workflowRequest2 = randomWorkflowMonitor(
val workflowRequest2 = randomWorkflow(
monitorIds = listOf(monitorResponse.id)
)
val workflowResponse2 = upsertWorkflow(workflowRequest2)!!
Expand Down Expand Up @@ -862,7 +862,7 @@ class WorkflowMonitorIT : WorkflowSingleNodeTestCase() {
val queryMonitor = randomQueryLevelMonitor()
val queryMonitorResponse = createMonitor(queryMonitor)!!

var workflow = randomWorkflowMonitor(
var workflow = randomWorkflow(
monitorIds = listOf(queryMonitorResponse.id, docMonitorResponse.id)
)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class WorkflowRunnerIT : WorkflowSingleNodeTestCase() {

val monitorResponse2 = createMonitor(monitor2)!!

var workflow = randomWorkflowMonitor(
var workflow = randomWorkflow(
monitorIds = listOf(monitorResponse.id, monitorResponse2.id)
)
val workflowResponse = upsertWorkflow(workflow)!!
Expand Down Expand Up @@ -209,7 +209,7 @@ class WorkflowRunnerIT : WorkflowSingleNodeTestCase() {

val monitorResponse2 = createMonitor(monitor2)!!

var workflow = randomWorkflowMonitor(
var workflow = randomWorkflow(
monitorIds = listOf(monitorResponse.id, monitorResponse2.id)
)
val workflowResponse = upsertWorkflow(workflow)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,91 @@ class WorkflowRestApiIT : WorkflowRestTestCase() {
}
}
}

fun `test delete workflow delete delegate monitors`() {
val query = randomQueryLevelMonitor()
val monitor = createMonitor(query)

val workflowRequest = randomWorkflow(
monitorIds = listOf(monitor.id)
)
val workflowResponse = createWorkflow(workflowRequest)
val workflowId = workflowResponse.id
val getWorkflowResponse = getWorkflow(workflowResponse.id)

assertNotNull(getWorkflowResponse)
assertEquals(workflowId, getWorkflowResponse.id)

client().makeRequest("DELETE", getWorkflowResponse.relativeUrl().plus("?deleteDelegateMonitors=true"))

// Verify that the workflow is deleted
try {
getWorkflow(workflowId)
} catch (e: ResponseException) {
assertEquals(RestStatus.NOT_FOUND, e.response.restStatus())
e.message?.let {
assertTrue(
"Exception not returning GetWorkflow Action error ",
it.contains("Workflow not found.")
)
}
}

// Verify that delegate monitor is deleted
try {
getMonitor(monitor.id)
} catch (e: ResponseException) {
assertEquals(RestStatus.NOT_FOUND, e.response.restStatus())
e.message?.let {
assertTrue(
"Exception not returning GetWorkflow Action error ",
it.contains("Monitor not found.")
)
}
}
}

fun `test delete workflow preserve delegate monitors`() {
val query = randomQueryLevelMonitor()
val monitor = createMonitor(query)

val workflowRequest = randomWorkflow(
monitorIds = listOf(monitor.id)
)
val workflowResponse = createWorkflow(workflowRequest)
val workflowId = workflowResponse.id
val getWorkflowResponse = getWorkflow(workflowResponse.id)

assertNotNull(getWorkflowResponse)
assertEquals(workflowId, getWorkflowResponse.id)

client().makeRequest("DELETE", getWorkflowResponse.relativeUrl().plus("?deleteDelegateMonitors=false"))

// Verify that the workflow is deleted
try {
getWorkflow(workflowId)
} catch (e: ResponseException) {
assertEquals(RestStatus.NOT_FOUND, e.response.restStatus())
e.message?.let {
assertTrue(
"Exception not returning GetWorkflow Action error ",
it.contains("Workflow not found.")
)
}
}

// Verify that delegate monitor is not deleted
val delegateMonitor = getMonitor(monitor.id)
assertNotNull(delegateMonitor)
}

@Throws(Exception::class)
fun `test deleting a workflow that doesn't exist`() {
try {
client().makeRequest("DELETE", "$WORKFLOW_ALERTING_BASE_URI/foobarbaz")
fail("expected 404 ResponseException")
} catch (e: ResponseException) {
assertEquals(RestStatus.NOT_FOUND, e.response.restStatus())
}
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 13aa3e2

Please sign in to comment.