forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUtils.java
409 lines (340 loc) · 13 KB
/
TestUtils.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
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.legacy;
import static com.google.common.base.Strings.isNullOrEmpty;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.json.JSONObject;
import org.opensearch.action.bulk.BulkRequest;
import org.opensearch.action.bulk.BulkResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.client.Client;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.common.xcontent.XContentType;
public class TestUtils {
private final static String MAPPING_FILE_PATH = "src/test/resources/indexDefinitions/";
/**
* Create test index by REST client.
*
* @param client client connection
* @param indexName test index name
* @param mapping test index mapping or null if no predefined mapping
*/
public static void createIndexByRestClient(RestClient client, String indexName, String mapping) {
Request request = new Request("PUT", "/" + indexName);
if (!isNullOrEmpty(mapping)) {
request.setJsonEntity(mapping);
}
performRequest(client, request);
}
/**
* https://github.com/elastic/elasticsearch/pull/49959
* Deprecate creation of dot-prefixed index names except for hidden and system indices.
* Create hidden index by REST client.
*
* @param client client connection
* @param indexName test index name
* @param mapping test index mapping or null if no predefined mapping
*/
public static void createHiddenIndexByRestClient(RestClient client, String indexName,
String mapping) {
Request request = new Request("PUT", "/" + indexName);
JSONObject jsonObject = isNullOrEmpty(mapping) ? new JSONObject() : new JSONObject(mapping);
jsonObject.put("settings", new JSONObject("{\"index\":{\"hidden\":true}}"));
request.setJsonEntity(jsonObject.toString());
performRequest(client, request);
}
/**
* Check if index already exists by OpenSearch index exists API which returns:
* 200 - specified indices or aliases exist
* 404 - one or more indices specified or aliases do not exist
*
* @param client client connection
* @param indexName index name
* @return true for index exist
*/
public static boolean isIndexExist(RestClient client, String indexName) {
try {
Response response = client.performRequest(new Request("HEAD", "/" + indexName));
return (response.getStatusLine().getStatusCode() == 200);
} catch (IOException e) {
throw new IllegalStateException("Failed to perform request", e);
}
}
/**
* Load test data set by REST client.
*
* @param client client connection
* @param indexName index name
* @param dataSetFilePath file path of test data set
* @throws IOException
*/
public static void loadDataByRestClient(RestClient client, String indexName,
String dataSetFilePath) throws IOException {
Path path = Paths.get(getResourceFilePath(dataSetFilePath));
Request request = new Request("POST", "/" + indexName + "/_bulk?refresh=true");
request.setJsonEntity(new String(Files.readAllBytes(path)));
performRequest(client, request);
}
/**
* Perform a request by REST client.
*
* @param client client connection
* @param request request object
*/
public static Response performRequest(RestClient client, Request request) {
try {
Response response = client.performRequest(request);
int status = response.getStatusLine().getStatusCode();
if (status >= 400) {
throw new IllegalStateException("Failed to perform request. Error code: " + status);
}
return response;
} catch (IOException e) {
throw new IllegalStateException("Failed to perform request", e);
}
}
public static String getAccountIndexMapping() {
String mappingFile = "account_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getPhraseIndexMapping() {
String mappingFile = "phrase_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDogIndexMapping() {
String mappingFile = "dog_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDogs2IndexMapping() {
String mappingFile = "dog2_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDogs3IndexMapping() {
String mappingFile = "dog3_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getPeople2IndexMapping() {
String mappingFile = "people2_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getGameOfThronesIndexMapping() {
String mappingFile = "game_of_thrones_index_mapping.json";
return getMappingFile(mappingFile);
}
// System
public static String getOdbcIndexMapping() {
String mappingFile = "odbc_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getLocationIndexMapping() {
String mappingFile = "location_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getEmployeeNestedTypeIndexMapping() {
String mappingFile = "employee_nested_type_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getNestedTypeIndexMapping() {
String mappingFile = "nested_type_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getJoinTypeIndexMapping() {
String mappingFile = "join_type_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getBankIndexMapping() {
String mappingFile = "bank_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getBankWithNullValuesIndexMapping() {
String mappingFile = "bank_with_null_values_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getStringIndexMapping() {
String mappingFile = "string_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getOrderIndexMapping() {
String mappingFile = "order_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getWeblogsIndexMapping() {
String mappingFile = "weblogs_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDateIndexMapping() {
String mappingFile = "date_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDateTimeIndexMapping() {
String mappingFile = "date_time_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getNestedSimpleIndexMapping() {
String mappingFile = "nested_simple_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDeepNestedIndexMapping() {
String mappingFile = "deep_nested_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDataTypeNumericIndexMapping() {
String mappingFile = "datatypes_numeric_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDataTypeNonnumericIndexMapping() {
String mappingFile = "datatypes_index_mapping.json";
return getMappingFile(mappingFile);
}
public static String getDataTextKeywordIndexMapping() {
String mappingFile = "text_keyword_index_mapping.json";
return getMappingFile(mappingFile);
}
public static void loadBulk(Client client, String jsonPath, String defaultIndex)
throws Exception {
System.out.println(String.format("Loading file %s into opensearch cluster", jsonPath));
String absJsonPath = getResourceFilePath(jsonPath);
BulkRequest bulkRequest = new BulkRequest();
try (final InputStream stream = new FileInputStream(absJsonPath);
final Reader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
final BufferedReader br = new BufferedReader(streamReader)) {
while (true) {
String actionLine = br.readLine();
if (actionLine == null || actionLine.trim().isEmpty()) {
break;
}
String sourceLine = br.readLine();
JSONObject actionJson = new JSONObject(actionLine);
IndexRequest indexRequest = new IndexRequest();
indexRequest.index(defaultIndex);
if (actionJson.getJSONObject("index").has("_id")) {
String docId = actionJson.getJSONObject("index").getString("_id");
indexRequest.id(docId);
}
if (actionJson.getJSONObject("index").has("_routing")) {
String routing = actionJson.getJSONObject("index").getString("_routing");
indexRequest.routing(routing);
}
indexRequest.source(sourceLine, XContentType.JSON);
bulkRequest.add(indexRequest);
}
}
BulkResponse bulkResponse = client.bulk(bulkRequest).actionGet();
if (bulkResponse.hasFailures()) {
throw new Exception("Failed to load test data into index " + defaultIndex + ", " +
bulkResponse.buildFailureMessage());
}
System.out.println(bulkResponse.getItems().length + " documents loaded.");
// ensure the documents are searchable
client.admin().indices().prepareRefresh(defaultIndex).execute().actionGet();
}
public static String getResourceFilePath(String relPath) {
String projectRoot = System.getProperty("project.root", null);
if (projectRoot == null) {
return new File(relPath).getAbsolutePath();
} else {
return new File(projectRoot + "/" + relPath).getAbsolutePath();
}
}
public static String getResponseBody(Response response) throws IOException {
return getResponseBody(response, false);
}
public static String getResponseBody(Response response, boolean retainNewLines)
throws IOException {
final StringBuilder sb = new StringBuilder();
try (final InputStream is = response.getEntity().getContent();
final BufferedReader br = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
if (retainNewLines) {
sb.append(String.format(Locale.ROOT, "%n"));
}
}
}
return sb.toString();
}
public static String fileToString(final String filePathFromProjectRoot,
final boolean removeNewLines)
throws IOException {
final String absolutePath = getResourceFilePath(filePathFromProjectRoot);
try (final InputStream stream = new FileInputStream(absolutePath);
final Reader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
final BufferedReader br = new BufferedReader(streamReader)) {
final StringBuilder stringBuilder = new StringBuilder();
String line = br.readLine();
while (line != null) {
stringBuilder.append(line);
if (!removeNewLines) {
stringBuilder.append(String.format(Locale.ROOT, "%n"));
}
line = br.readLine();
}
return stringBuilder.toString();
}
}
/**
* Builds all permutations of the given list of Strings
*
* @param items list of strings to permute
* @return list of permutations
*/
public static List<List<String>> getPermutations(final List<String> items) {
if (items.size() > 5) {
throw new IllegalArgumentException("Inefficient test, please refactor");
}
final List<List<String>> result = new LinkedList<>();
if (items.isEmpty() || 1 == items.size()) {
final List<String> onlyElement = new ArrayList<>();
if (1 == items.size()) {
onlyElement.add(items.get(0));
}
result.add(onlyElement);
return result;
}
for (int i = 0; i < items.size(); ++i) {
final List<String> smallerSet = new ArrayList<>();
if (i != 0) {
smallerSet.addAll(items.subList(0, i));
}
if (i != items.size() - 1) {
smallerSet.addAll(items.subList(i + 1, items.size()));
}
final String currentItem = items.get(i);
result.addAll(getPermutations(smallerSet).stream().map(smallerSetPermutation -> {
final List<String> permutation = new ArrayList<>();
permutation.add(currentItem);
permutation.addAll(smallerSetPermutation);
return permutation;
}).collect(Collectors.toCollection(LinkedList::new)));
}
return result;
}
public static String getMappingFile(String fileName) {
try {
return fileToString(MAPPING_FILE_PATH + fileName, false);
} catch (IOException e) {
return null;
}
}
}