Skip to content

Commit

Permalink
Format Watcher.status.lastChecked and lastMetCondition (#38790) backp…
Browse files Browse the repository at this point in the history
…ort#38626

The hardcoded '\n' in string will not work in Windows where there is a
different line separator. A System.lineSeparator should be used to make
it work on all platforms
closes #38705
backport #38626
  • Loading branch information
pgomulka authored Feb 12, 2019
1 parent daffce1 commit ae1b2ba
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
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,19 +12,26 @@
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 org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;

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.simpleInput;
import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule.Interval.Unit.SECONDS;
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.equalTo;
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() {
WatcherClient watcherClient = watcherClient();
watcherClient.preparePutWatch("_name")
Expand All @@ -44,10 +51,24 @@ 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()));
assertThat(getWatchResponse.getStatus().lastChecked(), isMillisResolution());
// not started yet, so both nulls
String lastMetCondition = source.getValue("status.last_met_condition");
assertThat(lastMetCondition, is(nullValue()));
assertThat(getWatchResponse.getStatus().lastMetCondition(), is(nullValue()));
}

assertThat(lastChecked, is(notNullValue()));
assertThat(getWatchResponse.getStatus().lastChecked().toString(), is(lastChecked));
private Matcher<ZonedDateTime> isMillisResolution() {
return new FeatureMatcher<ZonedDateTime,Boolean>(equalTo(true), "has millisecond precision", "precission") {
@Override
protected Boolean featureValueOf(ZonedDateTime actual) {
//if date has millisecond precision its nanosecond field will be rounded to millis (equal millis * 10^6)
return actual.getNano() == actual.get(ChronoField.MILLI_OF_SECOND) * 1000_000;
}
};
}

}

0 comments on commit ae1b2ba

Please sign in to comment.