<commit_before>/*
 * 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);

    }

}
<commit_msg>Fix the email template test to be a lightweight unit test case and not start any unneeded services.<commit_after>/*
 * 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.action.search.ShardSearchFailure;
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.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.env.Environment;
import org.elasticsearch.script.ScriptEngineService;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
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.*;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
*/
public class EmailTemplateTest extends ElasticsearchTestCase {

    public void testEmailTemplateRender() throws IOException {
        //createIndex("my-trigger-index");
        SearchRequest triggerRequest = new SearchRequest();

        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 = new SearchResponse(InternalSearchResponse.empty(),"",0,0,0L, new ShardSearchFailure[0]);

        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";

        Settings settings = ImmutableSettings.settingsBuilder().build();
        MustacheScriptEngineService mustacheScriptEngineService = new MustacheScriptEngineService(settings);
        ThreadPool tp;
        tp = new ThreadPool(ThreadPool.Names.SAME);
        Set<ScriptEngineService> engineServiceSet = new HashSet<>();
        engineServiceSet.add(mustacheScriptEngineService);

        ScriptService scriptService = new ScriptService(settings, new Environment(), engineServiceSet, new ResourceWatcherService(settings, tp));
        String parsedTemplate = SmtpAlertActionFactory.renderTemplate(template, alert, result, scriptService);
        tp.shutdownNow();
        assertEquals("test-email-template triggered with 0 hits", parsedTemplate);

    }

}

{'fraction_non_alphanumeric': 0.09631816063383564, 'fraction_numerical': 0.0024856299518409195, 'mean_word_length': 4.28137817883511, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3316955', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1604, 'n_words': 360}