Skip to content

Add integration tests, Docker on CI, and implement waitForPendingUpdate #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ jobs:
java-version: 1.8
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Docker setup
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics=true --master-key=masterKey
- name: Build with Gradle
run: ./gradlew build
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.json:json:20200518'
// Use JUnit test framework
testImplementation(platform('org.junit:junit-bom:5.7.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.ToString;

import java.util.Date;
import java.io.Serializable;

/**
Expand Down Expand Up @@ -137,4 +138,45 @@ public UpdateStatus[] getUpdates() throws Exception {
Gson gson = new Gson();
return gson.fromJson(this.documents.getUpdates(this.uid), UpdateStatus[].class);
}

/**
* Wait for a pending update to be processed
*
* @param updateId ID of the index update
* @param timeoutInMs number of milliseconds before throwing and Exception
* @param intervalInMs number of milliseconds before requesting the status again
* @throws Exception If timeout is reached
*/
public void waitForPendingUpdate(int updateId) throws Exception {
waitForPendingUpdate(updateId, 5000, 50);
}

/**
* Wait for a pending update to be processed
*
* @param updateId ID of the index update
* @param timeoutInMs number of milliseconds before throwing an Exception
* @param intervalInMs number of milliseconds before requesting the status again
* @throws Exception if timeout is reached
*/
public void waitForPendingUpdate(int updateId, int timeoutInMs, int intervalInMs) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like a CompletableFuture<UpdateStatus> would be better for something like this.
Supply the updateId -> thenApply(...) the while loop -> call .get(timeout)

Gson gson = new Gson();
UpdateStatus updateStatus;
String status = "";
long startTime = new Date().getTime();
long elapsedTime = 0;

while (!status.equals("processed")){
if (elapsedTime >= timeoutInMs){
throw new Exception();
}
updateStatus = gson.fromJson(
this.getUpdate(updateId),
UpdateStatus.class
);
status = updateStatus.getStatus();
Thread.sleep(intervalInMs);
elapsedTime = new Date().getTime() - startTime;
}
}
}
95 changes: 55 additions & 40 deletions src/test/java/com/meilisearch/sdk/DocumentsTest.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,77 @@
package com.meilisearch.sdk;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.reflect.TypeToken;

public class DocumentsTest {
import static org.junit.jupiter.api.Assertions.assertEquals;

Index meilisearchIndex;
public class DocumentsTest {

Client ms;
Gson gson = new Gson();
TestUtils testUtils = new TestUtils();

@BeforeEach
public void initialize() {
Client ms = new Client(new Config("http://localhost:7700", ""));

try {
// TODO: add uid of index for test
this.meilisearchIndex = ms.getIndex("movies");
} catch (Exception e) {

}
public void initialize() throws Exception {
ms = new Client(new Config("http://localhost:7700", "masterKey"));
}

@Test
public void get() throws Exception {
// TODO: input identifier for test
System.out.println(this.meilisearchIndex.getDocument("9999"));
@AfterAll
static void cleanMeiliSearch() throws Exception {
new TestUtils().deleteAllIndexes();
}

/**
* Test Add single document
*/
@Test
public void getAll() throws Exception {
System.out.println(this.meilisearchIndex.getDocuments());
}
public void testAddDocumentsSingle() throws Exception {

@Test
public void add() throws Exception {
String testDoc = "[{\n" +
" \"id\": 9999,\n" +
" \"title\": \"Shazam\",\n" +
" \"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\",\n" +
" \"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\",\n" +
" \"release_date\": \"2019-03-23\"\n" +
" }]";
// TODO: setup test document for 'add'
System.out.println(this.meilisearchIndex.addDocuments(""));
String indexUid = "addSingleDocument";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);

UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testUtils.movies_data),
UpdateStatus.class
);

index.waitForPendingUpdate(updateInfo.getUpdateId());
Movie[] movies = this.gson.fromJson(index.getDocuments(), Movie[].class);
for (int i=0; i<movies.length; i++) {
assertEquals(movies[i].title, this.testUtils.movies[i].title);
}
}

/**
* Test Add multiple documents
*/
@Test
public void delete() throws Exception {
// TODO: input identifier for test
System.out.println(this.meilisearchIndex.deleteDocument(""));
}
public void testAddDocumentsMultiple() throws Exception {

@Test
public void search() throws Exception {
System.out.println(this.meilisearchIndex.search("Batman"));
}
String indexUid = "addMultipleDocuments";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);

@Test
public void updates() throws Exception {
System.out.println(this.meilisearchIndex.getUpdates()[0].toString());
UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testUtils.movies_data),
UpdateStatus.class
);

index.waitForPendingUpdate(updateInfo.getUpdateId());
Movie[] movies = this.gson.fromJson(index.getDocuments(), Movie[].class);
for (int i=0; i<movies.length; i++) {
assertEquals(movies[i].title, this.testUtils.movies[i].title);
}
}

}

136 changes: 112 additions & 24 deletions src/test/java/com/meilisearch/sdk/IndexesTest.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,142 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package com.meilisearch.sdk;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;

import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.json.JSONArray;
import org.json.JSONObject;

public class IndexesTest {

Client ms;
Gson gson = new Gson();
String primaryKey = "id";
TestUtils testUtils = new TestUtils();

@BeforeEach
public void initialize() {
ms = new Client(new Config("http://localhost:7700", ""));
public void initializeClient() throws Exception {
ms = new Client(new Config("http://localhost:7700", "masterKey"));
}

@Test
public void createIndex() throws Exception {
System.out.println(ms.createIndex("videos"));
@AfterAll
static void cleanMeiliSearch() throws Exception {
new TestUtils().deleteAllIndexes();
}

/**
* Test Index creation without PrimaryKey
*/
@Test
public void getIndexes() throws Exception {
Index[] meilisearchIndices = ms.getIndexList();
for (int a = 0; a < meilisearchIndices.length; a++) {
System.out.println(meilisearchIndices[a]);
}

public void testCreateIndexWithoutPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);
assertEquals(index.uid, indexUid);
assertEquals(index.primaryKey, null);
ms.deleteIndex(index.uid);
}

/**
* Test Index creation with PrimaryKey
*/
@Test
public void getIndex() throws Exception {
// TODO: input uid for test
Index meilisearchIndex = ms.getIndex("movies");
System.out.println(meilisearchIndex);
public void testCreateIndexWithPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
ms.createIndex(indexUid, this.primaryKey);
Index index = ms.getIndex(indexUid);
assertEquals(index.uid, indexUid);
assertEquals(index.primaryKey, this.primaryKey);
ms.deleteIndex(index.uid);
}

/**
* Test update Index PrimaryKey
*/
@Test
public void put() throws Exception {

public void testUpdateIndexPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);
assertEquals(index.uid, indexUid);
assertEquals(index.primaryKey, null);
ms.updateIndex(indexUid, this.primaryKey);
index = ms.getIndex(indexUid);
assertEquals(index.uid, indexUid);
assertEquals(index.primaryKey, this.primaryKey);
ms.deleteIndex(index.uid);
}

/**
* Test getIndexList
*/
@Test
public void testGetIndexList() throws Exception {
String[] indexUids = {"IndexesTest", "IndexesTest2"};
ms.createIndex(indexUids[0]);
ms.createIndex(indexUids[1], this.primaryKey);
Index index1 = ms.getIndex(indexUids[0]);
Index index2 = ms.getIndex(indexUids[1]);
Index[] indexes = ms.getIndexList();
assertEquals(indexes.length, 2);
assert(Arrays.stream(indexUids).anyMatch(indexUids[0]::equals));
assert(Arrays.stream(indexUids).anyMatch(indexUids[1]::equals));
ms.deleteIndex(indexUids[0]);
ms.deleteIndex(indexUids[1]);
}

/**
* Test waitForPendingUpdate
*/
@Test
public void update() throws Exception {
System.out.println(ms.updateIndex("video", "videos_key"));
public void testWaitForPendingUpdate() throws Exception {
String indexUid = "IndexesTest2";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);

UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testUtils.movies_data),
UpdateStatus.class
);

index.waitForPendingUpdate(updateInfo.getUpdateId());

UpdateStatus updateStatus = this.gson.fromJson(
index.getUpdate(updateInfo.getUpdateId()),
UpdateStatus.class
);

assertEquals("processed", updateStatus.getStatus());

ms.deleteIndex(index.uid);
}

/**
* Test waitForPendingUpdate timeoutInMs
*/
@Test
public void delete() throws Exception {
System.out.println(ms.deleteIndex("videos"));
public void testWaitForPendingUpdateTimoutInMs() throws Exception {
String indexUid = "IndexesTest2";
ms.createIndex(indexUid);
Index index = ms.getIndex(indexUid);

UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testUtils.movies_data),
UpdateStatus.class
);

assertThrows(
Exception.class,
() -> index.waitForPendingUpdate(updateInfo.getUpdateId(), 0, 50)
);

ms.deleteIndex(index.uid);
}

}
11 changes: 11 additions & 0 deletions src/test/java/com/meilisearch/sdk/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.meilisearch.sdk;

public class Movie {
String id;
String title;
String poster;
String overview;
String release_date;
String language;
String[] genres ;
}
Loading