From 463821b0023e7524716905c67ce8a829a8f1bdd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristiano=20V=2E=20Gavi=C3=A3o?= Date: Tue, 16 Mar 2021 16:04:01 -0300 Subject: [PATCH 1/4] Fix #295 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cristiano V. Gavião --- .../io/vertx/codegen/type/EnumTypeInfo.java | 5 +- .../vertx/codegen/type/TypeMirrorFactory.java | 11 ++- .../codegen/type/TypeReflectionFactory.java | 3 +- .../codegen/testmodel/JsonMapperTCK.java | 33 +++++++ .../codegen/testmodel/JsonMapperTCKImpl.java | 97 +++++++++++++++++++ .../codegen/testmodel/TestCustomEnum.java | 34 +++++++ .../META-INF/vertx/json-mappers.properties | 2 + .../io/vertx/test/codegen/DataObjectTest.java | 20 ++++ .../java/io/vertx/test/codegen/EnumTest.java | 29 +++++- .../MyEnumWithCustomConstructor.java | 40 ++++++++ .../WithMyCustomEnumWithMapper.java | 27 ++++++ .../DataObjectWithEnumWithMapper.java | 44 +++++++++ .../MyEnumWithCustomConstructor.java | 31 ++++++ 13 files changed, 368 insertions(+), 8 deletions(-) create mode 100644 src/tck/java/io/vertx/codegen/testmodel/TestCustomEnum.java create mode 100644 src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java create mode 100644 src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java create mode 100644 src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java create mode 100644 src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java diff --git a/src/main/java/io/vertx/codegen/type/EnumTypeInfo.java b/src/main/java/io/vertx/codegen/type/EnumTypeInfo.java index 816d3fec3..be1b91b99 100644 --- a/src/main/java/io/vertx/codegen/type/EnumTypeInfo.java +++ b/src/main/java/io/vertx/codegen/type/EnumTypeInfo.java @@ -13,8 +13,9 @@ public class EnumTypeInfo extends ClassTypeInfo { final List values; final boolean gen; - public EnumTypeInfo(String fqcn, boolean gen, List values, ModuleInfo module, boolean nullable) { - super(ClassKind.ENUM, fqcn, module, nullable, Collections.emptyList(), null); + public EnumTypeInfo(String fqcn, boolean gen, List values, + ModuleInfo module, boolean nullable, DataObjectInfo dataObject) { + super(ClassKind.ENUM, fqcn, module, nullable, Collections.emptyList(), dataObject); this.gen = gen; this.values = values; diff --git a/src/main/java/io/vertx/codegen/type/TypeMirrorFactory.java b/src/main/java/io/vertx/codegen/type/TypeMirrorFactory.java index 911e6b73c..ba716d511 100644 --- a/src/main/java/io/vertx/codegen/type/TypeMirrorFactory.java +++ b/src/main/java/io/vertx/codegen/type/TypeMirrorFactory.java @@ -10,7 +10,6 @@ import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -25,7 +24,6 @@ public class TypeMirrorFactory { private static final ModuleInfo VERTX_CORE_MOD = new ModuleInfo("io.vertx.core", "vertx", "io.vertx", false); private static final ClassTypeInfo JSON_OBJECT = new ClassTypeInfo(ClassKind.JSON_OBJECT, "io.vertx.core.json.JsonObject", VERTX_CORE_MOD, false, Collections.emptyList(), null); - private static final ClassTypeInfo JSON_ARRAY = new ClassTypeInfo(ClassKind.JSON_ARRAY, "io.vertx.core.json.JsonArray", VERTX_CORE_MOD, false, Collections.emptyList(), null); private static final ClassTypeInfo STRING = new ClassTypeInfo(ClassKind.STRING, "java.lang.String", null, false, Collections.emptyList(), null); final Elements elementUtils; @@ -100,7 +98,6 @@ public TypeInfo create(TypeUse use, DeclaredType type, boolean checkTypeArgs) { PackageElement pkgElt = elementUtils.getPackageOf(elt); ModuleInfo module = ModuleInfo.resolve(elementUtils, pkgElt); String fqcn = elt.getQualifiedName().toString(); - String simpleName = elt.getSimpleName().toString(); boolean proxyGen = elt.getAnnotation(ProxyGen.class) != null; if (elt.getKind() == ElementKind.ENUM) { ArrayList values = new ArrayList<>(); @@ -110,7 +107,13 @@ public TypeInfo create(TypeUse use, DeclaredType type, boolean checkTypeArgs) { } } boolean gen = elt.getAnnotation(VertxGen.class) != null; - return new EnumTypeInfo(fqcn, gen, values, module, nullable); + MapperInfo serializer = serializers.get(type.toString()); + MapperInfo deserializer = deserializers.get(type.toString()); + DataObjectInfo dataObject = null; + if (serializer != null || deserializer != null) { + dataObject = new DataObjectInfo(false, serializer, deserializer); + } + return new EnumTypeInfo(fqcn, gen, values, module, nullable, dataObject); } else { ClassKind kind = ClassKind.getKind(fqcn, elt.getAnnotation(VertxGen.class) != null); List typeArgs = type.getTypeArguments(); diff --git a/src/main/java/io/vertx/codegen/type/TypeReflectionFactory.java b/src/main/java/io/vertx/codegen/type/TypeReflectionFactory.java index cd3ed0bf4..79e24bd2d 100644 --- a/src/main/java/io/vertx/codegen/type/TypeReflectionFactory.java +++ b/src/main/java/io/vertx/codegen/type/TypeReflectionFactory.java @@ -61,7 +61,8 @@ public static TypeInfo create(Type type) { classType.getDeclaredAnnotation(VertxGen.class) != null, Stream.of(classType.getEnumConstants()).map(Object::toString).collect(Collectors.toList()), module, - false + false, + null ); } else { ClassKind kind = ClassKind.getKind(fqcn, classType.getAnnotation(VertxGen.class) != null); diff --git a/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCK.java b/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCK.java index 7198bf30b..02109daff 100644 --- a/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCK.java +++ b/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCK.java @@ -36,6 +36,7 @@ static MyPojoToJsonObject deserializeMyPojoToJsonObject(JsonObject value) { return new MyPojoToJsonObject(value.getInteger("v")); } + @SuppressWarnings({ "unchecked", "rawtypes" }) @GenIgnore static JsonArray serializeMyPojoToJsonArray(MyPojoToJsonArray value) { return new JsonArray((List)value.stuff); @@ -56,6 +57,16 @@ static ZonedDateTime deserializeZonedDateTime(String value) { return ZonedDateTime.parse(value); } + @GenIgnore + static String serializeCustomEnum(TestCustomEnum value) { + return (value != null) ? value.getShortName() : null; + } + + @GenIgnore + static TestCustomEnum deserializeCustomEnum(String value) { + return (value != null) ? TestCustomEnum.of(value) : null; + } + // Java Type <-> Integer void methodWithTypeToIntegerParam(MyPojoToInteger myPojoToInteger); @@ -144,4 +155,26 @@ static ZonedDateTime deserializeZonedDateTime(String value) { void methodWithHandlerAsyncResultSetOfTypeToJsonObjectParam(Handler>> myPojoToJsonObjectSetHandler); void methodWithHandlerAsyncResultMapOfTypeToJsonObjectParam(Handler>> myPojoToJsonObjectMapHandler); + // CustomEnum <-> String + + void methodWithCustomEnumToStringParam(TestCustomEnum customEnum); + void methodWithListOfCustomEnumToStringParam(List customEnumList); + void methodWithSetOfCustomEnumToStringParam(Set customEnumSet); + void methodWithMapOfCustomEnumToStringParam(Map customEnumMap); + + TestCustomEnum methodWithCustomEnumToStringReturn(); + List methodWithListOfCustomEnumToStringReturn(); + Set methodWithSetOfCustomEnumToStringReturn(); + Map methodWithMapOfCustomEnumToStringReturn(); + + void methodWithHandlerCustomEnumToStringParam(Handler customEnumHandler); + void methodWithHandlerListOfCustomEnumToStringParam(Handler> customEnumListHandler); + void methodWithHandlerSetOfCustomEnumToStringParam(Handler> customEnumSetHandler); + void methodWithHandlerMapOfCustomEnumToStringParam(Handler> customEnumMapHandler); + + void methodWithHandlerAsyncResultCustomEnumToStringParam(Handler> customEnumHandler); + void methodWithHandlerAsyncResultListOfCustomEnumToStringParam(Handler>> customEnumListHandler); + void methodWithHandlerAsyncResultSetOfCustomEnumToStringParam(Handler>> customEnumSetHandler); + void methodWithHandlerAsyncResultMapOfCustomEnumToStringParam(Handler>> customEnumMapHandler); + } diff --git a/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCKImpl.java b/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCKImpl.java index 73278bb93..d190f5a36 100644 --- a/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCKImpl.java +++ b/src/tck/java/io/vertx/codegen/testmodel/JsonMapperTCKImpl.java @@ -366,4 +366,101 @@ public void methodWithHandlerAsyncResultSetOfTypeToJsonObjectParam(Handler>> myPojoToJsonObjectMapHandler) { myPojoToJsonObjectMapHandler.handle(Future.succeededFuture(methodWithMapOfTypeToJsonObjectReturn())); } + + @Override + public void methodWithCustomEnumToStringParam(TestCustomEnum customEnum) { + assertEquals(TestCustomEnum.of("development"), customEnum); + } + + @Override + public void methodWithListOfCustomEnumToStringParam(List customEnumList) { + assertEquals(2, customEnumList.size()); + assertEquals(TestCustomEnum.of("development"), customEnumList.get(0)); + assertEquals(TestCustomEnum.of("integration-test"), customEnumList.get(1)); + + } + + @Override + public void methodWithSetOfCustomEnumToStringParam(Set customEnumSet) { + assertEquals(2, customEnumSet.size()); + assertTrue(customEnumSet.contains(TestCustomEnum.of("development"))); + assertTrue(customEnumSet.contains(TestCustomEnum.of("integration-test"))); + + } + + @Override + public void methodWithMapOfCustomEnumToStringParam(Map customEnumMap) { + assertEquals(2, customEnumMap.size()); + assertEquals(TestCustomEnum.of("development"), customEnumMap.get("dev")); + assertEquals(TestCustomEnum.of("integration-test"), customEnumMap.get("itest")); + } + + @Override + public TestCustomEnum methodWithCustomEnumToStringReturn() { + return TestCustomEnum.of("development"); + } + + @Override + public List methodWithListOfCustomEnumToStringReturn() { + return Arrays.asList(TestCustomEnum.of("development"), TestCustomEnum.of("integration-test")); + } + + @Override + public Set methodWithSetOfCustomEnumToStringReturn() { + return new HashSet<>(methodWithListOfCustomEnumToStringReturn()); + } + + @Override + public Map methodWithMapOfCustomEnumToStringReturn() { + Map map = new HashMap<>(); + map.put("dev", TestCustomEnum.DEV); + map.put("itest", TestCustomEnum.ITEST); + return map; + } + + @Override + public void methodWithHandlerCustomEnumToStringParam(Handler customEnumHandler) { + customEnumHandler.handle(methodWithCustomEnumToStringReturn()); + } + + @Override + public void methodWithHandlerListOfCustomEnumToStringParam(Handler> customEnumListHandler) { + customEnumListHandler.handle(methodWithListOfCustomEnumToStringReturn()); + } + + @Override + public void methodWithHandlerSetOfCustomEnumToStringParam(Handler> customEnumSetHandler) { + customEnumSetHandler.handle(methodWithSetOfCustomEnumToStringReturn()); + } + + @Override + public void methodWithHandlerMapOfCustomEnumToStringParam( + Handler> customEnumMapHandler) { + customEnumMapHandler.handle(methodWithMapOfCustomEnumToStringReturn()); + } + + @Override + public void methodWithHandlerAsyncResultCustomEnumToStringParam( + Handler> customEnumHandler) { + customEnumHandler.handle(Future.succeededFuture(methodWithCustomEnumToStringReturn())); + } + + @Override + public void methodWithHandlerAsyncResultListOfCustomEnumToStringParam( + Handler>> customEnumListHandler) { + customEnumListHandler.handle(Future.succeededFuture(methodWithListOfCustomEnumToStringReturn())); + } + + @Override + public void methodWithHandlerAsyncResultSetOfCustomEnumToStringParam( + Handler>> customEnumSetHandler) { + customEnumSetHandler.handle(Future.succeededFuture(methodWithSetOfCustomEnumToStringReturn())); + } + + @Override + public void methodWithHandlerAsyncResultMapOfCustomEnumToStringParam( + Handler>> customEnumMapHandler) { + customEnumMapHandler.handle(Future.succeededFuture(methodWithMapOfCustomEnumToStringReturn())); + } + } diff --git a/src/tck/java/io/vertx/codegen/testmodel/TestCustomEnum.java b/src/tck/java/io/vertx/codegen/testmodel/TestCustomEnum.java new file mode 100644 index 000000000..892f771c7 --- /dev/null +++ b/src/tck/java/io/vertx/codegen/testmodel/TestCustomEnum.java @@ -0,0 +1,34 @@ +package io.vertx.codegen.testmodel; + +public enum TestCustomEnum { + + DEV("dev", "development"), + + ITEST("itest", "integration-test"); + + public static TestCustomEnum of(String pName) { + for (TestCustomEnum item : TestCustomEnum.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + + private String[] names = new String[2]; + + TestCustomEnum(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + +} diff --git a/src/tck/resources/META-INF/vertx/json-mappers.properties b/src/tck/resources/META-INF/vertx/json-mappers.properties index 56d6979a4..a2c58db66 100644 --- a/src/tck/resources/META-INF/vertx/json-mappers.properties +++ b/src/tck/resources/META-INF/vertx/json-mappers.properties @@ -6,3 +6,5 @@ io.vertx.codegen.testmodel.MyPojoToJsonArray.serializer=io.vertx.codegen.testmod io.vertx.codegen.testmodel.MyPojoToJsonArray.deserializer=io.vertx.codegen.testmodel.JsonMapperTCK#deserializeMyPojoToJsonArray java.time.ZonedDateTime.serializer=io.vertx.codegen.testmodel.JsonMapperTCK#serializeZonedDateTime java.time.ZonedDateTime.deserializer=io.vertx.codegen.testmodel.JsonMapperTCK#deserializeZonedDateTime +io.vertx.codegen.testmodel.TestCustomEnum.serializer=io.vertx.codegen.testmodel.JsonMapperTCK#serializeCustomEnum +io.vertx.codegen.testmodel.TestCustomEnum.deserializer=io.vertx.codegen.testmodel.JsonMapperTCK#deserializeCustomEnum diff --git a/src/test/java/io/vertx/test/codegen/DataObjectTest.java b/src/test/java/io/vertx/test/codegen/DataObjectTest.java index bafc45061..db1806456 100644 --- a/src/test/java/io/vertx/test/codegen/DataObjectTest.java +++ b/src/test/java/io/vertx/test/codegen/DataObjectTest.java @@ -15,7 +15,10 @@ import io.vertx.test.codegen.testdataobject.*; import io.vertx.test.codegen.testdataobject.imported.Imported; import io.vertx.test.codegen.testdataobject.jsonmapper.DataObjectWithPojoWithMapper; +import io.vertx.test.codegen.testdataobject.jsonmapper.MyEnumWithCustomConstructor; import io.vertx.test.codegen.testdataobject.jsonmapper.MyPojo; +import io.vertx.test.codegen.testdataobject.jsonmapper.DataObjectWithEnumWithMapper; + import org.junit.Test; import java.time.Instant; @@ -892,6 +895,23 @@ public void testDataObjectWithJsonMapper() throws Exception { assertTrue(myPojoProperty.getType().getDataObject().isSerializable()); } + @Test + public void testCustomEnumWithJsonMapper() throws Exception { + DataObjectModel model = new GeneratorHelper() + .registerConverter(MyEnumWithCustomConstructor.class, DataObjectWithEnumWithMapper.class.getName(), "serializeMyEnumWithCustomConstructor") + .registerConverter(MyEnumWithCustomConstructor.class, DataObjectWithEnumWithMapper.class.getName(), "deserializeMyEnumWithCustomConstructor") + .generateDataObject(DataObjectWithEnumWithMapper.class); + assertNotNull(model); + assertTrue(model.isClass()); + assertTrue(model.getGenerateConverter()); + assertTrue(model.isPublicConverter()); + + PropertyInfo myPojoProperty = model.getPropertyMap().get("customEnum"); + assertEquals(ClassKind.ENUM, myPojoProperty.getType().getKind()); + assertTrue(myPojoProperty.getType().getDataObject().isDeserializable()); + assertTrue(myPojoProperty.getType().getDataObject().isSerializable()); + } + private void assertInvalidDataObject(Class dataObjectClass) throws Exception { try { new GeneratorHelper().generateDataObject(dataObjectClass); diff --git a/src/test/java/io/vertx/test/codegen/EnumTest.java b/src/test/java/io/vertx/test/codegen/EnumTest.java index 63add4158..17e889344 100644 --- a/src/test/java/io/vertx/test/codegen/EnumTest.java +++ b/src/test/java/io/vertx/test/codegen/EnumTest.java @@ -1,8 +1,12 @@ package io.vertx.test.codegen; import io.vertx.codegen.*; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.codegen.type.ClassKind; import io.vertx.codegen.type.EnumTypeInfo; import io.vertx.codegen.type.TypeInfo; +import io.vertx.test.codegen.testapi.jsonmapper.MyEnumWithCustomConstructor; +import io.vertx.test.codegen.testapi.jsonmapper.WithMyCustomEnumWithMapper; import io.vertx.test.codegen.testenum.EnumAsParam; import io.vertx.test.codegen.testenum.InvalidEmptyEnum; import io.vertx.test.codegen.testenum.ValidEnum; @@ -16,7 +20,7 @@ /** * @author Julien Viet */ -public class EnumTest { +public class EnumTest extends ClassTestBase { @Test public void testEnum() throws Exception { @@ -34,6 +38,29 @@ public void testEnum() throws Exception { assertTrue(model.getType().isGen()); } + @Test + public void testJsonMapper() throws Exception { + ClassModel model = new GeneratorHelper() + .registerConverter(MyEnumWithCustomConstructor.class, WithMyCustomEnumWithMapper.class.getName(), "serializeMyEnumWithCustomConstructor") + .registerConverter(MyEnumWithCustomConstructor.class, WithMyCustomEnumWithMapper.class.getName(), "deserializeMyEnumWithCustomConstructor") + .generateClass(WithMyCustomEnumWithMapper.class); + assertFalse(model.getAnnotations().isEmpty()); + assertEquals(1, model.getAnnotations().size()); + assertEquals(VertxGen.class.getName(), model.getAnnotations().get(0).getName()); + + assertEquals(1, model.getReferencedDataObjectTypes().size()); + assertEquals("MyEnumWithCustomConstructor", model.getReferencedDataObjectTypes().iterator().next().getSimpleName()); + + checkMethod(model.getMethodMap().get("returnMyEnumWithCustomConstructor").get(0), + "returnMyEnumWithCustomConstructor", 0, new TypeLiteral() {}, MethodKind.OTHER); + + MethodInfo myPojoParam = model.getMethodMap().get("setMyEnumWithCustomConstructor").get(0); + checkMethod(myPojoParam, "setMyEnumWithCustomConstructor", 1, "void", MethodKind.OTHER); + checkParam(myPojoParam.getParam(0), "myEnum", MyEnumWithCustomConstructor.class.getName(), ClassKind.ENUM); + + + } + @Test public void testInvalidEmptyEnum() throws Exception { try { diff --git a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java new file mode 100644 index 000000000..9760af235 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java @@ -0,0 +1,40 @@ +package io.vertx.test.codegen.testapi.jsonmapper; + +import io.vertx.codegen.annotations.VertxGen; + +/**MyEnumWithCustomConstructor doc*/ +@VertxGen +public enum MyEnumWithCustomConstructor { + + /**DEV doc*/ + DEV("dev", "development"), + + /**ITEST doc*/ + ITEST("itest", "integration-test"); + + public static MyEnumWithCustomConstructor of(String pName) { + for (MyEnumWithCustomConstructor item : MyEnumWithCustomConstructor.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + + private String[] names = new String[2]; + + MyEnumWithCustomConstructor(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + +} diff --git a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java new file mode 100644 index 000000000..f01e8ab1b --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java @@ -0,0 +1,27 @@ +package io.vertx.test.codegen.testapi.jsonmapper; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import io.vertx.codegen.annotations.GenIgnore; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.AsyncResult; +import io.vertx.core.Handler; + +@VertxGen +public interface WithMyCustomEnumWithMapper { + + @GenIgnore + public static MyEnumWithCustomConstructor deserializeMyEnumWithCustomConstructor(String value) { + return (value != null) ? MyEnumWithCustomConstructor.of(value) : null; + } + + @GenIgnore + public static String serializeMyEnumWithCustomConstructor(MyEnumWithCustomConstructor value) { + return (value != null) ? value.getLongName() : null; + } + + MyEnumWithCustomConstructor returnMyEnumWithCustomConstructor(); + void setMyEnumWithCustomConstructor(MyEnumWithCustomConstructor myEnum); +} diff --git a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java new file mode 100644 index 000000000..496abf002 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java @@ -0,0 +1,44 @@ +package io.vertx.test.codegen.testdataobject.jsonmapper; + +import io.vertx.codegen.annotations.DataObject; +import io.vertx.codegen.annotations.GenIgnore; +import io.vertx.core.json.JsonObject; + +@DataObject(generateConverter = true, publicConverter = true) +public class DataObjectWithEnumWithMapper { + + @GenIgnore + public static MyEnumWithCustomConstructor deserializeMyEnumWithCustomConstructor(String value) { + return (value != null) ? MyEnumWithCustomConstructor.of(value) : null; + } + + @GenIgnore + public static String serializeMyEnumWithCustomConstructor(MyEnumWithCustomConstructor value) { + return (value != null) ? value.getLongName() : null; + } + + MyEnumWithCustomConstructor customEnum; + + public DataObjectWithEnumWithMapper(JsonObject customEnum) { + + } + + public DataObjectWithEnumWithMapper(MyEnumWithCustomConstructor customEnum) { + super(); + this.customEnum = customEnum; + } + + public MyEnumWithCustomConstructor getCustomEnum() { + return customEnum; + } + + public DataObjectWithEnumWithMapper setCustomEnum(MyEnumWithCustomConstructor a) { + this.customEnum = a; + return this; + } + + public JsonObject toJson() { + return null; + } + +} diff --git a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java new file mode 100644 index 000000000..3d9e3224a --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java @@ -0,0 +1,31 @@ +package io.vertx.test.codegen.testdataobject.jsonmapper; + +public enum MyEnumWithCustomConstructor { + DEV("dev", "development"), ITEST("itest", "integration-test"); + + public static MyEnumWithCustomConstructor of(String pName) { + for (MyEnumWithCustomConstructor item : MyEnumWithCustomConstructor.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + + private String[] names = new String[2]; + + MyEnumWithCustomConstructor(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + +} From a7f3f3548fdde8adf3bc30c3be7e843f0f1e5785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristiano=20V=2E=20Gavi=C3=A3o?= Date: Tue, 23 Mar 2021 13:25:18 -0300 Subject: [PATCH 2/4] renamed classes as required MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cristiano V. Gavião --- .../io/vertx/test/codegen/DataObjectTest.java | 12 ++--- .../java/io/vertx/test/codegen/EnumTest.java | 18 ++++---- ...ctor.java => MyEnumWithCustomFactory.java} | 10 ++--- .../WithMyCustomEnumWithMapper.java | 18 +++----- .../DataObjectWithEnumWithMapper.java | 44 ------------------- .../jsonmapper/DataObjectWithMappedEnum.java | 44 +++++++++++++++++++ ...ctor.java => MyEnumWithCustomFactory.java} | 8 ++-- 7 files changed, 74 insertions(+), 80 deletions(-) rename src/test/java/io/vertx/test/codegen/testapi/jsonmapper/{MyEnumWithCustomConstructor.java => MyEnumWithCustomFactory.java} (68%) delete mode 100644 src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java create mode 100644 src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithMappedEnum.java rename src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/{MyEnumWithCustomConstructor.java => MyEnumWithCustomFactory.java} (67%) diff --git a/src/test/java/io/vertx/test/codegen/DataObjectTest.java b/src/test/java/io/vertx/test/codegen/DataObjectTest.java index db1806456..8acd3f2ea 100644 --- a/src/test/java/io/vertx/test/codegen/DataObjectTest.java +++ b/src/test/java/io/vertx/test/codegen/DataObjectTest.java @@ -15,9 +15,9 @@ import io.vertx.test.codegen.testdataobject.*; import io.vertx.test.codegen.testdataobject.imported.Imported; import io.vertx.test.codegen.testdataobject.jsonmapper.DataObjectWithPojoWithMapper; -import io.vertx.test.codegen.testdataobject.jsonmapper.MyEnumWithCustomConstructor; +import io.vertx.test.codegen.testdataobject.jsonmapper.MyEnumWithCustomFactory; import io.vertx.test.codegen.testdataobject.jsonmapper.MyPojo; -import io.vertx.test.codegen.testdataobject.jsonmapper.DataObjectWithEnumWithMapper; +import io.vertx.test.codegen.testdataobject.jsonmapper.DataObjectWithMappedEnum; import org.junit.Test; @@ -898,14 +898,14 @@ public void testDataObjectWithJsonMapper() throws Exception { @Test public void testCustomEnumWithJsonMapper() throws Exception { DataObjectModel model = new GeneratorHelper() - .registerConverter(MyEnumWithCustomConstructor.class, DataObjectWithEnumWithMapper.class.getName(), "serializeMyEnumWithCustomConstructor") - .registerConverter(MyEnumWithCustomConstructor.class, DataObjectWithEnumWithMapper.class.getName(), "deserializeMyEnumWithCustomConstructor") - .generateDataObject(DataObjectWithEnumWithMapper.class); + .registerConverter(MyEnumWithCustomFactory.class, DataObjectWithMappedEnum.class.getName(), "serializeMyEnumWithCustomFactory") + .registerConverter(MyEnumWithCustomFactory.class, DataObjectWithMappedEnum.class.getName(), "deserializeMyEnumWithCustomFactory") + .generateDataObject(DataObjectWithMappedEnum.class); assertNotNull(model); assertTrue(model.isClass()); assertTrue(model.getGenerateConverter()); assertTrue(model.isPublicConverter()); - + PropertyInfo myPojoProperty = model.getPropertyMap().get("customEnum"); assertEquals(ClassKind.ENUM, myPojoProperty.getType().getKind()); assertTrue(myPojoProperty.getType().getDataObject().isDeserializable()); diff --git a/src/test/java/io/vertx/test/codegen/EnumTest.java b/src/test/java/io/vertx/test/codegen/EnumTest.java index 17e889344..553dab6c8 100644 --- a/src/test/java/io/vertx/test/codegen/EnumTest.java +++ b/src/test/java/io/vertx/test/codegen/EnumTest.java @@ -5,7 +5,7 @@ import io.vertx.codegen.type.ClassKind; import io.vertx.codegen.type.EnumTypeInfo; import io.vertx.codegen.type.TypeInfo; -import io.vertx.test.codegen.testapi.jsonmapper.MyEnumWithCustomConstructor; +import io.vertx.test.codegen.testapi.jsonmapper.MyEnumWithCustomFactory; import io.vertx.test.codegen.testapi.jsonmapper.WithMyCustomEnumWithMapper; import io.vertx.test.codegen.testenum.EnumAsParam; import io.vertx.test.codegen.testenum.InvalidEmptyEnum; @@ -41,22 +41,22 @@ public void testEnum() throws Exception { @Test public void testJsonMapper() throws Exception { ClassModel model = new GeneratorHelper() - .registerConverter(MyEnumWithCustomConstructor.class, WithMyCustomEnumWithMapper.class.getName(), "serializeMyEnumWithCustomConstructor") - .registerConverter(MyEnumWithCustomConstructor.class, WithMyCustomEnumWithMapper.class.getName(), "deserializeMyEnumWithCustomConstructor") + .registerConverter(MyEnumWithCustomFactory.class, WithMyCustomEnumWithMapper.class.getName(), "serializeMyEnumWithCustomFactory") + .registerConverter(MyEnumWithCustomFactory.class, WithMyCustomEnumWithMapper.class.getName(), "deserializeMyEnumWithCustomFactory") .generateClass(WithMyCustomEnumWithMapper.class); assertFalse(model.getAnnotations().isEmpty()); assertEquals(1, model.getAnnotations().size()); assertEquals(VertxGen.class.getName(), model.getAnnotations().get(0).getName()); assertEquals(1, model.getReferencedDataObjectTypes().size()); - assertEquals("MyEnumWithCustomConstructor", model.getReferencedDataObjectTypes().iterator().next().getSimpleName()); + assertEquals("MyEnumWithCustomFactory", model.getReferencedDataObjectTypes().iterator().next().getSimpleName()); - checkMethod(model.getMethodMap().get("returnMyEnumWithCustomConstructor").get(0), - "returnMyEnumWithCustomConstructor", 0, new TypeLiteral() {}, MethodKind.OTHER); + checkMethod(model.getMethodMap().get("returnMyEnumWithCustomFactory").get(0), + "returnMyEnumWithCustomFactory", 0, new TypeLiteral() {}, MethodKind.OTHER); - MethodInfo myPojoParam = model.getMethodMap().get("setMyEnumWithCustomConstructor").get(0); - checkMethod(myPojoParam, "setMyEnumWithCustomConstructor", 1, "void", MethodKind.OTHER); - checkParam(myPojoParam.getParam(0), "myEnum", MyEnumWithCustomConstructor.class.getName(), ClassKind.ENUM); + MethodInfo myPojoParam = model.getMethodMap().get("setMyEnumWithCustomFactory").get(0); + checkMethod(myPojoParam, "setMyEnumWithCustomFactory", 1, "void", MethodKind.OTHER); + checkParam(myPojoParam.getParam(0), "myEnum", MyEnumWithCustomFactory.class.getName(), ClassKind.ENUM); } diff --git a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomFactory.java similarity index 68% rename from src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java rename to src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomFactory.java index 9760af235..96d2b454d 100644 --- a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomConstructor.java +++ b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/MyEnumWithCustomFactory.java @@ -2,9 +2,9 @@ import io.vertx.codegen.annotations.VertxGen; -/**MyEnumWithCustomConstructor doc*/ +/**MyEnumWithCustomFactory doc*/ @VertxGen -public enum MyEnumWithCustomConstructor { +public enum MyEnumWithCustomFactory { /**DEV doc*/ DEV("dev", "development"), @@ -12,8 +12,8 @@ public enum MyEnumWithCustomConstructor { /**ITEST doc*/ ITEST("itest", "integration-test"); - public static MyEnumWithCustomConstructor of(String pName) { - for (MyEnumWithCustomConstructor item : MyEnumWithCustomConstructor.values()) { + public static MyEnumWithCustomFactory of(String pName) { + for (MyEnumWithCustomFactory item : MyEnumWithCustomFactory.values()) { if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) || pName.equalsIgnoreCase(item.name())) { return item; @@ -24,7 +24,7 @@ public static MyEnumWithCustomConstructor of(String pName) { private String[] names = new String[2]; - MyEnumWithCustomConstructor(String pShortName, String pLongName) { + MyEnumWithCustomFactory(String pShortName, String pLongName) { names[0] = pShortName; names[1] = pLongName; } diff --git a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java index f01e8ab1b..3a1dcf877 100644 --- a/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java +++ b/src/test/java/io/vertx/test/codegen/testapi/jsonmapper/WithMyCustomEnumWithMapper.java @@ -1,27 +1,21 @@ package io.vertx.test.codegen.testapi.jsonmapper; -import java.util.List; -import java.util.Map; -import java.util.Set; - import io.vertx.codegen.annotations.GenIgnore; import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.AsyncResult; -import io.vertx.core.Handler; @VertxGen public interface WithMyCustomEnumWithMapper { @GenIgnore - public static MyEnumWithCustomConstructor deserializeMyEnumWithCustomConstructor(String value) { - return (value != null) ? MyEnumWithCustomConstructor.of(value) : null; + public static MyEnumWithCustomFactory deserializeMyEnumWithCustomFactory(String value) { + return (value != null) ? MyEnumWithCustomFactory.of(value) : null; } @GenIgnore - public static String serializeMyEnumWithCustomConstructor(MyEnumWithCustomConstructor value) { + public static String serializeMyEnumWithCustomFactory(MyEnumWithCustomFactory value) { return (value != null) ? value.getLongName() : null; } - - MyEnumWithCustomConstructor returnMyEnumWithCustomConstructor(); - void setMyEnumWithCustomConstructor(MyEnumWithCustomConstructor myEnum); + + MyEnumWithCustomFactory returnMyEnumWithCustomFactory(); + void setMyEnumWithCustomFactory(MyEnumWithCustomFactory myEnum); } diff --git a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java deleted file mode 100644 index 496abf002..000000000 --- a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithEnumWithMapper.java +++ /dev/null @@ -1,44 +0,0 @@ -package io.vertx.test.codegen.testdataobject.jsonmapper; - -import io.vertx.codegen.annotations.DataObject; -import io.vertx.codegen.annotations.GenIgnore; -import io.vertx.core.json.JsonObject; - -@DataObject(generateConverter = true, publicConverter = true) -public class DataObjectWithEnumWithMapper { - - @GenIgnore - public static MyEnumWithCustomConstructor deserializeMyEnumWithCustomConstructor(String value) { - return (value != null) ? MyEnumWithCustomConstructor.of(value) : null; - } - - @GenIgnore - public static String serializeMyEnumWithCustomConstructor(MyEnumWithCustomConstructor value) { - return (value != null) ? value.getLongName() : null; - } - - MyEnumWithCustomConstructor customEnum; - - public DataObjectWithEnumWithMapper(JsonObject customEnum) { - - } - - public DataObjectWithEnumWithMapper(MyEnumWithCustomConstructor customEnum) { - super(); - this.customEnum = customEnum; - } - - public MyEnumWithCustomConstructor getCustomEnum() { - return customEnum; - } - - public DataObjectWithEnumWithMapper setCustomEnum(MyEnumWithCustomConstructor a) { - this.customEnum = a; - return this; - } - - public JsonObject toJson() { - return null; - } - -} diff --git a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithMappedEnum.java b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithMappedEnum.java new file mode 100644 index 000000000..201dfaf24 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/DataObjectWithMappedEnum.java @@ -0,0 +1,44 @@ +package io.vertx.test.codegen.testdataobject.jsonmapper; + +import io.vertx.codegen.annotations.DataObject; +import io.vertx.codegen.annotations.GenIgnore; +import io.vertx.core.json.JsonObject; + +@DataObject(generateConverter = true, publicConverter = true) +public class DataObjectWithMappedEnum { + + @GenIgnore + public static MyEnumWithCustomFactory deserializeMyEnumWithCustomFactory(String value) { + return (value != null) ? MyEnumWithCustomFactory.of(value) : null; + } + + @GenIgnore + public static String serializeMyEnumWithCustomFactory(MyEnumWithCustomFactory value) { + return (value != null) ? value.getLongName() : null; + } + + MyEnumWithCustomFactory customEnum; + + public DataObjectWithMappedEnum(JsonObject customEnum) { + + } + + public DataObjectWithMappedEnum(MyEnumWithCustomFactory customEnum) { + super(); + this.customEnum = customEnum; + } + + public MyEnumWithCustomFactory getCustomEnum() { + return customEnum; + } + + public DataObjectWithMappedEnum setCustomEnum(MyEnumWithCustomFactory a) { + this.customEnum = a; + return this; + } + + public JsonObject toJson() { + return null; + } + +} diff --git a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomFactory.java similarity index 67% rename from src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java rename to src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomFactory.java index 3d9e3224a..214f19dd6 100644 --- a/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomConstructor.java +++ b/src/test/java/io/vertx/test/codegen/testdataobject/jsonmapper/MyEnumWithCustomFactory.java @@ -1,10 +1,10 @@ package io.vertx.test.codegen.testdataobject.jsonmapper; -public enum MyEnumWithCustomConstructor { +public enum MyEnumWithCustomFactory { DEV("dev", "development"), ITEST("itest", "integration-test"); - public static MyEnumWithCustomConstructor of(String pName) { - for (MyEnumWithCustomConstructor item : MyEnumWithCustomConstructor.values()) { + public static MyEnumWithCustomFactory of(String pName) { + for (MyEnumWithCustomFactory item : MyEnumWithCustomFactory.values()) { if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) || pName.equalsIgnoreCase(item.name())) { return item; @@ -15,7 +15,7 @@ public static MyEnumWithCustomConstructor of(String pName) { private String[] names = new String[2]; - MyEnumWithCustomConstructor(String pShortName, String pLongName) { + MyEnumWithCustomFactory(String pShortName, String pLongName) { names[0] = pShortName; names[1] = pLongName; } From 2cb085c43b49f1a7b23aee4d3d390c6efcc2dc77 Mon Sep 17 00:00:00 2001 From: Fyro Date: Mon, 13 Sep 2021 18:30:41 +0200 Subject: [PATCH 3/4] update README.md & index.adoc to add documentation with custom enum. --- README.md | 74 ++++++++++++++++++++++++++++++++---- src/main/asciidoc/index.adoc | 58 +++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 60e0fe895..ad81ed2e2 100644 --- a/README.md +++ b/README.md @@ -432,12 +432,12 @@ A _Data object_ is a type that can be converted back and forth to a Json type. You can declare data objects by: -* Defining an annotated `@Mapper` method for it +* Defining a mapper in the `META-INF/vertx/json-mappers.properties` file * Or annotating the type itself with `@DataObject` ### Json mappers -A json mapper for type `T` is a method that maps any object of type `Type`, where `J` can be: +A json mapper for type `T` is a method that maps any object or enum of type `Type`, where `J` can be: * `JsonArray` or `JsonObject` * a concrete type extending `Number` such as `Long` or `Double` @@ -448,19 +448,77 @@ Json mapped types can be used anywhere a json types used are. A json mapper turns any Java type into a data object type. +####Object You can declare them as public static methods: +```java +package com.example; + +public class MyMappers { + + public static String serialize(ZonedDateTime date) { + return date.toString(); + } + public static ZonedDateTime deserialize(String s) { + return ZonedDateTime.parse(s); + } +} +``` +These mappers needs to be declared in a `META-INF/vertx/json-mappers.properties` file as follow: +```properties +java.time.ZonedDateTime.serializer=com.example.MyMappers#serializeZonedDateTime +java.time.ZonedDateTime.deserializer=com.example.MyMappers#deserializedZoneDateTime +``` +####Enum +Enum can be define with values parameters passed to a constructor. In this use case, you can't use default behavior of codegen (#valueOf() and #name()), you need to define like Object `serializer` and `deserializer`. ```java -@Mapper -public static String serialize(ZonedDateTime date) { - return date.toString(); +package com.example; + +public enum MyEnumWithCustomFactory { + DEV("dev", "development"), ITEST("itest", "integration-test"); + + private String[] names = new String[2]; + + MyEnumWithCustomFactory(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + + public static MyEnumWithCustomFactory of(String pName) { + for (MyEnumWithCustomFactory item : MyEnumWithCustomFactory.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + +} +``` +You can declare them as public static methods: +```java +public static String serialize(MyEnumWithCustomFactory value) { + return value.getLongName(); } -@Mapper -public static ZonedDateTime deserialize(String s) { - return ZonedDateTime.parse(s); +public static MyEnumWithCustomFactory deserialize(String value) { + return MyEnumWithCustomFactory.of(value); } ``` +These mappers needs to be declared in a `META-INF/vertx/json-mappers.properties` file as follow: +```properties +com.example.MyEnumWithCustomFactory.serializer=com.example.MyEnumWithCustomFactory#serialize +com.example.MyEnumWithCustomFactory.deserializer=com.example.MyEnumWithCustomFactory#deserialize +``` ### `@DataObject` annotated types diff --git a/src/main/asciidoc/index.adoc b/src/main/asciidoc/index.adoc index 26cdb8f19..f368e88f4 100644 --- a/src/main/asciidoc/index.adoc +++ b/src/main/asciidoc/index.adoc @@ -60,7 +60,7 @@ You can declare data objects by: ==== Json mappers -A json mapper for type `T` is a method that maps any object of type `Type`, where `J` can be: +A json mapper for type `T` is a method that maps any object or enum of type `Type`, where `J` can be: * `JsonArray` or `JsonObject` * a concrete type extending `Number` such as `Long` or `Double` @@ -71,6 +71,7 @@ Json mapped types can be used anywhere a json types used are. A json mapper turns any Java type into a data object type. +===== Object You can declare them as public static methods: [source,java] @@ -105,6 +106,61 @@ java.time.ZonedDateTime.serializer=com.example.MyMappers#SERIALIZER java.time.ZonedDateTime.deserializer=com.example.MyMappers#DESERIALIZER ---- +===== Enum + +Enum can be define with values parameters passed to a constructor. In this use case, you can't use default behavior of codegen (#valueOf() and #name()), you need to define like Object `serializer` and `deserializer`. + +[source,java] +---- +package com.example; + +public enum MyEnumWithCustomFactory { + DEV("dev", "development"), ITEST("itest", "integration-test"); + + private String[] names = new String[2]; + + MyEnumWithCustomFactory(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + + public static MyEnumWithCustomFactory of(String pName) { + for (MyEnumWithCustomFactory item : MyEnumWithCustomFactory.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + +} +---- +You can declare them as public static methods: +[source,java] +---- +public static String serialize(MyEnumWithCustomFactory value) { + return value.getLongName(); +} + +public static MyEnumWithCustomFactory deserialize(String value) { + return MyEnumWithCustomFactory.of(value); +} +---- +These mappers needs to be declared in a `META-INF/vertx/json-mappers.properties` file as follow: +---- +com.example.MyEnumWithCustomFactory.serializer=com.example.MyEnumWithCustomFactory#serialize +com.example.MyEnumWithCustomFactory.deserializer=com.example.MyEnumWithCustomFactory#deserialize +---- + ==== `@DataObject` annotated types A `@DataObject` annotated type is a Java class with the only purpose to be a container for data. From aa91543d75ab820554488efe0d1ccd5745ab26c4 Mon Sep 17 00:00:00 2001 From: Fyro Date: Tue, 19 Oct 2021 19:05:11 +0200 Subject: [PATCH 4/4] Tests Json generated converters use enum custom mapper Signed-off-by: Fyro --- .../converter/TestDataObjectConverter.java | 79 +++++++++++++++++++ .../codegen/converter/TestCustomEnum.java | 34 ++++++++ .../codegen/converter/TestDataObject.java | 67 ++++++++++++++++ .../META-INF/vertx/json-mappers.properties | 2 + .../codegen/converter/DataObjectTest.java | 34 ++++++++ 5 files changed, 216 insertions(+) create mode 100644 src/converters/java/io/vertx/test/codegen/converter/TestCustomEnum.java diff --git a/src/converters/generated/io/vertx/test/codegen/converter/TestDataObjectConverter.java b/src/converters/generated/io/vertx/test/codegen/converter/TestDataObjectConverter.java index 1824e9e63..6ddfa0d95 100644 --- a/src/converters/generated/io/vertx/test/codegen/converter/TestDataObjectConverter.java +++ b/src/converters/generated/io/vertx/test/codegen/converter/TestDataObjectConverter.java @@ -88,6 +88,14 @@ public static void fromJson(Iterable> json, }); } break; + case "addedEnumMappeds": + if (member.getValue() instanceof JsonArray) { + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof String) + obj.addAddedEnumMapped(io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)item)); + }); + } + break; case "addedHttpMethods": if (member.getValue() instanceof JsonArray) { ((Iterable)member.getValue()).forEach( item -> { @@ -467,6 +475,41 @@ public static void fromJson(Iterable> json, obj.setBufferSet(list); } break; + case "enumMapped": + if (member.getValue() instanceof String) { + obj.setEnumMapped(io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)member.getValue())); + } + break; + case "enumMappedList": + if (member.getValue() instanceof JsonArray) { + java.util.ArrayList list = new java.util.ArrayList<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof String) + list.add(io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)item)); + }); + obj.setEnumMappedList(list); + } + break; + case "enumMappedMap": + if (member.getValue() instanceof JsonObject) { + java.util.Map map = new java.util.LinkedHashMap<>(); + ((Iterable>)member.getValue()).forEach(entry -> { + if (entry.getValue() instanceof String) + map.put(entry.getKey(), io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)entry.getValue())); + }); + obj.setEnumMappedMap(map); + } + break; + case "enumMappedSet": + if (member.getValue() instanceof JsonArray) { + java.util.LinkedHashSet list = new java.util.LinkedHashSet<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof String) + list.add(io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)item)); + }); + obj.setEnumMappedSet(list); + } + break; case "httpMethod": if (member.getValue() instanceof String) { obj.setHttpMethod(java.util.concurrent.TimeUnit.valueOf((String)member.getValue())); @@ -679,6 +722,14 @@ public static void fromJson(Iterable> json, }); } break; + case "keyedEnumMappedValues": + if (member.getValue() instanceof JsonObject) { + ((Iterable>)member.getValue()).forEach(entry -> { + if (entry.getValue() instanceof String) + obj.addKeyedEnumMappedValue(entry.getKey(), io.vertx.test.codegen.converter.TestDataObject.deserializeCustomEnum((String)entry.getValue())); + }); + } + break; case "keyedEnumValues": if (member.getValue() instanceof JsonObject) { ((Iterable>)member.getValue()).forEach(entry -> { @@ -1007,6 +1058,11 @@ public static void toJson(TestDataObject obj, java.util.Map json obj.getAddedBuffers().forEach(item -> array.add(JsonUtil.BASE64_ENCODER.encodeToString(item.getBytes()))); json.put("addedBuffers", array); } + if (obj.getAddedEnumMappeds() != null) { + JsonArray array = new JsonArray(); + obj.getAddedEnumMappeds().forEach(item -> array.add(io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(item))); + json.put("addedEnumMappeds", array); + } if (obj.getAddedHttpMethods() != null) { JsonArray array = new JsonArray(); obj.getAddedHttpMethods().forEach(item -> array.add(item.name())); @@ -1209,6 +1265,24 @@ public static void toJson(TestDataObject obj, java.util.Map json obj.getBufferSet().forEach(item -> array.add(JsonUtil.BASE64_ENCODER.encodeToString(item.getBytes()))); json.put("bufferSet", array); } + if (obj.getEnumMapped() != null) { + json.put("enumMapped", io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(obj.getEnumMapped())); + } + if (obj.getEnumMappedList() != null) { + JsonArray array = new JsonArray(); + obj.getEnumMappedList().forEach(item -> array.add(io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(item))); + json.put("enumMappedList", array); + } + if (obj.getEnumMappedMap() != null) { + JsonObject map = new JsonObject(); + obj.getEnumMappedMap().forEach((key, value) -> map.put(key, io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(value))); + json.put("enumMappedMap", map); + } + if (obj.getEnumMappedSet() != null) { + JsonArray array = new JsonArray(); + obj.getEnumMappedSet().forEach(item -> array.add(io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(item))); + json.put("enumMappedSet", array); + } if (obj.getHttpMethod() != null) { json.put("httpMethod", obj.getHttpMethod().name()); } @@ -1326,6 +1400,11 @@ public static void toJson(TestDataObject obj, java.util.Map json obj.getKeyedBufferValues().forEach((key, value) -> map.put(key, JsonUtil.BASE64_ENCODER.encodeToString(value.getBytes()))); json.put("keyedBufferValues", map); } + if (obj.getKeyedEnumMappedValues() != null) { + JsonObject map = new JsonObject(); + obj.getKeyedEnumMappedValues().forEach((key, value) -> map.put(key, io.vertx.test.codegen.converter.TestDataObject.serializeCustomEnum(value))); + json.put("keyedEnumMappedValues", map); + } if (obj.getKeyedEnumValues() != null) { JsonObject map = new JsonObject(); obj.getKeyedEnumValues().forEach((key, value) -> map.put(key, value.name())); diff --git a/src/converters/java/io/vertx/test/codegen/converter/TestCustomEnum.java b/src/converters/java/io/vertx/test/codegen/converter/TestCustomEnum.java new file mode 100644 index 000000000..eccd28b20 --- /dev/null +++ b/src/converters/java/io/vertx/test/codegen/converter/TestCustomEnum.java @@ -0,0 +1,34 @@ +package io.vertx.test.codegen.converter; + +public enum TestCustomEnum { + + DEV("dev", "development"), + + ITEST("itest", "integration-test"); + + public static TestCustomEnum of(String pName) { + for (TestCustomEnum item : TestCustomEnum.values()) { + if (item.names[0].equalsIgnoreCase(pName) || item.names[1].equalsIgnoreCase(pName) + || pName.equalsIgnoreCase(item.name())) { + return item; + } + } + return DEV; + } + + private String[] names = new String[2]; + + TestCustomEnum(String pShortName, String pLongName) { + names[0] = pShortName; + names[1] = pLongName; + } + + public String getLongName() { + return names[1]; + } + + public String getShortName() { + return names[0]; + } + +} diff --git a/src/converters/java/io/vertx/test/codegen/converter/TestDataObject.java b/src/converters/java/io/vertx/test/codegen/converter/TestDataObject.java index 057220c13..19b6c3efd 100644 --- a/src/converters/java/io/vertx/test/codegen/converter/TestDataObject.java +++ b/src/converters/java/io/vertx/test/codegen/converter/TestDataObject.java @@ -35,6 +35,14 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { return ZonedDateTime.parse(value); } + public static String serializeCustomEnum(TestCustomEnum value) { + return (value != null) ? value.getShortName() : null; + } + + public static TestCustomEnum deserializeCustomEnum(String value) { + return (value != null) ? TestCustomEnum.of(value) : null; + } + private String string; private boolean primitiveBoolean; private byte primitiveByte; @@ -61,6 +69,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private ZonedDateTime methodMapped; private NoConverterDataObject notConvertibleDataObject; private ByteBuffer unmapped; + private TestCustomEnum enumMapped; private List stringList; private List boxedBooleanList; @@ -80,6 +89,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private List methodMappedList; private List objectList; private List notConvertibleDataObjectList; + private List enumMappedList; private Set stringSet; private Set boxedBooleanSet; @@ -99,6 +109,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private Set methodMappedSet; private Set objectSet; private Set notConvertibleDataObjectSet; + private Set enumMappedSet; private List addedStringValues = new ArrayList<>(); private List addedBoxedBooleanValues = new ArrayList<>(); @@ -117,6 +128,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private List addedHttpMethods = new ArrayList<>(); private List addedMethodMappeds = new ArrayList<>(); private List addedObjects = new ArrayList<>(); + private List addedEnumMappeds = new ArrayList<>(); private Map stringValueMap; private Map boxedBooleanValueMap; @@ -136,6 +148,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private Map methodMappedMap; private Map objectMap; private Map notConvertibleDataObjectMap; + private Map enumMappedMap; private Map keyedStringValues = new HashMap<>(); private Map keyedBoxedBooleanValues = new HashMap<>(); @@ -154,6 +167,7 @@ public static ZonedDateTime deserializeZonedDateTime(String value) { private Map keyedEnumValues = new HashMap<>(); private Map keyedMethodMappedValues = new HashMap<>(); private Map keyedObjectValues = new HashMap<>(); + private Map keyedEnumMappedValues = new HashMap<>(); public TestDataObject() { } @@ -398,6 +412,15 @@ public TestDataObject setUnmapped(ByteBuffer unmapped) { return this; } + public TestCustomEnum getEnumMapped() { + return enumMapped; + } + + public TestDataObject setEnumMapped(TestCustomEnum enumMapped) { + this.enumMapped = enumMapped; + return this; + } + public List getStringList() { return stringList; } @@ -551,6 +574,14 @@ public TestDataObject setObjectList(List objectList) { return this; } + public List getEnumMappedList() { + return enumMappedList; + } + + public TestDataObject setEnumMappedList(List enumMappedList) { + this.enumMappedList = enumMappedList; + return this; + } public Set getStringSet() { return stringSet; @@ -705,6 +736,15 @@ public TestDataObject setObjectSet(Set objectSet) { return this; } + public Set getEnumMappedSet() { + return enumMappedSet; + } + + public TestDataObject setEnumMappedSet(Set enumMappedSet) { + this.enumMappedSet = enumMappedSet; + return this; + } + public List getAddedStringValues() { return addedStringValues; } @@ -858,6 +898,15 @@ public TestDataObject addAddedObject(Object addedObject) { return this; } + public List getAddedEnumMappeds() { + return addedEnumMappeds; + } + + public TestDataObject addAddedEnumMapped(TestCustomEnum addedEnumMappeds) { + this.addedEnumMappeds.add(addedEnumMappeds); + return this; + } + public Map getStringValueMap() { return stringValueMap; } @@ -1011,6 +1060,15 @@ public TestDataObject setObjectMap(Map objectMap) { return this; } + public Map getEnumMappedMap() { + return enumMappedMap; + } + + public TestDataObject setEnumMappedMap(Map enumMappedMap) { + this.enumMappedMap = enumMappedMap; + return this; + } + public Map getKeyedStringValues() { return keyedStringValues; } @@ -1164,6 +1222,15 @@ public TestDataObject addKeyedObjectValue(String key, Object value) { return this; } + public Map getKeyedEnumMappedValues() { + return keyedEnumMappedValues; + } + + public TestDataObject addKeyedEnumMappedValue(String key, TestCustomEnum value) { + this.keyedEnumMappedValues.put(key, value); + return this; + } + public List getNotConvertibleDataObjectList() { return notConvertibleDataObjectList; } diff --git a/src/converters/resources/META-INF/vertx/json-mappers.properties b/src/converters/resources/META-INF/vertx/json-mappers.properties index 9d40d91bf..202d73ccd 100644 --- a/src/converters/resources/META-INF/vertx/json-mappers.properties +++ b/src/converters/resources/META-INF/vertx/json-mappers.properties @@ -1,2 +1,4 @@ java.time.ZonedDateTime.serializer=io.vertx.test.codegen.converter.TestDataObject#serializeZonedDateTime java.time.ZonedDateTime.deserializer=io.vertx.test.codegen.converter.TestDataObject#deserializeZonedDateTime +io.vertx.test.codegen.converter.TestCustomEnum.serializer=io.vertx.test.codegen.converter.TestDataObject#serializeCustomEnum +io.vertx.test.codegen.converter.TestCustomEnum.deserializer=io.vertx.test.codegen.converter.TestDataObject#deserializeCustomEnum diff --git a/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java b/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java index 42339a261..4550d5745 100644 --- a/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java +++ b/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java @@ -63,6 +63,7 @@ public void testJsonToDataObject() throws Exception { JsonArray jsonArray = new JsonArray().add(TestUtils.randomAlphaString(20)); TimeUnit httpMethod = TimeUnit.values()[TestUtils.randomPositiveInt() % TimeUnit.values().length]; ZonedDateTime methodMapped = ZonedDateTime.now(); + TestCustomEnum enumMapped = TestCustomEnum.DEV; Map map = new HashMap<>(); map.put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(20)); @@ -98,6 +99,7 @@ public void testJsonToDataObject() throws Exception { json.put("jsonArray", jsonArray); json.put("httpMethod", httpMethod.toString()); json.put("methodMapped", methodMapped.toString()); + json.put("enumMapped", enumMapped.getShortName()); json.put("stringList", new JsonArray().add(stringValue)); json.put("boxedBooleanList", new JsonArray().add(boxedBooleanValue)); json.put("boxedByteList", new JsonArray().add(boxedByteValue)); @@ -115,6 +117,7 @@ public void testJsonToDataObject() throws Exception { json.put("httpMethodList", new JsonArray().add(httpMethod.toString())); json.put("methodMappedList", new JsonArray().add(methodMapped.toString())); json.put("objectList", new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2))); + json.put("enumMappedList", new JsonArray().add(enumMapped.getShortName())); json.put("stringSet", new JsonArray().add(stringValue)); json.put("boxedBooleanSet", new JsonArray().add(boxedBooleanValue)); json.put("boxedByteSet", new JsonArray().add(boxedByteValue)); @@ -132,6 +135,7 @@ public void testJsonToDataObject() throws Exception { json.put("httpMethodSet", new JsonArray().add(httpMethod.toString())); json.put("methodMappedSet", new JsonArray().add(methodMapped.toString())); json.put("objectSet", new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2))); + json.put("enumMappedSet", new JsonArray().add(enumMapped.getShortName())); json.put("addedStringValues", new JsonArray().add(stringValue)); json.put("addedBooleanValues", new JsonArray().add(boxedBooleanValue)); json.put("addedByteValues", new JsonArray().add(boxedByteValue)); @@ -157,6 +161,7 @@ public void testJsonToDataObject() throws Exception { json.put("addedHttpMethods", new JsonArray().add(httpMethod.toString())); json.put("addedMethodMappeds", new JsonArray().add(methodMapped.toString())); json.put("addedObjects", new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2))); + json.put("addedEnumMappeds", new JsonArray().add(enumMapped.getShortName())); json.put("stringValueMap", new JsonObject().put(key, stringValue)); json.put("boxedBooleanValueMap", new JsonObject().put(key, boxedBooleanValue)); json.put("boxedByteValueMap", new JsonObject().put(key, boxedByteValue)); @@ -174,6 +179,7 @@ public void testJsonToDataObject() throws Exception { json.put("httpMethodMap", new JsonObject().put(key, httpMethod.toString())); json.put("methodMappedMap", new JsonObject().put(key, methodMapped.toString())); json.put("objectMap", toJson(map)); + json.put("enumMappedMap", new JsonObject().put(key, enumMapped.getShortName())); json.put("keyedStringValues", new JsonObject().put(key, stringValue)); json.put("keyedBoxedBooleanValues", new JsonObject().put(key, boxedBooleanValue)); json.put("keyedBoxedByteValues", new JsonObject().put(key, boxedByteValue)); @@ -191,6 +197,7 @@ public void testJsonToDataObject() throws Exception { json.put("keyedEnumValues", new JsonObject().put(key, httpMethod.name())); json.put("keyedMethodMappedValues", new JsonObject().put(key, methodMapped.toString())); json.put("keyedObjectValues", toJson(map)); + json.put("keyedEnumMappedValues", new JsonObject().put(key, enumMapped.getShortName())); TestDataObject obj = new TestDataObject(); TestDataObjectConverter.fromJson(json, obj); @@ -219,6 +226,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(jsonArray, obj.getJsonArray()); Assert.assertEquals(httpMethod, obj.getHttpMethod()); Assert.assertEquals(methodMapped, obj.getMethodMapped()); + Assert.assertEquals(enumMapped, obj.getEnumMapped()); Assert.assertEquals(Collections.singletonList(stringValue), obj.getStringList()); Assert.assertEquals(Collections.singletonList(boxedBooleanValue), obj.getBoxedBooleanList()); Assert.assertEquals(Collections.singletonList(boxedByteValue), obj.getBoxedByteList()); @@ -236,6 +244,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(Collections.singletonList(httpMethod), obj.getHttpMethodList()); Assert.assertEquals(Collections.singletonList(methodMapped), obj.getMethodMappedList()); Assert.assertEquals(list, obj.getObjectList()); + Assert.assertEquals(Collections.singletonList(enumMapped), obj.getEnumMappedList()); Assert.assertEquals(Collections.singleton(stringValue), obj.getStringSet()); Assert.assertEquals(Collections.singleton(boxedBooleanValue), obj.getBoxedBooleanSet()); Assert.assertEquals(Collections.singleton(boxedByteValue), obj.getBoxedByteSet()); @@ -253,6 +262,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(Collections.singleton(httpMethod), obj.getHttpMethodSet()); Assert.assertEquals(Collections.singleton(methodMapped), obj.getMethodMappedSet()); Assert.assertEquals(new LinkedHashSet<>(list), obj.getObjectSet()); + Assert.assertEquals(Collections.singleton(enumMapped), obj.getEnumMappedSet()); Assert.assertEquals(Collections.singletonList(stringValue), obj.getAddedStringValues()); Assert.assertEquals(Collections.singletonList(boxedBooleanValue), obj.getAddedBoxedBooleanValues()); Assert.assertEquals(Collections.singletonList(boxedByteValue), obj.getAddedBoxedByteValues()); @@ -270,6 +280,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(Collections.singletonList(httpMethod), obj.getAddedHttpMethods()); Assert.assertEquals(Collections.singletonList(methodMapped), obj.getAddedMethodMappeds()); Assert.assertEquals(list, obj.getAddedObjects()); + Assert.assertEquals(Collections.singletonList(enumMapped), obj.getAddedEnumMappeds()); Assert.assertEquals(Collections.singletonMap(key, stringValue), obj.getStringValueMap()); Assert.assertEquals(Collections.singletonMap(key, boxedBooleanValue), obj.getBoxedBooleanValueMap()); Assert.assertEquals(Collections.singletonMap(key, boxedByteValue), obj.getBoxedByteValueMap()); @@ -287,6 +298,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(Collections.singletonMap(key, httpMethod), obj.getHttpMethodMap()); Assert.assertEquals(Collections.singletonMap(key, methodMapped), obj.getMethodMappedMap()); Assert.assertEquals(map, obj.getObjectMap()); + Assert.assertEquals(Collections.singletonMap(key, enumMapped), obj.getEnumMappedMap()); Assert.assertEquals(Collections.singletonMap(key, stringValue), obj.getKeyedStringValues()); Assert.assertEquals(Collections.singletonMap(key, boxedBooleanValue), obj.getKeyedBoxedBooleanValues()); Assert.assertEquals(Collections.singletonMap(key, boxedByteValue), obj.getKeyedBoxedByteValues()); @@ -304,6 +316,7 @@ public void testJsonToDataObject() throws Exception { Assert.assertEquals(Collections.singletonMap(key, httpMethod), obj.getKeyedEnumValues()); Assert.assertEquals(Collections.singletonMap(key, methodMapped), obj.getKeyedMethodMappedValues()); Assert.assertEquals(map, obj.getObjectMap()); + Assert.assertEquals(Collections.singletonMap(key, enumMapped), obj.getKeyedEnumMappedValues()); // Sometimes json can use java collections so test it runs fine in this case // json = new JsonObject(); @@ -349,6 +362,7 @@ public void testEmptyJsonToDataObject() { Assert.assertEquals(null, obj.getJsonArray()); Assert.assertEquals(null, obj.getMethodMapped()); Assert.assertEquals(null, obj.getStringList()); + Assert.assertEquals(null, obj.getEnumMapped()); Assert.assertEquals(null, obj.getBoxedBooleanList()); Assert.assertEquals(null, obj.getBoxedByteList()); Assert.assertEquals(null, obj.getBoxedShortList()); @@ -365,6 +379,7 @@ public void testEmptyJsonToDataObject() { Assert.assertEquals(null, obj.getHttpMethodList()); Assert.assertEquals(null, obj.getMethodMappedList()); Assert.assertEquals(null, obj.getObjectList()); + Assert.assertEquals(null, obj.getEnumMappedList()); Assert.assertEquals(null, obj.getStringSet()); Assert.assertEquals(null, obj.getBoxedBooleanSet()); Assert.assertEquals(null, obj.getBoxedByteSet()); @@ -382,6 +397,7 @@ public void testEmptyJsonToDataObject() { Assert.assertEquals(null, obj.getHttpMethodSet()); Assert.assertEquals(null, obj.getMethodMappedSet()); Assert.assertEquals(null, obj.getObjectSet()); + Assert.assertEquals(null, obj.getEnumMappedSet()); Assert.assertEquals(Collections.emptyList(), obj.getAddedStringValues()); Assert.assertEquals(Collections.emptyList(), obj.getAddedBoxedBooleanValues()); Assert.assertEquals(Collections.emptyList(), obj.getAddedBoxedByteValues()); @@ -399,6 +415,7 @@ public void testEmptyJsonToDataObject() { Assert.assertEquals(Collections.emptyList(), obj.getAddedHttpMethods()); Assert.assertEquals(Collections.emptyList(), obj.getAddedMethodMappeds()); Assert.assertEquals(Collections.emptyList(), obj.getAddedObjects()); + Assert.assertEquals(Collections.emptyList(), obj.getAddedEnumMappeds()); Assert.assertEquals(null, obj.getStringValueMap()); Assert.assertEquals(null, obj.getBoxedBooleanValueMap()); Assert.assertEquals(null, obj.getBoxedByteValueMap()); @@ -416,6 +433,7 @@ public void testEmptyJsonToDataObject() { Assert.assertEquals(null, obj.getHttpMethodMap()); Assert.assertEquals(null, obj.getMethodMappedMap()); Assert.assertEquals(null, obj.getObjectMap()); + Assert.assertEquals(null, obj.getEnumMappedMap()); } @Test @@ -446,6 +464,7 @@ public void testDataObjectToJson() throws Exception { TimeUnit httpMethod = TimeUnit.values()[TestUtils.randomPositiveInt() % TimeUnit.values().length]; ZonedDateTime dateTime = ZonedDateTime.now(); Locale uri = new Locale("en"); + TestCustomEnum testCustomEnum = TestCustomEnum.DEV; Map map = new HashMap<>(); map.put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(20)); @@ -481,6 +500,7 @@ public void testDataObjectToJson() throws Exception { obj.setJsonArray(jsonArray); obj.setHttpMethod(httpMethod); obj.setMethodMapped(dateTime); + obj.setEnumMapped(testCustomEnum); obj.setStringList(Collections.singletonList(stringValue)); obj.setBoxedBooleanList(Collections.singletonList(boxedBooleanValue)); obj.setBoxedByteList(Collections.singletonList(boxedByteValue)); @@ -498,6 +518,7 @@ public void testDataObjectToJson() throws Exception { obj.setHttpMethodList(Collections.singletonList(httpMethod)); obj.setMethodMappedList(Collections.singletonList(dateTime)); obj.setObjectList(list); + obj.setEnumMappedList(Collections.singletonList(testCustomEnum)); obj.setStringValueMap(Collections.singletonMap(key, stringValue)); obj.setStringSet(Collections.singleton(stringValue)); obj.setBoxedBooleanSet(Collections.singleton(boxedBooleanValue)); @@ -516,6 +537,7 @@ public void testDataObjectToJson() throws Exception { obj.setHttpMethodSet(Collections.singleton(httpMethod)); obj.setMethodMappedSet(Collections.singleton(dateTime)); obj.setObjectSet(new LinkedHashSet<>(list)); + obj.setEnumMappedSet(Collections.singleton(testCustomEnum)); obj.setBoxedBooleanValueMap(Collections.singletonMap(key, boxedBooleanValue)); obj.setBoxedByteValueMap(Collections.singletonMap(key, boxedByteValue)); obj.setBoxedShortValueMap(Collections.singletonMap(key, boxedShortValue)); @@ -532,6 +554,7 @@ public void testDataObjectToJson() throws Exception { obj.setHttpMethodMap(Collections.singletonMap(key, httpMethod)); obj.setMethodMappedMap(Collections.singletonMap(key, dateTime)); obj.setObjectMap(map); + obj.setEnumMappedMap(Collections.singletonMap(key, testCustomEnum)); obj.addKeyedStringValue(key, stringValue); obj.addKeyedBoxedBooleanValue(key, boxedBooleanValue); obj.addKeyedBoxedByteValue(key, boxedByteValue); @@ -549,6 +572,7 @@ public void testDataObjectToJson() throws Exception { obj.addKeyedEnumValue(key, httpMethod); obj.addKeyedMethodMappedValue(key, dateTime); map.forEach(obj::addKeyedObjectValue); + obj.addKeyedEnumMappedValue(key, testCustomEnum); Map json = new HashMap<>(); TestDataObjectConverter.toJson(obj, json); @@ -577,6 +601,7 @@ public void testDataObjectToJson() throws Exception { assertEquals(jsonArray, json.get("jsonArray")); assertEquals(httpMethod.name(), json.get("httpMethod")); assertEquals(dateTime.toString(), json.get("methodMapped")); + assertEquals(testCustomEnum.getShortName(), json.get("enumMapped")); assertEquals(new JsonArray().add(stringValue), json.get("stringList")); assertEquals(new JsonArray().add(boxedBooleanValue), json.get("boxedBooleanList")); assertEquals(new JsonArray().add(boxedByteValue), json.get("boxedByteList")); @@ -596,6 +621,7 @@ public void testDataObjectToJson() throws Exception { assertEquals(new JsonArray().add(httpMethod.name()), json.get("httpMethodList")); assertEquals(new JsonArray().add(dateTime.toString()), json.get("methodMappedList")); assertEquals(new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2)), json.get("objectList")); + assertEquals(new JsonArray().add(testCustomEnum.getShortName()), json.get("enumMappedList")); assertEquals(new JsonArray().add(stringValue), json.get("stringSet")); assertEquals(new JsonArray().add(boxedBooleanValue), json.get("boxedBooleanSet")); assertEquals(new JsonArray().add(boxedByteValue), json.get("boxedByteSet")); @@ -615,6 +641,7 @@ public void testDataObjectToJson() throws Exception { assertEquals(new JsonArray().add(httpMethod.name()), json.get("httpMethodSet")); assertEquals(new JsonArray().add(dateTime.toString()), json.get("methodMappedSet")); assertEquals(new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2)), json.get("objectSet")); + assertEquals(new JsonArray().add(testCustomEnum.getShortName()), json.get("enumMappedSet")); assertEquals(new JsonObject().put(key, stringValue), json.get("stringValueMap")); assertEquals(new JsonObject().put(key, boxedBooleanValue), json.get("boxedBooleanValueMap")); assertEquals(new JsonObject().put(key, boxedByteValue), json.get("boxedByteValueMap")); @@ -634,6 +661,7 @@ public void testDataObjectToJson() throws Exception { assertEquals(new JsonObject().put(key, httpMethod.name()), json.get("httpMethodMap")); assertEquals(new JsonObject().put(key, dateTime.toString()), json.get("methodMappedMap")); assertEquals(toJson(map), json.get("objectMap")); + assertEquals(new JsonObject().put(key, testCustomEnum.getShortName()), json.get("enumMappedMap")); assertEquals(new JsonObject().put(key, stringValue), json.get("keyedStringValues")); assertEquals(new JsonObject().put(key, boxedBooleanValue), json.get("keyedBoxedBooleanValues")); assertEquals(new JsonObject().put(key, boxedByteValue), json.get("keyedBoxedByteValues")); @@ -653,6 +681,7 @@ public void testDataObjectToJson() throws Exception { assertEquals(new JsonObject().put(key, httpMethod.name()), json.get("keyedEnumValues")); assertEquals(new JsonObject().put(key, dateTime.toString()), json.get("keyedMethodMappedValues")); assertEquals(toJson(map), json.get("keyedObjectValues")); + assertEquals(new JsonObject().put(key, testCustomEnum.getShortName()), json.get("keyedEnumMappedValues")); } @Test @@ -687,6 +716,7 @@ public void testEmptyDataObjectToJson() { assertEquals(null, json.get("jsonArray")); assertEquals(null, json.get("httpMethod")); assertEquals(null, json.get("methodMapped")); + assertEquals(null, json.get("enumMapped")); assertEquals(null, json.get("stringList")); assertEquals(null, json.get("boxedBooleanList")); assertEquals(null, json.get("boxedByteList")); @@ -704,6 +734,7 @@ public void testEmptyDataObjectToJson() { assertEquals(null, json.get("httpMethodList")); assertEquals(null, json.get("methodMappedList")); assertEquals(null, json.get("objectList")); + assertEquals(null, json.get("enumMappedList")); assertEquals(null, json.get("stringSet")); assertEquals(null, json.get("boxedBooleanSet")); assertEquals(null, json.get("boxedByteSet")); @@ -721,6 +752,7 @@ public void testEmptyDataObjectToJson() { assertEquals(null, json.get("httpMethodSet")); assertEquals(null, json.get("methodMappedSet")); assertEquals(null, json.get("objectSet")); + assertEquals(null, json.get("enumMappedSet")); assertEquals(new JsonArray(), json.get("addedStringValues")); assertEquals(new JsonArray(), json.get("addedBoxedBooleanValues")); assertEquals(new JsonArray(), json.get("addedBoxedByteValues")); @@ -737,6 +769,7 @@ public void testEmptyDataObjectToJson() { assertEquals(new JsonArray(), json.get("addedHttpMethods")); assertEquals(new JsonArray(), json.get("addedMethodMappeds")); assertEquals(new JsonArray(), json.get("addedObjects")); + assertEquals(new JsonArray(), json.get("addedEnumMappeds")); assertEquals(null, json.get("stringValueMap")); assertEquals(null, json.get("boxedBooleanValueMap")); assertEquals(null, json.get("boxedByteValueMap")); @@ -753,6 +786,7 @@ public void testEmptyDataObjectToJson() { assertEquals(null, json.get("httpMethodMap")); assertEquals(null, json.get("methodMappedMap")); assertEquals(null, json.get("objectMap")); + assertEquals(null, json.get("enumMappedMap")); } @Test