EventUtils { static TypedValue typedValueFromJsonNode(JsonNode value, Type suggestedType) { if (value != null && value.isObject()) { return new StructureValue( typeOf(value).getName(), streamFrom(value.fields()) .collect(HashMap::new, (map, entry) -> map.put(entry.getKey(), typedValueFromJsonNode(entry.getValue(), null)), HashMap::putAll) ); } if (value != null && value.isArray()) { return new CollectionValue( typeOf(value).getName(), streamFrom(value.elements()).map(v -> typedValueFromJsonNode(v, null)).collect(Collectors.toList()) ); } Type finalType = Optional.ofNullable(suggestedType).orElseGet(() -> typeOf(value)); return new UnitValue(finalType.getName(), value); } private  EventUtils(); static JsonNode jsonNodeFrom(Object object); static List<O> map(List<I> input, Function<I, O> mapper); static Message messageFrom(DMNMessage message); static Message messageFrom(InternalMessageType message); static Message messageFrom(InternalMessageType message, Throwable throwable); static MessageExceptionField messageExceptionFieldFrom(Throwable throwable); static MessageFEELEvent messageFEELEventFrom(FEELEvent feelEvent); static MessageFEELEventSeverity messageFEELEventSeverityFrom(FEELEvent.Severity severity); static MessageLevel messageLevelFrom(org.kie.api.builder.Message.Level level); static Stream<T> streamFrom(Iterator<T> iterator); static TraceResourceId traceResourceIdFrom(String serviceUrl, DMNModel model); static TypedValue typedValueFrom(Object value); static TypedValue typedValueFrom(DMNType type, Object value);  }
@Test void testTypedVariableFromJsonNode() throws JsonProcessingException { ObjectReader reader = new ObjectMapper().reader(); TypedValue value = EventUtils.typedValueFromJsonNode(null, null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.UNKNOWN.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(reader.readTree("true"), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.BOOLEAN.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(reader.readTree("12"), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.NUMBER.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(reader.readTree("\"test\""), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.STRING.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(reader.readTree("[1,2,3]"), null); assertNotNull(value); assertSame(TypedValue.Kind.COLLECTION, value.getKind()); assertEquals(BuiltInType.LIST.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(reader.readTree("{\"name\": \"John\", \"age\": 45, \"married\": true}"), null); assertNotNull(value); assertSame(TypedValue.Kind.STRUCTURE, value.getKind()); assertEquals(BuiltInType.UNKNOWN.getName(), value.getType()); }
@Test void testTypedVariableFromJsonNodeWithDMNType() throws JsonProcessingException { ObjectReader reader = new ObjectMapper().reader(); TypedValue value = EventUtils.typedValueFromJsonNode(mockDMNType("Any"),null, null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.UNKNOWN.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(mockDMNType("boolean"), reader.readTree("true"), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.BOOLEAN.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(mockDMNType("number"), reader.readTree("12"), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.NUMBER.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(mockDMNType("string"), reader.readTree("\"test\""), null); assertNotNull(value); assertSame(TypedValue.Kind.UNIT, value.getKind()); assertEquals(BuiltInType.STRING.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(mockDMNType("number"), reader.readTree("[1,2,3]"), null); assertNotNull(value); assertSame(TypedValue.Kind.COLLECTION, value.getKind()); assertEquals(BuiltInType.NUMBER.getName(), value.getType()); value = EventUtils.typedValueFromJsonNode(mockDMNType("Person"), reader.readTree("{\"name\": \"John\", \"age\": 45, \"married\": true}"), null); assertNotNull(value); assertSame(TypedValue.Kind.STRUCTURE, value.getKind()); assertEquals("Person", value.getType()); }