diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java index ac8b0925e0..a356560bf1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java @@ -32,27 +32,15 @@ package org.opensearch.client.opensearch.experiments; -import jakarta.json.spi.JsonProvider; -import org.junit.Assert; import org.junit.Test; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpSerializable; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.opensearch.experiments.api.FooRequest; import org.opensearch.client.opensearch.indices.IndexSettings; import org.opensearch.client.opensearch.indices.IndexSettingsMapping; import org.opensearch.client.opensearch.indices.Translog; +import org.opensearch.client.opensearch.model.ModelTestCase; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; - -public class ParsingTests extends Assert { +public class ParsingTests extends ModelTestCase { @Test public void testFoo() { @@ -66,10 +54,10 @@ public void testFoo() { ) .build(); - String str = serialize(foo); + String str = toJson(foo); assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str); - FooRequest foo2 = deserialize(str, FooRequest.parser()); + FooRequest foo2 = fromJson(str, FooRequest.parser()); assertEquals(foo.name(), foo2.name()); assertEquals(foo.value(), foo2.value()); assertNull(foo2.size()); @@ -85,18 +73,18 @@ public void testIndexSettingsTranslogOptionsParsing() { .durability("async") .flushThresholdSize("256mb")))); - var str = serialize(indexSettings); + var str = toJson(indexSettings); assertEquals("{\"translog\":{\"durability\":\"async\",\"flush_threshold_size\":\"256mb\"," + "\"sync_interval\":\"10s\"}}", str); - IndexSettings deserialized = deserialize(str, IndexSettings._DESERIALIZER); + IndexSettings deserialized = fromJson(str, IndexSettings._DESERIALIZER); assertEquals(indexSettings.translog().syncInterval().time(), deserialized.translog().syncInterval().time()); assertEquals(indexSettings.translog().durability(), deserialized.translog().durability()); assertEquals(indexSettings.translog().flushThresholdSize(), deserialized.translog().flushThresholdSize()); var deprecatedForm = "{\"translog\":{\"sync_interval\":\"10s\"},\"translog.durability\":\"async\",\"translog" + ".flush_threshold_size\":\"256mb\"}"; - IndexSettings deprecatedDeserialized = deserialize(deprecatedForm, IndexSettings._DESERIALIZER); + IndexSettings deprecatedDeserialized = fromJson(deprecatedForm, IndexSettings._DESERIALIZER); assertEquals(indexSettings.translog().syncInterval().time(), deprecatedDeserialized.translog().syncInterval().time()); assertEquals(indexSettings.translog().durability(), deprecatedDeserialized.translog().durability()); assertEquals(indexSettings.translog().flushThresholdSize(), deprecatedDeserialized.translog().flushThresholdSize()); @@ -112,11 +100,11 @@ public void testIndexSettingsMappingParsing() { .nestedObjects(d -> d.limit(4L)) .fieldNameLength(d -> d.limit(5L))); - var str = serialize(mapping); + var str = toJson(mapping); assertEquals("{\"total_fields\":{\"limit\":1},\"depth\":{\"limit\":2},\"nested_fields\":{\"limit\":3}," + "\"nested_objects\":{\"limit\":4},\"field_name_length\":{\"limit\":5}}", str); - var deserialized = deserialize(str, IndexSettingsMapping._DESERIALIZER); + var deserialized = fromJson(str, IndexSettingsMapping._DESERIALIZER); assertEquals(mapping.totalFields().limit(), deserialized.totalFields().limit()); assertEquals(mapping.depth().limit(), deserialized.depth().limit()); assertEquals(mapping.nestedFields().limit(), deserialized.nestedFields().limit()); @@ -124,21 +112,4 @@ public void testIndexSettingsMappingParsing() { assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit()); } - private T deserialize(String serializedValue, JsonpDeserializer deserializer) { - var provider = JsonProvider.provider(); - var parser = provider.createParser(new ByteArrayInputStream(serializedValue.getBytes(StandardCharsets.UTF_8))); - - return deserializer.deserialize(parser, new JsonbJsonpMapper()); - } - - private String serialize(JsonpSerializable object) { - var baos = new ByteArrayOutputStream(); - var provider = JsonProvider.provider(); - - var generator = provider.createGenerator(baos); - object.serialize(generator, new JsonbJsonpMapper()); - generator.close(); - - return baos.toString(); - } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java index 5a6f564fd6..3342bbb65b 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java @@ -32,16 +32,9 @@ package org.opensearch.client.opensearch.experiments.containers; -import org.opensearch.client.opensearch.model.ModelTestCase; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import jakarta.json.spi.JsonProvider; -import jakarta.json.stream.JsonGenerator; -import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.StringReader; +import org.opensearch.client.opensearch.model.ModelTestCase; public class SomeUnionTest extends ModelTestCase { @@ -67,16 +60,11 @@ public void testDeserialization() { @Test public void testSerialization() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - JsonProvider provider = JsonProvider.provider(); - JsonGenerator generator = provider.createGenerator(baos); + String str = toJson(su); - su.serialize(generator, new JsonbJsonpMapper()); - generator.close(); + System.out.println(str); - System.out.println(baos.toString()); - - assertEquals(json, baos.toString()); + assertEquals(json, str); } @@ -84,11 +72,8 @@ public void testSerialization() { public void testMissingVariantDeserialization() { String json = "{}"; - JsonProvider provider = JsonProvider.provider(); - JsonParser parser = provider.createParser(new StringReader(json)); - JsonParsingException e = assertThrows(JsonParsingException.class, () -> { - SomeUnion c = SomeUnion._DESERIALIZER.deserialize(parser, new JsonbJsonpMapper()); + fromJson(json, SomeUnion._DESERIALIZER); }); assertEquals("Property 'type' not found", e.getMessage()); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java index 24ea0b8200..6f1f677844 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java @@ -32,26 +32,16 @@ package org.opensearch.client.opensearch.experiments.inheritance; +import org.junit.Test; import org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass; import org.opensearch.client.opensearch.experiments.inheritance.final_.FinalClass; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import jakarta.json.spi.JsonProvider; -import jakarta.json.stream.JsonGenerator; -import jakarta.json.stream.JsonParser; -import org.junit.Assert; -import org.junit.Test; +import org.opensearch.client.opensearch.model.ModelTestCase; -import java.io.ByteArrayOutputStream; -import java.io.StringReader; - -public class InheritanceTest extends Assert { +public class InheritanceTest extends ModelTestCase { @Test public void testSerialization() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - JsonProvider provider = JsonProvider.provider(); - FinalClass fc = new FinalClass.Builder() // Start fields from the top of the hierarchy to test setter return values .baseField("baseValue") @@ -59,49 +49,36 @@ public void testSerialization() { .finalField("finalValue") .build(); - JsonGenerator generator = provider.createGenerator(baos); - fc.serialize(generator, new JsonbJsonpMapper()); - - generator.close(); - String str = baos.toString(); + String str = toJson(fc); assertEquals("{\"baseField\":\"baseValue\",\"childField\":\"childValue\",\"finalField\":\"finalValue\"}", str); - baos.reset(); - ChildClass cc = new ChildClass.Builder() // Start fields from the top of the hierarchy to test setter return values .baseField("baseValue") .childField("childValue") .build(); - generator = provider.createGenerator(baos); - cc.serialize(generator, new JsonbJsonpMapper()); - - generator.close(); - str = baos.toString(); + str = toJson(cc); assertEquals("{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}", str); } @Test public void testDeserialization() { - JsonProvider provider = JsonProvider.provider(); - JsonParser parser = provider.createParser(new StringReader( - "{\"baseField\":\"baseValue\",\"childField\":\"childValue\",\"finalField\":\"finalValue\"}")); + String json = "{\"baseField\":\"baseValue\",\"childField\":\"childValue\",\"finalField\":\"finalValue\"}"; - FinalClass fc = FinalClass.JSONP_PARSER.deserialize(parser, new JsonbJsonpMapper()); + FinalClass fc = fromJson(json, FinalClass.JSONP_PARSER); assertEquals("baseValue", fc.baseField()); assertEquals("childValue", fc.childField()); assertEquals("finalValue", fc.finalField()); - parser = provider.createParser(new StringReader( - "{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}")); + json = "{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}"; - ChildClass cc = ChildClass.JSONP_PARSER.deserialize(parser, new JsonbJsonpMapper()); + ChildClass cc = fromJson(json, ChildClass.JSONP_PARSER); assertEquals("baseValue", cc.baseField()); assertEquals("childValue", cc.childField()); 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 50aa79eb93..b42c9b7897 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 @@ -32,18 +32,18 @@ package org.opensearch.client.opensearch.json; -import org.opensearch.client.json.JsonData; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import jakarta.json.JsonString; import jakarta.json.JsonValue; -import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; 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.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch.model.ModelTestCase; import java.io.StringReader; -import java.io.StringWriter; + public class JsonDataTest extends Assert { @@ -69,9 +69,7 @@ public void testSerialize() { String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + "\"stringValue\":\"foo\"}"; - JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json)); - JsonpMapperTest.SomeClass sc = - mapper.deserialize(parser, JsonpMapperTest.SomeClass.class); + JsonpMapperTest.SomeClass sc = ModelTestCase.fromJson(json, JsonpMapperTest.SomeClass.class, mapper); assertEquals("foo", sc.getStringValue()); assertEquals(1, sc.getChildren().size()); @@ -80,13 +78,7 @@ public void testSerialize() { JsonData data = JsonData.of(sc); - StringWriter sw = new StringWriter(); - JsonGenerator generator = mapper.jsonProvider().createGenerator(sw); - - data.serialize(generator, mapper); - generator.close(); - - assertEquals(json, sw.toString()); + assertEquals(json, ModelTestCase.toJson(data, mapper)); } @Test diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java index f7b69e4082..2cdb878e7e 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java @@ -32,25 +32,24 @@ package org.opensearch.client.opensearch.json; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.jackson.JacksonJsonpMapper; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import org.opensearch.client.opensearch.IOUtils; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.MappingJsonFactory; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.datatype.jsonp.JSONPModule; - import jakarta.json.Json; import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import org.junit.Assert; import org.junit.Test; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch.IOUtils; +import org.opensearch.client.opensearch.model.ModelTestCase; import java.io.StringReader; import java.io.StringWriter; @@ -186,20 +185,14 @@ private void testSerialize(JsonpMapper mapper, String expected) { other.setDoubleValue(3.2); something.setChildren(Collections.singletonList(other)); - StringWriter strw = new StringWriter(); - JsonGenerator generator = mapper.jsonProvider().createGenerator(strw); - - mapper.serialize(something, generator); - - generator.close(); + String str = ModelTestCase.toJson(something, mapper); - assertEquals(expected, strw.getBuffer().toString()); + assertEquals(expected, str); } private void testDeserialize(JsonpMapper mapper, String json) { - JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json)); - SomeClass parsed = mapper.deserialize(parser, SomeClass.class); + SomeClass parsed = ModelTestCase.fromJson(json, SomeClass.class, mapper); assertEquals(1, parsed.getIntValue()); assertEquals(2.1, parsed.getDoubleValue(), 0.0); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java index 42930ce102..c501ff12d6 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java @@ -32,6 +32,7 @@ package org.opensearch.client.opensearch.model; +import org.junit.Test; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.SortOptions; @@ -41,14 +42,10 @@ import org.opensearch.client.opensearch._types.query_dsl.SpanGapQuery; import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.indices.IndexSettings; -import org.junit.Test; -import java.io.StringReader; import java.util.ArrayList; import java.util.List; -import jakarta.json.stream.JsonParser; - public class BuiltinTypesTest extends ModelTestCase { @Test @@ -239,10 +236,9 @@ public void testNullableInt() { public void testNullableStringInArray() { // stringOrNullDeserializer allows to handle null events in string arrays String json = "[\"lettuce\", null, \"tomato\"]"; - JsonParser jsonParser = mapper.jsonProvider().createParser(new StringReader(json)); JsonpDeserializer stringDeserializer = JsonpDeserializer.stringOrNullDeserializer(); - List result = JsonpDeserializer.arrayDeserializer(stringDeserializer).deserialize(jsonParser, mapper); + List result = fromJson(json, JsonpDeserializer.arrayDeserializer(stringDeserializer)); List expected = new ArrayList<>(); expected.add("lettuce"); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java index 9ae0230e18..b61b15f723 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java @@ -32,23 +32,20 @@ package org.opensearch.client.opensearch.model; -import org.opensearch.client.opensearch.cat.NodesResponse; -import org.opensearch.client.opensearch.core.GetSourceResponse; -import org.opensearch.client.json.JsonpDeserializable; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpMapperBase; -import org.opensearch.client.json.JsonpUtils; import io.github.classgraph.ClassGraph; import io.github.classgraph.ClassInfo; import io.github.classgraph.ClassInfoList; import io.github.classgraph.ScanResult; import jakarta.json.Json; import jakarta.json.JsonValue; -import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; import org.junit.Test; - -import java.io.StringReader; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapperBase; +import org.opensearch.client.json.JsonpUtils; +import org.opensearch.client.opensearch.cat.NodesResponse; +import org.opensearch.client.opensearch.core.GetSourceResponse; public class SerializationTest extends ModelTestCase { @@ -71,8 +68,7 @@ public void loadAllDeserializers() throws Exception { assertNotNull(deserializer); // Deserialize something dummy to resolve lazy deserializers - JsonParser parser = mapper.jsonProvider().createParser(new StringReader("-")); - assertThrows(JsonParsingException.class, () -> deserializer.deserialize(parser, mapper)); + assertThrows(JsonParsingException.class, () -> fromJson("-", deserializer)); } // Check that all classes that have a _DESERIALIZER field also have the annotation @@ -119,7 +115,7 @@ public void testGenericValueBody() { JsonpDeserializer> deserializer = GetSourceResponse.createGetSourceResponseDeserializer(JsonpDeserializer.stringDeserializer()); - r = deserializer.deserialize(mapper.jsonProvider().createParser(new StringReader(json)), mapper); + r = fromJson(json, deserializer); assertEquals("The value", r.valueBody());