Skip to content

Commit

Permalink
refactor: use ModelTestCase in tests for json serialization/deseriali…
Browse files Browse the repository at this point in the history
…zation

Signed-off-by: Maksim Strutovskii <[email protected]>
  • Loading branch information
Mstrutov committed Jun 15, 2023
1 parent aec22a8 commit b7d50ca
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,15 @@

package org.opensearch.client.opensearch.experiments;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

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;

public class ParsingTests extends Assert {
public class ParsingTests extends ModelTestCase {

@Test
public void testFoo() {
Expand All @@ -62,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());
Expand All @@ -81,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());
Expand All @@ -108,33 +100,16 @@ 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());
assertEquals(mapping.nestedObjects().limit(), deserialized.nestedObjects().limit());
assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit());
}

private <T extends JsonpSerializable> T deserialize(String serializedValue, JsonpDeserializer<T> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -67,28 +60,20 @@ 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);

}

@Test
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,76 +32,53 @@

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")
.childField("childValue")
.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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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());
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<String> stringDeserializer = JsonpDeserializer.stringOrNullDeserializer();

List<String> result = JsonpDeserializer.arrayDeserializer(stringDeserializer).deserialize(jsonParser, mapper);
List<String> result = fromJson(json, JsonpDeserializer.arrayDeserializer(stringDeserializer));

List<String> expected = new ArrayList<>();
expected.add("lettuce");
Expand Down
Loading

0 comments on commit b7d50ca

Please sign in to comment.