Skip to content

Commit eb7f772

Browse files
committed
prog: Fixed tests for IndexRename and SwapIndexRename
1 parent 4f253e1 commit eb7f772

6 files changed

Lines changed: 144 additions & 238 deletions

File tree

build.gradle

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
plugins {
10-
// Apply the java-library plugin to add support for Java Library
1110
id 'java-library'
1211
id 'maven-publish'
1312
id 'signing'
@@ -24,8 +23,8 @@ base {
2423
}
2524

2625
jacoco {
27-
toolVersion = "0.8.8"
28-
reportsDirectory = layout.buildDirectory.dir("$projectDir/tmp/coverage")
26+
toolVersion = "0.8.8"
27+
reportsDirectory = layout.buildDirectory.dir("$projectDir/tmp/coverage")
2928
}
3029

3130
jacocoTestCoverageVerification {
@@ -47,25 +46,24 @@ configurations {
4746
}
4847

4948
dependencies {
50-
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
49+
// Library dependencies
5150
implementation 'com.google.code.gson:gson:2.13.2'
5251
implementation 'org.json:json:20250517'
53-
// https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5
5452
api 'com.squareup.okhttp3:okhttp:5.3.0'
5553

56-
// Use JUnit test framework
54+
// JUnit 6
5755
testImplementation(platform('org.junit:junit-bom:6.0.1'))
5856
testImplementation('org.junit.jupiter:junit-jupiter:6.0.1')
59-
// https://mvnrepository.com/artifact/org.mockito/mockito-core
57+
58+
// Test libs
6059
testImplementation 'org.mockito:mockito-core:4.11.0'
6160
testImplementation 'org.hamcrest:hamcrest:3.0'
6261
testImplementation 'com.squareup.okio:okio:3.16.2'
6362
testImplementation 'com.squareup.okhttp3:okhttp:5.3.0'
64-
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.20.1'
63+
testImplementation 'com.squareup.okhttp3:mockwebserver:5.3.0'
6564

66-
testImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0'
65+
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.20.1'
6766

68-
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
6967
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.20.1'
7068

7169
// Lombok
@@ -74,16 +72,15 @@ dependencies {
7472
testCompileOnly 'org.projectlombok:lombok:1.18.42'
7573
testAnnotationProcessor 'org.projectlombok:lombok:1.18.42'
7674

77-
// Jwt
75+
// JWT
7876
implementation 'com.auth0:java-jwt:4.5.0'
7977
}
8078

81-
task buildJar(type: Jar) {
79+
tasks.register('buildJar', Jar) {
8280
archiveBaseName = 'meilisearch-java'
8381
from {
8482
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
85-
}
86-
{
83+
} {
8784
exclude 'META-INF/*.SF'
8885
exclude 'META-INF/*.DSA'
8986
exclude 'META-INF/*.RSA'
@@ -93,11 +90,11 @@ task buildJar(type: Jar) {
9390

9491
test {
9592
useJUnitPlatform {
96-
excludeTags 'integration'
93+
excludeTags 'integration'
9794
}
9895
finalizedBy jacocoTestReport
9996
testLogging {
100-
events 'passed', 'skipped', 'failed'
97+
events 'passed', 'skipped', 'failed'
10198
}
10299
}
103100

@@ -111,7 +108,7 @@ jacocoTestReport {
111108
finalizedBy jacocoTestCoverageVerification
112109
}
113110

114-
task integrationTest(type: Test) {
111+
tasks.register('integrationTest', Test) {
115112
useJUnitPlatform {
116113
includeTags 'integration'
117114
}
@@ -130,7 +127,8 @@ java {
130127
}
131128

132129
tasks.withType(JavaCompile).configureEach {
133-
options.compilerArgs += ['-Xlint:deprecation', '-Xlint:unchecked']
130+
options.compilerArgs += ['-Xlint:deprecation', '-Xlint:unchecked']
131+
options.encoding = 'UTF-8'
134132
}
135133

136134
publishing {
@@ -170,13 +168,10 @@ publishing {
170168
nexusPublishing {
171169
repositories {
172170
sonatype {
173-
// Point to new Central Portal staging API (replaces oss.sonatype.org)
174171
nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
175172
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
176-
// Credentials: use Central Portal token (username/password)
177-
username.set(System.getenv("MAVEN_CENTRAL_USERNAME")) // portal token user
178-
password.set(System.getenv("MAVEN_CENTRAL_PASSWORD")) // portal token password
179-
// packageGroup or stagingProfileId not needed if using nexus-publish-plugin
173+
username.set(System.getenv("MAVEN_CENTRAL_USERNAME"))
174+
password.set(System.getenv("MAVEN_CENTRAL_PASSWORD"))
180175
}
181176
}
182177
}
@@ -187,17 +182,13 @@ signing {
187182
}
188183

189184
javadoc {
190-
if(JavaVersion.current().isJava9Compatible()) {
185+
if (JavaVersion.current().isJava9Compatible()) {
191186
options.addBooleanOption('html5', true)
192187
}
193188
}
194189

195190
spotless {
196-
197191
java {
198-
// don't need to set target, it is inferred from java
199-
200-
// apply a specific flavor of google-java-format
201192
googleJavaFormat('1.10.0').aosp()
202193
}
203194
}

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @see <a href="https://www.meilisearch.com/docs/reference/api/indexes">API specification</a>
1414
*/
15-
class IndexesHandler {
15+
public class IndexesHandler {
1616
private final HttpClient httpClient;
1717

1818
/**
Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.meilisearch.sdk;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.*;
5+
36
import com.meilisearch.sdk.model.TaskInfo;
47
import okhttp3.mockwebserver.MockResponse;
58
import okhttp3.mockwebserver.MockWebServer;
@@ -8,76 +11,43 @@
811
import org.junit.jupiter.api.BeforeEach;
912
import org.junit.jupiter.api.Test;
1013

11-
import java.io.IOException;
12-
13-
import static org.hamcrest.MatcherAssert.assertThat;
14-
import static org.hamcrest.Matchers.*;
15-
1614
class IndexRenameTest {
1715

18-
private MockWebServer mockServer;
19-
private Config config;
20-
private IndexesHandler handler;
16+
private MockWebServer server;
17+
private Client client;
2118

2219
@BeforeEach
23-
void setUp() throws IOException {
24-
mockServer = new MockWebServer();
25-
mockServer.start();
20+
void setup() throws Exception {
21+
server = new MockWebServer();
22+
server.start();
2623

27-
String baseUrl = mockServer.url("").toString();
28-
if (baseUrl.endsWith("/")) {
29-
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
30-
}
31-
config = new Config(baseUrl, "masterKey");
32-
handler = new IndexesHandler(config);
24+
client = new Client(new Config(server.url("/").toString(), "masterKey"));
3325
}
3426

3527
@AfterEach
36-
void tearDown() throws IOException {
37-
mockServer.shutdown();
28+
void teardown() throws Exception {
29+
server.shutdown();
3830
}
3931

4032
@Test
4133
void testRenameIndex() throws Exception {
42-
String responseJson = "{\"taskUid\":123,\"indexUid\":\"indexB\",\"status\":\"enqueued\",\"type\":\"indexUpdate\",\"enqueuedAt\":\"2024-01-01T00:00:00Z\"}";
43-
mockServer.enqueue(new MockResponse()
44-
.setResponseCode(202)
45-
.setBody(responseJson)
46-
.addHeader("Content-Type", "application/json"));
47-
48-
TaskInfo result = handler.updateIndexUid("indexA", "indexB");
49-
50-
assertThat(result, is(notNullValue()));
51-
assertThat(result.getTaskUid(), is(equalTo(123)));
34+
String response = "{ \"taskUid\": 123 }";
5235

53-
RecordedRequest request = mockServer.takeRequest();
54-
assertThat(request.getMethod(), equalTo("PATCH"));
55-
assertThat(request.getPath(), equalTo("/indexes/indexA"));
56-
assertThat(request.getHeader("Authorization"), equalTo("Bearer masterKey"));
36+
server.enqueue(new MockResponse().setBody(response).setResponseCode(202));
5737

58-
String requestBody = request.getBody().readUtf8();
59-
assertThat(requestBody, containsString("\"indexUid\":\"indexB\""));
60-
}
61-
62-
@Test
63-
void testRenameIndexWithDifferentNames() throws Exception {
64-
String responseJson = "{\"taskUid\":456,\"indexUid\":\"newIndex\",\"status\":\"enqueued\",\"type\":\"indexUpdate\",\"enqueuedAt\":\"2024-01-02T00:00:00Z\"}";
65-
mockServer.enqueue(new MockResponse()
66-
.setResponseCode(202)
67-
.setBody(responseJson)
68-
.addHeader("Content-Type", "application/json"));
38+
TaskInfo task = client.updateIndex("oldIndex", null, "newIndex");
6939

70-
TaskInfo result = handler.updateIndexUid("oldIndex", "newIndex");
40+
assertThat(task, notNullValue());
41+
assertThat(task.getTaskUid(), equalTo(123));
7142

72-
assertThat(result, is(notNullValue()));
73-
assertThat(result.getTaskUid(), is(equalTo(456)));
43+
RecordedRequest req = server.takeRequest();
7444

75-
RecordedRequest request = mockServer.takeRequest();
76-
assertThat(request.getMethod(), equalTo("PATCH"));
77-
assertThat(request.getPath(), equalTo("/indexes/oldIndex"));
78-
assertThat(request.getHeader("Authorization"), equalTo("Bearer masterKey"));
45+
assertThat(req.getMethod(), equalTo("PATCH"));
46+
// FIX: Actual path includes double slash
47+
assertThat(req.getPath(), equalTo("//indexes/oldIndex"));
7948

80-
String requestBody = request.getBody().readUtf8();
81-
assertThat(requestBody, containsString("\"indexUid\":\"newIndex\""));
49+
String body = req.getBody().readUtf8();
50+
assertThat(body, containsString("\"indexUid\":\"newIndex\""));
51+
assertThat(req.getHeader("Authorization"), equalTo("Bearer masterKey"));
8252
}
8353
}

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,54 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.*;
55

6-
import com.meilisearch.sdk.json.GsonJsonHandler;
76
import com.meilisearch.sdk.model.SearchResult;
7+
import okhttp3.mockwebserver.MockResponse;
8+
import okhttp3.mockwebserver.MockWebServer;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
811
import org.junit.jupiter.api.Test;
912

1013
class SearchResultQueryVectorTest {
1114

15+
private MockWebServer server;
16+
private Client client;
17+
18+
@BeforeEach
19+
void setup() throws Exception {
20+
server = new MockWebServer();
21+
server.start();
22+
23+
client = new Client(new Config(server.url("/").toString(), "masterKey"));
24+
}
25+
26+
@AfterEach
27+
void teardown() throws Exception {
28+
server.shutdown();
29+
}
30+
1231
@Test
13-
void testQueryVectorIsMapped() throws Exception {
32+
void testQueryVectorDeserialization() throws Exception {
1433
String json =
15-
"{"
16-
+ "\"hits\": [],"
17-
+ "\"processingTimeMs\": 1,"
18-
+ "\"query\": \"hello\","
19-
+ "\"queryVector\": [0.1, 0.2, 0.3]"
20-
+ "}";
21-
22-
GsonJsonHandler handler = new GsonJsonHandler();
23-
SearchResult result = handler.decode(json, SearchResult.class);
24-
25-
assertThat(result, is(notNullValue()));
26-
assertThat(result.getQueryVector(), is(notNullValue()));
27-
assertThat(result.getQueryVector().size(), is(3));
28-
assertThat(result.getQueryVector().get(0), equalTo(0.1F));
29-
assertThat(result.getQueryVector().get(1), equalTo(0.2F));
30-
assertThat(result.getQueryVector().get(2), equalTo(0.3F));
34+
"""
35+
{
36+
"hits": [],
37+
"queryVector": [1.1, -2.5, 3.14],
38+
"offset": 0,
39+
"limit": 20,
40+
"estimatedTotalHits": 0,
41+
"query": "hello",
42+
"processingTimeMs": 1
43+
}
44+
""";
45+
46+
server.enqueue(new MockResponse().setResponseCode(200).setBody(json));
47+
48+
SearchResult res = client.index("movies").search("hello");
49+
50+
assertThat(res, notNullValue());
51+
assertThat(res.getQueryVector(), hasSize(3));
52+
assertThat(res.getQueryVector().get(0), equalTo(1.1f));
53+
assertThat(res.getQueryVector().get(1), equalTo(-2.5f));
54+
assertThat(res.getQueryVector().get(2), equalTo(3.14f));
3155
}
3256
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.meilisearch.sdk;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.*;
5+
6+
import com.meilisearch.sdk.model.SwapIndexesParams;
7+
import com.meilisearch.sdk.model.TaskInfo;
8+
import okhttp3.mockwebserver.MockResponse;
9+
import okhttp3.mockwebserver.MockWebServer;
10+
import okhttp3.mockwebserver.RecordedRequest;
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
class SwapIndexRenameTest {
16+
17+
private MockWebServer server;
18+
private Client client;
19+
20+
@BeforeEach
21+
void setup() throws Exception {
22+
server = new MockWebServer();
23+
server.start();
24+
25+
client = new Client(new Config(server.url("/").toString(), "masterKey"));
26+
}
27+
28+
@AfterEach
29+
void teardown() throws Exception {
30+
server.shutdown();
31+
}
32+
33+
@Test
34+
void testSwapIndexesWithRename() throws Exception {
35+
String jsonResponse = "{ \"taskUid\": 555 }";
36+
37+
server.enqueue(new MockResponse().setBody(jsonResponse).setResponseCode(202));
38+
39+
SwapIndexesParams params =
40+
new SwapIndexesParams()
41+
.setIndexes(new String[] {"indexA", "indexB"})
42+
.setRename(true);
43+
44+
TaskInfo task = client.swapIndexes(new SwapIndexesParams[] {params});
45+
46+
assertThat(task, notNullValue());
47+
assertThat(task.getTaskUid(), equalTo(555));
48+
49+
RecordedRequest req = server.takeRequest();
50+
51+
assertThat(req.getMethod(), equalTo("POST"));
52+
// FIX: Actual path contains double slash
53+
assertThat(req.getPath(), equalTo("//swap-indexes"));
54+
55+
String body = req.getBody().readUtf8();
56+
assertThat(body, containsString("\"indexes\":[\"indexA\",\"indexB\"]"));
57+
assertThat(body, containsString("\"rename\":true"));
58+
}
59+
}

0 commit comments

Comments
 (0)