From aa9f389f88e06642c8b27af6b154cd7ad3ae6cce Mon Sep 17 00:00:00 2001 From: bfindlay Date: Sun, 26 Nov 2023 19:51:02 +1100 Subject: [PATCH] add itest for JsonData deserialization Signed-off-by: bfindlay --- .../opensearch/integTest/AbstractTasksIT.java | 69 +++++++++++++++++++ .../integTest/httpclient5/TasksIT.java | 5 ++ .../integTest/restclient/TasksIT.java | 16 +++++ 3 files changed, 90 insertions(+) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractTasksIT.java create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/TasksIT.java create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/TasksIT.java diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractTasksIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractTasksIT.java new file mode 100644 index 0000000000..05ad33bec7 --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractTasksIT.java @@ -0,0 +1,69 @@ +package org.opensearch.client.opensearch.integTest; + +import org.junit.Test; +import org.opensearch.client.opensearch.tasks.Status; + +public abstract class AbstractTasksIT extends OpenSearchJavaClientTestCase { + + @Test + public void getTasks_waitForCompletionFalse_jsonDataStatusCanBeDeserialized() { + try { + javaClient().indices().create(b -> b.index("test-index-a").settings(s -> s.refreshInterval(v -> v.time("1s")))); + javaClient().index(b -> b.index("test-index-a").id("a").document(new IndexData("test"))); + javaClient().indices().refresh(b -> b.index("test-index-a")); + + final var deleteByQueryResponse = javaClient().deleteByQuery( + d -> d.index("test-index-a").query(q -> q.matchAll(m -> m.queryName("match-all"))).waitForCompletion(false) + ); + + javaClient().indices().refresh(b -> b.index("test-index-a")); + + Thread.sleep(2000); + final var tasksResponse = javaClient().tasks().get(t -> t.taskId(deleteByQueryResponse.task())); + + assertNotNull(tasksResponse.response()); + assertTrue(tasksResponse.completed()); + + // Deserialize the JsonData to a typed Status response + final Status taskStatus = tasksResponse.task().status().to(Status.class); + + assertTrue(tasksResponse.completed()); + + // Ensure the JsonData can be deserialized + assertEquals(1, taskStatus.total()); + assertEquals(0, taskStatus.updated()); + assertEquals(0, taskStatus.created()); + assertEquals(1, taskStatus.deleted()); + assertEquals(1, taskStatus.batches()); + assertEquals(0, taskStatus.noops()); + assertEquals(0, taskStatus.versionConflicts()); + assertEquals(0, taskStatus.retries().bulk()); + assertEquals(0, taskStatus.retries().search()); + assertEquals(0, taskStatus.throttledMillis()); + assertEquals(-1f, taskStatus.requestsPerSecond(), 0.01); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + public class IndexData { + private String title; + + public IndexData(String title) { + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public String toString() { + return String.format("IndexData{title='%s'}", title); + } + } +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/TasksIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/TasksIT.java new file mode 100644 index 0000000000..a3b7c95231 --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/TasksIT.java @@ -0,0 +1,5 @@ +package org.opensearch.client.opensearch.integTest.httpclient5; + +import org.opensearch.client.opensearch.integTest.AbstractTasksIT; + +public class TasksIT extends AbstractTasksIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/TasksIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/TasksIT.java new file mode 100644 index 0000000000..7484a229df --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/TasksIT.java @@ -0,0 +1,16 @@ +package org.opensearch.client.opensearch.integTest.restclient; + +import java.io.IOException; +import org.apache.hc.core5.http.HttpHost; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; +import org.opensearch.client.opensearch.integTest.AbstractTasksIT; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.rest_client.RestClientTransport; +import org.opensearch.common.settings.Settings; + +public class TasksIT extends AbstractTasksIT { + @Override + public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { + return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper()); + } +}