Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> nodesMap = entityAsMap(nodesResponse);
Map<String, Object> nodes = (Map<String, Object>) 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading