From 13c9e64b42b1dc3c765afb87915127348ef3d071 Mon Sep 17 00:00:00 2001 From: Emily Guo <35637792+LilyCaroline17@users.noreply.github.com> Date: Fri, 12 Sep 2025 13:37:31 -0700 Subject: [PATCH] Added more parameter checks in TopQueries and LiveQueries integration tests Signed-off-by: Emily Guo <35637792+LilyCaroline17@users.noreply.github.com> --- .../live_queries/LiveQueriesRestIT.java | 77 +++++++++++++++ .../top_queries/TopQueriesRestIT.java | 98 +++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/live_queries/LiveQueriesRestIT.java b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/live_queries/LiveQueriesRestIT.java index 07102e71..e6b900f9 100644 --- a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/live_queries/LiveQueriesRestIT.java +++ b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/live_queries/LiveQueriesRestIT.java @@ -24,6 +24,7 @@ import org.junit.Assert; import org.opensearch.client.Request; import org.opensearch.client.Response; +import org.opensearch.client.ResponseException; import org.opensearch.common.xcontent.LoggingDeprecationHandler; import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -278,6 +279,82 @@ public void testAllParameters() throws IOException { } } + /** + * Test invalid sort parameter + * + * @throws IOException IOException + */ + public void testInvalidSortParameter() throws IOException { + String invalidSortParam = "?sort=invalid"; + + Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + invalidSortParam); + try { + client().performRequest(request); + fail("Should not succeed with invalid sort parameter"); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + + /** + * Test invalid size parameter + * + * @throws IOException IOException + */ + public void testInvalidSizeParameter() throws IOException { + String[] invalidSizeParams = { "?size=-1", "?size=invalid" }; + + for (String param : invalidSizeParams) { + Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + param); + try { + client().performRequest(request); + fail("Should not succeed with invalid size parameter: " + param); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + } + + /** + * Test multiple invalid parameters + * + * @throws IOException IOException + */ + public void testMultipleInvalidParameters() throws IOException { + String multipleInvalidParams = "?sort=invalid&size=-1&verbose=invalid"; + + Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + multipleInvalidParams); + try { + client().performRequest(request); + fail("Should not succeed with multiple invalid parameters: " + multipleInvalidParams); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + + /** + * Test valid parameters with unexpected extra parameter + * + * @throws IOException IOException + */ + public void testValidParametersWithExtraParams() throws IOException { + Request nodesRequest = new Request("GET", "/_nodes"); + Response nodesResponse = client().performRequest(nodesRequest); + Map nodesMap = entityAsMap(nodesResponse); + Map nodes = (Map) nodesMap.get("nodes"); + String nodeId = nodes.keySet().iterator().next(); + + // Test all expected parameters plus an unexpected one + String paramsWithExtra = "?sort=latency&verbose=true&size=5&nodeId=" + nodeId + "&unknownParam=value"; + Request request = new Request("GET", QueryInsightsSettings.LIVE_QUERIES_BASE_URI + paramsWithExtra); + try { + client().performRequest(request); + fail("Should not succeed with an unexpected extra parameter"); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + /** * Create a test index with the specified number of documents */ diff --git a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/TopQueriesRestIT.java b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/TopQueriesRestIT.java index 4d82542e..f0db3daa 100644 --- a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/TopQueriesRestIT.java +++ b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/TopQueriesRestIT.java @@ -81,6 +81,104 @@ public void testTopQueriesSettings() throws IOException { } } + /** + * Test missing time parameters + * + * @throws IOException IOException + */ + public void testMissingTimeParameters() throws IOException { + String[] missingTimeParams = { "?from=2025-01-01T00:00:00Z", "?to=2025-01-02T00:00:00Z" }; + + for (String param : missingTimeParams) { + Request request = new Request("GET", "/_insights/top_queries" + param); + try { + client().performRequest(request); + fail("Should not succeed with missing time parameter: " + param); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + } + + /** + * Test malformed timestamp parameters + * + * @throws IOException IOException + */ + public void testMalformedTimestamps() throws IOException { + String[] malformedTimeParams = { + "?from=invalid-timestamp&to=2025-01-02T00:00:00Z", + "?from=2025-01-01T00:00:00Z&to=invalid-timestamp", + "?from=2025-13-01T00:00:00Z&to=2025-01-02T00:00:00Z", + "?from=2025-01-32T00:00:00Z&to=2025-01-02T00:00:00Z", + "?from=not-a-date&to=not-a-date", + "?from=2025-01-02T00:00:00Z&to=2025-01-01T00:00:00Z" }; // to timestamp is before from timestamp + + for (String param : malformedTimeParams) { + Request request = new Request("GET", "/_insights/top_queries" + param); + try { + client().performRequest(request); + fail("Should not succeed with malformed timestamp: " + param); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + } + + /** + * Test multiple invalid parameters + * + * @throws IOException IOException + */ + public void testMultipleInvalidParameters() throws IOException { + String[] multipleInvalidParams = { "?type=invalid&from=invalid-timestamp", "?from=2025-01-01T00:00:00Z&type=invalid" }; + + for (String param : multipleInvalidParams) { + Request request = new Request("GET", "/_insights/top_queries" + param); + try { + client().performRequest(request); + fail("Should not succeed with multiple invalid parameters: " + param); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + } + + /** + * Test invalid metric type parameter + * + * @throws IOException IOException + */ + public void testInvalidTypeParameter() throws IOException { + String invalidTypeParam = "?type=invalid"; + + Request request = new Request("GET", "/_insights/top_queries" + invalidTypeParam); + try { + client().performRequest(request); + fail("Should not succeed with invalid type parameter"); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + + /** + * Test valid parameters with unexpected extra parameter + * + * @throws IOException IOException + */ + public void testValidParametersWithExtraParams() throws IOException { + // Test all expected parameters plus an unexpected one + String paramsWithExtra = + "?type=latency&verbose=true&from=2025-01-01T00:00:00Z&to=2025-01-02T00:00:00Z&id=test-id&unknownParam=value"; + Request request = new Request("GET", "/_insights/top_queries" + paramsWithExtra); + try { + client().performRequest(request); + fail("Should not succeed with an unexpected extra parameter"); + } catch (ResponseException e) { + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); + } + } + private String topQueriesByResourceUsagesSettings() { return "{\n" + " \"persistent\" : {\n"