DMNScenarioRunnerHelper extends AbstractRunnerHelper { @SuppressWarnings("unchecked") @Override protected Object createObject(ValueWrapper<Object> initialInstance, String className, Map<List<String>, Object> params, ClassLoader classLoader) { if (initialInstance.isValid() && !(initialInstance.getValue() instanceof Map)) { return initialInstance.getValue(); } Map<String, Object> toReturn = (Map<String, Object>) initialInstance.orElseGet(HashMap::new); for (Map.Entry<List<String>, Object> listObjectEntry : params.entrySet()) { if (listObjectEntry.getKey().isEmpty()) { continue; } List<String> allSteps = listObjectEntry.getKey(); List<String> steps = allSteps.subList(0, allSteps.size() - 1); String lastStep = allSteps.get(allSteps.size() - 1); Map<String, Object> targetMap = toReturn; for (String step : steps) { targetMap = (Map<String, Object>) targetMap.computeIfAbsent(step, k -> new HashMap<>()); } targetMap.put(lastStep, listObjectEntry.getValue()); } return toReturn; }    }
@SuppressWarnings("unchecked") @Test public void createObject() { Map<List<String>, Object> params = new HashMap<>(); params.put(asList("creator", "name"), "TestName"); params.put(asList("creator", "surname"), "TestSurname"); params.put(singletonList("age"), BigDecimal.valueOf(10)); ValueWrapper<Object> initialInstance = runnerHelper.getDirectMapping(params); Object objectRaw = runnerHelper.createObject( initialInstance, String.class.getCanonicalName(), params, this.getClass().getClassLoader()); assertTrue(objectRaw instanceof Map); Map<String, Object> object = (Map<String, Object>) objectRaw; assertEquals(BigDecimal.valueOf(10), object.get("age")); assertTrue(object.get("creator") instanceof Map); Map<String, Object> creator = (Map<String, Object>) object.get("creator"); assertEquals("TestName", creator.get("name")); assertEquals("TestSurname", creator.get("surname")); }
@Test public void createObjectDirectMappingSimpleType() { Map<List<String>, Object> params = new HashMap<>(); String directMappingSimpleTypeValue = "TestName"; params.put(emptyList(), directMappingSimpleTypeValue); ValueWrapper<Object> initialInstance = runnerHelper.getDirectMapping(params); Object objectRaw = runnerHelper.createObject( initialInstance, String.class.getCanonicalName(), params, this.getClass().getClassLoader()); assertTrue(objectRaw instanceof String); assertEquals(directMappingSimpleTypeValue, objectRaw); }
@Test public void createObjectDirectMappingSimpleTypeNull() { Map<List<String>, Object> params = new HashMap<>(); params.put(emptyList(), null); ValueWrapper<Object> initialInstance = runnerHelper.getDirectMapping(params); Object objectRaw = runnerHelper.createObject( initialInstance, String.class.getCanonicalName(), params, this.getClass().getClassLoader()); assertNull(objectRaw); }
@SuppressWarnings("unchecked") @Test public void createObjectDirectMappingComplexType() { Map<List<String>, Object> params = new HashMap<>(); Map<String, Object> directMappingComplexTypeValue = new HashMap<>(); directMappingComplexTypeValue.put("key1", "value1"); params.put(emptyList(), directMappingComplexTypeValue); params.put(singletonList("key2"), "value2"); ValueWrapper<Object> initialInstance = runnerHelper.getDirectMapping(params); Object objectRaw = runnerHelper.createObject( initialInstance, Map.class.getCanonicalName(), params, this.getClass().getClassLoader()); assertTrue(objectRaw instanceof Map); Map<String, Object> object = (Map<String, Object>) objectRaw; assertEquals("value1", object.get("key1")); assertEquals("value2", object.get("key2")); }