From a6468781f05637e0f8df72cd4a04603d398228ab Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Fri, 7 Nov 2014 17:43:35 +0000 Subject: [PATCH] Alert Trigger : add trigger unit test. This commit adds a simple unit test for the alert triggers. Original commit: elastic/x-pack-elasticsearch@d9f42aa9130e530e09a7e1d2c5ce38b3efa37a36 --- .../alerts/triggers/TriggerUnitTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/java/org/elasticsearch/alerts/triggers/TriggerUnitTest.java diff --git a/src/test/java/org/elasticsearch/alerts/triggers/TriggerUnitTest.java b/src/test/java/org/elasticsearch/alerts/triggers/TriggerUnitTest.java new file mode 100644 index 00000000000..d78ffb8bbf2 --- /dev/null +++ b/src/test/java/org/elasticsearch/alerts/triggers/TriggerUnitTest.java @@ -0,0 +1,90 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.alerts.triggers; + +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.ShardSearchFailure; +import org.elasticsearch.alerts.client.AlertsClient; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.env.Environment; +import org.elasticsearch.script.ScriptEngineService; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.groovy.GroovyScriptEngineService; +import org.elasticsearch.search.internal.InternalSearchResponse; +import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.watcher.ResourceWatcherService; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + + +public class TriggerUnitTest extends ElasticsearchTestCase { + + private XContentBuilder createTriggerContent(String script, String scriptLang, ScriptService.ScriptType scriptType) throws IOException { + XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); + jsonBuilder.startObject(); + jsonBuilder.field("script"); + jsonBuilder.startObject(); + jsonBuilder.field("script", script); + if (scriptLang != null) { + jsonBuilder.field("script_lang", scriptLang); + } + if (scriptType != null) { + jsonBuilder.field("script_type", scriptType.toString()); + } + jsonBuilder.endObject(); + jsonBuilder.endObject(); + return jsonBuilder; + } + + public void testInlineScriptTriggers() throws Exception { + + Settings settings = ImmutableSettings.settingsBuilder().build(); + GroovyScriptEngineService groovyScriptEngineService = new GroovyScriptEngineService(settings); + ThreadPool tp = new ThreadPool(ThreadPool.Names.SAME); + Set engineServiceSet = new HashSet<>(); + engineServiceSet.add(groovyScriptEngineService); + + ScriptService scriptService = new ScriptService(settings, new Environment(), engineServiceSet, new ResourceWatcherService(settings, tp)); + TriggerManager triggerManager = new TriggerManager(settings, null, scriptService); + + try { + XContentBuilder builder = createTriggerContent("hits.total > 1", null, null); + XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes()); + AlertTrigger trigger = triggerManager.instantiateAlertTrigger(parser); + + SearchRequest request = new SearchRequest(); + request.indices("my-index"); + request.types("my-type"); + + + SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]); + assertFalse(triggerManager.isTriggered(trigger, request, response).isTriggered()); + + + builder = createTriggerContent("return true", null, null); + parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes()); + trigger = triggerManager.instantiateAlertTrigger(parser); + + assertTrue(triggerManager.isTriggered(trigger, request, response).isTriggered()); + + + tp.shutdownNow(); + + } catch (IOException ioe) { + throw new ElasticsearchException("Failed to construct the trigger", ioe); + } + } + +}