Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swallez committed Oct 19, 2022
1 parent 8e4be9c commit 866cf9d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()")
Expand Down Expand Up @@ -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<JsonGenerator> c) {
StringWriter sw = new StringWriter();
JsonGenerator generator = JsonpUtils.provider().createGenerator(sw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testRegularObject() {
);

assertEquals("foo", s.field());
assertEquals(12, s.id());
assertEquals("12", s.id());
assertEquals(34, s.max());
}

Expand Down

0 comments on commit 866cf9d

Please sign in to comment.