Skip to content

Commit

Permalink
change tokens - do not mutate input map, do not cache output object t…
Browse files Browse the repository at this point in the history
…ype (finos#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
  • Loading branch information
mateusz-klatt authored and AFine-gs committed Jan 9, 2024
1 parent 643b441 commit c164651
Show file tree
Hide file tree
Showing 36 changed files with 1,154 additions and 1,246 deletions.
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

0 comments on commit c164651

Please sign in to comment.