Skip to content

Commit

Permalink
adds tests for workflow request and response objects
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <snistala@amazon.com>
  • Loading branch information
eirsep committed May 25, 2023
1 parent 3f512dc commit 4c0c72b
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import org.opensearch.commons.alerting.action.GetAlertsRequest
import org.opensearch.commons.alerting.action.GetAlertsResponse
import org.opensearch.commons.alerting.action.GetFindingsRequest
import org.opensearch.commons.alerting.action.GetFindingsResponse
import org.opensearch.commons.alerting.action.GetWorkflowRequest
import org.opensearch.commons.alerting.action.GetWorkflowResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.action.IndexWorkflowRequest
Expand Down Expand Up @@ -134,6 +136,23 @@ internal class AlertingPluginInterfaceTests {
AlertingPluginInterface.deleteWorkflow(client, request, listener)
}

@Test
fun getWorkflow() {
val request = mock(GetWorkflowRequest::class.java)
val response = GetWorkflowResponse(
id = "id", version = 1, seqNo = 1, primaryTerm = 1, status = RestStatus.OK, workflow = randomWorkflow()
)
val listener: ActionListener<GetWorkflowResponse> =
mock(ActionListener::class.java) as ActionListener<GetWorkflowResponse>

Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetWorkflowResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())

AlertingPluginInterface.getWorkflow(client, request, listener)
}

@Test
fun getAlerts() {
val monitor = randomQueryLevelMonitor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ fun randomClusterMetricsInput(
return ClusterMetricsInput(path, pathParams, url)
}

fun Workflow.toJsonString(): String {
val builder = XContentFactory.jsonBuilder()
return this.toXContentWithUser(builder, ToXContent.EMPTY_PARAMS).string()
}

fun Monitor.toJsonString(): String {
val builder = XContentFactory.jsonBuilder()
return this.toXContent(builder, ToXContent.EMPTY_PARAMS).string()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.opensearch.commons.alerting.action

import org.junit.Assert
import org.junit.Test
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.common.io.stream.StreamInput

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.opensearch.commons.alerting.action

import org.junit.Assert
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.common.io.stream.StreamInput

class DeleteWorkflowResponseTests {

@Test
fun `test delete workflow response`() {

val res = DeleteWorkflowResponse(id = "w1", version = 1, nonDeletedMonitors = listOf("m1"))
Assert.assertNotNull(res)
Assert.assertEquals("w1", res.id)

val out = BytesStreamOutput()
res.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newRes = DeleteWorkflowResponse(sin)
Assert.assertEquals("w1", newRes.id)
Assert.assertEquals("m1", newRes.nonDeletedMonitors!!.get(0))
Assert.assertEquals(1, newRes.version)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.opensearch.commons.alerting.action

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.rest.RestRequest

class GetWorkflowRequestTests {

@Test
fun testGetWorkflowRequest() {
val request = GetWorkflowRequest("w1", RestRequest.Method.GET)
Assertions.assertNull(request.validate())

val out = BytesStreamOutput()
request.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = GetWorkflowRequest(sin)
Assertions.assertEquals("w1", newReq.workflowId)
Assertions.assertEquals(RestRequest.Method.GET, newReq.method)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.opensearch.commons.alerting.action

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.commons.alerting.randomWorkflow
import org.opensearch.rest.RestStatus

class GetWorkflowResponseTests {

@Test
fun testGetWorkflowRequest() {
val workflow = randomWorkflow()
val response = GetWorkflowResponse(
id = "id", version = 1, seqNo = 1, primaryTerm = 1, status = RestStatus.OK, workflow = workflow
)
val out = BytesStreamOutput()
response.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newRes = GetWorkflowResponse(sin)
Assertions.assertEquals("id", newRes.id)
Assertions.assertEquals(workflow.name, newRes.workflow!!.name)
Assertions.assertEquals(workflow.owner, newRes.workflow!!.owner)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.opensearch.commons.alerting.randomQueryLevelTrigger
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.authuser.User
import org.opensearch.search.builder.SearchSourceBuilder

Expand Down Expand Up @@ -81,6 +82,16 @@ class WriteableTests {
Assertions.assertEquals(monitor, newMonitor, "Round tripping QueryLevelMonitor doesn't work")
}

@Test
fun `test workflow as stream`() {
val workflow = randomWorkflow(monitorIds = listOf("1", "2", "3", "4"))
val out = BytesStreamOutput()
workflow.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newWorkflow = Workflow(sin)
Assertions.assertEquals(newWorkflow, workflow, "Round tripping Workflow failed")
}

@Test
fun `test query-level trigger as stream`() {
val trigger = randomQueryLevelTrigger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ class XContentTests {
Assertions.assertNull(parsedMonitor.user)
}

@Test
fun `test workflow parsing`() {
val workflow = randomWorkflow(monitorIds = listOf("1", "2", "3"))

val monitorString = workflow.toJsonString()
val parsedWorkflow = Workflow.parse(parser(monitorString))
Assertions.assertEquals(workflow, parsedWorkflow, "Round tripping workflow failed")
}

@Test
fun `test old monitor format parsing`() {
val monitorString = """
Expand Down

0 comments on commit 4c0c72b

Please sign in to comment.