@@ -727,6 +727,85 @@ public void testDoRewriteWithMissingBracketVariable() throws IOException {
727
727
assertTrue (exception .getMessage ().contains ("Unclosed variable in template" ));
728
728
}
729
729
730
+ /**
731
+ * Tests the replaceVariables method when the template is null.
732
+ * Verifies that an IllegalArgumentException is thrown with the appropriate error message.
733
+ */
734
+
735
+ public void testReplaceVariablesWithNullTemplate () {
736
+ TemplateQueryBuilder templateQueryBuilder = new TemplateQueryBuilder ((Map <String , Object >) null );
737
+
738
+ QueryCoordinatorContext queryRewriteContext = mockQueryRewriteContext ();
739
+ Map <String , Object > contextVariables = new HashMap <>();
740
+ contextVariables .put ("response" , "foo" );
741
+ when (queryRewriteContext .getContextVariables ()).thenReturn (contextVariables );
742
+
743
+ IllegalArgumentException exception = expectThrows (
744
+ IllegalArgumentException .class ,
745
+ () -> templateQueryBuilder .doRewrite (queryRewriteContext )
746
+ );
747
+ assertEquals ("Template string cannot be null. A valid template must be provided." , exception .getMessage ());
748
+ }
749
+
750
+ /**
751
+ * Tests the replaceVariables method when the template is empty.
752
+ * Verifies that an IllegalArgumentException is thrown with the appropriate error message.
753
+ */
754
+
755
+ public void testReplaceVariablesWithEmptyTemplate () {
756
+ Map <String , Object > template = new HashMap <>();
757
+ TemplateQueryBuilder templateQueryBuilder = new TemplateQueryBuilder (template );
758
+
759
+ QueryCoordinatorContext queryRewriteContext = mockQueryRewriteContext ();
760
+ Map <String , Object > contextVariables = new HashMap <>();
761
+ contextVariables .put ("response" , "foo" );
762
+ when (queryRewriteContext .getContextVariables ()).thenReturn (contextVariables );
763
+
764
+ IllegalArgumentException exception = expectThrows (
765
+ IllegalArgumentException .class ,
766
+ () -> templateQueryBuilder .doRewrite (queryRewriteContext )
767
+ );
768
+ assertEquals ("Template string cannot be empty. A valid template must be provided." , exception .getMessage ());
769
+
770
+ }
771
+
772
+ /**
773
+ * Tests the replaceVariables method when the variables map is null.
774
+ * Verifies that the method returns the original template unchanged,
775
+ * since a null variables map is treated as no replacement.
776
+ */
777
+ public void testReplaceVariablesWithNullVariables () throws IOException {
778
+
779
+ Map <String , Object > template = new HashMap <>();
780
+ Map <String , Object > term = new HashMap <>();
781
+ Map <String , Object > message = new HashMap <>();
782
+
783
+ message .put ("value" , "foo" );
784
+ term .put ("message" , message );
785
+ template .put ("term" , term );
786
+ TemplateQueryBuilder templateQueryBuilder = new TemplateQueryBuilder (template );
787
+ TermQueryBuilder termQueryBuilder = new TermQueryBuilder ("message" , "foo" );
788
+
789
+ QueryCoordinatorContext queryRewriteContext = mockQueryRewriteContext ();
790
+
791
+ when (queryRewriteContext .getContextVariables ()).thenReturn (null );
792
+
793
+ TermQueryBuilder newQuery = (TermQueryBuilder ) templateQueryBuilder .doRewrite (queryRewriteContext );
794
+
795
+ assertEquals (newQuery , termQueryBuilder );
796
+ assertEquals (
797
+ "{\n "
798
+ + " \" term\" : {\n "
799
+ + " \" message\" : {\n "
800
+ + " \" value\" : \" foo\" ,\n "
801
+ + " \" boost\" : 1.0\n "
802
+ + " }\n "
803
+ + " }\n "
804
+ + "}" ,
805
+ newQuery .toString ()
806
+ );
807
+ }
808
+
730
809
/**
731
810
* Helper method to create a mock QueryCoordinatorContext for testing.
732
811
*/
0 commit comments