From 60f2bb368deeb6e1447f93bb97f8d81029674a4f Mon Sep 17 00:00:00 2001 From: Subhobrata Dey Date: Thu, 6 Jun 2024 16:41:48 +0000 Subject: [PATCH] update test case to verify custom triggers work with remote doc-level monitors (#1566) Signed-off-by: Subhobrata Dey --- .../alerting/SampleRemoteMonitorRestHandler.java | 5 ++++- ...TransportRemoteDocLevelMonitorFanOutAction.java | 14 +++++++++++++- .../opensearch/alerting/SampleRemoteMonitorIT.java | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/SampleRemoteMonitorRestHandler.java b/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/SampleRemoteMonitorRestHandler.java index a8b5c57de..ac9d9f29e 100644 --- a/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/SampleRemoteMonitorRestHandler.java +++ b/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/SampleRemoteMonitorRestHandler.java @@ -229,7 +229,10 @@ public void onFailure(Exception e) { null, 0, List.of(remoteDocLevelMonitorInput), - List.of(), + List.of(new RemoteMonitorTrigger("id", "name", "1", + List.of(new Action("name", "destinationId", new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "Hello World", Map.of()), + new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "Hello World", Map.of()), false, new Throttle(60, ChronoUnit.MINUTES), + "id", null)), trigger1Serialized)), Map.of(), new DataSources(), "sample-remote-monitor-plugin" diff --git a/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/monitor/fanouts/TransportRemoteDocLevelMonitorFanOutAction.java b/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/monitor/fanouts/TransportRemoteDocLevelMonitorFanOutAction.java index 71a16c718..57f3496aa 100644 --- a/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/monitor/fanouts/TransportRemoteDocLevelMonitorFanOutAction.java +++ b/sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/monitor/fanouts/TransportRemoteDocLevelMonitorFanOutAction.java @@ -12,6 +12,7 @@ import org.opensearch.action.support.WriteRequest; import org.opensearch.alerting.monitor.inputs.SampleRemoteDocLevelMonitorInput; import org.opensearch.alerting.monitor.runners.SampleRemoteDocLevelMonitorRunner; +import org.opensearch.alerting.monitor.triggers.SampleRemoteMonitorTrigger1; import org.opensearch.client.Client; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; @@ -21,7 +22,9 @@ import org.opensearch.commons.alerting.model.DocLevelMonitorInput; import org.opensearch.commons.alerting.model.InputRunResults; import org.opensearch.commons.alerting.model.Monitor; +import org.opensearch.commons.alerting.model.Trigger; import org.opensearch.commons.alerting.model.remote.monitors.RemoteDocLevelMonitorInput; +import org.opensearch.commons.alerting.model.remote.monitors.RemoteMonitorTrigger; import org.opensearch.core.action.ActionListener; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.core.common.io.stream.StreamInput; @@ -71,9 +74,18 @@ protected void doExecute(Task task, DocLevelMonitorFanOutRequest request, Action DocLevelMonitorInput docLevelMonitorInput = input.getDocLevelMonitorInput(); String index = docLevelMonitorInput.getIndices().get(0); + BytesReference customTriggerSerialized = null; + Trigger trigger = monitor.getTriggers().get(0); + if (trigger instanceof RemoteMonitorTrigger) { + customTriggerSerialized = ((RemoteMonitorTrigger) trigger).getTrigger(); + } + StreamInput triggerSin = StreamInput.wrap(customTriggerSerialized.toBytesRef().bytes); + SampleRemoteMonitorTrigger1 remoteMonitorTrigger = new SampleRemoteMonitorTrigger1(triggerSin); + + ((Map) lastRunContext.get(index)).put("0", 0); IndexRequest indexRequest = new IndexRequest(SampleRemoteDocLevelMonitorRunner.SAMPLE_REMOTE_DOC_LEVEL_MONITOR_RUNNER_INDEX) - .source(sampleRemoteDocLevelMonitorInput.getB()).setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); + .source(Map.of(sampleRemoteDocLevelMonitorInput.getA(), remoteMonitorTrigger.getA())).setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); this.client.index(indexRequest, new ActionListener<>() { @Override public void onResponse(IndexResponse indexResponse) { diff --git a/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java b/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java index b6c889a8e..5e76f23d5 100644 --- a/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java +++ b/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java @@ -168,8 +168,8 @@ public void testSampleRemoteDocLevelMonitor() throws IOException, InterruptedExc searchResponse.getEntity().getContent() ).map(); found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) == 1 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("world") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("world").toString().equals("1")); + ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("hello") && + ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("hello").toString().equals("hello")); return found.get(); } catch (IOException ex) { return false;