Skip to content

Commit ef79cd5

Browse files
Include check that from timestamp is before to timestamp (#413)
Signed-off-by: Emily Guo <[email protected]>
1 parent 1b3d71c commit ef79cd5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesAction.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ private static void validateTimeRange(RestRequest request, String from, String t
120120
)
121121
);
122122
}
123+
if (ZonedDateTime.parse(from).isAfter(ZonedDateTime.parse(to))) {
124+
throw new IllegalArgumentException(
125+
String.format(
126+
Locale.ROOT,
127+
"request [%s] contains invalid time range. 'from' date [%s] must be before 'to' date [%s]",
128+
request.path(),
129+
from,
130+
to
131+
)
132+
);
133+
}
123134
}
124135

125136
@Override

src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesActionTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ public void testInValidTo() {
9090
);
9191
}
9292

93+
public void testFromAfterTo() {
94+
Map<String, String> params = new HashMap<>();
95+
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
96+
String from = now.plusHours(1).format(DateTimeFormatter.ISO_DATE_TIME);
97+
String to = now.format(DateTimeFormatter.ISO_DATE_TIME);
98+
params.put("from", from);
99+
params.put("to", to);
100+
RestRequest restRequest = buildRestRequest(params);
101+
Exception exception = assertThrows(IllegalArgumentException.class, () -> { RestTopQueriesAction.prepareRequest(restRequest); });
102+
assertEquals(
103+
String.format(
104+
Locale.ROOT,
105+
"request [%s] contains invalid time range. 'from' date [%s] must be before 'to' date [%s]",
106+
restRequest.path(),
107+
from,
108+
to
109+
),
110+
exception.getMessage()
111+
);
112+
}
113+
93114
public void testMissingOneTimeParam() {
94115
Map<String, String> params = new HashMap<>();
95116
params.put("from", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME));

0 commit comments

Comments
 (0)