24
24
import org .junit .Assert ;
25
25
import org .opensearch .client .Request ;
26
26
import org .opensearch .client .Response ;
27
+ import org .opensearch .client .ResponseException ;
27
28
import org .opensearch .common .xcontent .LoggingDeprecationHandler ;
28
29
import org .opensearch .common .xcontent .json .JsonXContent ;
29
30
import org .opensearch .core .xcontent .NamedXContentRegistry ;
@@ -121,7 +122,7 @@ public void testLiveQueriesWithConcurrentSearches() throws Exception {
121
122
Map <String , Object > nodes = (Map <String , Object >) nodesMap .get ("nodes" );
122
123
String nodeId = nodes .keySet ().iterator ().next ();
123
124
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 };
125
126
Map <String , Boolean > foundParams = new java .util .HashMap <>();
126
127
for (String param : params ) {
127
128
foundParams .put (param , false );
@@ -267,7 +268,7 @@ public void testAllParameters() throws IOException {
267
268
String nodeId = nodes .keySet ().iterator ().next ();
268
269
269
270
// 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 };
271
272
for (String param : params ) {
272
273
String uri = QueryInsightsSettings .LIVE_QUERIES_BASE_URI + param ;
273
274
Request req = new Request ("GET" , uri );
@@ -278,6 +279,82 @@ public void testAllParameters() throws IOException {
278
279
}
279
280
}
280
281
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
+
281
358
/**
282
359
* Create a test index with the specified number of documents
283
360
*/
0 commit comments