public String getDescription() { return factMappingValues.stream() .filter(e -> e.getExpressionIdentifier().equals(ExpressionIdentifier.DESCRIPTION) && e.getFactIdentifier().equals(FactIdentifier.DESCRIPTION) && e.getRawValue() != null) .map(e -> (String) e.getRawValue()) .findFirst().orElse(""); }
public String getDescription() { return factMappingValues.stream() .filter(e -> e.getExpressionIdentifier().equals(ExpressionIdentifier.DESCRIPTION) && e.getFactIdentifier().equals(FactIdentifier.DESCRIPTION) && e.getRawValue() != null) .map(e -> (String) e.getRawValue()) .findFirst().orElse(""); }
public Map<FactIdentifier, List<FactMappingValue>> groupByFactIdentifierAndFilter(List<FactMappingValue> factMappingValues, FactMappingType type) { Map<FactIdentifier, List<FactMappingValue>> groupByFactIdentifier = new HashMap<>(); for (FactMappingValue factMappingValue : factMappingValues) { FactIdentifier factIdentifier = factMappingValue.getFactIdentifier(); // null means skip if (factMappingValue.getRawValue() == null) { continue; } ExpressionIdentifier expressionIdentifier = factMappingValue.getExpressionIdentifier(); if (expressionIdentifier == null) { throw new IllegalArgumentException("ExpressionIdentifier malformed"); } if (!Objects.equals(expressionIdentifier.getType(), type)) { continue; } groupByFactIdentifier.computeIfAbsent(factIdentifier, key -> new ArrayList<>()) .add(factMappingValue); } return groupByFactIdentifier; }
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; }
/** * This method <i>insert</i> a row to the grid and populate it with values taken from given <code>Scenario</code> * @param row */ public void insertRowGridOnly(final int rowIndex, final GridRow row, final Scenario scenario) { checkSimulation(); super.insertRow(rowIndex, row); scenario.getUnmodifiableFactMappingValues().forEach(value -> { FactIdentifier factIdentifier = value.getFactIdentifier(); ExpressionIdentifier expressionIdentifier = value.getExpressionIdentifier(); if (value.getRawValue() == null || value.getRawValue() instanceof String) { // Let' put a placeholder String stringValue = (String) value.getRawValue(); int columnIndex = simulation.getSimulationDescriptor().getIndexByIdentifier(factIdentifier, expressionIdentifier); String placeHolder = ((ScenarioGridColumn) columns.get(columnIndex)).getPlaceHolder(); setCell(rowIndex, columnIndex, () -> new ScenarioGridCell(new ScenarioGridCellValue(stringValue, placeHolder))); } else { throw new UnsupportedOperationException("Only string is supported at the moment"); } }); updateIndexColumn(); }
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 addOrUpdateMappingValue() { Object value1 = "Test 1"; Object value2 = "Test 2"; FactMappingValue factMappingValue = scenario.addMappingValue(factIdentifier, expressionIdentifier, value1); assertEquals(factMappingValue.getRawValue(), value1); FactMappingValue factMappingValue1 = scenario.addOrUpdateMappingValue(factIdentifier, expressionIdentifier, value2); assertEquals(factMappingValue, factMappingValue1); assertEquals(factMappingValue1.getRawValue(), value2); }
@Test public void addOrUpdateMappingValue() { Object value1 = "Test 1"; Object value2 = "Test 2"; FactMappingValue factMappingValue = scenario.addMappingValue(factIdentifier, expressionIdentifier, value1); assertEquals(factMappingValue.getRawValue(), value1); FactMappingValue factMappingValue1 = scenario.addOrUpdateMappingValue(factIdentifier, expressionIdentifier, value2); assertEquals(factMappingValue, factMappingValue1); assertEquals(factMappingValue1.getRawValue(), value2); }
@Test public void validateAssertionTest() { List<ScenarioResult> scenarioFailResult = new ArrayList<>(); scenarioFailResult.add(new ScenarioResult(disputeFactIdentifier, amountNameExpectedFactMappingValue, "SOMETHING_ELSE")); try { runnerHelper.validateAssertion(scenarioFailResult, scenario2); fail(); } catch (ScenarioException ignored) { } List<ScenarioResult> scenarioSuccessResult = new ArrayList<>(); scenarioSuccessResult.add(new ScenarioResult(disputeFactIdentifier, amountNameExpectedFactMappingValue, amountNameExpectedFactMappingValue.getRawValue()).setResult(true)); runnerHelper.validateAssertion(scenarioSuccessResult, scenario2); }
@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); } }