@@ -45,7 +45,10 @@ public void setUp() throws Exception {
45
45
super .setUp ();
46
46
mlCommonsClientAccessor = mock (MLCommonsClientAccessor .class );
47
47
queryTextExtractorRegistry = new QueryTextExtractorRegistry ();
48
- highlighterEngine = new SemanticHighlighterEngine (mlCommonsClientAccessor , queryTextExtractorRegistry );
48
+ highlighterEngine = SemanticHighlighterEngine .builder ()
49
+ .mlCommonsClient (mlCommonsClientAccessor )
50
+ .queryTextExtractorRegistry (queryTextExtractorRegistry )
51
+ .build ();
49
52
50
53
// Setup default mock behavior
51
54
setupDefaultMockBehavior ();
@@ -123,7 +126,7 @@ public void testExtractOriginalQuery() {
123
126
}
124
127
125
128
public void testGetHighlightedSentences () {
126
- String result = highlighterEngine .getHighlightedSentences (MODEL_ID , TEST_QUERY , TEST_CONTENT );
129
+ String result = highlighterEngine .getHighlightedSentences (MODEL_ID , TEST_QUERY , TEST_CONTENT , "<em>" , "</em>" );
127
130
128
131
assertNotNull ("Should return highlighted text" , result );
129
132
assertTrue ("Should contain highlighting tags" , result .contains ("<em>" ) && result .contains ("</em>" ));
@@ -149,7 +152,7 @@ public void testApplyHighlighting() {
149
152
resultMap .put ("highlights" , highlightsList );
150
153
151
154
String text = "This is a test string" ;
152
- String result = highlighterEngine .applyHighlighting (text , resultMap );
155
+ String result = highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" );
153
156
154
157
assertEquals ("Should apply highlights correctly" , "<em>This</em> is <em>a te</em>st string" , result );
155
158
}
@@ -170,7 +173,10 @@ public void testApplyHighlightingWithInvalidPositions() {
170
173
highlightsList .add (highlight1 );
171
174
resultMap .put ("highlights" , highlightsList );
172
175
173
- OpenSearchException exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
176
+ OpenSearchException exception = expectThrows (
177
+ OpenSearchException .class ,
178
+ () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" )
179
+ );
174
180
assertEquals (
175
181
"Should throw correct error message for invalid positions" ,
176
182
String .format (
@@ -188,7 +194,7 @@ public void testApplyHighlightingWithInvalidPositions() {
188
194
highlight2 .put ("end" , 100 );
189
195
highlightsList .add (highlight2 );
190
196
191
- exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
197
+ exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" ));
192
198
assertEquals (
193
199
"Should throw correct error message for invalid positions" ,
194
200
String .format (
@@ -206,7 +212,7 @@ public void testApplyHighlightingWithInvalidPositions() {
206
212
highlight3 .put ("end" , 5 );
207
213
highlightsList .add (highlight3 );
208
214
209
- exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
215
+ exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" ));
210
216
assertEquals (
211
217
"Should throw correct error message for invalid positions" ,
212
218
String .format (
@@ -238,20 +244,15 @@ public void testApplyHighlightingWithUnsortedPositions() {
238
244
239
245
resultMap .put ("highlights" , highlightsList );
240
246
241
- OpenSearchException exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
247
+ OpenSearchException exception = expectThrows (
248
+ OpenSearchException .class ,
249
+ () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" )
250
+ );
242
251
assertEquals (
243
252
"Should throw correct error message for unsorted positions" ,
244
253
"Internal error while applying semantic highlight: received unsorted highlights from model" ,
245
254
exception .getMessage ()
246
255
);
247
- // Verify that sorted positions work correctly
248
- highlightsList .clear ();
249
- // Add highlights in sorted order
250
- highlightsList .add (highlight2 ); // start=0
251
- highlightsList .add (highlight1 ); // start=8
252
-
253
- String result = highlighterEngine .applyHighlighting (text , resultMap );
254
- assertEquals ("Should successfully highlight with sorted positions" , "<em>This</em> is <em>a</em> test string" , result );
255
256
}
256
257
257
258
public void testApplyHighlightingWithInvalidHighlightMap () {
@@ -268,7 +269,10 @@ public void testApplyHighlightingWithInvalidHighlightMap() {
268
269
resultMap .put ("highlights" , highlightsList );
269
270
270
271
String text = "This is a test string" ;
271
- ClassCastException exception = expectThrows (ClassCastException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
272
+ ClassCastException exception = expectThrows (
273
+ ClassCastException .class ,
274
+ () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" )
275
+ );
272
276
assertTrue (exception .getMessage ().contains ("cannot be cast to class java.lang.Number" ));
273
277
}
274
278
@@ -286,7 +290,10 @@ public void testApplyHighlightingWithMissingPositions() {
286
290
resultMap .put ("highlights" , highlightsList );
287
291
288
292
String text = "This is a test string" ;
289
- OpenSearchException exception = expectThrows (OpenSearchException .class , () -> highlighterEngine .applyHighlighting (text , resultMap ));
293
+ OpenSearchException exception = expectThrows (
294
+ OpenSearchException .class ,
295
+ () -> highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" )
296
+ );
290
297
assertTrue (exception .getMessage ().contains ("Missing start or end position" ));
291
298
}
292
299
@@ -296,17 +303,31 @@ public void testApplyHighlightingWithEmptyHighlights() {
296
303
resultMap .put ("highlights" , new ArrayList <>());
297
304
298
305
String text = "This is a test string" ;
299
- String result = highlighterEngine .applyHighlighting (text , resultMap );
306
+ String result = highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" );
300
307
assertEquals ("Should return original text when no highlights" , text , result );
301
308
}
302
309
303
310
public void testApplyHighlightingWithMissingHighlightsKey () {
304
- // Test with missing highlights key
305
311
Map <String , Object > resultMap = new HashMap <>();
306
- // No highlights key
307
-
308
312
String text = "This is a test string" ;
309
- String result = highlighterEngine .applyHighlighting (text , resultMap );
313
+ String result = highlighterEngine .applyHighlighting (text , resultMap , "<em>" , "</em>" );
310
314
assertNull ("Should return null when highlights key is missing" , result );
311
315
}
316
+
317
+ public void testCustomTags () {
318
+ // Test with custom tags
319
+ String result = highlighterEngine .getHighlightedSentences (MODEL_ID , TEST_QUERY , TEST_CONTENT , "<mark>" , "</mark>" );
320
+ assertNotNull ("Should return highlighted text" , result );
321
+ assertTrue ("Should contain custom highlighting tags" , result .contains ("<mark>" ) && result .contains ("</mark>" ));
322
+ assertFalse ("Should not contain default highlighting tags" , result .contains ("<em>" ) || result .contains ("</em>" ));
323
+
324
+ // Test with different custom tags
325
+ result = highlighterEngine .getHighlightedSentences (MODEL_ID , TEST_QUERY , TEST_CONTENT , "<span class='highlight'>" , "</span>" );
326
+ assertNotNull ("Should return highlighted text" , result );
327
+ assertTrue (
328
+ "Should contain new custom highlighting tags" ,
329
+ result .contains ("<span class='highlight'>" ) && result .contains ("</span>" )
330
+ );
331
+ assertFalse ("Should not contain previous highlighting tags" , result .contains ("<mark>" ) || result .contains ("</mark>" ));
332
+ }
312
333
}
0 commit comments