/*
 * 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.actions;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.alerts.AbstractAlertingTests;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertAckState;
import org.elasticsearch.alerts.triggers.ScriptedTrigger;
import org.elasticsearch.alerts.triggers.TriggerResult;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.script.ScriptService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;

/**
*/
public class EmailTemplateTest extends AbstractAlertingTests {

    public void testEmailTemplateRender() throws IOException {
        createIndex("my-trigger-index");

        SearchRequest triggerRequest = createTriggerSearchRequest("my-trigger-index").source(searchSource().query(matchAllQuery()));

        List<AlertAction> actions = new ArrayList<>();


        Alert alert = new Alert("test-email-template",
                triggerRequest,
                new ScriptedTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"),
                actions,
                "0/5 * * * * ? *",
                new DateTime(),
                0,
                new TimeValue(0),
                AlertAckState.NOT_TRIGGERED);

        SearchResponse searchResponse = client().prepareSearch("my-trigger-index").get();
        XContentBuilder responseBuilder = jsonBuilder().startObject().value(searchResponse).endObject();
        Map<String, Object> responseMap = XContentHelper.convertToMap(responseBuilder.bytes(), false).v2();

        TriggerResult result = new TriggerResult(true, triggerRequest, responseMap, alert.getTrigger());

        String template = "{{alert_name}} triggered with {{response.hits.total}} hits";


        ScriptService scriptService = internalTestCluster().getInstance(ScriptService.class);
        String parsedTemplate = SmtpAlertActionFactory.renderTemplate(template, alert, result, scriptService);
        assertEquals("test-email-template triggered with 0 hits", parsedTemplate);

    }

}

@@ -417,35 +417,39 @@
 ch.a
-lerts.AbstractAlertingTests
+ction.search.ShardSearchFailure
 ;%0Aim
@@ -698,16 +698,127 @@
 teTime;%0A
+import org.elasticsearch.common.settings.ImmutableSettings;%0Aimport org.elasticsearch.common.settings.Settings;%0A
 import o
@@ -968,24 +968,66 @@
 tentHelper;%0A
+import org.elasticsearch.env.Environment;%0A
 import org.e
@@ -1048,24 +1048,30 @@
 cript.Script
+Engine
 Service;%0A%0Aim
@@ -1071,118 +1071,197 @@
 ce;%0A
-%0A
 import 
-java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map;%0A
+org.elasticsearch.script.ScriptService;%0Aimport org.elasticsearch.script.mustache.MustacheScriptEngineService;%0Aimport org.elasticsearch.search.internal.InternalSearchResponse;
 %0Aimport 
 stat
@@ -1248,39 +1248,32 @@
 esponse;%0Aimport 
-static 
 org.elasticsearc
@@ -1278,205 +1278,266 @@
 rch.
-common.xcon
 te
-nt.XContentFactory.jsonBuilder;%0Aimport static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;%0Aimport static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource
+st.ElasticsearchTestCase;%0Aimport org.elasticsearch.threadpool.ThreadPool;%0Aimport org.elasticsearch.watcher.ResourceWatcherService;%0A%0Aimport java.io.IOException;%0Aimport java.util.*;%0A%0Aimport static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder
 ;%0A%0A/
@@ -1585,29 +1585,29 @@
 nds 
-AbstractAlertingTests
+ElasticsearchTestCase
  %7B%0A%0A
@@ -1677,16 +1677,18 @@
         
+//
 createIn
@@ -1712,17 +1712,16 @@
 ndex%22);%0A
-%0A
         
@@ -1755,21 +1755,12 @@
 t = 
-createTrigger
+new 
 Sear
@@ -1773,72 +1773,8 @@
 est(
-%22my-trigger-index%22).source(searchSource().query(matchAllQuery())
 );%0A%0A
@@ -2248,57 +2248,97 @@
 e = 
-client().prepareSearch(%22my-trigger-index%22).get();
+new SearchResponse(InternalSearchResponse.empty(),%22%22,0,0,0L, new ShardSearchFailure%5B0%5D);%0A
 %0A   
@@ -2747,100 +2747,537 @@
 %22;%0A%0A
-%0A        ScriptService scriptService = internalTestCluster().getInstance(ScriptService.class
+        Settings settings = ImmutableSettings.settingsBuilder().build();%0A        MustacheScriptEngineService mustacheScriptEngineService = new MustacheScriptEngineService(settings);%0A        ThreadPool tp;%0A        tp = new ThreadPool(ThreadPool.Names.SAME);%0A        Set%3CScriptEngineService%3E engineServiceSet = new HashSet%3C%3E();%0A        engineServiceSet.add(mustacheScriptEngineService);%0A%0A        ScriptService scriptService = new ScriptService(settings, new Environment(), engineServiceSet, new ResourceWatcherService(settings, tp)
 );%0A 
@@ -3386,16 +3386,42 @@
 rvice);%0A
+        tp.shutdownNow();%0A
         

Fix the email template test to be a lightweight unit test case and not start any unneeded services.