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 #2477

Closed
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 @@ -103,7 +103,7 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
@Test
public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::OuterClass\",\n" +
Expand All @@ -112,7 +112,8 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
" \"abc\": \"someValue\"\n" +
" }\n" +
"}\n", Map.class);
"}\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");

Expand All @@ -124,5 +125,6 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" \"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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException
@Test
public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg123\", \n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -73,7 +73,8 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" +
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" +
" ]\n" +
"}", Map.class);
"}";
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(
Expand All @@ -88,13 +89,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" \"abc\": true\n" +
"}", Map.class); // updated version and new default value field added
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -104,7 +105,8 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" +
" ],\n" +
" \"abc\": true\n" +
"}", Map.class);
"}";
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(
Expand All @@ -118,13 +120,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" ]\n" +
"}", Map.class); // remove default values
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -134,9 +136,11 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" +
" ],\n" +
" \"abc\": false\n" +
"}", Map.class);
"}";
Map<String,Object> jsonNode = mapper.readValue(input, 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());
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException
@Test
public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg123\", \n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -73,7 +73,8 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" +
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" +
" ]\n" +
"}", Map.class);
"}";
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(
Expand All @@ -88,13 +89,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" \"abc\": true\n" +
"}", Map.class); // updated version and new default value field added
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -104,7 +105,8 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" +
" ],\n" +
" \"abc\": true\n" +
"}", Map.class);
"}";
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(
Expand All @@ -118,13 +120,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" ]\n" +
"}", Map.class); // remove default values
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -134,9 +136,11 @@ public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMetho
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\", \"abc\": true}]\n" +
" ],\n" +
" \"abc\": false\n" +
"}", Map.class);
"}";
Map<String,Object> jsonNode = mapper.readValue(input, 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());
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void testVersions() throws NoSuchMethodException, InvocationTargetExcepti
@Test
public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg123\", \n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -101,7 +101,8 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" +
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" +
" ]\n" +
"}", Map.class);
"}";
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(
Expand All @@ -116,13 +117,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" \"abc\": 100, \"def\": 200\n" +
"}", Map.class); // updated version and new default value field added
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg789\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -132,7 +133,8 @@ 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<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(
Expand All @@ -146,13 +148,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" ]\n" +
"}", Map.class); // remove default values
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg789\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -162,9 +164,11 @@ 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);
"}";
Map<String,Object> jsonNode = mapper.readValue(input, 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());
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void setupSuite() throws IOException, ClassNotFoundException
@Test
public void testUpcast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg123\", \n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -47,7 +47,8 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" {\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}, \n" +
" [{\"@type\": \"meta::pure::changetoken::tests::SampleClass\"}]\n" +
" ]\n" +
"}", Map.class);
"}";
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(
Expand All @@ -62,13 +63,13 @@ public void testUpcast() throws JsonProcessingException, NoSuchMethodException,
" \"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);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncast() throws JsonProcessingException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -78,7 +79,8 @@ 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<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(
Expand All @@ -92,13 +94,13 @@ public void testDowncast() throws JsonProcessingException, NoSuchMethodException
" ]\n" +
"}", Map.class); // remove default values
Assert.assertEquals(expectedJsonNodeOut, jsonNodeOut);
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}

@Test
public void testDowncastNonDefault() throws JsonProcessingException, NoSuchMethodException
{
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> jsonNode = mapper.readValue(
String input =
"{\n" +
" \"version\":\"ftdm:abcdefg456\",\n" +
" \"@type\": \"meta::pure::changetoken::tests::SampleClass\",\n" +
Expand All @@ -108,9 +110,11 @@ 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);
"}";
Map<String,Object> jsonNode = mapper.readValue(input, 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());
Assert.assertEquals("Cannot remove non-default value:{@type=Custom, restricted=true, range={min=-1, round=0.0, max=1, @type=intMinMax}, value=1}", re.getCause().getMessage());
Assert.assertEquals(mapper.readValue(input, Map.class), jsonNode);
}
}
Loading
Loading