Skip to content

Commit

Permalink
refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-klatt committed Nov 28, 2023
1 parent 0f763c6 commit 70bc4a9
Show file tree
Hide file tree
Showing 32 changed files with 536 additions and 1,556 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,38 +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
{
String input =
"{\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<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> 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);
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 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 70bc4a9

Please sign in to comment.