13
13
import java .time .Instant ;
14
14
import java .time .ZoneOffset ;
15
15
import java .time .format .DateTimeFormatter ;
16
- import java .util .*;
16
+ import java .util .Collections ;
17
+ import java .util .List ;
18
+ import java .util .Locale ;
19
+ import java .util .Map ;
20
+ import java .util .Optional ;
17
21
import java .util .function .Supplier ;
18
22
import java .util .logging .Logger ;
19
23
import java .util .regex .Matcher ;
36
40
import org .junit .After ;
37
41
import org .junit .Assert ;
38
42
import org .junit .Before ;
39
- import org .opensearch .client .*;
43
+ import org .opensearch .client .Request ;
44
+ import org .opensearch .client .Response ;
45
+ import org .opensearch .client .ResponseException ;
46
+ import org .opensearch .client .RestClient ;
47
+ import org .opensearch .client .RestClientBuilder ;
40
48
import org .opensearch .common .settings .Settings ;
41
49
import org .opensearch .common .unit .TimeValue ;
42
50
import org .opensearch .common .util .concurrent .ThreadContext ;
47
55
import org .opensearch .core .xcontent .XContentParser ;
48
56
import org .opensearch .plugin .insights .settings .QueryInsightsSettings ;
49
57
import org .opensearch .test .rest .OpenSearchRestTestCase ;
50
- import org .opensearch .client .Response ;
51
58
52
59
public abstract class QueryInsightsRestTestCase extends OpenSearchRestTestCase {
53
60
protected static final String QUERY_INSIGHTS_INDICES_PREFIX = "top_queries" ;
@@ -390,7 +397,7 @@ protected void createDocument() throws IOException {
390
397
assertEquals (201 , response .getStatusLine ().getStatusCode ());
391
398
}
392
399
393
- protected void performSearch () throws IOException , InterruptedException {
400
+ protected void performSearch () throws IOException , InterruptedException {
394
401
Thread .sleep (3000 );
395
402
396
403
String searchJson = "{ \" query\" : { \" match\" : { \" title\" : \" Test Document\" } } }" ;
@@ -411,9 +418,11 @@ protected void setLatencyWindowSize(String size) throws IOException {
411
418
412
419
protected void defaultExporterSettings () throws IOException {
413
420
Request request = new Request ("PUT" , "/_cluster/settings" );
414
- request .setJsonEntity ("{ \" persistent\" : { " +
415
- "\" search.insights.top_queries.exporter.type\" : \" local_index\" , " +
416
- "\" search.insights.top_queries.latency.enabled\" : \" true\" } }" );
421
+ request .setJsonEntity (
422
+ "{ \" persistent\" : { "
423
+ + "\" search.insights.top_queries.exporter.type\" : \" local_index\" , "
424
+ + "\" search.insights.top_queries.latency.enabled\" : \" true\" } }"
425
+ );
417
426
Response response = client ().performRequest (request );
418
427
Assert .assertEquals (200 , response .getStatusLine ().getStatusCode ());
419
428
}
@@ -432,9 +441,9 @@ protected void cleanup() throws IOException, InterruptedException {
432
441
logger .warning ("Cleanup: Failed to delete /my-index-0: " + e .getMessage ());
433
442
}
434
443
435
- String resetSettings = "{ \" persistent\" : { " +
436
- "\" search.insights.top_queries.exporter.type\" : \" none\" , " +
437
- "\" search.insights.top_queries.latency.enabled\" : \" false\" } }" ;
444
+ String resetSettings = "{ \" persistent\" : { "
445
+ + "\" search.insights.top_queries.exporter.type\" : \" none\" , "
446
+ + "\" search.insights.top_queries.latency.enabled\" : \" false\" } }" ;
438
447
Request resetReq = new Request ("PUT" , "/_cluster/settings" );
439
448
resetReq .setJsonEntity (resetSettings );
440
449
client ().performRequest (resetReq );
@@ -449,6 +458,7 @@ protected void cleanupIndextemplate() throws IOException, InterruptedException {
449
458
logger .warning ("Failed to delete /_index_template: " + e .getMessage ());
450
459
}
451
460
}
461
+
452
462
protected void checkLocalIndices () throws IOException {
453
463
Request indicesRequest = new Request ("GET" , "/_cat/indices?v" );
454
464
Response response = client ().performRequest (indicesRequest );
@@ -476,9 +486,13 @@ protected void checkLocalIndices() throws IOException {
476
486
477
487
byte [] bytes = fetchResponse .getEntity ().getContent ().readAllBytes ();
478
488
479
- try (XContentParser parser = JsonXContent .jsonXContent .createParser (
480
- NamedXContentRegistry .EMPTY , DeprecationHandler .THROW_UNSUPPORTED_OPERATION , bytes
481
- )) {
489
+ try (
490
+ XContentParser parser = JsonXContent .jsonXContent .createParser (
491
+ NamedXContentRegistry .EMPTY ,
492
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
493
+ bytes
494
+ )
495
+ ) {
482
496
Map <String , Object > responseMap = parser .map ();
483
497
484
498
Map <String , Object > hitsWrapper = (Map <String , Object >) responseMap .get ("hits" );
@@ -507,8 +521,7 @@ protected void checkLocalIndices() throws IOException {
507
521
assertTrue (measurements .containsKey ("latency" ));
508
522
assertTrue (measurements .containsKey ("memory" ));
509
523
510
- List <Map <String , Object >> taskResourceUsages =
511
- (List <Map <String , Object >>) source .get ("task_resource_usages" );
524
+ List <Map <String , Object >> taskResourceUsages = (List <Map <String , Object >>) source .get ("task_resource_usages" );
512
525
assertTrue ("Expected non-empty task_resource_usages" , taskResourceUsages .size () > 0 );
513
526
}
514
527
}
@@ -518,9 +531,13 @@ protected void checkQueryInsightsIndexTemplate() throws IOException {
518
531
Response response = client ().performRequest (request );
519
532
byte [] bytes = response .getEntity ().getContent ().readAllBytes ();
520
533
521
- try (XContentParser parser = JsonXContent .jsonXContent .createParser (
522
- NamedXContentRegistry .EMPTY , DeprecationHandler .THROW_UNSUPPORTED_OPERATION , bytes
523
- )) {
534
+ try (
535
+ XContentParser parser = JsonXContent .jsonXContent .createParser (
536
+ NamedXContentRegistry .EMPTY ,
537
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
538
+ bytes
539
+ )
540
+ ) {
524
541
Map <String , Object > parsed = parser .map ();
525
542
526
543
List <Map <String , Object >> templates = (List <Map <String , Object >>) parsed .get ("index_templates" );
@@ -561,8 +578,6 @@ protected void setLocalIndexToDebug() throws IOException {
561
578
client ().performRequest (debugExporterRequest );
562
579
}
563
580
564
-
565
-
566
581
protected void disableLocalIndexExporter () throws IOException {
567
582
String disableExporterJson = "{ \" persistent\" : { \" search.insights.top_queries.exporter.type\" : \" none\" } }" ;
568
583
Request disableExporterRequest = new Request ("PUT" , "/_cluster/settings" );
@@ -573,8 +588,7 @@ protected void disableLocalIndexExporter() throws IOException {
573
588
protected String [] invalidExporterSettings () {
574
589
return new String [] {
575
590
"{ \" persistent\" : { \" search.insights.top_queries.exporter.type\" : invalid_type } }" ,
576
- "{ \" persistent\" : { \" search.insights.top_queries.exporter.type\" : local_index, \" search.insights.top_queries.exporter.config.index\" : \" 1a2b\" } }"
577
- };
591
+ "{ \" persistent\" : { \" search.insights.top_queries.exporter.type\" : local_index, \" search.insights.top_queries.exporter.config.index\" : \" 1a2b\" } }" };
578
592
}
579
593
580
594
protected void fetchHistoricalTopQueries () throws IOException {
@@ -588,11 +602,13 @@ protected void fetchHistoricalTopQueries() throws IOException {
588
602
assertEquals (200 , fetchResponse .getStatusLine ().getStatusCode ());
589
603
byte [] content = fetchResponse .getEntity ().getContent ().readAllBytes ();
590
604
591
- try (XContentParser parser = JsonXContent .jsonXContent .createParser (
592
- NamedXContentRegistry .EMPTY ,
593
- DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
594
- content
595
- )) {
605
+ try (
606
+ XContentParser parser = JsonXContent .jsonXContent .createParser (
607
+ NamedXContentRegistry .EMPTY ,
608
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
609
+ content
610
+ )
611
+ ) {
596
612
Map <String , Object > root = parser .map ();
597
613
List <Map <String , Object >> topQueries = (List <Map <String , Object >>) root .get ("top_queries" );
598
614
assertNotNull ("Expected 'top_queries' field" , topQueries );
@@ -634,5 +650,4 @@ protected void fetchHistoricalTopQueries() throws IOException {
634
650
}
635
651
}
636
652
637
-
638
653
}
0 commit comments