From f5c48576bc0ed97b56aa59b7fa03f238f5d63fff Mon Sep 17 00:00:00 2001 From: uboness Date: Wed, 4 Mar 2015 10:37:38 +0100 Subject: [PATCH] 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@eac6b63e62fdcc6a8e27daa45b9d6f08abcfdb31 --- .../actions/email/EmailActionTests.java | 12 ++----- .../script/ScriptConditionTests.java | 33 +++---------------- .../alerts/test/AlertsTestUtils.java | 9 ++++- .../alerts/throttle/AckThrottlerTests.java | 14 ++++---- .../alerts/throttle/PeriodThrottlerTests.java | 18 ++++------ .../transform/ScriptTransformTests.java | 13 ++------ .../transform/SearchTransformTests.java | 30 +++-------------- 7 files changed, 38 insertions(+), 91 deletions(-) diff --git a/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java b/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java index 0d55c95c3de..e84ee138c39 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java +++ b/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java @@ -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"); diff --git a/src/test/java/org/elasticsearch/alerts/condition/script/ScriptConditionTests.java b/src/test/java/org/elasticsearch/alerts/condition/script/ScriptConditionTests.java index 5fcbde45b7e..6471ccee649 100644 --- a/src/test/java/org/elasticsearch/alerts/condition/script/ScriptConditionTests.java +++ b/src/test/java/org/elasticsearch/alerts/condition/script/ScriptConditionTests.java @@ -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.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); diff --git a/src/test/java/org/elasticsearch/alerts/test/AlertsTestUtils.java b/src/test/java/org/elasticsearch/alerts/test/AlertsTestUtils.java index ad8777030b0..28b679666ec 100644 --- a/src/test/java/org/elasticsearch/alerts/test/AlertsTestUtils.java +++ b/src/test/java/org/elasticsearch/alerts/test/AlertsTestUtils.java @@ -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.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); diff --git a/src/test/java/org/elasticsearch/alerts/throttle/AckThrottlerTests.java b/src/test/java/org/elasticsearch/alerts/throttle/AckThrottlerTests.java index 45001ff365a..ad98dce3928 100644 --- a/src/test/java/org/elasticsearch/alerts/throttle/AckThrottlerTests.java +++ b/src/test/java/org/elasticsearch/alerts/throttle/AckThrottlerTests.java @@ -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)); diff --git a/src/test/java/org/elasticsearch/alerts/throttle/PeriodThrottlerTests.java b/src/test/java/org/elasticsearch/alerts/throttle/PeriodThrottlerTests.java index 6bd84b69613..6c32e7e83e2 100644 --- a/src/test/java/org/elasticsearch/alerts/throttle/PeriodThrottlerTests.java +++ b/src/test/java/org/elasticsearch/alerts/throttle/PeriodThrottlerTests.java @@ -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); diff --git a/src/test/java/org/elasticsearch/alerts/transform/ScriptTransformTests.java b/src/test/java/org/elasticsearch/alerts/transform/ScriptTransformTests.java index 6a8a455ba0c..dd6d8a2daff 100644 --- a/src/test/java/org/elasticsearch/alerts/transform/ScriptTransformTests.java +++ b/src/test/java/org/elasticsearch/alerts/transform/ScriptTransformTests.java @@ -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.builder().put("key", "value").build()); + Payload payload = simplePayload("key", "value"); Map model = Variables.createCtxModel(ctx, payload); diff --git a/src/test/java/org/elasticsearch/alerts/transform/SearchTransformTests.java b/src/test/java/org/elasticsearch/alerts/transform/SearchTransformTests.java index 5aefee1623a..ca9897273cf 100644 --- a/src/test/java/org/elasticsearch/alerts/transform/SearchTransformTests.java +++ b/src/test/java/org/elasticsearch/alerts/transform/SearchTransformTests.java @@ -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()); - - 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.builder() - .put("value", "val_3") - .build()); + Payload payload = simplePayload("value", "val_3"); Transform.Result result = transform.apply(ctx, payload); assertThat(result, notNullValue());