Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change tokens - do not mutate input map, do not cache output object type #2479

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<String,Object> jsonNode = mapper.readValue(input, Map.class);
Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode);

Map<String,Object> 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" +
Expand All @@ -93,36 +87,26 @@ 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<String,Object> jsonNode = mapper.readValue(
"{\n" +
expect(downcast("{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" +
" \"existingValue\": \"someValue\",\n" +
" \"innerObject\": {\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
" \"abc\": \"someValue\"\n" +
" }\n" +
"}\n", Map.class);

Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123");

Map<String,Object> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<String,Object> jsonNode = mapper.readValue(input, Map.class);
Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode);

Map<String,Object> 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" +
Expand All @@ -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<String,Object> jsonNode = mapper.readValue(input, Map.class);

Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123");
// Map<String,Object> jsonNodeOut = TempGenerated.downcast(jsonNode, "ftdm:abcdefg123");

Map<String,Object> 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" +
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<String,Object> jsonNode = mapper.readValue(input, Map.class);
Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("upcast", Map.class).invoke(null, jsonNode);

Map<String,Object> 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" +
Expand All @@ -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<String,Object> jsonNode = mapper.readValue(input, Map.class);

Map<String,Object> jsonNodeOut = (Map<String,Object>) compiledClass.getMethod("downcast", Map.class, String.class).invoke(null, jsonNode, "ftdm:abcdefg123");
// Map<String,Object> jsonNodeOut = TempGenerated.downcast(jsonNode, "ftdm:abcdefg123");

Map<String,Object> 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" +
Expand All @@ -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");
}
}
Loading
Loading