Skip to content

Commit 78cfc2b

Browse files
committed
SNAPSHOT Test fixup
1 parent 75c834c commit 78cfc2b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistry.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
import java.io.IOException;
1313
import java.time.DateTimeException;
1414
import java.time.ZoneId;
15-
import java.time.ZoneOffset;
1615
import java.util.HashMap;
1716
import java.util.Map;
18-
import java.util.Optional;
1917
import java.util.Set;
2018
import java.util.TimeZone;
2119

22-
import static java.util.Optional.ofNullable;
23-
2420
public class ScheduleRegistry {
2521
private final Map<String, Schedule.Parser<? extends Schedule>> parsers = new HashMap<>();
2622

@@ -36,19 +32,13 @@ public Schedule parse(String context, XContentParser parser) throws IOException
3632
String type = null;
3733
XContentParser.Token token;
3834
Schedule schedule = null;
39-
TimeZone timeZone = TimeZone.getTimeZone(ZoneOffset.UTC); // Default to UTC
35+
TimeZone timeZone = null; // Default to UTC
4036
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
4137
if (token == XContentParser.Token.FIELD_NAME) {
42-
if (parser.currentName().equals(ScheduleTrigger.TIMEZONE_FIELD)) {
43-
if (schedule != null && schedule instanceof CronnableSchedule == false) {
44-
throw new ElasticsearchParseException(
45-
"could not parse schedule. timezone is not supported for [{}] schedule type",
46-
schedule.type()
47-
);
48-
}
49-
timeZone = parseTimezone(parser).orElse(null);
38+
var fieldName = parser.currentName();
39+
if (fieldName.equals(ScheduleTrigger.TIMEZONE_FIELD)) {
40+
timeZone = parseTimezone(parser);
5041
} else {
51-
parser.nextToken();
5242
type = parser.currentName();
5343
}
5444
} else if (type != null) {
@@ -66,12 +56,17 @@ public Schedule parse(String context, XContentParser parser) throws IOException
6656

6757
if (timeZone != null && schedule instanceof CronnableSchedule cronnableSchedule) {
6858
cronnableSchedule.setTimeZone(timeZone);
59+
} else if (timeZone != null) {
60+
throw new ElasticsearchParseException(
61+
"could not parse schedule. Timezone is not supported for schedule type [{}]",
62+
schedule.type()
63+
);
6964
}
7065

7166
return schedule;
7267
}
7368

74-
private static Optional<TimeZone> parseTimezone(XContentParser parser) throws IOException {
69+
private static TimeZone parseTimezone(XContentParser parser) throws IOException {
7570
TimeZone timeZone;
7671
XContentParser.Token token = parser.nextToken();
7772
if (token == XContentParser.Token.VALUE_STRING) {
@@ -87,7 +82,7 @@ private static Optional<TimeZone> parseTimezone(XContentParser parser) throws IO
8782
token
8883
);
8984
}
90-
return ofNullable(timeZone);
85+
return timeZone;
9186
}
9287

9388
public Schedule parse(String context, String type, XContentParser parser) throws IOException {

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistryTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.HashSet;
1616
import java.util.Set;
17+
import java.util.TimeZone;
1718

1819
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
1920
import static org.hamcrest.Matchers.equalTo;
@@ -50,14 +51,21 @@ public void testParserInterval() throws Exception {
5051

5152
public void testParseCron() throws Exception {
5253
Object cron = randomBoolean() ? Schedules.cron("* 0/5 * * * ?") : Schedules.cron("* 0/2 * * * ?", "* 0/3 * * * ?", "* 0/5 * * * ?");
53-
XContentBuilder builder = jsonBuilder().startObject().field(CronSchedule.TYPE, cron).endObject();
54+
TimeZone timeZone = null;
55+
XContentBuilder builder = jsonBuilder().startObject().field(CronSchedule.TYPE, cron);
56+
if (randomBoolean()) {
57+
timeZone = randomTimeZone();
58+
builder.field(ScheduleTrigger.TIMEZONE_FIELD, timeZone.getID());
59+
}
60+
builder.endObject();
5461
BytesReference bytes = BytesReference.bytes(builder);
5562
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
5663
parser.nextToken();
57-
Schedule schedule = registry.parse("ctx", parser);
64+
CronnableSchedule schedule = (CronnableSchedule) registry.parse("ctx", parser);
5865
assertThat(schedule, notNullValue());
5966
assertThat(schedule, instanceOf(CronSchedule.class));
6067
assertThat(schedule, is(cron));
68+
assertThat(schedule.getTimeZone(), equalTo(timeZone));
6169
}
6270

6371
public void testParseHourly() throws Exception {

0 commit comments

Comments
 (0)