Skip to content

Commit

Permalink
change tokens - do not mutate input map, do not cache output object type
Browse files Browse the repository at this point in the history
  • Loading branch information
Klatt, Matt [Engineering] authored and Klatt, Matt [Engineering] committed Nov 24, 2023
1 parent 85fef69 commit c61d96b
Show file tree
Hide file tree
Showing 32 changed files with 1,256 additions and 343 deletions.
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

0 comments on commit c61d96b

Please sign in to comment.