-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathSemanticSearchToolTest.java
More file actions
426 lines (331 loc) · 16.2 KB
/
SemanticSearchToolTest.java
File metadata and controls
426 lines (331 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package org.openmetadata.mcp.tools;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openmetadata.service.Entity;
import org.openmetadata.service.search.SearchRepository;
import org.openmetadata.service.search.vector.OpenSearchVectorService;
import org.openmetadata.service.search.vector.utils.DTOs.VectorSearchResponse;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.auth.CatalogSecurityContext;
@ExtendWith(MockitoExtension.class)
class SemanticSearchToolTest {
private SemanticSearchTool semanticSearchTool;
private Authorizer authorizer;
private CatalogSecurityContext securityContext;
private SearchRepository searchRepository;
private OpenSearchVectorService vectorService;
@BeforeEach
void setUp() {
semanticSearchTool = new SemanticSearchTool();
authorizer = mock(Authorizer.class);
securityContext = mock(CatalogSecurityContext.class);
searchRepository = mock(SearchRepository.class);
vectorService = mock(OpenSearchVectorService.class);
Entity.setSearchRepository(searchRepository);
}
@Test
void testMissingQueryReturnsError() throws Exception {
Map<String, Object> params = new HashMap<>();
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
assertNotNull(result.get("error"));
assertTrue(result.get("error").toString().contains("'query' parameter is required"));
}
@Test
void testBlankQueryReturnsError() throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("query", " ");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertNotNull(result.get("error"));
}
@Test
void testVectorEmbeddingDisabledReturnsError() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(false);
Map<String, Object> params = new HashMap<>();
params.put("query", "test query");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertTrue(result.get("error").toString().contains("Semantic search is not enabled"));
}
@Test
void testVectorServiceNotInitializedReturnsError() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(null);
Map<String, Object> params = new HashMap<>();
params.put("query", "test query");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertTrue(result.get("error").toString().contains("not initialized"));
}
}
@Test
void testEmptyResultsResponse() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
VectorSearchResponse response = new VectorSearchResponse(15L, Collections.emptyList());
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test query");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals("test query", result.get("query"));
assertEquals(15L, result.get("tookMillis"));
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
assertTrue(((List<?>) result.get("results")).isEmpty());
}
}
@Test
void testNullHitsResponse() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
VectorSearchResponse response = new VectorSearchResponse(5L, null);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test query");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
}
}
@Test
void testSuccessfulSearchIncludesTotalFound() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
List<Map<String, Object>> hits = new ArrayList<>();
hits.add(createHit("table", "db.schema.users", "Users Table", 0.95));
hits.add(createHit("table", "db.schema.orders", "Orders Table", 0.87));
VectorSearchResponse response = new VectorSearchResponse(25L, hits);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "user data");
params.put("size", 10);
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals("user data", result.get("query"));
assertEquals(25L, result.get("tookMillis"));
assertEquals(2, result.get("totalFound"));
assertEquals(2, result.get("returnedCount"));
List<?> results = (List<?>) result.get("results");
assertEquals(2, results.size());
}
}
@Test
void testHitFieldsCleaned() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
Map<String, Object> hit = new HashMap<>();
hit.put("entityType", "table");
hit.put("fullyQualifiedName", "db.schema.users");
hit.put("name", "users");
hit.put("displayName", "Users");
hit.put("serviceType", "BigQuery");
hit.put("_score", 0.95);
hit.put("description", "A short description");
hit.put("columns", List.of(Map.of("name", "id", "dataType", "INT")));
hit.put("embedding", new float[] {0.1f, 0.2f});
hit.put("fingerprint", "abc123");
hit.put(
"textToLLMContext", "name: users; entityType: table | description: A short description");
VectorSearchResponse response = new VectorSearchResponse(10L, List.of(hit));
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "users");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
List<?> results = (List<?>) result.get("results");
@SuppressWarnings("unchecked")
Map<String, Object> cleaned = (Map<String, Object>) results.get(0);
assertEquals("table", cleaned.get("entityType"));
assertEquals("db.schema.users", cleaned.get("fullyQualifiedName"));
assertEquals("users", cleaned.get("name"));
assertEquals("Users", cleaned.get("displayName"));
assertEquals("BigQuery", cleaned.get("serviceType"));
assertEquals("A short description", cleaned.get("description"));
assertNotNull(cleaned.get("columns"));
assertEquals(0.95, cleaned.get("similarityScore"));
assertTrue(!cleaned.containsKey("_score"));
assertTrue(!cleaned.containsKey("embedding"));
assertTrue(!cleaned.containsKey("fingerprint"));
assertTrue(!cleaned.containsKey("textToLLMContext"));
}
}
@Test
void testLongDescriptionIsTruncated() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
String longText = "x".repeat(600);
Map<String, Object> hit = new HashMap<>();
hit.put("fullyQualifiedName", "db.schema.table");
hit.put("description", longText);
VectorSearchResponse response = new VectorSearchResponse(10L, List.of(hit));
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
List<?> results = (List<?>) result.get("results");
@SuppressWarnings("unchecked")
Map<String, Object> cleaned = (Map<String, Object>) results.get(0);
String truncated = (String) cleaned.get("description");
assertEquals(453, truncated.length());
assertTrue(truncated.endsWith("..."));
}
}
@Test
void testSizeClampedToMax() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
VectorSearchResponse response = new VectorSearchResponse(10L, Collections.emptyList());
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", 100);
semanticSearchTool.execute(authorizer, securityContext, params);
}
}
@Test
void testMessageWhenResultsEqualRequestedSize() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
List<Map<String, Object>> hits = new ArrayList<>();
for (int i = 0; i < 3; i++) {
hits.add(createHit("table", "db.schema.t" + i, "Table " + i, 0.9 - i * 0.1));
}
VectorSearchResponse response = new VectorSearchResponse(10L, hits);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", 3);
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result.get("message"));
assertTrue(result.get("message").toString().contains("Showing 3 results"));
}
}
@Test
void testNoMessageWhenResultsLessThanRequestedSize() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
List<Map<String, Object>> hits = new ArrayList<>();
hits.add(createHit("table", "db.schema.t1", "Table 1", 0.9));
VectorSearchResponse response = new VectorSearchResponse(10L, hits);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", 10);
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertTrue(!result.containsKey("message"));
}
}
@Test
void testSearchExceptionReturnsError() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenThrow(new RuntimeException("Connection refused"));
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertTrue(result.get("error").toString().contains("Connection refused"));
}
}
@Test
void testFiltersPassedAsMap() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
VectorSearchResponse response = new VectorSearchResponse(10L, Collections.emptyList());
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> filters = new HashMap<>();
filters.put("entityType", List.of("table", "topic"));
filters.put("service", "my_db");
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("filters", filters);
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
}
}
@Test
void testStringParamsAreParsed() throws Exception {
when(searchRepository.isVectorEmbeddingEnabled()).thenReturn(true);
VectorSearchResponse response = new VectorSearchResponse(10L, Collections.emptyList());
try (MockedStatic<OpenSearchVectorService> vectorMock =
mockStatic(OpenSearchVectorService.class)) {
vectorMock.when(OpenSearchVectorService::getInstance).thenReturn(vectorService);
when(vectorService.search(anyString(), anyMap(), anyInt(), anyInt(), anyInt(), anyDouble()))
.thenReturn(response);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", "5");
params.put("k", "500");
params.put("threshold", "0.5");
Map<String, Object> result = semanticSearchTool.execute(authorizer, securityContext, params);
assertNotNull(result);
}
}
private Map<String, Object> createHit(String entityType, String fqn, String name, double score) {
Map<String, Object> hit = new HashMap<>();
hit.put("entityType", entityType);
hit.put("fullyQualifiedName", fqn);
hit.put("name", name);
hit.put("_score", score);
return hit;
}
}