Skip to content

Commit e7a7579

Browse files
committed
Add addDocumentsTests
1 parent dab8ef9 commit e7a7579

File tree

4 files changed

+144
-36
lines changed

4 files changed

+144
-36
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies {
2222
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
2323
implementation 'com.google.guava:guava:29.0-jre'
2424
implementation 'com.google.code.gson:gson:2.8.6'
25+
implementation 'org.json:json:20200518'
2526
// Use JUnit test framework
2627
testImplementation(platform('org.junit:junit-bom:5.7.0'))
2728
testImplementation('org.junit.jupiter:junit-jupiter')

src/test/java/com/meilisearch/sdk/DeleteAllIndexes.java

Whitespace-only changes.
Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,118 @@
11
package com.meilisearch.sdk;
22

3+
import com.google.gson.Gson;
34

45
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.AfterAll;
57
import org.junit.jupiter.api.Test;
68

7-
public class DocumentsTest {
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
814

9-
Index meilisearchIndex;
10-
String indexUid = "movies";
15+
public class DocumentsTest {
16+
17+
Client ms;
18+
Index index;
19+
Gson gson = new Gson();
20+
21+
String primaryKey = "id";
1122

1223
@BeforeEach
13-
public void initialize() {
24+
public void initialize() throws Exception {
25+
ms = new Client(new Config("http://localhost:7700", "masterKey"));
26+
}
27+
28+
@AfterAll
29+
static void cleanMeiliSearch() throws Exception {
30+
Client ms = new Client(new Config("http://localhost:7700", "masterKey"));
31+
Index[] indexes = ms.getIndexList();
32+
for (int i = 0; i < indexes.length; i++) {
33+
ms.deleteIndex(indexes[i].uid);
34+
}
35+
}
36+
37+
/**
38+
* Test Add single document
39+
*/
40+
@Test
41+
public void testAddDocumentsSingle() throws Exception {
42+
43+
String indexUid = "DocumentsTests";
44+
ms.createIndex(indexUid);
45+
this.index = ms.getIndex(indexUid);
46+
47+
JSONArray jsonArray = new JSONArray();
48+
JSONObject jsonObject = new JSONObject();
49+
50+
jsonObject.put("id", "1111");
51+
jsonObject.put("title", "Alice in wonderland");
52+
jsonArray.put(jsonObject);
1453

54+
UpdateStatus updateInfo = this.gson.fromJson(
55+
index.addDocuments(jsonArray.toString()),
56+
UpdateStatus.class
57+
);
58+
59+
// TODO: Replace by WaitForPendingUpdate()
60+
String status = "";
61+
while (!status.equals("processed")){
62+
UpdateStatus updateStatus = this.gson.fromJson(
63+
index.getUpdate(updateInfo.getUpdateId()),
64+
UpdateStatus.class
65+
);
66+
status = updateStatus.getStatus();
67+
Thread.sleep(20);
68+
}
69+
assertEquals(index.getDocuments(), jsonArray.toString());
70+
}
71+
72+
/**
73+
* Test Add multiple documents
74+
*/
75+
@Test
76+
public void testAddDocumentsMultiple() throws Exception {
77+
78+
String indexUid = "DocumentsTests";
79+
ms.createIndex(indexUid);
80+
this.index = ms.getIndex(indexUid);
81+
82+
JSONArray jsonArray2 = new JSONArray();
83+
JSONObject jsonObject2 = new JSONObject();
84+
85+
jsonObject2.put("id", "1111");
86+
jsonObject2.put("title", "Alice in wonderland");
87+
jsonArray2.put(jsonObject2);
88+
89+
jsonObject2 = new JSONObject();
90+
jsonObject2.put("id", "222");
91+
jsonObject2.put("title", "Blice in wonderland");
92+
jsonArray2.put(jsonObject2);
93+
94+
jsonObject2 = new JSONObject();
95+
jsonObject2.put("id", "333");
96+
jsonObject2.put("title", "Clice in wonderland");
97+
jsonArray2.put(jsonObject2);
98+
99+
UpdateStatus updateInfo = this.gson.fromJson(
100+
index.addDocuments(jsonArray2.toString()),
101+
UpdateStatus.class
102+
);
103+
104+
// TODO: Replace by WaitForPendingUpdate()
105+
String status = "";
106+
while (!status.equals("processed")){
107+
UpdateStatus updateStatus = this.gson.fromJson(
108+
index.getUpdate(updateInfo.getUpdateId()),
109+
UpdateStatus.class
110+
);
111+
status = updateStatus.getStatus();
112+
Thread.sleep(20);
113+
}
114+
assertEquals(index.getDocuments(), jsonArray2.toString());
15115
}
16116

17117
}
118+
Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
/*
2-
* This Java source file was generated by the Gradle 'init' task.
3-
*/
41
package com.meilisearch.sdk;
52

63
import static org.junit.jupiter.api.Assertions.assertEquals;
74

5+
import java.util.Arrays;
86
import com.google.gson.Gson;
9-
107
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.AfterAll;
119
import org.junit.jupiter.api.Test;
1210

1311
public class IndexesTest {
1412

1513
Client ms;
1614
Gson gson = new Gson();
17-
18-
String indexUid = "movies";
19-
String indexUid2 = "movies2";
2015
String primaryKey = "id";
2116

2217
@BeforeEach
23-
public void initializeClient() {
18+
public void initializeClient() throws Exception {
2419
ms = new Client(new Config("http://localhost:7700", "masterKey"));
2520
}
2621

22+
@AfterAll
23+
static void cleanMeiliSearch() throws Exception {
24+
Client ms = new Client(new Config("http://localhost:7700", "masterKey"));
25+
Index[] indexes = ms.getIndexList();
26+
for (int i = 0; i < indexes.length; i++) {
27+
ms.deleteIndex(indexes[i].uid);
28+
}
29+
}
30+
2731
/**
2832
* Test Index creation without PrimaryKey
2933
*/
3034
@Test
3135
public void testCreateIndexWithoutPrimaryKey() throws Exception {
32-
ms.createIndex(this.indexUid);
33-
Index index = ms.getIndex(this.indexUid);
34-
assertEquals(index.uid, this.indexUid);
36+
String indexUid = "IndexesTest";
37+
ms.createIndex(indexUid);
38+
Index index = ms.getIndex(indexUid);
39+
assertEquals(index.uid, indexUid);
3540
assertEquals(index.primaryKey, null);
3641
ms.deleteIndex(index.uid);
3742
}
@@ -41,9 +46,10 @@ public void testCreateIndexWithoutPrimaryKey() throws Exception {
4146
*/
4247
@Test
4348
public void testCreateIndexWithPrimaryKey() throws Exception {
44-
ms.createIndex(this.indexUid, this.primaryKey);
45-
Index index = ms.getIndex(this.indexUid);
46-
assertEquals(index.uid, this.indexUid);
49+
String indexUid = "IndexesTest";
50+
ms.createIndex(indexUid, this.primaryKey);
51+
Index index = ms.getIndex(indexUid);
52+
assertEquals(index.uid, indexUid);
4753
assertEquals(index.primaryKey, this.primaryKey);
4854
ms.deleteIndex(index.uid);
4955
}
@@ -53,13 +59,14 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
5359
*/
5460
@Test
5561
public void testUpdateIndexPrimaryKey() throws Exception {
56-
ms.createIndex(this.indexUid);
57-
Index index = ms.getIndex(this.indexUid);
58-
assertEquals(index.uid, this.indexUid);
62+
String indexUid = "IndexesTest";
63+
ms.createIndex(indexUid);
64+
Index index = ms.getIndex(indexUid);
65+
assertEquals(index.uid, indexUid);
5966
assertEquals(index.primaryKey, null);
60-
ms.updateIndex(this.indexUid, this.primaryKey);
61-
index = ms.getIndex(this.indexUid);
62-
assertEquals(index.uid, this.indexUid);
67+
ms.updateIndex(indexUid, this.primaryKey);
68+
index = ms.getIndex(indexUid);
69+
assertEquals(index.uid, indexUid);
6370
assertEquals(index.primaryKey, this.primaryKey);
6471
ms.deleteIndex(index.uid);
6572
}
@@ -69,17 +76,16 @@ public void testUpdateIndexPrimaryKey() throws Exception {
6976
*/
7077
@Test
7178
public void testGetIndexList() throws Exception {
72-
ms.createIndex(this.indexUid);
73-
ms.createIndex(this.indexUid2, this.primaryKey);
74-
Index index1 = ms.getIndex(this.indexUid);
75-
Index index2 = ms.getIndex(this.indexUid2);
79+
String[] indexUids = {"IndexesTest", "IndexesTest2"};
80+
ms.createIndex(indexUids[0]);
81+
ms.createIndex(indexUids[1], this.primaryKey);
82+
Index index1 = ms.getIndex(indexUids[0]);
83+
Index index2 = ms.getIndex(indexUids[1]);
7684
Index[] indexes = ms.getIndexList();
77-
assertEquals(indexes[0].uid, this.indexUid);
78-
assertEquals(indexes[1].uid, this.indexUid2);
79-
assertEquals(indexes[1].primaryKey, this.primaryKey);
80-
ms.deleteIndex(indexes[0].uid);
81-
ms.deleteIndex(indexes[1].uid);
82-
}
83-
84-
85+
assertEquals(indexes.length, 2);
86+
assert(Arrays.stream(indexUids).anyMatch(indexUids[0]::equals));
87+
assert(Arrays.stream(indexUids).anyMatch(indexUids[1]::equals));
88+
ms.deleteIndex(indexUids[0]);
89+
ms.deleteIndex(indexUids[1]);
90+
}
8591
}

0 commit comments

Comments
 (0)