/*
 * 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.xpack.watcher.test.integration;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.LicenseService;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
import org.elasticsearch.xpack.watcher.condition.CompareCondition;
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;

import java.util.concurrent.TimeUnit;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction;
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.xpack.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.templateRequest;
import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

public class RejectedExecutionTests extends AbstractWatcherIntegrationTestCase {

    @Override
    protected boolean timeWarped() {
        //need to use the real scheduler
        return false;
    }

    @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/41734")
    public void testHistoryAndTriggeredOnRejection() throws Exception {
        WatcherClient watcherClient = watcherClient();
        createIndex("idx");
        client().prepareIndex("idx", "_doc").setSource("field", "a").get();
        refresh();
        WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "a")), "idx");
        watcherClient.preparePutWatch(randomAlphaOfLength(5))
            .setSource(watchBuilder()
                .trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.SECONDS)))
                .input(searchInput(request))
                .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))
                .addAction("_logger", loggingAction("_logging")
                    .setCategory("_category")))
            .get();

        assertBusy(() -> {
            flushAndRefresh(".watcher-history-*");
            SearchResponse searchResponse = client().prepareSearch(".watcher-history-*").get();
            assertThat(searchResponse.getHits().getTotalHits().value, greaterThanOrEqualTo(2L));
        }, 10, TimeUnit.SECONDS);

        flushAndRefresh(".triggered_watches");
        SearchResponse searchResponse = client().prepareSearch(".triggered_watches").get();
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {

        return Settings.builder()
            .put(super.nodeSettings(nodeOrdinal))
            .put(XPackSettings.MONITORING_ENABLED.getKey(), false)
            .put(XPackSettings.SECURITY_ENABLED.getKey(), false)
            .put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial")
            .put("thread_pool.write.size", 1)
            .put("thread_pool.write.queue_size", 1)
            .put("xpack.watcher.thread_pool.size", 1)
            .put("xpack.watcher.thread_pool.queue_size", 0)
            .build();
    }


}

@@ -876,47 +876,8 @@
 e;%0A%0A
-import java.util.concurrent.TimeUnit;%0A%0A
 impo
@@ -941,16 +941,16 @@
 mQuery;%0A
+
 import s
@@ -1524,53 +1524,8 @@
 al;%0A
-import static org.hamcrest.Matchers.equalTo;%0A
 impo
@@ -1786,89 +1786,8 @@
  %7D%0A%0A
-    @AwaitsFix(bugUrl = %22https://github.com/elastic/elasticsearch/issues/41734%22)%0A
     
@@ -1813,20 +1813,8 @@
 tory
-AndTriggered
 OnRe
@@ -2874,24 +2874,24 @@
 ualTo(2L));%0A
+
         %7D, 1
@@ -2891,250 +2891,8 @@
    %7D
-, 10, TimeUnit.SECONDS);%0A%0A        flushAndRefresh(%22.triggered_watches%22);%0A        SearchResponse searchResponse = client().prepareSearch(%22.triggered_watches%22).get();%0A        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)
 );%0A 

fix org.elasticsearch.xpack.watcher.test.integration.RejectedExecutionTests (#41777)