From c1646515dba35f4a22161544303704c44a1728fc Mon Sep 17 00:00:00 2001 From: Mateusz Klatt Date: Tue, 28 Nov 2023 17:59:55 +0100 Subject: [PATCH] change tokens - do not mutate input map, do not cache output object type (#2479) * change tokens - do not mutate input map, do not cache output object type * fix issue with dependency plugin analyzer in changetoken pure module * refactor unit tests --- .../generation/GenerateCast2Test.java | 50 +-- .../generation/GenerateCast3Test.java | 80 ++--- .../generation/GenerateCast4Test.java | 80 ++--- .../GenerateCastBooleanStringTest.java | 39 +-- .../generation/GenerateCastBooleanTest.java | 39 +-- .../generation/GenerateCastChainTest.java | 38 +-- .../generation/GenerateCastCustomMapTest.java | 39 +-- .../GenerateCastCustomNestedTest.java | 39 +-- .../GenerateCastCustomObjectTest.java | 39 +-- ...GenerateCastCustomPrimitiveStringTest.java | 39 +-- .../GenerateCastCustomPrimitiveTest.java | 39 +-- .../generation/GenerateCastCustomTest.java | 39 +-- .../GenerateCastDoubleStringTest.java | 39 +-- .../generation/GenerateCastDoubleTest.java | 39 +-- .../generation/GenerateCastFromJsonTest.java | 52 +--- .../GenerateCastFromVersionsTest.java | 53 +--- .../GenerateCastIntegerStringTest.java | 39 +-- .../GenerateCastMoveExtractTest.java | 26 +- .../GenerateCastMoveNestedTest.java | 109 ++----- .../generation/GenerateCastMoveTest.java | 76 ++--- .../generation/GenerateCastRemoveTest.java | 51 +-- ...nerateCastRenameFieldRenamedClassTest.java | 293 ++++++++++++++++++ .../GenerateCastRenameNestedPropertyTest.java | 120 ++----- .../GenerateCastRenamePropertyTest.java | 120 ++----- .../generation/GenerateCastRenameTest.java | 120 ++----- ...nerateCastRenamedClassRenameFieldTest.java | 293 ++++++++++++++++++ .../GenerateCastRenamedClassTest.java | 53 +--- .../GenerateCastStringQuotesTest.java | 39 +-- .../generation/GenerateCastStringTest.java | 39 +-- .../generation/GenerateCastTest.java | 51 +-- .../generation/GenerateCastTestBase.java | 66 +++- .../generation/GenerateCastUtilTest.java | 18 +- .../legend-engine-xt-changetoken-pure/pom.xml | 20 ++ .../cast_generation.pure | 85 +++-- .../changetoken_test.pure | 7 - .../changetoken/test/Test_Pure_Suite.java | 32 ++ 36 files changed, 1154 insertions(+), 1246 deletions(-) create mode 100644 legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameFieldRenamedClassTest.java create mode 100644 legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassRenameFieldTest.java create mode 100644 legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/test/java/org/finos/legend/engine/changetoken/test/Test_Pure_Suite.java diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast2Test.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast2Test.java index 4cd128b3187..a2db314b027 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast2Test.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast2Test.java @@ -15,13 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Map; public class GenerateCast2Test extends GenerateCastTestBase { @@ -63,23 +61,19 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String input = "{\n" + - " \"version\": \"ftdm:abcdefg123\",\n" + - " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + - " \"array\": [\n" + - " {\n" + - " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + - " \"existingValue\": \"someValue\",\n" + - " \"innerObject\": {\n" + - " \"@type\": \"meta::pure::changetoken::tests::SampleClass\"\n" + - " }\n" + - " }\n" + - " ]\n" + - "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + expect(upcast("{\n" + + " \"version\": \"ftdm:abcdefg123\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + + " \"array\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + + " \"existingValue\": \"someValue\",\n" + + " \"innerObject\": {\n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\"\n" + + " }\n" + + " }\n" + + " ]\n" + + "}\n"), "{\n" + " \"version\": \"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -93,18 +87,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " }\n" + " }\n" + " ]\n" + - "}\n", Map.class); // copied 'someValue' from '../existingValue' - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - - // assert that the input is not mutated - Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + " \"existingValue\": \"someValue\",\n" + @@ -112,17 +101,12 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"abc\": \"someValue\"\n" + " }\n" + - "}\n", Map.class); - - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123"); - - Map expectedJsonNodeOut = mapper.readValue( + "}\n", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + " \"existingValue\": \"someValue\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}\n" + - "}\n", Map.class); // moved 'someValue' to '../existingValue' - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast3Test.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast3Test.java index adf0e20ef7b..bcf81fec6c3 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast3Test.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast3Test.java @@ -15,13 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Map; public class GenerateCast3Test extends GenerateCastTestBase { @@ -56,24 +54,20 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String input = "{\n" + - " \"version\": \"ftdm:abcdefg123\",\n" + - " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + - " \"array\": [\n" + - " {\n" + - " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + - " \"existingValue\": \"someValue\",\n" + - " \"innerObject\": {\n" + - " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + - " \"abc\": 123\n" + - " }\n" + - " }\n" + - " ]\n" + - "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + expect(upcast("{\n" + + " \"version\": \"ftdm:abcdefg123\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + + " \"array\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + + " \"existingValue\": \"someValue\",\n" + + " \"innerObject\": {\n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"abc\": 123\n" + + " }\n" + + " }\n" + + " ]\n" + + "}\n"), "{\n" + " \"version\": \"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -87,36 +81,26 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " }\n" + " }\n" + " ]\n" + - "}\n", Map.class); // 123 converted to "123" - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - - // assert that the input is not mutated - Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String input = "{\n" + - " \"version\": \"ftdm:abcdefg456\",\n" + - " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + - " \"array\": [\n" + - " {\n" + - " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + - " \"existingValue\": \"someValue\",\n" + - " \"innerObject\": {\n" + - " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + - " \"abc\": \"123\"\n" + - " }\n" + - " }\n" + - " ]\n" + - "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123"); -// Map jsonNodeOut = TempGenerated.downcast(jsonNode, "ftdm:abcdefg123"); - - Map expectedJsonNodeOut = mapper.readValue( + expect(downcast("{\n" + + " \"version\": \"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + + " \"array\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + + " \"existingValue\": \"someValue\",\n" + + " \"innerObject\": {\n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"abc\": \"123\"\n" + + " }\n" + + " }\n" + + " ]\n" + + "}\n", "ftdm:abcdefg123"), "{\n" + " \"version\": \"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -130,10 +114,6 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " }\n" + " }\n" + " ]\n" + - "}\n", Map.class); // "123" converted to 123 - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - - // assert that the input is not mutated - Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode); + "}\n"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast4Test.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast4Test.java index abefa9ef9d5..44440ecbb1f 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast4Test.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCast4Test.java @@ -15,13 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Map; public class GenerateCast4Test extends GenerateCastTestBase { @@ -56,24 +54,20 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String input = "{\n" + - " \"version\": \"ftdm:abcdefg123\",\n" + - " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + - " \"array\": [\n" + - " {\n" + - " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + - " \"existingValue\": \"someValue\",\n" + - " \"innerObject\": {\n" + - " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + - " \"abc\": 123\n" + - " }\n" + - " }\n" + - " ]\n" + - "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + expect(upcast("{\n" + + " \"version\": \"ftdm:abcdefg123\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + + " \"array\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + + " \"existingValue\": \"someValue\",\n" + + " \"innerObject\": {\n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"abc\": 123\n" + + " }\n" + + " }\n" + + " ]\n" + + "}\n"), "{\n" + " \"version\": \"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -87,36 +81,26 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " }\n" + " }\n" + " ]\n" + - "}\n", Map.class); // 123 converted to 123 optional - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - - // assert that the input is not mutated - Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String input = "{\n" + - " \"version\": \"ftdm:abcdefg456\",\n" + - " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + - " \"array\": [\n" + - " {\n" + - " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + - " \"existingValue\": \"someValue\",\n" + - " \"innerObject\": {\n" + - " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + - " \"abc\": \"123\"\n" + - " }\n" + - " }\n" + - " ]\n" + - "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123"); -// Map jsonNodeOut = TempGenerated.downcast(jsonNode, "ftdm:abcdefg123"); - - Map expectedJsonNodeOut = mapper.readValue( + expect(downcast("{\n" + + " \"version\": \"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + + " \"array\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" + + " \"existingValue\": \"someValue\",\n" + + " \"innerObject\": {\n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"abc\": \"123\"\n" + + " }\n" + + " }\n" + + " ]\n" + + "}\n", "ftdm:abcdefg123"), "{\n" + " \"version\": \"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -130,10 +114,6 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " }\n" + " }\n" + " ]\n" + - "}\n", Map.class); // "123" optional converted to "123" - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - - // assert that the input is not mutated - Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode); + "}\n"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanStringTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanStringTest.java index ec0c4b4fec1..f1218adb444 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanStringTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanStringTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastBooleanStringTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": true\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": true\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": false\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:false", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:false"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanTest.java index 0e52c62d805..73af6a72109 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastBooleanTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastBooleanTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": true\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": true\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" + " ],\n" + " \"abc\": false\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:false", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:false"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastChainTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastChainTest.java index 74f17734b3f..bf8d11307e0 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastChainTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastChainTest.java @@ -15,19 +15,14 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastChainTest extends GenerateCastTestBase { @@ -92,8 +87,7 @@ public void testVersions() throws NoSuchMethodException, InvocationTargetExcepti @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -101,10 +95,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg789\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -114,16 +105,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100, \"def\": 200}]\n" + " ],\n" + " \"abc\": 100, \"def\": 200\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg789\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100, \"def\": 200},\n" + @@ -132,10 +120,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100, \"def\": 200}]\n" + " ],\n" + " \"abc\": 100, \"def\": 200\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -144,16 +129,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg789\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100, \"def\": 200},\n" + @@ -162,9 +144,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100, \"def\": 200}]\n" + " ],\n" + " \"abc\": 100, \"def\": 300\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:300"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomMapTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomMapTest.java index ec981f1bc7a..d72f00fbd06 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomMapTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomMapTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomMapTest extends GenerateCastTestBase { @@ -38,8 +32,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -47,10 +40,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -60,16 +50,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -78,10 +65,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -90,16 +74,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -108,9 +89,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.0}, \"value\":1}}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, max=1, @type=intMinMax, round=0.0}, value=1}", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, round=0.0, max=1, @type=intMinMax}, value=1}"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomNestedTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomNestedTest.java index a418b3af4ce..3e7e2a39ffb 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomNestedTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomNestedTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomNestedTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.0}, \"value\":1}}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, max=1, @type=intMinMax, round=0.0}, value=1}", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, round=0.0, max=1, @type=intMinMax}, value=1}"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomObjectTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomObjectTest.java index 9c3ce0197a9..0b81983ace2 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomObjectTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomObjectTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomObjectTest extends GenerateCastTestBase { @@ -74,8 +68,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -83,10 +76,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -96,16 +86,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -114,10 +101,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -126,16 +110,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}},\n" + @@ -144,9 +125,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.5}, \"value\":0}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"restricted\":true, \"range\":{\"min\":-1, \"max\":1, \"@type\":\"intMinMax\", \"round\":0.0}, \"value\":1}}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, max=1, @type=intMinMax, round=0.0}, value=1}", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, round=0.0, max=1, @type=intMinMax}, value=1}"); } } \ No newline at end of file diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveStringTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveStringTest.java index 93b612dfd90..89e157ecb15 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveStringTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveStringTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomPrimitiveStringTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"1970-01-01T00:00:01Z\"\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"1970-01-01T00:00:01Z\"\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"2023-06-22T18:30:01Z\"\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:2023-06-22T18:30:01Z", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:2023-06-22T18:30:01Z"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveTest.java index 2c16eadd887..c8b2886e3c2 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomPrimitiveTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomPrimitiveTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"1970-01-01T00:00:01Z\"\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"1970-01-01T00:00:01Z\"\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"1970-01-01T00:00:01Z\"}]\n" + " ],\n" + " \"abc\": \"2023-06-22T18:30:01Z\"\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:2023-06-22T18:30:01Z", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:2023-06-22T18:30:01Z"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomTest.java index df18411e728..4c3ba0d0ceb 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastCustomTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastCustomTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"0d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:{@type=Custom, value=1d}", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:{@type=Custom, value=1d}"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleStringTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleStringTest.java index 07124e68775..224fbc8ca6c 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleStringTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleStringTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastDoubleStringTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 123.45\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 123.45\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 67.89\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:67.89", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:67.89"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleTest.java index 83ff399a302..df96a82d589 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastDoubleTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastDoubleTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 123.45\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 123.45\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 123.45}]\n" + " ],\n" + " \"abc\": 67.89\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:67.89", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:67.89"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromJsonTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromJsonTest.java index d93094a8ac0..4611d8d3a95 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromJsonTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromJsonTest.java @@ -15,18 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.finos.legend.pure.generated.*; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastFromJsonTest extends GenerateCastTestBase { @@ -65,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -74,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -87,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -105,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -117,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNull() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null},\n" + @@ -135,10 +115,7 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null}]\n" + " ],\n" + " \"abc\": null\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -147,16 +124,13 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -165,9 +139,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 300\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:300"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromVersionsTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromVersionsTest.java index d2bdee908a9..8af0c5d917a 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromVersionsTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastFromVersionsTest.java @@ -15,18 +15,12 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import org.finos.legend.pure.generated.*; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastFromVersionsTest extends GenerateCastTestBase { @@ -43,7 +37,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException addField._fieldName("abc"); addField._fieldType("Integer[1]"); addField._safeCast(true); - Root_meta_pure_changetoken_ConstValue value = new Root_meta_pure_changetoken_ConstValue_Impl("Anonymous_NoCounter"); + Root_meta_pure_changetoken_ConstValue value = new Root_meta_pure_changetoken_ConstValue_Impl("Anonymous_NoCounter"); value._value(100L); addField._defaultValue(value); abcdefg456._changeTokensAdd(addField); @@ -56,8 +50,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -65,10 +58,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -78,16 +68,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -96,10 +83,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -108,16 +92,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNull() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null},\n" + @@ -126,10 +107,7 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null}]\n" + " ],\n" + " \"abc\": null\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -138,16 +116,13 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -156,9 +131,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 300\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:300"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastIntegerStringTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastIntegerStringTest.java index 5b64b824fb0..c853e37bd71 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastIntegerStringTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastIntegerStringTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastIntegerStringTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 300\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:300"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveExtractTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveExtractTest.java index 886c595a339..40f9576508f 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveExtractTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveExtractTest.java @@ -15,14 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.Map; public class GenerateCastMoveExtractTest extends GenerateCastTestBase { @@ -83,8 +80,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"names\": {\"@type\":\"NamesClass\", \"first\":\"1d\", \"middle\":\"\", \"last\":\"2d\"}},\n" + @@ -93,10 +89,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"names\": {\"@type\":\"NamesClass\", \"first\":\"5d\", \"middle\":\"\", \"last\":\"6d\"}}]\n" + " ],\n" + " \"names\": {\"@type\":\"NamesClass\", \"first\":\"7d\", \"middle\":\"\", \"last\":\"8d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -106,16 +99,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"firstName\":\"5d\", \"lastName\":\"6d\"}]\n" + " ],\n" + " \"firstName\":\"7d\", \"lastName\":\"8d\"\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"firstName\":\"1d\", \"lastName\":\"2d\"},\n" + @@ -124,10 +114,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"firstName\":\"5d\", \"lastName\":\"6d\"}]\n" + " ],\n" + " \"firstName\":\"7d\", \"lastName\":\"8d\"\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -137,7 +124,6 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"names\": {\"@type\":\"NamesClass\", \"first\":\"5d\", \"middle\":\"\", \"last\":\"6d\"}}]\n" + " ],\n" + " \"names\": {\"@type\":\"NamesClass\", \"first\":\"7d\", \"middle\":\"\", \"last\":\"8d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveNestedTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveNestedTest.java index f58af557859..4d8af2165bd 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveNestedTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveNestedTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastMoveNestedTest extends GenerateCastTestBase { @@ -65,8 +59,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"String\"}},\n" + @@ -75,10 +68,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -88,16 +78,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"1d\"}, \"abc\": {\"@type\":\"String\"}},\n" + @@ -106,10 +93,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -119,15 +103,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"String\"}},\n" + @@ -136,10 +118,7 @@ public void testUpcastSame() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -149,16 +128,13 @@ public void testUpcastSame() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"1d\"}, \"abc\": {\"@type\":\"String\"}},\n" + @@ -167,10 +143,7 @@ public void testDowncastSame() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -180,15 +153,13 @@ public void testDowncastSame() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"String\"}},\n" + @@ -197,18 +168,14 @@ public void testUpcastDifferent() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"String\", \"value\":\"5d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Cannot overwrite with different value:5d", re.getCause().getMessage()); + "}"), + "Cannot overwrite with different value:5d"); } @Test public void testDowncastDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"1d\"}, \"abc\": {\"@type\":\"String\"}},\n" + @@ -217,17 +184,14 @@ public void testDowncastDifferent() throws JsonProcessingException, NoSuchMethod " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": {\"@type\":\"String\", \"value\":\"6d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot overwrite with different value:6d", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:6d"); } @Test public void testUpcastInvalidDestination() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"String\"}},\n" + @@ -236,18 +200,14 @@ public void testUpcastInvalidDestination() throws JsonProcessingException, NoSuc " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"3d\"}, \"xyz\": \"invalid\"}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Destination is not a map: xyz", re.getCause().getMessage()); + "}"), + "Destination is not a map: xyz"); } @Test public void testDowncastInvalidDestination() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"1d\"}, \"abc\": {\"@type\":\"String\"}},\n" + @@ -256,17 +216,14 @@ public void testDowncastInvalidDestination() throws JsonProcessingException, NoS " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"3d\"}, \"abc\": \"invalid\"}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Destination is not a map: abc", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Destination is not a map: abc"); } @Test public void testUpcastInvalidSource() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"String\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"String\"}},\n" + @@ -275,18 +232,14 @@ public void testUpcastInvalidSource() throws JsonProcessingException, NoSuchMeth " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"invalid\", \"xyz\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"String\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Source is not a map: abc", re.getCause().getMessage()); + "}"), + "Source is not a map: abc"); } @Test public void testDowncastInvalidSource() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"String\", \"value\":\"1d\"}, \"abc\": {\"@type\":\"String\"}},\n" + @@ -295,9 +248,7 @@ public void testDowncastInvalidSource() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": \"invalid\", \"abc\": {\"@type\":\"String\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"String\", \"value\":\"4d\"}, \"abc\": {\"@type\":\"String\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Source is not a map: xyz", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Source is not a map: xyz"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveTest.java index 003fbe69468..0241f0ab169 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastMoveTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastMoveTest extends GenerateCastTestBase { @@ -75,8 +69,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -85,10 +78,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -98,15 +88,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0, \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0, \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -115,10 +103,7 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -128,16 +113,13 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } - @Test public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -146,10 +128,7 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -159,16 +138,13 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0}, \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0}, \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0, \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}}},\n" + @@ -177,10 +153,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0, \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"SampleNestedClass\", \"active\":true, \"step\":0, \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -190,16 +163,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -208,10 +178,7 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -221,16 +188,13 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -239,10 +203,7 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -252,7 +213,6 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRemoveTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRemoveTest.java index 280bd838c9c..f02987e157c 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRemoveTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRemoveTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastRemoveTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -74,9 +67,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -85,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void tesUpcastNull() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null},\n" + @@ -103,9 +91,7 @@ public void tesUpcastNull() throws JsonProcessingException, NoSuchMethodExceptio " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null}]\n" + " ],\n" + " \"abc\": null\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -114,16 +100,13 @@ public void tesUpcastNull() throws JsonProcessingException, NoSuchMethodExceptio " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -132,18 +115,14 @@ public void testUpcastNonDefault() throws JsonProcessingException, NoSuchMethodE " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 300\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}"), + "Cannot remove non-default value:300"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -151,10 +130,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -164,7 +140,6 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); - } + "}\n"); + } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameFieldRenamedClassTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameFieldRenamedClassTest.java new file mode 100644 index 00000000000..09f096071f2 --- /dev/null +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameFieldRenamedClassTest.java @@ -0,0 +1,293 @@ +// Copyright 2023 Goldman Sachs +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.finos.legend.engine.changetoken.generation; + +import com.fasterxml.jackson.core.JsonProcessingException; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +public class GenerateCastRenameFieldRenamedClassTest extends GenerateCastTestBase +{ + @BeforeClass + public static void setupSuite() throws IOException, ClassNotFoundException + { + setupSuiteFromJson("{\n" + + " \"@type\": \"meta::pure::changetoken::Versions\",\n" + + " \"versions\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::Version\",\n" + + " \"version\": \"ftdm:abcdefg123\"\n" + + " },\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::Version\",\n" + + " \"version\": \"ftdm:abcdefg456\",\n" + + " \"prevVersion\": \"ftdm:abcdefg123\",\n" + + " \"changeTokens\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::RenameField\",\n" + + " \"oldFieldName\": [\n" + + " \"abc\"\n" + + " ],\n" + + " \"newFieldName\": [\n" + + " \"xyz\"\n" + + " ],\n" + + " \"class\": \"meta::pure::changetoken::tests::SampleClass\"\n" + + " },\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::RenamedClass\",\n" + + " \"newName\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"class\": \"meta::pure::changetoken::tests::SampleClass\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}\n"); + } + + @Test + public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + exception(() -> upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + + "}"), + "Cannot overwrite with different value:{@type=Custom, value=9d}"); + } + + @Test + public void testDowncastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + exception(() -> downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:{@type=Custom, value=8d}"); + } +} diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameNestedPropertyTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameNestedPropertyTest.java index 5dc94a807ce..3ce0a394284 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameNestedPropertyTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameNestedPropertyTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastRenameNestedPropertyTest extends GenerateCastTestBase { @@ -65,8 +59,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -75,10 +68,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -88,15 +78,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}},\n" + @@ -105,10 +93,7 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -118,16 +103,13 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } - @Test public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"1d\"}},\n" + @@ -136,10 +118,7 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -149,16 +128,13 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}},\n" + @@ -167,10 +143,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"2d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -180,16 +153,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}},\n" + @@ -198,10 +168,7 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -211,16 +178,13 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}},\n" + @@ -229,10 +193,7 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -242,15 +203,13 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\", \"valueCustom\":\"1d\"}},\n" + @@ -259,10 +218,7 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -272,16 +228,13 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\", \"valueCustom\":\"5d\"}},\n" + @@ -290,10 +243,7 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\", \"valueCustom\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\", \"valueCustom\":\"8d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -303,15 +253,13 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\", \"valueCustom\":\"1d\"}},\n" + @@ -320,18 +268,14 @@ public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoS " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\", \"valueCustom\":\"9d\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Cannot overwrite with different value:9d", re.getCause().getMessage()); - } + "}"), + "Cannot overwrite with different value:9d"); + } @Test public void testDowncastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\", \"valueCustom\":\"5d\"}},\n" + @@ -340,9 +284,7 @@ public void testDowncastExistingTheDifferent() throws JsonProcessingException, N " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\", \"valueCustom\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\", \"valueCustom\":\"9d\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot overwrite with different value:8d", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:8d"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamePropertyTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamePropertyTest.java index c28951b2d7c..0458f6c6aba 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamePropertyTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamePropertyTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastRenamePropertyTest extends GenerateCastTestBase { @@ -63,8 +57,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,15 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}},\n" + @@ -103,10 +91,7 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -116,16 +101,13 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } - @Test public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"1d\"}},\n" + @@ -134,10 +116,7 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -147,16 +126,13 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}},\n" + @@ -165,10 +141,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"valueCustom\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"valueCustom\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -178,16 +151,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"OtherCustom\", \"value\":\"4d\"}},\n" + @@ -196,10 +166,7 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"OtherCustom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -209,16 +176,13 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"OtherCustom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"OtherCustom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"4d\"}},\n" + @@ -227,10 +191,7 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -240,15 +201,13 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"valueOther\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\", \"valueCustom\":\"1d\"}},\n" + @@ -257,10 +216,7 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -270,16 +226,13 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"valueCustom\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\", \"valueCustom\":\"5d\"}},\n" + @@ -288,10 +241,7 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\", \"valueCustom\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\", \"valueCustom\":\"8d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -301,15 +251,13 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\", \"valueCustom\":\"1d\"}},\n" + @@ -318,18 +266,14 @@ public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoS " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\", \"valueCustom\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\", \"valueCustom\":\"9d\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Cannot overwrite with different value:9d", re.getCause().getMessage()); - } + "}"), + "Cannot overwrite with different value:9d"); + } @Test public void testDowncastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\", \"valueCustom\":\"5d\"}},\n" + @@ -338,9 +282,7 @@ public void testDowncastExistingTheDifferent() throws JsonProcessingException, N " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\", \"valueCustom\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\", \"valueCustom\":\"9d\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot overwrite with different value:8d", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:8d"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameTest.java index 081c332666d..4fa70b49728 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenameTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastRenameTest extends GenerateCastTestBase { @@ -63,8 +57,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,15 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -103,10 +91,7 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -116,16 +101,13 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } - @Test public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -134,10 +116,7 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -147,16 +126,13 @@ public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodExce " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -165,10 +141,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -178,16 +151,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -196,10 +166,7 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -209,16 +176,13 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -227,10 +191,7 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -240,15 +201,13 @@ public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodEx " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -257,10 +216,7 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -270,16 +226,13 @@ public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMe " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + @@ -288,10 +241,7 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -301,15 +251,13 @@ public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuch " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -318,18 +266,14 @@ public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoS " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + - "}", Map.class); - Method upcastMethod = compiledClass.getMethod("upcast", Map.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> upcastMethod.invoke(null, jsonNode)); - Assert.assertEquals("Cannot overwrite with different value:{@type=Custom, value=9d}", re.getCause().getMessage()); - } + "}"), + "Cannot overwrite with different value:{@type=Custom, value=9d}"); + } @Test public void testDowncastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + @@ -338,9 +282,7 @@ public void testDowncastExistingTheDifferent() throws JsonProcessingException, N " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot overwrite with different value:{@type=Custom, value=8d}", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:{@type=Custom, value=8d}"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassRenameFieldTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassRenameFieldTest.java new file mode 100644 index 00000000000..45442796a27 --- /dev/null +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassRenameFieldTest.java @@ -0,0 +1,293 @@ +// Copyright 2023 Goldman Sachs +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.finos.legend.engine.changetoken.generation; + +import com.fasterxml.jackson.core.JsonProcessingException; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +public class GenerateCastRenamedClassRenameFieldTest extends GenerateCastTestBase +{ + @BeforeClass + public static void setupSuite() throws IOException, ClassNotFoundException + { + setupSuiteFromJson("{\n" + + " \"@type\": \"meta::pure::changetoken::Versions\",\n" + + " \"versions\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::Version\",\n" + + " \"version\": \"ftdm:abcdefg123\"\n" + + " },\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::Version\",\n" + + " \"version\": \"ftdm:abcdefg456\",\n" + + " \"prevVersion\": \"ftdm:abcdefg123\",\n" + + " \"changeTokens\": [\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::RenamedClass\",\n" + + " \"newName\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"class\": \"meta::pure::changetoken::tests::SampleClass\"\n" + + " },\n" + + " {\n" + + " \"@type\": \"meta::pure::changetoken::RenameField\",\n" + + " \"oldFieldName\": [\n" + + " \"abc\"\n" + + " ],\n" + + " \"newFieldName\": [\n" + + " \"xyz\"\n" + + " ],\n" + + " \"class\": \"meta::pure::changetoken::tests::nested::NewSampleClass\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}\n"); + } + + @Test + public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastMissing() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"3d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"def\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + + " ],\n" + + " \"def\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}"), + "{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + + "}\n"); + } + + @Test + public void testDowncastExistingTheSame() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + expect(downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + + "}", "ftdm:abcdefg123"), + "{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}\n" + + "}\n"); + } + + @Test + public void testUpcastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + exception(() -> upcast("{\n" + + " \"version\":\"ftdm:abcdefg123\", \n" + + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"2d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + + "}"), + "Cannot overwrite with different value:{@type=Custom, value=9d}"); + } + + @Test + public void testDowncastExistingTheDifferent() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + exception(() -> downcast("{\n" + + " \"version\":\"ftdm:abcdefg456\",\n" + + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"5d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"5d\"}},\n" + + " \"innerNestedArray\":[\n" + + " {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"6d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"6d\"}},\n" + + " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"7d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"7d\"}}]\n" + + " ],\n" + + " \"abc\": {\"@type\":\"Custom\", \"value\":\"8d\"}, \"xyz\": {\"@type\":\"Custom\", \"value\":\"9d\"}\n" + + "}", "ftdm:abcdefg123"), + "Cannot overwrite with different value:{@type=Custom, value=8d}"); + } +} diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassTest.java index 8ee7752ff70..79c6b745cd6 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastRenamedClassTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastRenamedClassTest extends GenerateCastTestBase { @@ -58,8 +52,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -68,10 +61,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + @@ -81,15 +71,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testUpcastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"1d\"}},\n" + @@ -98,10 +86,7 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -111,16 +96,13 @@ public void testUpcastType() throws JsonProcessingException, NoSuchMethodExcepti " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"abc\": {\"@type\":\"Custom\", \"value\":\"3d\"}}]\n" + " ],\n" + " \"abc\": {\"@type\":\"Custom\", \"value\":\"4d\"}\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -129,10 +111,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::nested::NewSampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -142,16 +121,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastType() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"4d\"}},\n" + @@ -160,10 +136,7 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::OtherClass\",\n" + @@ -173,8 +146,6 @@ public void testDowncastType() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::OtherClass\", \"xyz\": {\"@type\":\"Custom\", \"value\":\"2d\"}}]\n" + " ],\n" + " \"xyz\": {\"@type\":\"Custom\", \"value\":\"1d\"}\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } } - diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringQuotesTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringQuotesTest.java index aa77990d46e..15c8f783a21 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringQuotesTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringQuotesTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastStringQuotesTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"one \\\" two\"\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"one \\\" two\"\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"two \\\" one\"\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:two \" one", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:two \" one"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringTest.java index c99b95675bb..ef87ca95cef 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastStringTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastStringTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"one \\\" two\"\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"one \\\" two\"\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"},\n" + @@ -134,9 +115,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": \"one \\\" two\"}]\n" + " ],\n" + " \"abc\": \"two \\\" one\"\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:two \" one", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:two \" one"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTest.java index 57a52661b5b..15b1e1840f8 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTest.java @@ -15,17 +15,11 @@ package org.finos.legend.engine.changetoken.generation; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - -import static org.junit.Assert.assertThrows; public class GenerateCastTest extends GenerateCastTestBase { @@ -64,8 +58,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException @Test public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map jsonNode = mapper.readValue( - "{\n" + + expect(upcast("{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"},\n" + @@ -73,10 +66,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode); - - Map expectedJsonNodeOut = mapper.readValue( + "}"), "{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -86,16 +76,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException, " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); // updated version and new default value field added - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -104,10 +91,7 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 100\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -116,16 +100,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNull() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + expect(downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null},\n" + @@ -134,10 +115,7 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": null}]\n" + " ],\n" + " \"abc\": null\n" + - "}", Map.class); - Map jsonNodeOut = (Map) compiledClass.getMethod("downcast", Map.class, String.class) - .invoke(null, jsonNode, "ftdm:abcdefg123"); - Map expectedJsonNodeOut = mapper.readValue( + "}", "ftdm:abcdefg123"), "{\n" + " \"version\":\"ftdm:abcdefg123\", \n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + @@ -146,16 +124,13 @@ public void testDowncastNull() throws JsonProcessingException, NoSuchMethodExcep " {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" + " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" + " ]\n" + - "}", Map.class); // remove default values - Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut); + "}\n"); } @Test public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException { - ObjectMapper mapper = new ObjectMapper(); - Map jsonNode = mapper.readValue( - "{\n" + + exception(() -> downcast("{\n" + " \"version\":\"ftdm:abcdefg456\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" + " \"innerObject\": {\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100},\n" + @@ -164,9 +139,7 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho " [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": 100}]\n" + " ],\n" + " \"abc\": 300\n" + - "}", Map.class); - Method downcastMethod = compiledClass.getMethod("downcast", Map.class, String.class); - InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, () -> downcastMethod.invoke(null, jsonNode, "ftdm:abcdefg123")); - Assert.assertEquals("Cannot remove non-default value:300", re.getCause().getMessage()); + "}", "ftdm:abcdefg123"), + "Cannot remove non-default value:300"); } } diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTestBase.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTestBase.java index 7beacf2a2cf..5863d50d323 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTestBase.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastTestBase.java @@ -14,6 +14,7 @@ package org.finos.legend.engine.changetoken.generation; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.classgraph.ClassGraph; import org.finos.legend.engine.external.language.java.generation.GenerateJavaProject; @@ -22,26 +23,27 @@ import org.finos.legend.pure.runtime.java.compiled.compiler.MemoryFileManager; import org.junit.Assert; import org.junit.ClassRule; +import org.junit.function.ThrowingRunnable; import org.junit.rules.TemporaryFolder; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; -import javax.tools.ToolProvider; +import javax.tools.*; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; +import java.util.Map; + +import static org.junit.Assert.assertThrows; public abstract class GenerateCastTestBase { private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(GenerateCastTestBase.class); protected static Class compiledClass; - protected static final ObjectMapper mapper = new ObjectMapper(); + private static final ObjectMapper mapper = new ObjectMapper(); @ClassRule public static TemporaryFolder tmpFolder = new TemporaryFolder(); @@ -105,6 +107,58 @@ private static void setupSuiteFrom(Object fromValue, String generatorType) throw compiledClass = new MemoryClassLoader(fileManager, Thread.currentThread().getContextClassLoader()).loadClass(fullClassName); } + private Map upcast(Map objectNode) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + return (Map) compiledClass.getMethod("upcast", Map.class).invoke(null, objectNode); + } + + private Map downcast(Map objectNode, String targetVersion) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + return (Map) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, objectNode, targetVersion); + } + + protected Map parse(String value) throws JsonProcessingException + { + return mapper.readValue(value, Map.class); + } + + public Map upcast(String input) throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + Map jsonNode = parse(input); + try + { + return upcast(jsonNode); + } + finally + { + Assert.assertEquals(parse(input), jsonNode); + } + } + + public Map downcast(String input, String targetVersion) throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException + { + Map jsonNode = parse(input); + try + { + return downcast(jsonNode, targetVersion); + } + finally + { + Assert.assertEquals(parse(input), jsonNode); + } + } + + public void expect(Map actual, String expected) throws JsonProcessingException + { + Assert.assertEquals(parse(expected), actual); + } + + public void exception(ThrowingRunnable runnable, String expected) + { + InvocationTargetException re = assertThrows("non-default", InvocationTargetException.class, runnable); + Assert.assertEquals(expected, re.getCause().getMessage()); + } + private static class SourceFile extends SimpleJavaFileObject { private final Path path; diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastUtilTest.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastUtilTest.java index 8413368d138..f024e2dbc0f 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastUtilTest.java +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-compiler/src/test/java/org/finos/legend/engine/changetoken/generation/GenerateCastUtilTest.java @@ -62,12 +62,10 @@ public static void setupSuite() throws IOException, ClassNotFoundException public void testResolvePath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method m = compiledClass.getMethod("resolvePath", List.class, String.class); - // note that "0" is cast to int in resolveRelativeReference or setRelativeReference Assert.assertArrayEquals( Arrays.asList("a", "b", 1, "c", "0").toArray(), - ((List)m.invoke(null, Arrays.asList("a", "b", 1, "c", 2), "../0")).toArray()); - + ((List) m.invoke(null, Arrays.asList("a", "b", 1, "c", 2), "../0")).toArray()); InvocationTargetException ex = Assert.assertThrows(InvocationTargetException.class, () -> m.invoke(null, Arrays.asList("a", "b", 1, "c", 2), "../../../../../../x")); Assert.assertEquals("Relative reference escapes root (a/b/1/c/2) at index 5 of ../../../../../../x", ex.getCause().getMessage()); @@ -77,7 +75,6 @@ public void testResolvePath() throws NoSuchMethodException, InvocationTargetExce public void testResolveRelativeReference() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method m = compiledClass.getMethod("resolveRelativeReference", Map.class, List.class, String.class); - ObjectMapper mapper = new ObjectMapper(); String input = "{\n" + " \"version\": \"ftdm:abcdefg123\",\n" + @@ -92,18 +89,15 @@ public void testResolveRelativeReference() throws JsonProcessingException, NoSuc " }\n" + " ]\n" + "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - + Map jsonNode = parse(input); Assert.assertEquals("someValue", m.invoke(null, jsonNode, Arrays.asList("array", 0, "innerObject", "@type"), "../../existingValue")); - Assert.assertEquals("meta::pure::changetoken::tests::SampleClass", m.invoke(null, jsonNode, Collections.emptyList(), "array/0/innerObject/@type")); - InvocationTargetException ex = Assert.assertThrows(InvocationTargetException.class, () -> m.invoke(null, jsonNode, Collections.emptyList(), @@ -115,8 +109,7 @@ public void testResolveRelativeReference() throws JsonProcessingException, NoSuc public void testSetRelativeReference() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method m = compiledClass.getMethod("setRelativeReference", Map.class, List.class, String.class, Object.class); - - ObjectMapper mapper = new ObjectMapper(); + String input = "{\n" + " \"version\": \"ftdm:abcdefg123\",\n" + " \"@type\": \"meta::pure::changetoken::tests::SomeClassWithAnArray\",\n" + @@ -130,15 +123,13 @@ public void testSetRelativeReference() throws JsonProcessingException, NoSuchMet " }\n" + " ]\n" + "}\n"; - Map jsonNode = mapper.readValue(input, Map.class); - + Map jsonNode = parse(input); m.invoke(null, jsonNode, Arrays.asList("array", 0, "innerObject", "@type"), "../newValue", 123); Assert.assertEquals(123, compiledClass.getMethod("resolveRelativeReference", Map.class, List.class, String.class).invoke(null, jsonNode, Arrays.asList("array", 0, "innerObject", "@type"), "../newValue")); - { InvocationTargetException ex = Assert.assertThrows(InvocationTargetException.class, () -> m.invoke(null, jsonNode, @@ -146,7 +137,6 @@ public void testSetRelativeReference() throws JsonProcessingException, NoSuchMet "array/blah", 456)); Assert.assertEquals("java.lang.NumberFormatException: For input string: \"blah\": at index 1 of array/blah", ex.getCause().getMessage()); } - { InvocationTargetException ex = Assert.assertThrows(InvocationTargetException.class, () -> m.invoke(null, jsonNode, diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/pom.xml b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/pom.xml index d6d8b750ef7..3764a9c76f0 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/pom.xml +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/pom.xml @@ -29,6 +29,21 @@ + + org.apache.maven.plugins + maven-dependency-plugin + + + dependency-analyze + + + + junit:junit + + + + + org.finos.legend.pure legend-pure-maven-generation-par @@ -168,5 +183,10 @@ org.eclipse.collections eclipse-collections-api + + + junit + junit + diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/cast_generation.pure b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/cast_generation.pure index bf0afebd2b8..c19864c91ba 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/cast_generation.pure +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/cast_generation.pure @@ -258,7 +258,7 @@ function <> meta::pure::changetoken::cast_generation::_generateS j_return($resVar) ]))); - let typeVar = j_variable(javaString(), 'type'); + let typeVar = $resVar->j_invoke('get', [j_string('@type')], javaObject()); let rootObjVar = j_parameter(objectNode(), 'rootObjectNode'); let pathVar = j_parameter(javaList(javaObject()), 'path'); let changeTokenHandlers = $version.changeTokens @@ -282,13 +282,10 @@ function <> meta::pure::changetoken::cast_generation::_generateS javaRuntimeException()->j_new(j_string('Missing @type'))->j_throw() ), // Map res = new HashMap<>() - $resVar->j_declare(javaHashMap()->j_new([])), - - // String type = (String) objectNode.get("@type") - $typeVar->j_declare($objVar->j_invoke('get', [j_string('@type')], javaObject())->j_cast(javaString())) + $resVar->j_declare(javaHashMap()->j_new([])) ] - ->concatenate($changeTokenHandlers) ->concatenate($class2->_generateSingleCastObjectRecurseBlock($objRecurseFuncName, $arrRecurseFuncName, $objVar, $pathVar, $rootObjVar, $resVar)) + ->concatenate($changeTokenHandlers) ->concatenate([ j_return($resVar) ])))); @@ -336,7 +333,7 @@ function <> meta::pure::changetoken::cast_generation::_generateS j_return($resVar) ]))); - let typeVar = j_variable(javaString(), 'type'); + let typeVar = $resVar->j_invoke('get', [j_string('@type')], javaObject()); let changeTokenHandlers = $version.changeTokens->reverse() ->map(t|$t->match([ t:AddField[1]|_handleAddFieldDowncast($class2, $t, $objVar, $typeVar, $version.version, $pathVar, $rootObjVar, $resVar), @@ -358,12 +355,10 @@ function <> meta::pure::changetoken::cast_generation::_generateS javaRuntimeException()->j_new(j_string('Missing @type'))->j_throw() ), // Map res = new HashMap<>() - $resVar->j_declare(javaHashMap()->j_new([])), - // String type = (String) objectNode.get("@type") - $typeVar->j_declare($objVar->j_invoke('get', [j_string('@type')], javaObject())->j_cast(javaString())) + $resVar->j_declare(javaHashMap()->j_new([])) ] - ->concatenate($changeTokenHandlers) ->concatenate($class2->_generateSingleCastObjectRecurseBlock($objRecurseFuncName, $arrRecurseFuncName, $objVar, $pathVar, $rootObjVar, $resVar)) + ->concatenate($changeTokenHandlers) ->concatenate([ j_return($resVar) ])))); @@ -537,33 +532,33 @@ function <> meta::pure::changetoken::cast_generation::_handleRen ):Code[1] { $oldFieldName->size()->range()->tail()->reverse()->fold({i,r| - j_if($oldFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar)->j_instanceof(javaRawMap()),$r, - j_if(j_ne($oldFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar),j_null()), + j_if($oldFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar)->j_instanceof(javaRawMap()),$r, + j_if(j_ne($oldFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar),j_null()), javaRuntimeException()->j_new(j_plus(j_string('Source is not a map: '), j_string($oldFieldName->take($i)->joinStrings('.'))))->j_throw() ))}, j_if( - j_ne($oldFieldName->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar),j_null()) + j_ne($oldFieldName->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar),j_null()) , j_block( $newFieldName->size()->range()->tail()->map({i| - j_if($newFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar)->j_instanceof(javaRawMap())->j_not(), + j_if($newFieldName->take($i)->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar)->j_instanceof(javaRawMap())->j_not(), javaRuntimeException()->j_new(j_plus(j_string('Destination is not a map: '), j_string($newFieldName->take($i)->joinStrings('.'))))->j_throw() )})->concatenate([ - j_if($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + j_if($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('get', [j_string($newFieldName->last()->toOne())], javaObject())->j_ne(j_null()) - ->j_and($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + ->j_and($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('get', [j_string($newFieldName->last()->toOne())], javaObject())->j_invoke('equals', - $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('get', [j_string($oldFieldName->last()->toOne())], javaVoid()))->j_not()), javaRuntimeException()->j_new([j_string('Cannot overwrite with different value:') - ->j_plus($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + ->j_plus($newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('get', [j_string($newFieldName->last()->toOne())], javaObject()))])->j_throw()), - $newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + $newFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('put', [j_string($newFieldName->last()->toOne()), - $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('get', [j_string($oldFieldName->last()->toOne())], javaVoid()) ], javaVoid()), - $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $objVar) + $oldFieldName->init()->fold({t,res | $res->j_cast(objectNode())->j_invoke('get', [j_string($t)], javaObject()) }, $resVar) ->j_cast(objectNode())->j_invoke('remove', [j_string($oldFieldName->last()->toOne())], javaVoid()) ]) ) @@ -580,7 +575,7 @@ token:RenamedClass[1], objVar:Code[1], typeVar:Code[1], version:String[1], pathV // if (type.equals(token.class)) j_if( $typeVar->j_invoke('equals', [j_string($token.class)]), - $objVar->j_invoke('put', [j_string('@type'), j_string($token.newName)]) + $resVar->j_invoke('put', [j_string('@type'), j_string($token.newName)]) ); } @@ -593,7 +588,7 @@ token:RenamedClass[1], objVar:Code[1], typeVar:Code[1], version:String[1], pathV // if (type.equals(token.newName)) j_if( $typeVar->j_invoke('equals', [j_string($token.newName)]), - $objVar->j_invoke('put', [j_string('@type'), j_string($token.class)]) + $resVar->j_invoke('put', [j_string('@type'), j_string($token.class)]) ); } @@ -614,17 +609,17 @@ function <> meta::pure::changetoken::cast_generation::_handleAdd df:ConstValue[1] | if(_isPrimitive($token.fieldType) || !_isComplex($df.value),| [ $valVar->j_declare([_getValueAsFieldType($token.fieldType, $df.value)]), - j_if($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), - javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), - $objVar->j_invoke('put', [j_string($token.fieldName), $valVar], javaVoid()) + j_if($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), + javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), + $resVar->j_invoke('put', [j_string($token.fieldName), $valVar], javaVoid()) ],| j_block([ $mapVar->j_declare(javaStream()->j_invoke('of', $df.value->cast(@String)->parseJSON()->cast(@meta::json::JSONObject).keyValuePairs->map(kv | _inlineJsonValue($kv.key.value, $kv.value)), javaVoid())->j_invoke('collect', [javaCollectors()->j_invoke('toMap', [j_methodReference(AbstractMap_SimpleEntry(), 'getKey', javaFunctionType([], javaObject())), j_methodReference(AbstractMap_SimpleEntry(), 'getValue', javaFunctionType([], javaObject()))], javaVoid())], javaVoid())) ]->concatenate([ - j_if($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $mapVar)->j_not()), - javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), - $objVar->j_invoke('put', [j_string($token.fieldName), $mapVar], javaVoid()) + j_if($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $mapVar)->j_not()), + javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), + $resVar->j_invoke('put', [j_string($token.fieldName), $mapVar], javaVoid()) ])) ), df:CopyValue[1] | @@ -636,8 +631,8 @@ function <> meta::pure::changetoken::cast_generation::_handleAdd j_string($df.source->cast(@RelativeFieldReference).path) // TODO: handle other types of references ], javaObject())), // res.put("abc", value); - j_if($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), - javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), + j_if($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), + javaRuntimeException()->j_new([j_string('Cannot overwrite existing non-default value:')->j_plus($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), $resVar->j_invoke('put', [j_string($token.fieldName), $valVar], javaVoid()) ] ]) @@ -787,7 +782,7 @@ function <> meta::pure::changetoken::cast_generation::_handleCha $class->j_invoke(_getChangeFieldTypeConverter($token.oldFieldType, $token.newFieldType), [ $pathVar, - $objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()) + $resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()) ], javaObject()) ], javaVoid()) ] @@ -821,11 +816,11 @@ function <> meta::pure::changetoken::cast_generation::_handleAdd $valVar->j_declare([_getValueAsFieldType($token.fieldType, $df.value)]) ]->concatenate([ // if ((Integer)objectNode.get(fieldName) != defaultValue) throw new RuntimeException("Cannot remove non-default value" + objectNode.get("fieldName")) // TODO: handle non-int - j_if($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), - javaRuntimeException()->j_new([j_string('Cannot remove non-default value:')->j_plus($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), + j_if($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $valVar)->j_not()), + javaRuntimeException()->j_new([j_string('Cannot remove non-default value:')->j_plus($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), - // objectNode.remove(fieldName) - $objVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) + // res.remove(fieldName) + $resVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) ])), | j_block([ @@ -834,11 +829,11 @@ function <> meta::pure::changetoken::cast_generation::_handleAdd javaVoid())->j_invoke('collect', [javaCollectors()->j_invoke('toMap', [j_methodReference(AbstractMap_SimpleEntry(), 'getKey', javaFunctionType([], javaObject())), j_methodReference(AbstractMap_SimpleEntry(), 'getValue', javaFunctionType([], javaObject()))], javaVoid())], javaVoid())) ]->concatenate([ // if ((Integer)objectNode.get(fieldName) != defaultValue) throw new RuntimeException("Cannot remove non-default value" + objectNode.get("fieldName")) // TODO: handle non-int - j_if($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $mapVar)->j_not()), - javaRuntimeException()->j_new([j_string('Cannot remove non-default value:')->j_plus($objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), + j_if($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_ne(j_null())->j_and($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject())->j_invoke('equals', $mapVar)->j_not()), + javaRuntimeException()->j_new([j_string('Cannot remove non-default value:')->j_plus($resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()))])->j_throw()), - // objectNode.remove(fieldName) - $objVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) + // res.remove(fieldName) + $resVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) ])) ), df:CopyValue[1] | @@ -848,11 +843,11 @@ function <> meta::pure::changetoken::cast_generation::_handleAdd $rootObjVar, $pathVar, j_string($df.source->cast(@RelativeFieldReference).path), // TODO: handle other types of references - $objVar->j_invoke('get', [j_string($token.fieldName)], javaVoid()) + $resVar->j_invoke('get', [j_string($token.fieldName)], javaVoid()) ], javaObject()), - // objectNode.remove("abc"); - $objVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) + // res.remove("abc"); + $resVar->j_invoke('remove', [j_string($token.fieldName)], javaVoid()) ] ]) ); @@ -874,7 +869,7 @@ function <> meta::pure::changetoken::cast_generation::_handleCha $class->j_invoke(_getChangeFieldTypeConverter($token.newFieldType, $token.oldFieldType), [ $pathVar, - $objVar->j_invoke('get', [j_string($token.fieldName)], javaObject()) + $resVar->j_invoke('get', [j_string($token.fieldName)], javaObject()) ], javaObject()) ], javaVoid()) ] diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/changetoken_test.pure b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/changetoken_test.pure index f680a552999..901e8f17665 100644 --- a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/changetoken_test.pure +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/main/resources/core_pure_changetoken/changetoken_test.pure @@ -64,10 +64,3 @@ function meta::pure::changetoken::tests::getVersionsCustomMap():Versions[1] ] ); } - -function <> meta::pure::changetoken::tests::testGenerateCast():Boolean[1] -{ - let project = generateCast('meta::pure::changetoken:tests::getVersions3', 'TestCastFunction'); - printProject($project); - assertInstanceOf($project, Project); -} diff --git a/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/test/java/org/finos/legend/engine/changetoken/test/Test_Pure_Suite.java b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/test/java/org/finos/legend/engine/changetoken/test/Test_Pure_Suite.java new file mode 100644 index 00000000000..57c2a841055 --- /dev/null +++ b/legend-engine-xts-changetoken/legend-engine-xt-changetoken-pure/src/test/java/org/finos/legend/engine/changetoken/test/Test_Pure_Suite.java @@ -0,0 +1,32 @@ +// Copyright 2023 Goldman Sachs +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.finos.legend.engine.changetoken.test; + +import junit.framework.TestSuite; +import org.finos.legend.pure.m3.execution.test.PureTestBuilder; +import org.finos.legend.pure.m3.execution.test.TestCollection; +import org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport; +import org.finos.legend.pure.runtime.java.compiled.testHelper.PureTestBuilderCompiled; + +public class Test_Pure_Suite +{ + public static TestSuite suite() + { + CompiledExecutionSupport executionSupport = PureTestBuilderCompiled.getClassLoaderExecutionSupport(); + TestSuite suite = new TestSuite(); + suite.addTest(PureTestBuilderCompiled.buildSuite(TestCollection.collectTests("meta::pure::changetoken", executionSupport.getProcessorSupport(), ci -> PureTestBuilder.satisfiesConditions(ci, executionSupport.getProcessorSupport())), executionSupport)); + return suite; + } +}