6161import org .opensearch .ml .common .memorycontainer .MemoryStrategy ;
6262import org .opensearch .ml .common .memorycontainer .MemoryType ;
6363import org .opensearch .remote .metadata .client .GetDataObjectResponse ;
64- import org .opensearch .remote .metadata .client .SearchDataObjectResponse ;
6564import org .opensearch .search .SearchHit ;
6665import org .opensearch .search .SearchHits ;
6766import org .opensearch .search .aggregations .InternalAggregations ;
@@ -253,16 +252,21 @@ public void testSearchData() {
253252 request .source (new SearchSourceBuilder ());
254253
255254 SearchResponse searchResponse = createSearchResponse (2 );
256- SearchDataObjectResponse response = new SearchDataObjectResponse (searchResponse );
257- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (CompletableFuture .completedFuture (response ));
255+ doAnswer (invocation -> {
256+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
257+ listener .onResponse (searchResponse );
258+ return null ;
259+ }).when (client ).search (any (), any ());
258260
259261 PlainActionFuture <SearchResponse > future = PlainActionFuture .newFuture ();
260262 helper .searchData (configuration , request , future );
261263 assertSame (searchResponse , future .actionGet ());
262264
263- CompletableFuture <SearchDataObjectResponse > failed = new CompletableFuture <>();
264- failed .completeExceptionally (new RuntimeException ("search failure" ));
265- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (failed );
265+ doAnswer (invocation -> {
266+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
267+ listener .onFailure (new RuntimeException ("search failure" ));
268+ return null ;
269+ }).when (client ).search (any (), any ());
266270
267271 PlainActionFuture <SearchResponse > failure = PlainActionFuture .newFuture ();
268272 helper .searchData (configuration , request , failure );
@@ -277,17 +281,23 @@ public void testSearchDataWithSystemIndex() {
277281 request .source (new SearchSourceBuilder ());
278282
279283 SearchResponse searchResponse = createSearchResponse (3 );
280- SearchDataObjectResponse response = new SearchDataObjectResponse (searchResponse );
281- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (CompletableFuture .completedFuture (response ));
284+
285+ doAnswer (invocation -> {
286+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
287+ listener .onResponse (searchResponse );
288+ return null ;
289+ }).when (client ).search (any (), any ());
282290
283291 PlainActionFuture <SearchResponse > future = PlainActionFuture .newFuture ();
284292 helper .searchData (systemConfig , request , future );
285293 assertSame (searchResponse , future .actionGet ());
286294
287295 // Test failure with system index
288- CompletableFuture <SearchDataObjectResponse > failed = new CompletableFuture <>();
289- failed .completeExceptionally (new RuntimeException ("system index search failure" ));
290- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (failed );
296+ doAnswer (invocation -> {
297+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
298+ listener .onFailure (new RuntimeException ("system index search failure" ));
299+ return null ;
300+ }).when (client ).search (any (), any ());
291301
292302 PlainActionFuture <SearchResponse > failure = PlainActionFuture .newFuture ();
293303 helper .searchData (systemConfig , request , failure );
@@ -642,33 +652,39 @@ public void testGetLlmResultPath() {
642652 public void testCountContainersWithPrefix () {
643653 // Test with null prefix
644654 PlainActionFuture <Long > nullFuture = PlainActionFuture .newFuture ();
645- helper .countContainersWithPrefix (null , null , nullFuture );
655+ helper .countContainersWithPrefix (null , nullFuture );
646656 assertEquals (0L , nullFuture .actionGet ().longValue ());
647657
648658 // Test with blank prefix
649659 PlainActionFuture <Long > blankFuture = PlainActionFuture .newFuture ();
650- helper .countContainersWithPrefix ("" , null , blankFuture );
660+ helper .countContainersWithPrefix ("" , blankFuture );
651661 assertEquals (0L , blankFuture .actionGet ().longValue ());
652662
653663 SearchResponse searchResponse = createSearchResponse (3 );
654- SearchDataObjectResponse response = new SearchDataObjectResponse (searchResponse );
655- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (CompletableFuture .completedFuture (response ));
664+
665+ doAnswer (invocation -> {
666+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
667+ listener .onResponse (searchResponse );
668+ return null ;
669+ }).when (client ).search (any (), any ());
656670
657671 PlainActionFuture <Long > future = PlainActionFuture .newFuture ();
658- helper .countContainersWithPrefix ("prefix" , null , future );
672+ helper .countContainersWithPrefix ("prefix" , future );
659673 assertEquals (3L , future .actionGet ().longValue ());
660674
661675 // Test with tenant ID
662676 PlainActionFuture <Long > tenantFuture = PlainActionFuture .newFuture ();
663- helper .countContainersWithPrefix ("prefix" , "tenant123" , tenantFuture );
677+ helper .countContainersWithPrefix ("prefix" , tenantFuture );
664678 assertEquals (3L , tenantFuture .actionGet ().longValue ());
665679
666- CompletableFuture <SearchDataObjectResponse > failed = new CompletableFuture <>();
667- failed .completeExceptionally (new RuntimeException ("fail" ));
668- when (sdkClient .searchDataObjectAsync (any ())).thenReturn (failed );
680+ doAnswer (invocation -> {
681+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[1 ];
682+ listener .onFailure (new RuntimeException ("fail" ));
683+ return null ;
684+ }).when (client ).search (any (), any ());
669685
670686 PlainActionFuture <Long > failure = PlainActionFuture .newFuture ();
671- helper .countContainersWithPrefix ("prefix" , null , failure );
687+ helper .countContainersWithPrefix ("prefix" , failure );
672688 RuntimeException exception = expectThrows (RuntimeException .class , failure ::actionGet );
673689 assertEquals ("fail" , exception .getMessage ());
674690 }
0 commit comments