Cleaned up the use of execution context mocking in the tests
Added helper methods in `AlertsTestUtils` to mock `ExecutionContext` and create a simple payload Original commit: elastic/x-pack-elasticsearch@eac6b63e62
This commit is contained in:
parent
4874fa2f1b
commit
f5c48576bc
|
@ -6,7 +6,6 @@
|
|||
package org.elasticsearch.alerts.actions.email;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.actions.ActionSettingsException;
|
||||
|
@ -31,6 +30,7 @@ import java.io.IOException;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.mockExecutionContext;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -79,17 +79,11 @@ public class EmailActionTests extends ElasticsearchTestCase {
|
|||
}
|
||||
};
|
||||
|
||||
DateTime now = new DateTime(DateTimeZone.UTC);
|
||||
DateTime now = DateTime.now(DateTimeZone.UTC);
|
||||
|
||||
String ctxId = randomAsciiOfLength(5);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
ExecutionContext ctx = mockExecutionContext(now, now, "alert1", payload);
|
||||
when(ctx.id()).thenReturn(ctxId);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("alert1");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
when(ctx.payload()).thenReturn(payload);
|
||||
if (transform != null) {
|
||||
Transform.Result transformResult = mock(Transform.Result.class);
|
||||
when(transformResult.type()).thenReturn("_transform_type");
|
||||
|
|
|
@ -37,6 +37,7 @@ import java.io.IOException;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.mockExecutionContext;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
@ -62,13 +63,7 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
ScriptServiceProxy scriptService = getScriptServiceProxy(tp);
|
||||
ScriptCondition condition = new ScriptCondition(logger, scriptService, new Script("ctx.payload.hits.total > 1"));
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
ExecutionContext ctx = mockExecutionContext("_name", new Payload.ActionResponse(response));
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
}
|
||||
|
||||
|
@ -78,13 +73,7 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
Script script = new Script("ctx.payload.hits.total > threshold", ScriptService.ScriptType.INLINE, ScriptService.DEFAULT_LANG, ImmutableMap.<String, Object>of("threshold", 1));
|
||||
ScriptCondition condition = new ScriptCondition(logger, scriptService, script);
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
ExecutionContext ctx = mockExecutionContext("_name", new Payload.ActionResponse(response));
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
}
|
||||
|
||||
|
@ -98,13 +87,7 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
ScriptCondition condition = conditionParser.parse(parser);
|
||||
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
ExecutionContext ctx = mockExecutionContext("_name", new Payload.ActionResponse(response));
|
||||
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
|
||||
|
@ -114,11 +97,7 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
parser.nextToken();
|
||||
condition = conditionParser.parse(parser);
|
||||
|
||||
reset(ctx);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
ctx = mockExecutionContext("_name", new Payload.ActionResponse(response));
|
||||
|
||||
assertTrue(condition.execute(ctx).met());
|
||||
}
|
||||
|
@ -171,8 +150,6 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
fail("expected a condition exception trying to parse an invalid condition XContent");
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static ScriptServiceProxy getScriptServiceProxy(ThreadPool tp) {
|
||||
Settings settings = ImmutableSettings.settingsBuilder().build();
|
||||
GroovyScriptEngineService groovyScriptEngineService = new GroovyScriptEngineService(settings);
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.elasticsearch.alerts.support.template.ScriptTemplate;
|
|||
import org.elasticsearch.alerts.support.template.Template;
|
||||
import org.elasticsearch.alerts.transform.SearchTransform;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.netty.handler.codec.http.HttpMethod;
|
||||
|
@ -54,6 +55,8 @@ import static org.mockito.Mockito.when;
|
|||
*/
|
||||
public final class AlertsTestUtils {
|
||||
|
||||
public static final Payload EMPTY_PAYLOAD = new Payload.Simple(ImmutableMap.<String, Object>of());
|
||||
|
||||
private AlertsTestUtils() {
|
||||
}
|
||||
|
||||
|
@ -77,12 +80,16 @@ public final class AlertsTestUtils {
|
|||
return request;
|
||||
}
|
||||
|
||||
public static Payload simplePayload(String key, Object value) {
|
||||
return new Payload.Simple(key, value);
|
||||
}
|
||||
|
||||
public static ExecutionContext mockExecutionContext(String alertName, Payload payload) {
|
||||
DateTime now = DateTime.now();
|
||||
return mockExecutionContext(now, now, alertName, payload);
|
||||
}
|
||||
|
||||
public static ExecutionContext mockExecutionContext(DateTime firedTime, DateTime scheduledTime, String alertName, Payload payload) {
|
||||
public static ExecutionContext mockExecutionContext(DateTime scheduledTime, DateTime firedTime, String alertName, Payload payload) {
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.scheduledTime()).thenReturn(scheduledTime);
|
||||
when(ctx.fireTime()).thenReturn(firedTime);
|
||||
|
|
|
@ -7,11 +7,14 @@ package org.elasticsearch.alerts.throttle;
|
|||
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.test.AlertsTestUtils;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.elasticsearch.alerts.support.AlertsDateUtils.formatDate;
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.EMPTY_PAYLOAD;
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.mockExecutionContext;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -25,14 +28,13 @@ public class AckThrottlerTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testWhenAcked() throws Exception {
|
||||
DateTime timestamp = new DateTime();
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
ExecutionContext ctx = mockExecutionContext("_alert", EMPTY_PAYLOAD);
|
||||
Alert alert = ctx.alert();
|
||||
Alert.Status status = mock(Alert.Status.class);
|
||||
when(status.ackStatus()).thenReturn(new Alert.Status.AckStatus(Alert.Status.AckStatus.State.ACKED, timestamp));
|
||||
when(alert.status()).thenReturn(status);
|
||||
when(alert.name()).thenReturn("_alert");
|
||||
when(alert.acked()).thenReturn(true);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
AckThrottler throttler = new AckThrottler();
|
||||
Throttler.Result result = throttler.throttle(ctx);
|
||||
assertThat(result.throttle(), is(true));
|
||||
|
@ -42,15 +44,13 @@ public class AckThrottlerTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testWhenNotAcked() throws Exception {
|
||||
DateTime timestamp = new DateTime();
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
ExecutionContext ctx = mockExecutionContext("_alert", EMPTY_PAYLOAD);
|
||||
Alert alert = ctx.alert();
|
||||
Alert.Status status = mock(Alert.Status.class);
|
||||
Alert.Status.AckStatus.State state = randomFrom(Alert.Status.AckStatus.State.AWAITS_EXECUTION, Alert.Status.AckStatus.State.ACKABLE);
|
||||
when(status.ackStatus()).thenReturn(new Alert.Status.AckStatus(state, timestamp));
|
||||
when(alert.status()).thenReturn(status);
|
||||
when(alert.name()).thenReturn("_alert");
|
||||
when(alert.acked()).thenReturn(false);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
AckThrottler throttler = new AckThrottler();
|
||||
Throttler.Result result = throttler.throttle(ctx);
|
||||
assertThat(result.throttle(), is(false));
|
||||
|
|
|
@ -14,10 +14,10 @@ import org.elasticsearch.common.unit.TimeValue;
|
|||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.EMPTY_PAYLOAD;
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.mockExecutionContext;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
@ -32,11 +32,9 @@ public class PeriodThrottlerTests extends ElasticsearchTestCase {
|
|||
TimeValue period = TimeValue.timeValueSeconds(randomIntBetween(2, 5));
|
||||
PeriodThrottler throttler = new PeriodThrottler(period, periodType);
|
||||
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
ExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD);
|
||||
Alert.Status status = mock(Alert.Status.class);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(alert.status()).thenReturn(status);
|
||||
when(ctx.alert().status()).thenReturn(status);
|
||||
when(status.lastExecuted()).thenReturn(new DateTime().minusSeconds((int) period.seconds() - 1));
|
||||
|
||||
Throttler.Result result = throttler.throttle(ctx);
|
||||
|
@ -52,11 +50,9 @@ public class PeriodThrottlerTests extends ElasticsearchTestCase {
|
|||
TimeValue period = TimeValue.timeValueSeconds(randomIntBetween(2, 5));
|
||||
PeriodThrottler throttler = new PeriodThrottler(period, periodType);
|
||||
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
ExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD);
|
||||
Alert.Status status = mock(Alert.Status.class);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(alert.status()).thenReturn(status);
|
||||
when(ctx.alert().status()).thenReturn(status);
|
||||
when(status.lastExecuted()).thenReturn(new DateTime().minusSeconds((int) period.seconds() + 1));
|
||||
|
||||
Throttler.Result result = throttler.throttle(ctx);
|
||||
|
|
|
@ -5,14 +5,12 @@
|
|||
*/
|
||||
package org.elasticsearch.alerts.transform;
|
||||
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.support.Script;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
import org.elasticsearch.alerts.support.init.proxy.ScriptServiceProxy;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
|
@ -24,6 +22,7 @@ import org.junit.Test;
|
|||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.*;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -42,15 +41,9 @@ public class ScriptTransformTests extends ElasticsearchTestCase {
|
|||
Script script = new Script("_script", type, "_lang", params);
|
||||
ScriptTransform transform = new ScriptTransform(service, script);
|
||||
|
||||
DateTime now = new DateTime();
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
ExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD);
|
||||
|
||||
Payload payload = new Payload.Simple(ImmutableMap.<String, Object>builder().put("key", "value").build());
|
||||
Payload payload = simplePayload("key", "value");
|
||||
|
||||
Map<String, Object> model = Variables.createCtxModel(ctx, payload);
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ package org.elasticsearch.alerts.transform;
|
|||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
|
@ -16,8 +15,6 @@ import org.elasticsearch.alerts.support.init.proxy.ClientProxy;
|
|||
import org.elasticsearch.alerts.test.AbstractAlertsSingleNodeTests;
|
||||
import org.elasticsearch.client.Requests;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -29,14 +26,13 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.alerts.support.AlertsDateUtils.parseDate;
|
||||
import static org.elasticsearch.alerts.test.AlertsTestUtils.*;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.filteredQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -57,18 +53,9 @@ public class SearchTransformTests extends AbstractAlertsSingleNodeTests {
|
|||
.endObject());
|
||||
SearchTransform transform = new SearchTransform(logger, scriptService(), ClientProxy.of(client()), request);
|
||||
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
DateTime now = new DateTime();
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
ExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD);
|
||||
|
||||
|
||||
Payload payload = new Payload.Simple(new HashMap<String, Object>());
|
||||
|
||||
Transform.Result result = transform.apply(ctx, payload);
|
||||
Transform.Result result = transform.apply(ctx, EMPTY_PAYLOAD);
|
||||
assertThat(result, notNullValue());
|
||||
assertThat(result.type(), is(SearchTransform.TYPE));
|
||||
|
||||
|
@ -123,16 +110,9 @@ public class SearchTransformTests extends AbstractAlertsSingleNodeTests {
|
|||
|
||||
SearchTransform transform = new SearchTransform(logger, scriptService(), ClientProxy.of(client()), request);
|
||||
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.scheduledTime()).thenReturn(parseDate("2015-01-01T00:00:00"));
|
||||
when(ctx.fireTime()).thenReturn(parseDate("2015-01-04T00:00:00"));
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
ExecutionContext ctx = mockExecutionContext(parseDate("2015-01-01T00:00:00"), parseDate("2015-01-04T00:00:00"), "_name", EMPTY_PAYLOAD);
|
||||
|
||||
Payload payload = new Payload.Simple(ImmutableMap.<String, Object>builder()
|
||||
.put("value", "val_3")
|
||||
.build());
|
||||
Payload payload = simplePayload("value", "val_3");
|
||||
|
||||
Transform.Result result = transform.apply(ctx, payload);
|
||||
assertThat(result, notNullValue());
|
||||
|
|
Loading…
Reference in New Issue