-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathSearchMetadataToolTest.java
More file actions
265 lines (214 loc) · 11.1 KB
/
SearchMetadataToolTest.java
File metadata and controls
265 lines (214 loc) · 11.1 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
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.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.ws.rs.core.Response;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openmetadata.schema.entity.teams.User;
import org.openmetadata.schema.search.SearchRequest;
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.service.Entity;
import org.openmetadata.service.search.SearchRepository;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.auth.CatalogSecurityContext;
import org.openmetadata.service.security.policyevaluator.SubjectCache;
import org.openmetadata.service.security.policyevaluator.SubjectContext;
/**
* Unit tests for SearchMetadataTool.
*
* <p>Tests verify the correct behavior of SearchMetadataTool including:
* - Index name resolution with cluster aliases
* - Entity type-specific searches
* - Default dataAsset index usage when no entity type is specified
* - Response formatting and structure
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class SearchMetadataToolTest {
private SearchMetadataTool searchMetadataTool;
private Authorizer authorizer;
private CatalogSecurityContext securityContext;
private SearchRepository searchRepository;
private User mockUser;
@BeforeEach
void setUp() {
searchMetadataTool = new SearchMetadataTool();
authorizer = mock(Authorizer.class);
securityContext = mock(CatalogSecurityContext.class);
searchRepository = mock(SearchRepository.class);
Principal mockPrincipal = mock(Principal.class);
when(mockPrincipal.getName()).thenReturn("test-user");
when(securityContext.getUserPrincipal()).thenReturn(mockPrincipal);
mockUser = new User();
mockUser.setId(UUID.randomUUID());
mockUser.setName("test-user");
mockUser.setIsAdmin(false);
mockUser.setIsBot(false);
Entity.setSearchRepository(searchRepository);
}
@Test
void testSearchWithClusterAlias() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", 10);
when(searchRepository.getIndexOrAliasName("dataAsset")).thenReturn("openmetadata_dataAsset");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.search(any(), any(SubjectContext.class))).thenReturn(mockResponse);
Map<String, Object> result = searchMetadataTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
assertNotNull(result.get("results"));
}
}
@Test
void testSearchWithoutClusterAlias() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("size", 10);
when(searchRepository.getIndexOrAliasName("dataAsset")).thenReturn("dataAsset");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.search(any(), any(SubjectContext.class))).thenReturn(mockResponse);
Map<String, Object> result = searchMetadataTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
assertNotNull(result.get("results"));
}
}
@Test
void testSearchWithSpecificEntityType() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("entityType", "dashboard");
params.put("size", 10);
when(searchRepository.getIndexOrAliasName("dashboard")).thenReturn("openmetadata_dashboard");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.search(any(), any(SubjectContext.class))).thenReturn(mockResponse);
Map<String, Object> result = searchMetadataTool.execute(authorizer, securityContext, params);
assertNotNull(result);
assertEquals(0, result.get("totalFound"));
assertEquals(0, result.get("returnedCount"));
assertNotNull(result.get("results"));
}
}
@Test
void testEntityTypeIsAlwaysAppliedAsExplicitFilter() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
params.put("entityType", "metric");
when(searchRepository.getIndexOrAliasName("metric")).thenReturn("metric");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.search(any(), any(SubjectContext.class))).thenReturn(mockResponse);
searchMetadataTool.execute(authorizer, securityContext, params);
ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
verify(searchRepository).search(captor.capture(), any(SubjectContext.class));
SearchRequest sent = captor.getValue();
assertEquals("test", sent.getQuery());
assertNotNull(sent.getQueryFilter());
JsonNode filter = JsonUtils.readTree(sent.getQueryFilter());
assertEquals("metric", filter.at("/query/bool/filter/0/term/entityType").asText());
}
}
@Test
void testNoEntityTypeLeavesQueryFilterEmpty() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("query", "test");
when(searchRepository.getIndexOrAliasName("dataAsset")).thenReturn("dataAsset");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.search(any(), any(SubjectContext.class))).thenReturn(mockResponse);
searchMetadataTool.execute(authorizer, securityContext, params);
ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
verify(searchRepository).search(captor.capture(), any(SubjectContext.class));
SearchRequest sent = captor.getValue();
assertEquals("test", sent.getQuery());
assertNull(sent.getQueryFilter());
}
}
@Test
void testAddEntityTypeFilterWithoutExistingFilter() throws Exception {
String result = SearchMetadataTool.addEntityTypeFilter(null, "metric");
assertNotNull(result);
JsonNode root = JsonUtils.readTree(result);
assertEquals("metric", root.at("/query/bool/filter/0/term/entityType").asText());
}
@Test
void testAddEntityTypeFilterWrapsExistingBoolQuery() throws Exception {
String existing = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"owners.name\":\"team\"}}]}}}";
String result = SearchMetadataTool.addEntityTypeFilter(existing, "topic");
JsonNode root = JsonUtils.readTree(result);
assertEquals("topic", root.at("/query/bool/filter/0/term/entityType").asText());
assertEquals("team", root.at("/query/bool/must/0/term/owners.name").asText());
}
@Test
void testAddEntityTypeFilterWrapsNonBoolQuery() throws Exception {
String existing = "{\"query\":{\"term\":{\"owners.name\":\"team\"}}}";
String result = SearchMetadataTool.addEntityTypeFilter(existing, "pipeline");
JsonNode root = JsonUtils.readTree(result);
assertEquals("pipeline", root.at("/query/bool/filter/0/term/entityType").asText());
assertEquals("team", root.at("/query/bool/must/0/term/owners.name").asText());
}
@Test
void testAddEntityTypeFilterIsNoopWhenEntityTypeMissing() {
String existing = "{\"query\":{\"term\":{\"owners.name\":\"team\"}}}";
assertEquals(existing, SearchMetadataTool.addEntityTypeFilter(existing, null));
assertEquals(existing, SearchMetadataTool.addEntityTypeFilter(existing, ""));
assertEquals(existing, SearchMetadataTool.addEntityTypeFilter(existing, " "));
assertNull(SearchMetadataTool.addEntityTypeFilter(null, null));
}
@Test
void testEntityTypeFilterMergesWithUserQueryFilter() throws Exception {
try (MockedStatic<SubjectCache> subjectCacheMock = mockStatic(SubjectCache.class)) {
subjectCacheMock.when(() -> SubjectCache.getUserContext("test-user")).thenReturn(mockUser);
Map<String, Object> params = new HashMap<>();
params.put("entityType", "metric");
params.put(
"queryFilter",
"{\"query\":{\"bool\":{\"must\":[{\"term\":{\"owners.name\":\"finance\"}}]}}}");
when(searchRepository.getIndexOrAliasName("metric")).thenReturn("metric");
Response mockResponse = mock(Response.class);
when(mockResponse.getEntity()).thenReturn("{\"hits\":{\"hits\":[],\"total\":{\"value\":0}}}");
when(searchRepository.searchWithDirectQuery(any(), any(SubjectContext.class)))
.thenReturn(mockResponse);
searchMetadataTool.execute(authorizer, securityContext, params);
ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
verify(searchRepository).searchWithDirectQuery(captor.capture(), any(SubjectContext.class));
SearchRequest sent = captor.getValue();
JsonNode filter = JsonUtils.readTree(sent.getQueryFilter());
assertEquals("metric", filter.at("/query/bool/filter/0/term/entityType").asText());
assertEquals("finance", filter.at("/query/bool/must/0/term/owners.name").asText());
}
}
}