Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public ZonedDateTime lastChecked() {
return lastChecked;
}

public ZonedDateTime lastMetCondition() {
return lastMetCondition;
}

public ActionStatus actionStatus(String actionId) {
return actions.get(actionId);
}
Expand Down Expand Up @@ -252,10 +256,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(Field.STATE.getPreferredName(), state, params);
}
if (lastChecked != null) {
builder.timeField(Field.LAST_CHECKED.getPreferredName(), lastChecked);
writeDate(Field.LAST_CHECKED.getPreferredName(), builder, lastChecked);
}
if (lastMetCondition != null) {
builder.timeField(Field.LAST_MET_CONDITION.getPreferredName(), lastMetCondition);
writeDate(Field.LAST_MET_CONDITION.getPreferredName(), builder, lastMetCondition);
}
if (actions != null) {
builder.startObject(Field.ACTIONS.getPreferredName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.xpack.core.watcher.common.secret.Secret;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.execution.Wid;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
Expand Down Expand Up @@ -52,6 +53,7 @@
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleTrigger;
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleTriggerEvent;
import org.hamcrest.Matcher;

import javax.mail.internet.AddressException;
import java.io.IOException;
Expand All @@ -69,6 +71,7 @@
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.ESTestCase.randomFrom;
import static org.hamcrest.Matchers.is;

public final class WatcherTestUtils {

Expand Down Expand Up @@ -188,4 +191,13 @@ public static Watch createTestWatch(String watchName, Client client, HttpClient
public static SearchType getRandomSupportedSearchType() {
return randomFrom(SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH);
}

public static Matcher<String> isSameDate(ZonedDateTime zonedDateTime) {
/*
When comparing timestamps returned from _search/.watcher-history* the same format of date has to be used
during serialisation to json on index time.
The toString of ZonedDateTime is omitting the millisecond part when is 0. This was not the case in joda.
*/
return is(WatcherDateTimeUtils.formatDate(zonedDateTime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
import org.elasticsearch.xpack.core.watcher.actions.ActionStatus;
import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder;
import org.elasticsearch.xpack.core.watcher.input.Input;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.core.watcher.watch.WatchStatus;
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
import org.hamcrest.Matcher;

import java.time.ZonedDateTime;
import java.util.Locale;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
Expand Down Expand Up @@ -152,7 +150,6 @@ public void testPayloadInputWithDotsInFieldNameWorks() throws Exception {
}
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38693")
public void testThatHistoryContainsStatus() throws Exception {
watcherClient().preparePutWatch("test_watch")
.setSource(watchBuilder()
Expand All @@ -176,10 +173,12 @@ public void testThatHistoryContainsStatus() throws Exception {
assertThat(active, is(status.state().isActive()));

String timestamp = source.getValue("status.state.timestamp");
assertThat(timestamp, isSameDate(status.state().getTimestamp()));
assertThat(timestamp, WatcherTestUtils.isSameDate(status.state().getTimestamp()));

String lastChecked = source.getValue("status.last_checked");
assertThat(lastChecked, isSameDate(status.lastChecked()));
assertThat(lastChecked, WatcherTestUtils.isSameDate(status.lastChecked()));
String lastMetCondition = source.getValue("status.last_met_condition");
assertThat(lastMetCondition, WatcherTestUtils.isSameDate(status.lastMetCondition()));

Integer version = source.getValue("status.version");
int expectedVersion = (int) (status.version() - 1);
Expand All @@ -202,12 +201,4 @@ public void testThatHistoryContainsStatus() throws Exception {
}


private Matcher<String> isSameDate(ZonedDateTime zonedDateTime) {
/*
When comparing timestamps returned from _search/.watcher-history* the same format of date has to be used
during serialisation to json on index time.
The toString of ZonedDateTime is omitting the millisecond part when is 0. This was not the case in joda.
*/
return is(WatcherDateTimeUtils.formatDate(zonedDateTime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchResponse;
import org.elasticsearch.xpack.watcher.condition.NeverCondition;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction;
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
Expand All @@ -21,11 +25,14 @@
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class WatchStatusIntegrationTests extends AbstractWatcherIntegrationTestCase {

@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/38619")
public void testThatStatusGetsUpdated() {
timeWarp().clock().setTime(
ZonedDateTime.of(2019,02,9,15,9,50,687670, ZoneOffset.UTC));

WatcherClient watcherClient = watcherClient();
watcherClient.preparePutWatch("_name")
.setSource(watchBuilder()
Expand All @@ -44,10 +51,14 @@ public void testThatStatusGetsUpdated() {
GetResponse getResponse = client().prepareGet(".watches", "doc", "_name").get();
getResponse.getSource();
XContentSource source = new XContentSource(getResponse.getSourceAsBytesRef(), XContentType.JSON);

String lastChecked = source.getValue("status.last_checked");
assertThat(lastChecked, WatcherTestUtils.isSameDate(getWatchResponse.getStatus().lastChecked()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also assert that the nano seconds are not present here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - updated the PR


assertThat(lastChecked, is(notNullValue()));
assertThat(getWatchResponse.getStatus().lastChecked().toString(), is(lastChecked));
// not started yet, so both nulls
String lastMetCondition = source.getValue("status.last_met_condition");
assertThat(lastMetCondition, is(nullValue()));
assertThat(getWatchResponse.getStatus().lastMetCondition(), is(nullValue()));
}

}