Skip to content

Commit 51c24fe

Browse files
Added more parameter checks in TopQueries and LiveQueries integration tests
Signed-off-by: Emily Guo <[email protected]>
1 parent 652eb07 commit 51c24fe

File tree

2 files changed

+177
-2
lines changed

2 files changed

+177
-2
lines changed

src/test/java/org/opensearch/plugin/insights/rules/resthandler/live_queries/LiveQueriesRestIT.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Assert;
2525
import org.opensearch.client.Request;
2626
import org.opensearch.client.Response;
27+
import org.opensearch.client.ResponseException;
2728
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
2829
import org.opensearch.common.xcontent.json.JsonXContent;
2930
import org.opensearch.core.xcontent.NamedXContentRegistry;
@@ -121,7 +122,7 @@ public void testLiveQueriesWithConcurrentSearches() throws Exception {
121122
Map<String, Object> nodes = (Map<String, Object>) nodesMap.get("nodes");
122123
String nodeId = nodes.keySet().iterator().next();
123124

124-
String[] params = new String[] { "?size=1", "", "?size=0", "?sort=cpu", "?verbose=false", "?nodeId=" + nodeId };
125+
String[] params = new String[] { "?size=1", "", "?sort=cpu", "?verbose=false", "?nodeId=" + nodeId };
125126
Map<String, Boolean> foundParams = new java.util.HashMap<>();
126127
for (String param : params) {
127128
foundParams.put(param, false);
@@ -267,7 +268,7 @@ public void testAllParameters() throws IOException {
267268
String nodeId = nodes.keySet().iterator().next();
268269

269270
// Define parameter combinations to test
270-
String[] params = new String[] { "", "?verbose=false", "?sort=cpu", "?size=1", "?size=0", "?nodeId=" + nodeId };
271+
String[] params = new String[] { "", "?verbose=false", "?sort=cpu", "?size=1", "?nodeId=" + nodeId };
271272
for (String param : params) {
272273
String uri = QueryInsightsSettings.LIVE_QUERIES_BASE_URI + param;
273274
Request req = new Request("GET", uri);
@@ -278,6 +279,82 @@ public void testAllParameters() throws IOException {
278279
}
279280
}
280281

282+
/**
283+
* Test invalid sort parameter
284+
*
285+
* @throws IOException IOException
286+
*/
287+
public void testInvalidSortParameter() throws IOException {
288+
String invalidSortParam = "?sort=invalid";
289+
290+
Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + invalidSortParam);
291+
try {
292+
client().performRequest(request);
293+
fail("Should not succeed with invalid sort parameter");
294+
} catch (ResponseException e) {
295+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
296+
}
297+
}
298+
299+
/**
300+
* Test invalid size parameter
301+
*
302+
* @throws IOException IOException
303+
*/
304+
public void testInvalidSizeParameter() throws IOException {
305+
String[] invalidSizeParams = { "?size=-1", "?size=invalid" };
306+
307+
for (String param : invalidSizeParams) {
308+
Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + param);
309+
try {
310+
client().performRequest(request);
311+
fail("Should not succeed with invalid size parameter: " + param);
312+
} catch (ResponseException e) {
313+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
314+
}
315+
}
316+
}
317+
318+
/**
319+
* Test multiple invalid parameters
320+
*
321+
* @throws IOException IOException
322+
*/
323+
public void testMultipleInvalidParameters() throws IOException {
324+
String multipleInvalidParams = "?sort=invalid&size=-1&verbose=invalid";
325+
326+
Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + multipleInvalidParams);
327+
try {
328+
client().performRequest(request);
329+
fail("Should not succeed with multiple invalid parameters: " + multipleInvalidParams);
330+
} catch (ResponseException e) {
331+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
332+
}
333+
}
334+
335+
/**
336+
* Test valid parameters with unexpected extra parameter
337+
*
338+
* @throws IOException IOException
339+
*/
340+
public void testValidParametersWithExtraParams() throws IOException {
341+
Request nodesRequest = new Request("GET", "/_nodes");
342+
Response nodesResponse = client().performRequest(nodesRequest);
343+
Map<String, Object> nodesMap = entityAsMap(nodesResponse);
344+
Map<String, Object> nodes = (Map<String, Object>) nodesMap.get("nodes");
345+
String nodeId = nodes.keySet().iterator().next();
346+
347+
// Test all expected parameters plus an unexpected one
348+
String paramsWithExtra = "?sort=latency&verbose=true&size=5&nodeId=" + nodeId + "&unknownParam=value";
349+
Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + paramsWithExtra);
350+
try {
351+
client().performRequest(request);
352+
fail("Should not succeed with an unexpected extra parameter");
353+
} catch (ResponseException e) {
354+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
355+
}
356+
}
357+
281358
/**
282359
* Create a test index with the specified number of documents
283360
*/

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,104 @@ public void testTopQueriesSettings() throws IOException {
8181
}
8282
}
8383

84+
/**
85+
* Test missing time parameters
86+
*
87+
* @throws IOException IOException
88+
*/
89+
public void testMissingTimeParameters() throws IOException {
90+
String[] missingTimeParams = { "?from=2025-01-01T00:00:00Z", "?to=2025-01-02T00:00:00Z" };
91+
92+
for (String param : missingTimeParams) {
93+
Request request = new Request("GET", "/_insights/top_queries" + param);
94+
try {
95+
client().performRequest(request);
96+
fail("Should not succeed with missing time parameter: " + param);
97+
} catch (ResponseException e) {
98+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
99+
}
100+
}
101+
}
102+
103+
/**
104+
* Test malformed timestamp parameters
105+
*
106+
* @throws IOException IOException
107+
*/
108+
public void testMalformedTimestamps() throws IOException {
109+
String[] malformedTimeParams = {
110+
"?from=invalid-timestamp&to=2025-01-02T00:00:00Z",
111+
"?from=2025-01-01T00:00:00Z&to=invalid-timestamp",
112+
"?from=2025-13-01T00:00:00Z&to=2025-01-02T00:00:00Z",
113+
"?from=2025-01-32T00:00:00Z&to=2025-01-02T00:00:00Z",
114+
"?from=not-a-date&to=not-a-date",
115+
"?from=2025-01-02T00:00:00Z&to=2025-01-01T00:00:00Z" }; // to timestamp is before from timestamp
116+
117+
for (String param : malformedTimeParams) {
118+
Request request = new Request("GET", "/_insights/top_queries" + param);
119+
try {
120+
client().performRequest(request);
121+
fail("Should not succeed with malformed timestamp: " + param);
122+
} catch (ResponseException e) {
123+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
124+
}
125+
}
126+
}
127+
128+
/**
129+
* Test multiple invalid parameters
130+
*
131+
* @throws IOException IOException
132+
*/
133+
public void testMultipleInvalidParameters() throws IOException {
134+
String[] multipleInvalidParams = { "?type=invalid&from=invalid-timestamp", "?from=2025-01-01T00:00:00Z&type=invalid" };
135+
136+
for (String param : multipleInvalidParams) {
137+
Request request = new Request("GET", "/_insights/top_queries" + param);
138+
try {
139+
client().performRequest(request);
140+
fail("Should not succeed with multiple invalid parameters: " + param);
141+
} catch (ResponseException e) {
142+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
143+
}
144+
}
145+
}
146+
147+
/**
148+
* Test invalid metric type parameter
149+
*
150+
* @throws IOException IOException
151+
*/
152+
public void testInvalidTypeParameter() throws IOException {
153+
String invalidTypeParam = "?type=invalid";
154+
155+
Request request = new Request("GET", "/_insights/top_queries" + invalidTypeParam);
156+
try {
157+
client().performRequest(request);
158+
fail("Should not succeed with invalid type parameter");
159+
} catch (ResponseException e) {
160+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
161+
}
162+
}
163+
164+
/**
165+
* Test valid parameters with unexpected extra parameter
166+
*
167+
* @throws IOException IOException
168+
*/
169+
public void testValidParametersWithExtraParams() throws IOException {
170+
// Test all expected parameters plus an unexpected one
171+
String paramsWithExtra =
172+
"?type=latency&verbose=true&from=2025-01-01T00:00:00Z&to=2025-01-02T00:00:00Z&id=test-id&unknownParam=value";
173+
Request request = new Request("GET", "/_insights/top_queries" + paramsWithExtra);
174+
try {
175+
client().performRequest(request);
176+
fail("Should not succeed with an unexpected extra parameter");
177+
} catch (ResponseException e) {
178+
assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
179+
}
180+
}
181+
84182
private String topQueriesByResourceUsagesSettings() {
85183
return "{\n"
86184
+ " \"persistent\" : {\n"

0 commit comments

Comments
 (0)