test: moved all tests over to use source builders.

Original commit: elastic/x-pack-elasticsearch@e13e5b8b43
This commit is contained in:
Martijn van Groningen 2015-05-01 11:54:40 +02:00
parent 9df97fe845
commit f66fc7575d
4 changed files with 30 additions and 80 deletions

View File

@ -6,7 +6,6 @@
package org.elasticsearch.watcher.test;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
@ -16,15 +15,12 @@ import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.common.base.Charsets;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.netty.util.internal.SystemPropertyUtil;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.license.plugin.LicensePlugin;
@ -38,6 +34,7 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.InternalTestCluster;
import org.elasticsearch.test.TestCluster;
import org.elasticsearch.watcher.WatcherPlugin;
import org.elasticsearch.watcher.WatcherService;
import org.elasticsearch.watcher.actions.email.service.Authentication;
import org.elasticsearch.watcher.actions.email.service.Email;
import org.elasticsearch.watcher.actions.email.service.EmailService;
@ -47,19 +44,14 @@ import org.elasticsearch.watcher.execution.ExecutionService;
import org.elasticsearch.watcher.history.HistoryStore;
import org.elasticsearch.watcher.history.WatchRecord;
import org.elasticsearch.watcher.license.LicenseService;
import org.elasticsearch.watcher.support.WatcherUtils;
import org.elasticsearch.watcher.support.clock.ClockMock;
import org.elasticsearch.watcher.support.http.HttpClient;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsResponse;
import org.elasticsearch.watcher.trigger.ScheduleTriggerEngineMock;
import org.elasticsearch.watcher.trigger.TriggerService;
import org.elasticsearch.watcher.trigger.schedule.Schedule;
import org.elasticsearch.watcher.trigger.schedule.ScheduleModule;
import org.elasticsearch.watcher.trigger.schedule.ScheduleTrigger;
import org.elasticsearch.watcher.trigger.schedule.Schedules;
import org.elasticsearch.watcher.watch.Watch;
import org.elasticsearch.watcher.WatcherService;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@ -70,7 +62,6 @@ import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.*;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.SUITE;
@ -238,67 +229,6 @@ public abstract class AbstractWatcherIntegrationTests extends ElasticsearchInteg
return builder.get().getHits().getTotalHits();
}
protected BytesReference createWatchSource(String cron, SearchRequest conditionRequest, String conditionScript) throws IOException {
return createWatchSource(cron, conditionRequest, conditionScript, null);
}
protected BytesReference createWatchSource(Schedule schedule, SearchRequest conditionRequest, String conditionScript) throws IOException {
return createWatchSource(schedule, conditionRequest, conditionScript, null);
}
protected BytesReference createWatchSource(String cron, SearchRequest conditionRequest, String conditionScript, Map<String, Object> metadata) throws IOException {
return createWatchSource(Schedules.cron(cron), conditionRequest, conditionScript, metadata);
}
protected BytesReference createWatchSource(Schedule schedule, SearchRequest conditionRequest, String conditionScript, Map<String, Object> metadata) throws IOException {
ScheduleTrigger trigger = new ScheduleTrigger(schedule);
XContentBuilder builder = jsonBuilder();
builder.startObject();
{
builder.startObject("trigger")
.field(trigger.type(), trigger)
.endObject();
if (metadata != null) {
builder.field("meta", metadata);
}
builder.startObject("input");
{
builder.startObject("search");
builder.field("request");
WatcherUtils.writeSearchRequest(conditionRequest, builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
}
builder.endObject();
builder.startObject("condition");
{
builder.startObject("script");
builder.field("script", conditionScript);
builder.endObject();
}
builder.endObject();
builder.startObject("actions");
{
builder.startObject("_action_id");
{
builder.startObject("index")
.field("index", "my-index")
.field("doc_type", "trail")
.endObject();
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return builder.bytes();
}
protected <T> T getInstanceFromMaster(Class<T> type) {
return internalTestCluster().getInstance(type, internalTestCluster().getMasterName());
}

View File

@ -18,6 +18,7 @@ import org.elasticsearch.watcher.WatcherException;
import org.elasticsearch.watcher.WatcherSettingsException;
import org.elasticsearch.watcher.client.WatchSourceBuilder;
import org.elasticsearch.watcher.client.WatcherClient;
import org.elasticsearch.watcher.condition.ConditionBuilders;
import org.elasticsearch.watcher.history.HistoryStore;
import org.elasticsearch.watcher.support.WatcherUtils;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
@ -405,7 +406,10 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests {
assertAcked(prepareCreate("events").addMapping("event", "_timestamp", "enabled=true", "level", "type=string"));
watcherClient().preparePutWatch(watchName)
.setSource(createWatchSource(interval("5s"), request, "return ctx.payload.hits.total >= 3"))
.setSource(watchBuilder()
.trigger(schedule(interval("5s")))
.input(searchInput(request))
.condition(ConditionBuilders.scriptCondition("return ctx.payload.hits.total >= 3")))
.get();
logger.info("created watch [{}] at [{}]", watchName, DateTime.now());

View File

@ -11,7 +11,6 @@ import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.base.Predicate;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
@ -24,6 +23,7 @@ import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.watcher.WatcherService;
import org.elasticsearch.watcher.client.WatchSourceBuilder;
import org.elasticsearch.watcher.client.WatchSourceBuilders;
import org.elasticsearch.watcher.condition.ConditionBuilders;
import org.elasticsearch.watcher.execution.ExecutionService;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
import org.elasticsearch.watcher.test.WatcherTestUtils;
@ -37,9 +37,12 @@ import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.TEST;
import static org.elasticsearch.watcher.actions.ActionBuilders.loggingAction;
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.watcher.condition.ConditionBuilders.alwaysCondition;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.watcher.input.InputBuilders.simpleInput;
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.watcher.trigger.schedule.Schedules.cron;
import static org.elasticsearch.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
@ -85,7 +88,10 @@ public class NoMasterNodeTests extends AbstractWatcherIntegrationTests {
// Have a sample document in the index, the watch is going to evaluate
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
SearchRequest searchRequest = WatcherTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
BytesReference watchSource = createWatchSource("0/5 * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
WatchSourceBuilder watchSource = watchBuilder()
.trigger(schedule(cron("0/5 * * * * ? *")))
.input(searchInput(searchRequest))
.condition(ConditionBuilders.scriptCondition("ctx.payload.hits.total == 1"));
// we first need to make sure the license is enabled, otherwise all APIs will be blocked
ensureLicenseEnabled();
@ -186,7 +192,10 @@ public class NoMasterNodeTests extends AbstractWatcherIntegrationTests {
for (int i = 1; i <= numberOfWatches; i++) {
String watchName = "watch" + i;
SearchRequest searchRequest = WatcherTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
BytesReference watchSource = createWatchSource("0/5 * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
WatchSourceBuilder watchSource = watchBuilder()
.trigger(schedule(cron("0/5 * * * * ? *")))
.input(searchInput(searchRequest))
.condition(ConditionBuilders.scriptCondition("ctx.payload.hits.total == 1"));
watcherClient().preparePutWatch(watchName).setSource(watchSource).get();
}
ensureGreen();

View File

@ -6,17 +6,17 @@
package org.elasticsearch.watcher.test.integration;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.watcher.WatcherBuild;
import org.elasticsearch.watcher.WatcherService;
import org.elasticsearch.watcher.WatcherVersion;
import org.elasticsearch.watcher.client.WatcherClient;
import org.elasticsearch.watcher.condition.ConditionBuilders;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
import org.elasticsearch.watcher.test.WatcherTestUtils;
import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsRequest;
import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsResponse;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
@ -24,6 +24,10 @@ 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.test.ElasticsearchIntegrationTest.Scope.TEST;
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.watcher.trigger.schedule.Schedules.cron;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
@ -57,9 +61,12 @@ public class WatchStatsTests extends AbstractWatcherIntegrationTests {
assertThat(response.getWatchServiceState(), equalTo(WatcherService.State.STARTED));
SearchRequest searchRequest = WatcherTestUtils.newInputSearchRequest("idx").source(searchSource().query(termQuery("field", "value")));
BytesReference watchSource = createWatchSource("* * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
watcherClient().preparePutWatch("_name")
.setSource(watchSource)
.setSource(watchBuilder()
.trigger(schedule(cron("* * * * * ? *")))
.input(searchInput(searchRequest))
.condition(ConditionBuilders.scriptCondition("ctx.payload.hits.total == 1"))
)
.get();
if (timeWarped()) {