|
1 | 1 | package com.meilisearch.sdk;
|
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
3 | 4 |
|
4 | 5 | import org.junit.jupiter.api.BeforeEach;
|
| 6 | +import org.junit.jupiter.api.AfterAll; |
5 | 7 | import org.junit.jupiter.api.Test;
|
6 | 8 |
|
7 |
| -public class DocumentsTest { |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 10 |
|
9 |
| - Index meilisearchIndex; |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONObject; |
10 | 13 |
|
11 |
| - @BeforeEach |
12 |
| - public void initialize() { |
13 |
| - Client ms = new Client(new Config("http://localhost:7700", "")); |
14 | 14 |
|
15 |
| - try { |
16 |
| - // TODO: add uid of index for test |
17 |
| - this.meilisearchIndex = ms.getIndex("movies"); |
18 |
| - } catch (Exception e) { |
| 15 | +public class DocumentsTest { |
| 16 | + |
| 17 | + Client ms; |
| 18 | + Index index; |
| 19 | + Gson gson = new Gson(); |
| 20 | + |
| 21 | + String primaryKey = "id"; |
19 | 22 |
|
20 |
| - } |
| 23 | + @BeforeEach |
| 24 | + public void initialize() throws Exception { |
| 25 | + ms = new Client(new Config("http://localhost:7700", "masterKey")); |
21 | 26 | }
|
22 | 27 |
|
23 |
| - @Test |
24 |
| - public void get() throws Exception { |
25 |
| - // TODO: input identifier for test |
26 |
| - System.out.println(this.meilisearchIndex.getDocument("9999")); |
| 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 | + } |
27 | 35 | }
|
28 | 36 |
|
| 37 | + /** |
| 38 | + * Test Add single document |
| 39 | + */ |
29 | 40 | @Test
|
30 |
| - public void getAll() throws Exception { |
31 |
| - System.out.println(this.meilisearchIndex.getDocuments()); |
32 |
| - } |
| 41 | + public void testAddDocumentsSingle() throws Exception { |
33 | 42 |
|
34 |
| - @Test |
35 |
| - public void add() throws Exception { |
36 |
| - String testDoc = "[{\n" + |
37 |
| - " \"id\": 9999,\n" + |
38 |
| - " \"title\": \"Shazam\",\n" + |
39 |
| - " \"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\",\n" + |
40 |
| - " \"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\",\n" + |
41 |
| - " \"release_date\": \"2019-03-23\"\n" + |
42 |
| - " }]"; |
43 |
| - // TODO: setup test document for 'add' |
44 |
| - System.out.println(this.meilisearchIndex.addDocuments("")); |
45 |
| - } |
| 43 | + String indexUid = "addSingleDocument"; |
| 44 | + ms.createIndex(indexUid); |
| 45 | + this.index = ms.getIndex(indexUid); |
46 | 46 |
|
47 |
| - @Test |
48 |
| - public void delete() throws Exception { |
49 |
| - // TODO: input identifier for test |
50 |
| - System.out.println(this.meilisearchIndex.deleteDocument("")); |
51 |
| - } |
| 47 | + JSONArray jsonArray = new JSONArray(); |
| 48 | + JSONObject jsonObject = new JSONObject(); |
52 | 49 |
|
53 |
| - @Test |
54 |
| - public void search() throws Exception { |
55 |
| - System.out.println(this.meilisearchIndex.search("Batman")); |
| 50 | + jsonObject.put("id", "1111"); |
| 51 | + jsonObject.put("title", "Alice in wonderland"); |
| 52 | + jsonArray.put(jsonObject); |
| 53 | + |
| 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()); |
56 | 70 | }
|
57 | 71 |
|
| 72 | + /** |
| 73 | + * Test Add multiple documents |
| 74 | + */ |
58 | 75 | @Test
|
59 |
| - public void updates() throws Exception { |
60 |
| - System.out.println(this.meilisearchIndex.getUpdates()[0].toString()); |
| 76 | + public void testAddDocumentsMultiple() throws Exception { |
| 77 | + |
| 78 | + String indexUid = "addMultipleDocuments"; |
| 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()); |
61 | 115 | }
|
| 116 | + |
62 | 117 | }
|
| 118 | + |
0 commit comments