@@ -95,6 +95,38 @@ public void testFactoryCreate() {
9595 assertEquals (QueryPlanningTool .TYPE , tool .getName ());
9696 }
9797
98+ @ Test
99+ public void testCreateWithInvalidSearchTemplatesDescription () throws IllegalArgumentException {
100+ Map <String , Object > params = new HashMap <>();
101+ params .put ("generation_type" , "user_templates" );
102+ params .put (MODEL_ID_FIELD , "test_model_id" );
103+ params
104+ .put (
105+ SYSTEM_PROMPT_FIELD ,
106+ "You are a query generation agent. Generate a dsl query for the following question: ${parameters.query_text}"
107+ );
108+ params .put ("query_text" , "help me find some books related to wind" );
109+ params .put ("search_templates" , "[{'template_id': 'template_id', 'template_des': 'test_description'}]" );
110+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
111+ assertEquals ("search_templates field entries must have a template_description" , exception .getMessage ());
112+ }
113+
114+ @ Test
115+ public void testCreateWithInvalidSearchTemplatesID () throws IllegalArgumentException {
116+ Map <String , Object > params = new HashMap <>();
117+ params .put ("generation_type" , "user_templates" );
118+ params .put (MODEL_ID_FIELD , "test_model_id" );
119+ params
120+ .put (
121+ SYSTEM_PROMPT_FIELD ,
122+ "You are a query generation agent. Generate a dsl query for the following question: ${parameters.query_text}"
123+ );
124+ params .put ("query_text" , "help me find some books related to wind" );
125+ params .put ("search_templates" , "[{'templateid': 'template_id', 'template_description': 'test_description'}]" );
126+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
127+ assertEquals ("search_templates field entries must have a template_id" , exception .getMessage ());
128+ }
129+
98130 @ Test
99131 public void testRun () throws ExecutionException , InterruptedException {
100132 String matchQueryString = "{\" query\" :{\" match\" :{\" title\" :\" wind\" }}}" ;
@@ -552,4 +584,101 @@ public void testFactoryCreateWhenAgenticSearchDisabled() {
552584 Exception exception = assertThrows (OpenSearchException .class , () -> factory .create (map ));
553585 assertEquals (ML_COMMONS_AGENTIC_SEARCH_DISABLED_MESSAGE , exception .getMessage ());
554586 }
587+
588+ @ Test
589+ public void testCreateWithValidSearchTemplates () {
590+ Map <String , Object > params = new HashMap <>();
591+ params .put ("generation_type" , "user_templates" );
592+ params .put (MODEL_ID_FIELD , "test_model_id" );
593+ params
594+ .put (
595+ "search_templates" ,
596+ "[{'template_id': 'template1', 'template_description': 'description1'}, {'template_id': 'template2', 'template_description': 'description2'}]"
597+ );
598+
599+ QueryPlanningTool tool = factory .create (params );
600+ assertNotNull (tool );
601+ assertEquals ("user_templates" , tool .getGenerationType ());
602+ }
603+
604+ @ Test
605+ public void testCreateWithEmptySearchTemplatesList () {
606+ Map <String , Object > params = new HashMap <>();
607+ params .put ("generation_type" , "user_templates" );
608+ params .put (MODEL_ID_FIELD , "test_model_id" );
609+ params .put ("search_templates" , "[]" );
610+
611+ QueryPlanningTool tool = factory .create (params );
612+ assertNotNull (tool );
613+ assertEquals ("user_templates" , tool .getGenerationType ());
614+ }
615+
616+ @ Test
617+ public void testCreateWithMissingSearchTemplatesField () {
618+ Map <String , Object > params = new HashMap <>();
619+ params .put ("generation_type" , "user_templates" );
620+ params .put (MODEL_ID_FIELD , "test_model_id" );
621+
622+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
623+ assertEquals ("search_templates field is required when generation_type is 'user_templates'" , exception .getMessage ());
624+ }
625+
626+ @ Test
627+ public void testCreateWithInvalidSearchTemplatesJson () {
628+ Map <String , Object > params = new HashMap <>();
629+ params .put ("generation_type" , "user_templates" );
630+ params .put (MODEL_ID_FIELD , "test_model_id" );
631+ params .put ("search_templates" , "invalid_json" );
632+
633+ assertThrows (com .google .gson .JsonSyntaxException .class , () -> factory .create (params ));
634+ }
635+
636+ @ Test
637+ public void testCreateWithNullTemplateId () {
638+ Map <String , Object > params = new HashMap <>();
639+ params .put ("generation_type" , "user_templates" );
640+ params .put (MODEL_ID_FIELD , "test_model_id" );
641+ params .put ("search_templates" , "[{'template_id': null, 'template_description': 'description'}]" );
642+
643+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
644+ assertEquals ("search_templates field entries must have a template_id" , exception .getMessage ());
645+ }
646+
647+ @ Test
648+ public void testCreateWithBlankTemplateDescription () {
649+ Map <String , Object > params = new HashMap <>();
650+ params .put ("generation_type" , "user_templates" );
651+ params .put (MODEL_ID_FIELD , "test_model_id" );
652+ params .put ("search_templates" , "[{'template_id': 'template1', 'template_description': ' '}]" );
653+
654+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
655+ assertEquals ("search_templates field entries must have a template_description" , exception .getMessage ());
656+ }
657+
658+ @ Test
659+ public void testCreateWithMixedValidAndInvalidTemplates () {
660+ Map <String , Object > params = new HashMap <>();
661+ params .put ("generation_type" , "user_templates" );
662+ params .put (MODEL_ID_FIELD , "test_model_id" );
663+ params
664+ .put (
665+ "search_templates" ,
666+ "[{'template_id': 'template1', 'template_description': 'description1'}, {'template_description': 'description2'}]"
667+ );
668+
669+ Exception exception = assertThrows (IllegalArgumentException .class , () -> factory .create (params ));
670+ assertEquals ("search_templates field entries must have a template_id" , exception .getMessage ());
671+ }
672+
673+ @ Test
674+ public void testCreateWithExtraFieldsInSearchTemplates () {
675+ Map <String , Object > params = new HashMap <>();
676+ params .put ("generation_type" , "user_templates" );
677+ params .put (MODEL_ID_FIELD , "test_model_id" );
678+ params .put ("search_templates" , "[{'template_id': 'template1', 'template_description': 'description1', 'extra_field': 'value'}]" );
679+
680+ QueryPlanningTool tool = factory .create (params );
681+ assertNotNull (tool );
682+ assertEquals ("user_templates" , tool .getGenerationType ());
683+ }
555684}
0 commit comments