forked from opensearch-project/skills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractRetrieverToolTests.java
246 lines (210 loc) · 10.8 KB
/
AbstractRetrieverToolTests.java
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.agent.tools;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.opensearch.agent.tools.AbstractRetrieverTool.DEFAULT_DESCRIPTION;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.ml.common.spi.tools.Tool;
import org.opensearch.search.SearchModule;
import org.opensearch.transport.client.Client;
import lombok.SneakyThrows;
public class AbstractRetrieverToolTests {
static public final String TEST_QUERY = "{\"query\":{\"match_all\":{}}}";
static public final String TEST_INDEX = "test index";
static public final String[] TEST_SOURCE_FIELDS = new String[] { "test 1", "test 2" };
static public final Integer TEST_DOC_SIZE = 3;
static public final NamedXContentRegistry TEST_XCONTENT_REGISTRY_FOR_QUERY = new NamedXContentRegistry(
new SearchModule(Settings.EMPTY, List.of()).getNamedXContents()
);
private String mockedSearchResponseString;
private String mockedEmptySearchResponseString;
private AbstractRetrieverTool mockedImpl;
@Before
@SneakyThrows
public void setup() {
try (InputStream searchResponseIns = AbstractRetrieverTool.class.getResourceAsStream("retrieval_tool_search_response.json")) {
if (searchResponseIns != null) {
mockedSearchResponseString = new String(searchResponseIns.readAllBytes(), StandardCharsets.UTF_8);
}
}
try (InputStream searchResponseIns = AbstractRetrieverTool.class.getResourceAsStream("retrieval_tool_empty_search_response.json")) {
if (searchResponseIns != null) {
mockedEmptySearchResponseString = new String(searchResponseIns.readAllBytes(), StandardCharsets.UTF_8);
}
}
mockedImpl = Mockito
.mock(
AbstractRetrieverTool.class,
Mockito
.withSettings()
.useConstructor(null, TEST_XCONTENT_REGISTRY_FOR_QUERY, TEST_INDEX, TEST_SOURCE_FIELDS, TEST_DOC_SIZE)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
);
when(mockedImpl.getQueryBody(any(String.class))).thenReturn(TEST_QUERY);
}
@Test
@SneakyThrows
public void testRunAsyncWithSearchResults() {
Client client = mock(Client.class);
SearchResponse mockedSearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedSearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedSearchResponse);
return null;
}).when(client).search(any(), any());
mockedImpl.setClient(client);
final CompletableFuture<String> future = new CompletableFuture<>();
ActionListener<String> listener = ActionListener.wrap(r -> { future.complete(r); }, e -> { future.completeExceptionally(e); });
mockedImpl.run(Map.of(AbstractRetrieverTool.INPUT_FIELD, "hello world"), listener);
future.join();
assertEquals(
"{\"_index\":\"hybrid-index\",\"_source\":{\"passage_text\":\"Company test_mock have a history of 100 years.\"},\"_id\":\"1\",\"_score\":89.2917}\n"
+ "{\"_index\":\"hybrid-index\",\"_source\":{\"passage_text\":\"the price of the api is 2$ per invocation\"},\"_id\":\"2\",\"_score\":0.10702579}\n",
future.get()
);
}
@Test
@SneakyThrows
public void testRunAsyncWithEmptySearchResponse() {
Client client = mock(Client.class);
SearchResponse mockedEmptySearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedEmptySearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedEmptySearchResponse);
return null;
}).when(client).search(any(), any());
mockedImpl.setClient(client);
final CompletableFuture<String> future = new CompletableFuture<>();
ActionListener<String> listener = ActionListener.wrap(r -> { future.complete(r); }, e -> { future.completeExceptionally(e); });
mockedImpl.run(Map.of(AbstractRetrieverTool.INPUT_FIELD, "hello world"), listener);
future.join();
assertEquals("Can not get any match from search result.", future.get());
}
@Test
@SneakyThrows
public void testRunAsyncWithIllegalQueryThenListenerOnFailure() {
Client client = mock(Client.class);
mockedImpl.setClient(client);
final CompletableFuture<String> future1 = new CompletableFuture<>();
ActionListener<String> listener1 = ActionListener.wrap(future1::complete, future1::completeExceptionally);
mockedImpl.run(Map.of(AbstractRetrieverTool.INPUT_FIELD, ""), listener1);
Exception exception1 = assertThrows(Exception.class, future1::join);
assertTrue(exception1.getCause() instanceof IllegalArgumentException);
assertEquals(exception1.getCause().getMessage(), "[input] is null or empty, can not process it.");
final CompletableFuture<String> future2 = new CompletableFuture<>();
ActionListener<String> listener2 = ActionListener.wrap(future2::complete, future2::completeExceptionally);
mockedImpl.run(Map.of(AbstractRetrieverTool.INPUT_FIELD, " "), listener2);
Exception exception2 = assertThrows(Exception.class, future2::join);
assertTrue(exception2.getCause() instanceof IllegalArgumentException);
assertEquals(exception2.getCause().getMessage(), "[input] is null or empty, can not process it.");
final CompletableFuture<String> future3 = new CompletableFuture<>();
ActionListener<String> listener3 = ActionListener.wrap(future3::complete, future3::completeExceptionally);
mockedImpl.run(Map.of("test", "hello world"), listener3);
Exception exception3 = assertThrows(Exception.class, future3::join);
assertTrue(exception3.getCause() instanceof IllegalArgumentException);
assertEquals(exception3.getCause().getMessage(), "[input] is null or empty, can not process it.");
final CompletableFuture<String> future4 = new CompletableFuture<>();
ActionListener<String> listener4 = ActionListener.wrap(future4::complete, future4::completeExceptionally);
mockedImpl.run(null, listener4);
Exception exception4 = assertThrows(Exception.class, future4::join);
assertTrue(exception4.getCause() instanceof NullPointerException);
}
@Test
@SneakyThrows
public void testValidate() {
assertTrue(mockedImpl.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, "hi")));
assertFalse(mockedImpl.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, "")));
assertFalse(mockedImpl.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, " ")));
assertFalse(mockedImpl.validate(Map.of("test", " ")));
assertFalse(mockedImpl.validate(new HashMap<>()));
assertFalse(mockedImpl.validate(null));
}
@Test
public void testGetAttributes() {
assertEquals(mockedImpl.getVersion(), null);
assertEquals(mockedImpl.getIndex(), TEST_INDEX);
assertEquals(mockedImpl.getDocSize(), TEST_DOC_SIZE);
assertEquals(mockedImpl.getSourceFields(), TEST_SOURCE_FIELDS);
assertEquals(mockedImpl.getQueryBody(TEST_QUERY), TEST_QUERY);
}
@Test
public void testGetQueryBodySuccess() {
assertEquals(mockedImpl.getQueryBody(TEST_QUERY), TEST_QUERY);
}
@Test
@SneakyThrows
public void testRunWithRuntimeException() {
Client client = mock(Client.class);
mockedImpl.setClient(client);
ActionListener listener = mock(ActionListener.class);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> actionListener = invocation.getArgument(1);
actionListener.onFailure(new RuntimeException("Failed to search index"));
return null;
}).when(client).search(any(), any());
mockedImpl.run(Map.of(AbstractRetrieverTool.INPUT_FIELD, "hello world"), listener);
verify(listener).onFailure(any(RuntimeException.class));
ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
verify(listener).onFailure(argumentCaptor.capture());
assertEquals("Failed to search index", argumentCaptor.getValue().getMessage());
}
@Test
public void testFactory() {
// Create a mock object of the abstract Factory class
Client client = mock(Client.class);
AbstractRetrieverTool.Factory<Tool> factoryMock = new AbstractRetrieverTool.Factory<>() {
public AbstractRetrieverTool create(Map<String, Object> params) {
return null;
}
@Override
public String getDefaultType() {
return null;
}
@Override
public String getDefaultVersion() {
return null;
}
};
factoryMock.init(client, TEST_XCONTENT_REGISTRY_FOR_QUERY);
assertNotNull(factoryMock.client);
assertNotNull(factoryMock.xContentRegistry);
assertEquals(client, factoryMock.client);
assertEquals(TEST_XCONTENT_REGISTRY_FOR_QUERY, factoryMock.xContentRegistry);
String defaultDescription = factoryMock.getDefaultDescription();
assertEquals(DEFAULT_DESCRIPTION, defaultDescription);
}
}