1414import static org .mockito .ArgumentMatchers .any ;
1515import static org .mockito .Mockito .doAnswer ;
1616import static org .mockito .Mockito .mock ;
17+ import static org .mockito .Mockito .when ;
18+ import static org .opensearch .ml .common .settings .MLCommonsSettings .ML_COMMONS_AGENTIC_SEARCH_DISABLED_MESSAGE ;
1719import static org .opensearch .ml .engine .tools .QueryPlanningTool .DEFAULT_DESCRIPTION ;
1820import static org .opensearch .ml .engine .tools .QueryPlanningTool .MODEL_ID_FIELD ;
1921
3133import org .mockito .ArgumentCaptor ;
3234import org .mockito .Mock ;
3335import org .mockito .MockitoAnnotations ;
36+ import org .opensearch .OpenSearchException ;
3437import org .opensearch .core .action .ActionListener ;
38+ import org .opensearch .ml .common .settings .MLFeatureEnabledSetting ;
3539import org .opensearch .ml .common .spi .tools .Tool ;
3640import org .opensearch .transport .client .Client ;
3741
@@ -46,6 +50,9 @@ public class QueryPlanningToolTests {
4650 @ Mock
4751 private MLModelTool queryGenerationTool ;
4852
53+ @ Mock
54+ private MLFeatureEnabledSetting mlFeatureEnabledSetting ;
55+
4956 private Map <String , String > validParams ;
5057 private Map <String , String > emptyParams ;
5158
@@ -55,7 +62,14 @@ public class QueryPlanningToolTests {
5562 public void setup () {
5663 MockitoAnnotations .openMocks (this );
5764 MLModelTool .Factory .getInstance ().init (client );
58- factory = new QueryPlanningTool .Factory ();
65+
66+ // Mock the MLFeatureEnabledSetting to return true for agentic search
67+ when (mlFeatureEnabledSetting .isAgenticSearchEnabled ()).thenReturn (true );
68+
69+ // Initialize the factory with mocked dependencies
70+ factory = QueryPlanningTool .Factory .getInstance ();
71+ factory .init (client , mlFeatureEnabledSetting );
72+
5973 validParams = new HashMap <>();
6074 validParams .put ("prompt" , "test prompt" );
6175 emptyParams = Collections .emptyMap ();
@@ -269,4 +283,16 @@ public void testFactoryCreateWithInvalidType() {
269283 Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (map ));
270284 assertEquals ("Invalid generation type: invalid. The current supported types are llmGenerated." , exception .getMessage ());
271285 }
286+
287+ @ Test
288+ public void testFactoryCreateWhenAgenticSearchDisabled () {
289+ // Mock the MLFeatureEnabledSetting to return false for agentic search
290+ when (mlFeatureEnabledSetting .isAgenticSearchEnabled ()).thenReturn (false );
291+
292+ Map <String , Object > map = new HashMap <>();
293+ map .put (QueryPlanningTool .MODEL_ID_FIELD , "modelId" );
294+
295+ Exception exception = assertThrows (OpenSearchException .class , () -> factory .create (map ));
296+ assertEquals (ML_COMMONS_AGENTIC_SEARCH_DISABLED_MESSAGE , exception .getMessage ());
297+ }
272298}
0 commit comments