diff --git a/CHANGELOG.md b/CHANGELOG.md index d6063eabc2..8705d44028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added - Add support for knn_vector field type ([#529](https://github.com/opensearch-project/opensearch-java/pull/524)) +- Add translog option object and missing translog sync interval option in index settings ([#518](https://github.com/opensearch-project/opensearch-java/pull/518)) ### Dependencies - Bumps `jackson` from 2.14.2 to 2.15.2 ((#537)[https://github.com/opensearch-project/opensearch-java/pull/537]) @@ -36,6 +37,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Changed ### Deprecated +- Deprecate translogDurability and translogFlushThresholdSize in IndexSettings in favor of a separate translog object ([#518](https://github.com/opensearch-project/opensearch-java/pull/518)) ### Removed diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java b/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java index 4b7e26558d..e0ec17254a 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java @@ -36,20 +36,21 @@ package org.opensearch.client.opensearch.indices; -import org.opensearch.client.opensearch._types.Time; +import jakarta.json.stream.JsonGenerator; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; import org.opensearch.client.util.ObjectBuilderBase; -import jakarta.json.stream.JsonGenerator; + +import javax.annotation.Nullable; import java.util.List; import java.util.function.Function; -import javax.annotation.Nullable; // typedef: indices._types.IndexSettings @@ -199,10 +200,7 @@ public class IndexSettings implements JsonpSerializable { private final Integer maxSlicesPerScroll; @Nullable - private final String translogDurability; - - @Nullable - private final String translogFlushThresholdSize; + private final Translog translog; @Nullable private final Boolean queryStringLenient; @@ -280,8 +278,7 @@ private IndexSettings(Builder builder) { this.verifiedBeforeClose = builder.verifiedBeforeClose; this.format = builder.format; this.maxSlicesPerScroll = builder.maxSlicesPerScroll; - this.translogDurability = builder.translogDurability; - this.translogFlushThresholdSize = builder.translogFlushThresholdSize; + this.translog = builder.translog; this.queryStringLenient = builder.queryStringLenient; this.priority = builder.priority; this.topMetricsMaxSize = builder.topMetricsMaxSize; @@ -680,20 +677,32 @@ public final Integer maxSlicesPerScroll() { return this.maxSlicesPerScroll; } + /** + * API name: {@code translog} + */ + @Nullable + public final Translog translog() { + return this.translog; + } + /** * API name: {@code translog.durability} + * @deprecated use {@link #translog()} instead */ + @Deprecated @Nullable public final String translogDurability() { - return this.translogDurability; + return this.translog != null ? this.translog.durability() : null; } /** * API name: {@code translog.flush_threshold_size} + * @deprecated use {@link #translog()} instead */ + @Deprecated @Nullable public final String translogFlushThresholdSize() { - return this.translogFlushThresholdSize; + return this.translog != null ? this.translog.flushThresholdSize() : null; } /** @@ -1016,14 +1025,9 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write(this.maxSlicesPerScroll); } - if (this.translogDurability != null) { - generator.writeKey("translog.durability"); - generator.write(this.translogDurability); - - } - if (this.translogFlushThresholdSize != null) { - generator.writeKey("translog.flush_threshold_size"); - generator.write(this.translogFlushThresholdSize); + if (this.translog != null) { + generator.writeKey("translog"); + this.translog.serialize(generator, mapper); } if (this.queryStringLenient != null) { @@ -1221,10 +1225,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder b.durability(value)); + } else { + translog = Translog.of(b -> b.durability(value) + .flushThresholdSize(translog.flushThresholdSize()) + .syncInterval(translog.syncInterval())); + } return this; } /** * API name: {@code translog.flush_threshold_size} + * @deprecated use {@link #translog()} instead */ + @Deprecated public final Builder translogFlushThresholdSize(@Nullable String value) { - this.translogFlushThresholdSize = value; + if (translog == null) { + translog = Translog.of(b -> b.flushThresholdSize(value)); + } else { + translog = Translog.of(b -> b.flushThresholdSize(value) + .durability(translog.durability()) + .syncInterval(translog.syncInterval())); + } return this; } @@ -1920,6 +1945,7 @@ protected static void setupIndexSettingsDeserializer(ObjectDeserializer> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * API name: {@code durability} + */ + @Nullable + public final String durability() { + return this.durability; + } + + /** + * API name: {@code flush_threshold_size} + */ + @Nullable + public final String flushThresholdSize() { + return this.flushThresholdSize; + } + + /** + * API name: {@code sync_interval} + */ + @Nullable + public final Time syncInterval() { + return this.syncInterval; + } + + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + + if (this.durability != null) { + generator.writeKey("durability"); + generator.write(this.durability); + + } + if (this.flushThresholdSize != null) { + generator.writeKey("flush_threshold_size"); + generator.write(this.flushThresholdSize); + + } + if (this.syncInterval != null) { + generator.writeKey("sync_interval"); + this.syncInterval.serialize(generator, mapper); + + } + + } + + // --------------------------------------------------------------------------------------------- + + /** + * Builder for {@link Translog}. + */ + public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + + @Nullable + private String durability; + + @Nullable + private String flushThresholdSize; + + @Nullable + private Time syncInterval; + + /** + * API name: {@code durability} + */ + public final Translog.Builder durability(@Nullable String value) { + this.durability = value; + return this; + } + + /** + * API name: {@code flush_threshold_size} + */ + public final Translog.Builder flushThresholdSize(@Nullable String value) { + this.flushThresholdSize = value; + return this; + } + + /** + * API name: {@code sync_interval} + */ + public final Translog.Builder syncInterval(@Nullable Time value) { + this.syncInterval = value; + return this; + } + + /** + * Builds a {@link Translog}. + * + * @throws NullPointerException + * if some of the required fields are null. + */ + public Translog build() { + _checkSingleUse(); + + return new Translog(this); + } + } + + /** + * Json deserializer for {@link Translog} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new, + Translog::setupTranslogDeserializer); + + protected static void setupTranslogDeserializer(ObjectDeserializer op) { + + op.add(Translog.Builder::durability, JsonpDeserializer.stringDeserializer(), "durability"); + op.add(Translog.Builder::flushThresholdSize, JsonpDeserializer.stringDeserializer(), "flush_threshold_size"); + op.add(Translog.Builder::syncInterval, Time._DESERIALIZER, "sync_interval"); + + } + +} 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 6bbf1917a8..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,96 +32,84 @@ package org.opensearch.client.opensearch.experiments; -import jakarta.json.spi.JsonProvider; -import jakarta.json.stream.JsonGenerator; -import jakarta.json.stream.JsonParser; -import jakarta.json.stream.JsonParsingException; -import org.junit.Assert; import org.junit.Test; -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; - -public class ParsingTests extends Assert { +public class ParsingTests extends ModelTestCase { @Test - public void testFoo() throws Exception { - - try { - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - FooRequest foo = FooRequest.builder() - .name("z") - .value(1) - .indices("a", "b", "c") - .bar(b -> b - .name("Raise the bar") - ) - .build(); - - JsonProvider provider = JsonProvider.provider(); - JsonGenerator generator = provider.createGenerator(baos); - foo.serialize(generator, new JsonbJsonpMapper()); - - generator.close(); - String str = baos.toString(); - - assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str); - - JsonParser parser = provider.createParser(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))); - - FooRequest foo2 = FooRequest.parser().deserialize(parser, new JsonbJsonpMapper()); + public void testFoo() { + + FooRequest foo = FooRequest.builder() + .name("z") + .value(1) + .indices("a", "b", "c") + .bar(b -> b + .name("Raise the bar") + ) + .build(); + + String str = toJson(foo); + assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str); + + FooRequest foo2 = fromJson(str, FooRequest.parser()); + assertEquals(foo.name(), foo2.name()); + assertEquals(foo.value(), foo2.value()); + assertNull(foo2.size()); + assertEquals(foo.indices(), foo2.indices()); + assertEquals("Raise the bar", foo.bar().name()); + } - assertEquals(foo.name(), foo2.name()); - assertEquals(foo.value(), foo2.value()); - assertNull(foo2.size()); - assertEquals(foo.indices(), foo2.indices()); - assertEquals("Raise the bar", foo.bar().name()); - } catch (JsonParsingException je) { - throw new JsonParsingException(je.getMessage() + " at " + je.getLocation(), je, je.getLocation()); - } + @Test + public void testIndexSettingsTranslogOptionsParsing() { + + var indexSettings = IndexSettings.of(_1 -> _1.translog(Translog.of(tr -> tr + .syncInterval(Time.of(t -> t.time("10s"))) + .durability("async") + .flushThresholdSize("256mb")))); + + var str = toJson(indexSettings); + assertEquals("{\"translog\":{\"durability\":\"async\",\"flush_threshold_size\":\"256mb\"," + + "\"sync_interval\":\"10s\"}}", str); + + 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 = 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()); } @Test public void testIndexSettingsMappingParsing() { - try { - - var baos = new ByteArrayOutputStream(); - - var mapping = IndexSettingsMapping.of(b -> b - .totalFields(d -> d.limit(1L)) - .depth(d -> d.limit(2L)) - .nestedFields(d -> d.limit(3L)) - .nestedObjects(d -> d.limit(4L)) - .fieldNameLength(d -> d.limit(5L))); - - var provider = JsonProvider.provider(); - var generator = provider.createGenerator(baos); - mapping.serialize(generator, new JsonbJsonpMapper()); - - generator.close(); - var str = baos.toString(); - - assertEquals("{\"total_fields\":{\"limit\":1},\"depth\":{\"limit\":2},\"nested_fields\":{\"limit\":3}," + - "\"nested_objects\":{\"limit\":4},\"field_name_length\":{\"limit\":5}}", str); - - var parser = provider.createParser(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))); - - var deserialized = IndexSettingsMapping._DESERIALIZER.deserialize(parser, new JsonbJsonpMapper()); - - assertEquals(mapping.totalFields().limit(), deserialized.totalFields().limit()); - assertEquals(mapping.depth().limit(), deserialized.depth().limit()); - assertEquals(mapping.nestedFields().limit(), deserialized.nestedFields().limit()); - assertEquals(mapping.nestedObjects().limit(), deserialized.nestedObjects().limit()); - assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit()); - } catch (JsonParsingException je) { - throw new JsonParsingException(je.getMessage() + " at " + je.getLocation(), je, je.getLocation()); - } + var mapping = IndexSettingsMapping.of(b -> b + .totalFields(d -> d.limit(1L)) + .depth(d -> d.limit(2L)) + .nestedFields(d -> d.limit(3L)) + .nestedObjects(d -> d.limit(4L)) + .fieldNameLength(d -> d.limit(5L))); + + 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 = 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()); + assertEquals(mapping.nestedObjects().limit(), deserialized.nestedObjects().limit()); + assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit()); } + } 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/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index c8529fc470..c52908bbcb 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -82,6 +82,7 @@ import org.opensearch.client.opensearch.indices.IndexSettings; import org.opensearch.client.opensearch.indices.IndexSettingsAnalysis; import org.opensearch.client.opensearch.indices.IndexState; +import org.opensearch.client.opensearch.indices.Translog; import org.opensearch.client.opensearch.model.ModelTestCase; import org.opensearch.client.transport.endpoints.BooleanResponse; @@ -126,10 +127,11 @@ public void testIndexCreation() throws Exception { } @Test - public void testIndexSettingsMapping() throws Exception { + public void testIndexSettings() throws Exception { var createResponse = javaClient().indices() .create(r -> r.index("test-settings") .settings(s -> s + .translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("10s"))))) .mapping(m -> m .fieldNameLength(f -> f.limit(300L)) .totalFields(f -> f.limit(30L)) @@ -145,6 +147,9 @@ public void testIndexSettingsMapping() throws Exception { assertNotNull(createdIndexSettings); assertNotNull(createdIndexSettings.settings()); assertNotNull(createdIndexSettings.settings().index()); + assertNotNull(createdIndexSettings.settings().index().translog()); + assertNotNull(createdIndexSettings.settings().index().translog().syncInterval()); + assertEquals("10s", createdIndexSettings.settings().index().translog().syncInterval().time()); var createdMappingSettings = createdIndexSettings.settings().index().mapping(); assertNotNull(createdMappingSettings); assertNotNull(createdMappingSettings.fieldNameLength()); @@ -161,6 +166,7 @@ public void testIndexSettingsMapping() throws Exception { var putSettingsResponse = javaClient().indices().putSettings(r -> r .index("test-settings") .settings(s -> s + .translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("5s"))))) .mapping(m -> m .fieldNameLength(f -> f.limit(400L)) .totalFields(f -> f.limit(130L)) @@ -175,6 +181,9 @@ public void testIndexSettingsMapping() throws Exception { assertNotNull(updatedIndexSettings); assertNotNull(updatedIndexSettings.settings()); assertNotNull(updatedIndexSettings.settings().index()); + assertNotNull(updatedIndexSettings.settings().index().translog()); + assertNotNull(updatedIndexSettings.settings().index().translog().syncInterval()); + assertEquals("5s", updatedIndexSettings.settings().index().translog().syncInterval().time()); var updatedMappingSettings = updatedIndexSettings.settings().index().mapping(); assertNotNull(updatedMappingSettings); assertNotNull(updatedMappingSettings.fieldNameLength()); @@ -557,7 +566,7 @@ public void testDefaultIndexSettings() throws IOException { public void testCompletionSuggesterFailure() throws IOException { String index = "test-completion-suggester-failure"; - + Property intValueProp = new Property.Builder() .long_(v -> v) @@ -623,7 +632,7 @@ public void testPit() throws IOException { } assumeTrue("The PIT is supported in OpenSearch 2.4.0 and later", Version.fromString(version).onOrAfter(Version.fromString("2.4.0"))); - + String index = "test-point-in-time"; javaClient().indices().create(c -> c @@ -736,7 +745,7 @@ public void testCompletionSuggester() throws IOException { public void testTermSuggester() throws IOException { String index = "test-term-suggester"; - + // term suggester does not require a special mapping javaClient().indices().create(c -> c .index(index)); @@ -818,7 +827,7 @@ public void testPhraseSuggester() throws IOException { .text(new TextProperty.Builder().analyzer("trigram").build()) .build()).build()) .build()).build(); - + javaClient().indices().create(c -> c .index(index) 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());