public FactMapping addFactMapping(int index, String factAlias, FactIdentifier factIdentifier, ExpressionIdentifier expressionIdentifier) { if (getFactMapping(factIdentifier, expressionIdentifier).isPresent()) { throw new IllegalArgumentException( new StringBuilder().append("An expression with name '").append(expressionIdentifier.getName()) .append("' already exists for the fact '").append(factIdentifier.getName()).append("'").toString()); } if (index > factMappings.size()) { throw new IllegalArgumentException( new StringBuilder().append("Impossible to add an element at position ").append(index) .append(" because there are only ").append(factMappings.size()).append(" elements").toString()); } FactMapping factMapping = new FactMapping(factAlias, factIdentifier, expressionIdentifier); factMappings.add(index, factMapping); return factMapping; }
public FactMapping addFactMapping(int index, String factAlias, FactIdentifier factIdentifier, ExpressionIdentifier expressionIdentifier) { if (getFactMapping(factIdentifier, expressionIdentifier).isPresent()) { throw new IllegalArgumentException( new StringBuilder().append("An expression with name '").append(expressionIdentifier.getName()) .append("' already exists for the fact '").append(factIdentifier.getName()).append("'").toString()); } if (index > factMappings.size()) { throw new IllegalArgumentException( new StringBuilder().append("Impossible to add an element at position ").append(index) .append(" because there are only ").append(factMappings.size()).append(" elements").toString()); } FactMapping factMapping = new FactMapping(factAlias, factIdentifier, expressionIdentifier); factMappings.add(index, factMapping); return factMapping; }
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); } }; }
@Override public void verifyConditions(SimulationDescriptor simulationDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluator expressionEvaluator, RequestContext requestContext) { DMNResult dmnResult = requestContext.getOutput(DMNScenarioExecutableBuilder.DMN_RESULT); for (ScenarioExpect output : scenarioRunnerData.getExpects()) { FactIdentifier factIdentifier = output.getFactIdentifier(); String decisionName = factIdentifier.getName(); DMNDecisionResult decisionResult = dmnResult.getDecisionResultByName(decisionName); if (decisionResult == null) { throw new ScenarioException("DMN execution has not generated a decision result with name " + decisionName); } for (FactMappingValue expectedResult : output.getExpectedResult()) { ExpressionIdentifier expressionIdentifier = expectedResult.getExpressionIdentifier(); FactMapping factMapping = simulationDescriptor.getFactMapping(factIdentifier, expressionIdentifier) .orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen")); ScenarioResult scenarioResult = fillResult(expectedResult, factIdentifier, () -> getSingleFactValueResult(factMapping, expectedResult, decisionResult, expressionEvaluator)); scenarioRunnerData.addResult(scenarioResult); } } }