From 24e3f2983645b21966c1b7bbdca86b2fe6e81b02 Mon Sep 17 00:00:00 2001 From: tamirschwarz Date: Sun, 29 Oct 2023 23:27:25 +0200 Subject: [PATCH] add searchRequestToJsonTest --- .../client/opensearch/json/JsonDataTest.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java index e9361a46b9..0116c26b2d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java @@ -36,13 +36,18 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonParser; import java.io.StringReader; + import org.junit.Assert; import org.junit.Test; import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpUtils; import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch._types.query_dsl.TermQuery; import org.opensearch.client.opensearch.core.IndexRequest; +import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.experiments.api.query2.BoolQuery; import org.opensearch.client.opensearch.model.ModelTestCase; public class JsonDataTest extends Assert { @@ -114,7 +119,7 @@ public String getName() { } @Test - public void testToJson() { + public void indexRequestToJsonTest() { String expectedJson = "{\"aga\":42,\"name\":\"koko\"}"; Person person = new Person(42, "koko"); IndexRequest indexRequest = new IndexRequest.Builder() @@ -129,4 +134,27 @@ public void testToJson() { assertEquals(expectedJson, json); } + @Test + public void searchRequestToJsonTest() { + String expectedJson = "{\"query\":{\"term\":{\"key-field\":{\"value\":\"value-filed\"}}}}"; + + SearchRequest searchRequest = new SearchRequest + .Builder() + .index("my-index") + .query(new TermQuery + .Builder() + .field("key-field") + .value(FieldValue.of("value-filed")) + .build() + ._toQuery() + ) + .build(); + + String json = JsonpUtils.toJson(searchRequest); + System.out.println(json); + + assertEquals(expectedJson, json); + } + + }