forked from meilisearch/meilisearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapIndexesRenameTest.java
More file actions
133 lines (107 loc) · 4.83 KB
/
SwapIndexesRenameTest.java
File metadata and controls
133 lines (107 loc) · 4.83 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
package com.meilisearch.sdk;
import com.meilisearch.sdk.model.SwapIndexesParams;
import com.meilisearch.sdk.model.TaskInfo;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class SwapIndexesRenameTest {
private MockWebServer mockServer;
private Config config;
private Client client;
@BeforeEach
void setUp() throws IOException {
mockServer = new MockWebServer();
mockServer.start();
String baseUrl = mockServer.url("").toString();
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
config = new Config(baseUrl, "masterKey");
client = new Client(config);
}
@AfterEach
void tearDown() throws IOException {
mockServer.shutdown();
}
@Test
void testSwapIndexesWithRename() throws Exception {
String responseJson = "{\"taskUid\":789,\"status\":\"enqueued\",\"type\":\"indexSwap\",\"enqueuedAt\":\"2024-01-01T00:00:00Z\"}";
mockServer.enqueue(new MockResponse()
.setResponseCode(202)
.setBody(responseJson)
.addHeader("Content-Type", "application/json"));
SwapIndexesParams[] params = {
new SwapIndexesParams()
.setIndexes(new String[]{"indexA", "indexB"})
.setRename(true)
};
TaskInfo result = client.swapIndexes(params);
assertThat(result, is(notNullValue()));
assertThat(result.getTaskUid(), is(equalTo(789)));
RecordedRequest request = mockServer.takeRequest();
assertThat(request.getMethod(), equalTo("POST"));
assertThat(request.getPath(), equalTo("/swap-indexes"));
assertThat(request.getHeader("Authorization"), equalTo("Bearer masterKey"));
String requestBody = request.getBody().readUtf8();
assertThat(requestBody, containsString("\"rename\":true"));
assertThat(requestBody, containsString("\"indexes\""));
assertThat(requestBody, containsString("\"indexA\""));
assertThat(requestBody, containsString("\"indexB\""));
}
@Test
void testSwapIndexesWithoutRename() throws Exception {
String responseJson = "{\"taskUid\":790,\"status\":\"enqueued\",\"type\":\"indexSwap\",\"enqueuedAt\":\"2024-01-02T00:00:00Z\"}";
mockServer.enqueue(new MockResponse()
.setResponseCode(202)
.setBody(responseJson)
.addHeader("Content-Type", "application/json"));
SwapIndexesParams[] params = {
new SwapIndexesParams()
.setIndexes(new String[]{"indexC", "indexD"})
.setRename(false)
};
TaskInfo result = client.swapIndexes(params);
assertThat(result, is(notNullValue()));
assertThat(result.getTaskUid(), is(equalTo(790)));
RecordedRequest request = mockServer.takeRequest();
assertThat(request.getMethod(), equalTo("POST"));
assertThat(request.getPath(), equalTo("/swap-indexes"));
String requestBody = request.getBody().readUtf8();
assertThat(requestBody, containsString("\"rename\":false"));
assertThat(requestBody, containsString("\"indexC\""));
assertThat(requestBody, containsString("\"indexD\""));
}
@Test
void testSwapMultipleIndexPairs() throws Exception {
String responseJson = "{\"taskUid\":791,\"status\":\"enqueued\",\"type\":\"indexSwap\",\"enqueuedAt\":\"2024-01-03T00:00:00Z\"}";
mockServer.enqueue(new MockResponse()
.setResponseCode(202)
.setBody(responseJson)
.addHeader("Content-Type", "application/json"));
SwapIndexesParams[] params = {
new SwapIndexesParams()
.setIndexes(new String[]{"indexA", "indexB"})
.setRename(true),
new SwapIndexesParams()
.setIndexes(new String[]{"indexC", "indexD"})
.setRename(false)
};
TaskInfo result = client.swapIndexes(params);
assertThat(result, is(notNullValue()));
assertThat(result.getTaskUid(), is(equalTo(791)));
RecordedRequest request = mockServer.takeRequest();
String requestBody = request.getBody().readUtf8();
assertThat(requestBody, containsString("\"indexA\""));
assertThat(requestBody, containsString("\"indexB\""));
assertThat(requestBody, containsString("\"indexC\""));
assertThat(requestBody, containsString("\"indexD\""));
assertThat(requestBody, startsWith("["));
assertThat(requestBody, endsWith("]"));
}
}