public static String getPropertyName(final Simulation simulation, final int columnIndex) { return String.join(".", simulation.getSimulationDescriptor().getFactMappingByIndex(columnIndex).getExpressionElementsWithoutClass() .stream() .map(ExpressionElement::getStep) .collect(Collectors.toSet())); }
public Map<List<String>, Object> getParamsForBean(SimulationDescriptor simulationDescriptor, FactIdentifier factIdentifier, List<FactMappingValue> factMappingValues, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator) { Map<List<String>, Object> paramsForBean = new HashMap<>(); for (FactMappingValue factMappingValue : factMappingValues) { ExpressionIdentifier expressionIdentifier = factMappingValue.getExpressionIdentifier(); FactMapping factMapping = simulationDescriptor.getFactMapping(factIdentifier, expressionIdentifier) .orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen")); List<String> pathToField = factMapping.getExpressionElementsWithoutClass().stream() .map(ExpressionElement::getStep).collect(toList()); try { Object value = expressionEvaluator.getValueForGiven(factMapping.getClassName(), factMappingValue.getRawValue()); paramsForBean.put(pathToField, value); } catch (IllegalArgumentException e) { factMappingValue.setError(true); throw new ScenarioException(e.getMessage(), e); } } return paramsForBean; }
protected Function<Object, ResultWrapper> createExtractorFunction(ExpressionEvaluator expressionEvaluator, FactMappingValue expectedResult, SimulationDescriptor simulationDescriptor) { return objectToCheck -> { ExpressionIdentifier expressionIdentifier = expectedResult.getExpressionIdentifier(); FactMapping factMapping = simulationDescriptor.getFactMapping(expectedResult.getFactIdentifier(), expressionIdentifier) .orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen")); List<String> pathToValue = factMapping.getExpressionElementsWithoutClass().stream().map(ExpressionElement::getStep).collect(toList()); ScenarioBeanWrapper<?> scenarioBeanWrapper = ScenarioBeanUtil.navigateToObject(objectToCheck, pathToValue, false); Object resultValue = scenarioBeanWrapper.getBean(); try { return expressionEvaluator.evaluate(expectedResult.getRawValue(), resultValue, scenarioBeanWrapper.getBeanClass()) ? createResult(resultValue) : createErrorResult(); } catch (Exception e) { expectedResult.setError(true); throw new ScenarioException(e.getMessage(), e); } }; }
@Test public void getExpressionElementsWithoutClass() { FactMapping original = new FactMapping("FACT_ALIAS", new FactIdentifier("FI_TEST", "com.test.Foo"), new ExpressionIdentifier("EI_TEST", FactMappingType.GIVEN)); assertThatThrownBy(original::getExpressionElementsWithoutClass) .isInstanceOf(IllegalStateException.class) .hasMessage("ExpressionElements malformed"); assertEquals(0, original.getExpressionElements().size()); original.addExpressionElement("STEP", String.class.getCanonicalName()); assertEquals(0, original.getExpressionElementsWithoutClass().size()); assertEquals(1, original.getExpressionElements().size()); } }
@SuppressWarnings("unchecked") protected ResultWrapper getSingleFactValueResult(FactMapping factMapping, FactMappingValue expectedResult, DMNDecisionResult decisionResult, ExpressionEvaluator expressionEvaluator) { Object resultRaw = decisionResult.getResult(); if (!SUCCEEDED.equals(decisionResult.getEvaluationStatus())) { return createErrorResult(); } for (ExpressionElement expressionElement : factMapping.getExpressionElementsWithoutClass()) { if (!(resultRaw instanceof Map)) { throw new ScenarioException("Wrong resultRaw structure because it is not a complex type as expected"); } Map<String, Object> result = (Map<String, Object>) resultRaw; resultRaw = result.get(expressionElement.getStep()); } Class<?> resultClass = resultRaw != null ? resultRaw.getClass() : null; try { return expressionEvaluator.evaluate(expectedResult.getRawValue(), resultRaw, resultClass) ? createResult(resultRaw) : createErrorResult(); } catch (Exception e) { expectedResult.setError(true); throw new ScenarioException(e.getMessage(), e); } }
@Test public void getExpressionElementsWithoutClass() { FactMapping original = new FactMapping("FACT_ALIAS", new FactIdentifier("FI_TEST", "com.test.Foo"), new ExpressionIdentifier("EI_TEST", FactMappingType.GIVEN)); assertThatThrownBy(original::getExpressionElementsWithoutClass) .isInstanceOf(IllegalStateException.class) .hasMessage("ExpressionElements malformed"); assertEquals(0, original.getExpressionElements().size()); original.addExpressionElement("STEP", String.class.getCanonicalName()); assertEquals(0, original.getExpressionElementsWithoutClass().size()); assertEquals(1, original.getExpressionElements().size()); } }