forked from meilisearch/meilisearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.java
More file actions
400 lines (333 loc) · 15.8 KB
/
ClientTest.java
File metadata and controls
400 lines (333 loc) · 15.8 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
package com.meilisearch.integration;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.gson.*;
import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.ExportIndexFilter;
import com.meilisearch.sdk.ExportRequest;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.utils.Movie;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.*;
@Tag("integration")
public class ClientTest extends AbstractIT {
String primaryKey = "id";
private TestData<Movie> testData;
@BeforeEach
public void initialize() {
setUp();
setUpJacksonClient();
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
cleanup();
}
@AfterAll
static void cleanMeilisearch() {
cleanup();
}
/** Test Index creation without PrimaryKey */
@Test
public void testCreateIndexWithoutPrimaryKey() throws Exception {
String indexUid = "CreateIndexWithoutPrimaryKey";
Index index = createEmptyIndex(indexUid);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(nullValue()));
}
/** Test Index creation without PrimaryKey with Jackson Json Handler */
@Test
public void testCreateIndexWithoutPrimaryKeyWithJacksonJsonHandler() throws Exception {
String indexUid = "CreateIndexWithoutPrimaryKeyWithJacksonJsonHandler";
TaskInfo task = clientJackson.createIndex(indexUid);
clientJackson.waitForTask(task.getTaskUid());
Index index = clientJackson.getIndex(indexUid);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(nullValue()));
}
/** Test Index creation with PrimaryKey */
@Test
public void testCreateIndexWithPrimaryKey() throws Exception {
String indexUid = "CreateIndexWithPrimaryKey";
Index index = createEmptyIndex(indexUid, this.primaryKey);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(equalTo(this.primaryKey)));
}
/** Test Index creation with PrimaryKey with Jackson Json Handler */
@Test
public void testCreateIndexWithPrimaryKeyWithJacksonJsonHandler() throws Exception {
String indexUid = "CreateIndexWithPrimaryKeyWithJacksonJsonHandler";
TaskInfo task = clientJackson.createIndex(indexUid, this.primaryKey);
clientJackson.waitForTask(task.getTaskUid());
Index index = clientJackson.getIndex(indexUid);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(equalTo(this.primaryKey)));
}
/** Test Index creation twice doesn't throw an error: already exists */
@Test
public void testCreateIndexAlreadyExists() throws Exception {
String indexUid = "CreateIndexAlreadyExists";
Index index = createEmptyIndex(indexUid, this.primaryKey);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(equalTo(this.primaryKey)));
Index indexDuplicate = createEmptyIndex(indexUid, this.primaryKey);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(indexDuplicate.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(equalTo(this.primaryKey)));
assertThat(indexDuplicate.getPrimaryKey(), is(equalTo(this.primaryKey)));
}
/** Test update Index PrimaryKey */
@Test
public void testUpdateIndexPrimaryKey() throws Exception {
String indexUid = "UpdateIndexPrimaryKey";
Index index = createEmptyIndex(indexUid);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(nullValue()));
TaskInfo task = client.updateIndex(indexUid, this.primaryKey);
client.waitForTask(task.getTaskUid());
index = client.getIndex(indexUid);
assertThat(index, instanceOf(Index.class));
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThat(index.getPrimaryKey(), is(equalTo(this.primaryKey)));
}
/** Test getIndex */
@Test
public void testGetIndex() throws Exception {
String indexUid = "GetIndex";
Index index = createEmptyIndex(indexUid);
Index getIndex = client.getIndex(indexUid);
assertThat(getIndex.getUid(), is(equalTo(index.getUid())));
assertThat(getIndex.getPrimaryKey(), is(equalTo(index.getPrimaryKey())));
}
/** Test getIndexes */
@Test
public void testGetIndexes() throws Exception {
String[] indexUids = {"GetIndexes", "GetIndexes2"};
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Results<Index> indexes = client.getIndexes();
assertThat(indexes.getResults(), is(arrayWithSize(2)));
}
/** Test getIndexes with limit */
@Test
public void testGetIndexesLimit() throws Exception {
int limit = 1;
String[] indexUids = {"GetIndexesLimit", "GetIndexesLimit2"};
IndexesQuery query = new IndexesQuery().setLimit(limit);
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Results<Index> indexes = client.getIndexes(query);
assertThat(indexes.getResults(), is(arrayWithSize(limit)));
assertThat(indexes.getLimit(), is(equalTo(limit)));
}
/** Test getIndexes with limit and offset */
@Test
public void testGetIndexesLimitAndOffset() throws Exception {
int limit = 1;
int offset = 1;
String[] indexUids = {"GetIndexesLimitOffset", "GetIndexesLimitOffset2"};
IndexesQuery query = new IndexesQuery().setLimit(limit).setOffset(offset);
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
Results<Index> indexes = client.getIndexes(query);
assertThat(indexes.getResults(), is(arrayWithSize(limit)));
assertThat(indexes.getLimit(), is(equalTo(limit)));
assertThat(indexes.getOffset(), is(equalTo(offset)));
}
/** Test getRawIndexes */
@Test
public void testGetRawIndexes() throws Exception {
String[] indexUids = {"GetRawIndexes", "GetRawIndexes2"};
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
String indexes = client.getRawIndexes();
JsonObject jsonIndexObject = JsonParser.parseString(indexes).getAsJsonObject();
JsonArray jsonIndexArray = jsonIndexObject.getAsJsonArray("results");
assertThat(jsonIndexArray.size(), is(equalTo(2)));
assertThat(
jsonIndexArray.get(0).getAsJsonObject().get("uid").getAsString(),
is(in(indexUids)));
assertThat(
jsonIndexArray.get(1).getAsJsonObject().get("uid").getAsString(),
is(in(indexUids)));
}
/** Test getRawIndexes with limits */
@Test
public void testGetRawIndexesLimit() throws Exception {
int limit = 1;
String[] indexUids = {"GetRawIndexes", "GetRawIndexes2"};
createEmptyIndex(indexUids[0]);
createEmptyIndex(indexUids[1], this.primaryKey);
IndexesQuery query = new IndexesQuery().setLimit(limit);
String indexes = client.getRawIndexes(query);
JsonObject jsonIndexObject = JsonParser.parseString(indexes).getAsJsonObject();
JsonArray jsonIndexArray = jsonIndexObject.getAsJsonArray("results");
assertThat(jsonIndexArray.size(), is(equalTo(limit)));
assertThat(jsonIndexObject.get("limit").getAsInt(), is(equalTo(limit)));
assertThat(
jsonIndexArray.get(0).getAsJsonObject().get("uid").getAsString(),
is(in(indexUids)));
}
/** Test deleteIndex */
@Test
public void testDeleteIndex() throws Exception {
String indexUid = "DeleteIndex";
Index index = createEmptyIndex(indexUid);
TaskInfo task = client.deleteIndex(index.getUid());
client.waitForTask(task.getTaskUid());
assertThrows(MeilisearchApiException.class, () -> client.getIndex(indexUid));
}
/** Test swapIndexes */
@Test
public void testSwapIndexes() throws Exception {
String indexUidA = "IndexA";
String indexUidB = "IndexB";
Index indexA = createEmptyIndex(indexUidA);
Index indexB = createEmptyIndex(indexUidB);
TaskInfo taskAddDocumentIndexA =
indexA.addDocuments(
"[{"
+ "\"id\": 1,"
+ "\"title\": \"Document1\""
+ "},"
+ "{"
+ "\"id\": 2,"
+ "\"title\": \"Document2\""
+ "}]");
indexA.waitForTask(taskAddDocumentIndexA.getTaskUid());
SwapIndexesParams[] params =
new SwapIndexesParams[] {
new SwapIndexesParams().setIndexes(new String[] {indexUidA, indexUidB})
};
TaskInfo task = client.swapIndexes(params);
client.waitForTask(task.getTaskUid());
assertThat(task.getType(), is(equalTo("indexSwap")));
assertThat(indexB.getDocument("1", Movie.class).getTitle(), is(equalTo("Document1")));
assertThat(indexB.getDocument("2", Movie.class).getTitle(), is(equalTo("Document2")));
assertThrows(MeilisearchApiException.class, () -> indexA.getDocument("1", Movie.class));
assertThrows(MeilisearchApiException.class, () -> indexA.getDocument("2", Movie.class));
}
/** Test call to index method with an inexistent index */
@Test
public void testIndexMethodCallInexistentIndex() throws Exception {
String indexUid = "IndexMethodCallInexistentIndex";
Index index = client.index(indexUid);
assertThat(index.getUid(), is(equalTo(indexUid)));
assertThrows(MeilisearchApiException.class, () -> client.getIndex(indexUid));
}
/** Test call to index method with an existing index */
@Test
public void testIndexMethodCallExistingIndex() throws Exception {
String indexUid = "IndexMethodCallExistingIndex";
Index createdIndex = createEmptyIndex(indexUid);
Index index = client.index(indexUid);
assertThat(index.getUid(), is(equalTo(createdIndex.getUid())));
assertThat(index.getPrimaryKey(), is(nullValue()));
}
/** Test call to index method with an existing index with primary key */
@Test
public void testIndexMethodCallExistingIndexWithPrimaryKey() throws Exception {
String indexUid = "IndexMethodCallExistingIndexWithPrimaryKey";
String primaryKey = "PrimaryKey";
Index createdIndex = createEmptyIndex(indexUid, primaryKey);
Index index = client.index(indexUid);
assertThat(index.getUid(), is(equalTo(createdIndex.getUid())));
assertThat(index.getPrimaryKey(), is(nullValue()));
index.fetchPrimaryKey();
assertThat(index.getPrimaryKey(), is(equalTo(primaryKey)));
}
/** Test call to create dump */
@Test
public void testCreateDump() throws Exception {
TaskInfo task = client.createDump();
client.waitForTask(task.getTaskUid());
Task dump = client.getTask(task.getTaskUid());
assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
assertThat(dump.getType(), is(equalTo("dumpCreation")));
}
/** Test call to create snapshot */
@Test
public void testCreateSnapshot() throws Exception {
TaskInfo task = client.createSnapshot();
client.waitForTask(task.getTaskUid());
Task snapshot = client.getTask(task.getTaskUid());
assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
assertThat(snapshot.getType(), is(equalTo("snapshotCreation")));
}
/** Test call to initiate export */
@Test
public void testExport() throws Exception {
Map<String, ExportIndexFilter> indexes = new HashMap<>();
indexes.put("*", ExportIndexFilter.builder().filter("genres = action").build());
ExportRequest payload =
ExportRequest.builder().url(getMeilisearchHost()).indexes(indexes).build();
TaskInfo task = client.export(payload);
client.waitForTask(task.getTaskUid());
Task exportTask = client.getTask(task.getTaskUid());
assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
assertThat(exportTask.getType(), is(equalTo("export")));
}
/**
* Test the exclusion of transient fields.
*
* @see <a href="https://github.com/meilisearch/meilisearch-java/issues/655">Issue #655</a>
*/
@Test
public void testTransientFieldExclusion() throws MeilisearchException {
Index test = client.index("Transient");
Gson gsonWithTransient =
new GsonBuilder().excludeFieldsWithModifiers(Modifier.STATIC).create();
Assertions.assertThrows(
InaccessibleObjectException.class, () -> gsonWithTransient.toJson(test));
Assertions.assertDoesNotThrow(() -> gson.toJson(test));
}
@Test
public void testCompactWithDocuments() throws Exception {
String indexUid = "testCompactWithDocuments";
Index index = createEmptyIndex(indexUid, this.primaryKey);
TaskInfo addTask =
index.addDocuments(
"[{"
+ "\"id\": 1,"
+ "\"title\": \"Document1\","
+ "\"description\": \"Test document 1\""
+ "},"
+ "{"
+ "\"id\": 2,"
+ "\"title\": \"Document2\","
+ "\"description\": \"Test document 2\""
+ "}]");
index.waitForTask(addTask.getTaskUid());
TaskInfo compactTask = index.compact();
client.waitForTask(compactTask.getTaskUid());
Task completedCompactTask = client.getTask(compactTask.getTaskUid());
assertThat(compactTask.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
assertThat(completedCompactTask.getType(), is(equalTo("indexCompaction")));
assertThat(completedCompactTask.getStatus(), is(equalTo(TaskStatus.SUCCEEDED)));
assertThat(index.getDocument("1", Movie.class).getTitle(), is(equalTo("Document1")));
assertThat(index.getDocument("2", Movie.class).getTitle(), is(equalTo("Document2")));
}
/** Test getTaskDocuments */
@Test
public void testGetTaskDocuments() throws Exception {
String indexUid = "GetTaskDocuments";
Index index = createEmptyIndex(indexUid, this.primaryKey);
TaskInfo addTask =
index.addDocuments("[{" + "\"id\": 1," + "\"title\": \"Document1\"" + "}]");
index.waitForTask(addTask.getTaskUid());
String taskDocuments = client.getTaskDocuments(addTask.getTaskUid());
assertThat(taskDocuments, is(notNullValue()));
String[] lines = taskDocuments.split("\n");
assertThat(lines.length, is(greaterThan(0)));
JsonObject firstDocument = JsonParser.parseString(lines[0]).getAsJsonObject();
assertThat(firstDocument.has("id"), is(true));
assertThat(firstDocument.has("title"), is(true));
assertThat(firstDocument.get("id").getAsInt(), is(equalTo(1)));
assertThat(firstDocument.get("title").getAsString(), is(equalTo("Document1")));
}
}