From 866cf9d8f80edcd751e99687d87bac4407b752e1 Mon Sep 17 00:00:00 2001 From: Sylvain Wallez Date: Wed, 19 Oct 2022 17:43:07 +0200 Subject: [PATCH] Add and update tests --- .../elasticsearch/json/JsonpUtilsTest.java | 61 ++++++++++++++++++- .../co/elastic/clients/json/WithJsonTest.java | 2 +- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java b/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java index 8978e3429..cf8434f0a 100644 --- a/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java +++ b/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java @@ -20,12 +20,16 @@ package co.elastic.clients.elasticsearch.json; import co.elastic.clients.elasticsearch.core.search.Hit; +import co.elastic.clients.elasticsearch.model.ModelTestCase; +import co.elastic.clients.elasticsearch.security.IndexPrivilege; +import co.elastic.clients.elasticsearch.security.IndicesPrivileges; +import co.elastic.clients.elasticsearch.security.RoleTemplateScript; +import co.elastic.clients.elasticsearch.security.UserIndicesPrivileges; import co.elastic.clients.json.JsonpUtils; import co.elastic.clients.util.AllowForbiddenApis; import jakarta.json.JsonException; import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonGenerator; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.StringWriter; @@ -34,7 +38,7 @@ import java.util.Enumeration; import java.util.function.Consumer; -public class JsonpUtilsTest extends Assertions { +public class JsonpUtilsTest extends ModelTestCase { @Test @AllowForbiddenApis("Testing JsonpUtil.provider()") @@ -142,6 +146,59 @@ public void testSerializeIntOrNull() { assertEquals("{\"a\":0}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 0, 0))); } + @Test + public void testJsonString() { + // Single value + { + IndicesPrivileges priv = IndicesPrivileges.of(i -> i + .names("bar") + .query(q -> q.term(t -> t.field("baz").value(1))) + .privileges(IndexPrivilege.All) + ); + + String json = "{\"names\":[\"bar\"],\"privileges\":[\"all\"],\"query\":\"{\\\"term\\\":{\\\"baz\\\":{\\\"value\\\":1}}}\"}"; + + assertEquals(json, toJson(priv)); + + priv = fromJson(json, IndicesPrivileges.class); + assertEquals("baz", priv.query().term().field()); + } + + // Single value template + { + IndicesPrivileges priv = IndicesPrivileges.of(i -> i + .names("bar") + .query(q -> q._custom("template", RoleTemplateScript.of(s -> s.stored(v -> v.id("foo"))))) + .privileges(IndexPrivilege.All) + ); + + String json = "{\"names\":[\"bar\"],\"privileges\":[\"all\"],\"query\":\"{\\\"template\\\":{\\\"id\\\":\\\"foo\\\"}}\"}"; + + assertEquals(json, toJson(priv)); + + priv = fromJson(json, IndicesPrivileges.class); + assertEquals("foo", priv.query()._custom().to(RoleTemplateScript.class).stored().id()); + } + + // Array value + { + UserIndicesPrivileges priv = UserIndicesPrivileges.of(i -> i + .names("bar") + .query(q -> q.term(t -> t.field("baz").value(1))) + .privileges(IndexPrivilege.All) + .allowRestrictedIndices(false) + ); + + String json = "{\"names\":[\"bar\"],\"privileges\":[\"all\"],\"query\":[\"{\\\"term\\\":{\\\"baz\\\":{\\\"value\\\":1}}}\"]," + + "\"allow_restricted_indices\":false}"; + + assertEquals(json, toJson(priv)); + + priv = fromJson(json, UserIndicesPrivileges.class); + assertEquals("baz", priv.query().get(0).term().field()); + } + } + private static String orNullHelper(Consumer c) { StringWriter sw = new StringWriter(); JsonGenerator generator = JsonpUtils.provider().createGenerator(sw); diff --git a/java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java b/java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java index c555ce323..e0241d297 100644 --- a/java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java +++ b/java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java @@ -74,7 +74,7 @@ public void testRegularObject() { ); assertEquals("foo", s.field()); - assertEquals(12, s.id()); + assertEquals("12", s.id()); assertEquals(34, s.max()); }