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(""); }
FactMappingValue cloneFactMappingValue() { FactMappingValue cloned = new FactMappingValue(); cloned.expressionIdentifier = expressionIdentifier; cloned.factIdentifier = factIdentifier; cloned.rawValue = rawValue; return cloned; }
public void resetErrors() { factMappingValues.forEach(elem -> elem.setError(false)); }
public void sort() { factMappingValues.sort((a, b) -> { Integer aIndex = simulationDescriptor.getIndexByIdentifier(a.getFactIdentifier(), a.getExpressionIdentifier()); Integer bIndex = simulationDescriptor.getIndexByIdentifier(b.getFactIdentifier(), b.getExpressionIdentifier()); return aIndex.compareTo(bIndex); }); }
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); } }; }
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; }
@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); }
@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 refreshErrorsRow() { FactMappingValue factMappingValue = mock(FactMappingValue.class); when(factMappingValue.isError()).thenReturn(true); when(scenarioMock.getFactMappingValue(any(), any())).thenReturn(Optional.empty()); scenarioGridModel.refreshErrorsRow(0); verify(gridCellMock, times(6)).setErrorMode(false); when(scenarioMock.getFactMappingValue(any(), any())).thenReturn(Optional.of(factMappingValue)); scenarioGridModel.refreshErrorsRow(0); verify(gridCellMock, times(6)).setErrorMode(true); } }
@Test public void getParamsForBeanTest() { List<FactMappingValue> factMappingValues = new ArrayList<>(); FactMappingValue factMappingValue = new FactMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, "NOT PARSABLE"); factMappingValues.add(factMappingValue); try { runnerHelper.getParamsForBean(simulation.getSimulationDescriptor(), disputeFactIdentifier, factMappingValues, classLoader, expressionEvaluator); fail(); } catch (ScenarioException ignored) { } Assert.assertTrue(factMappingValue.isError()); }
@Test(expected = IllegalArgumentException.class) public void groupByFactIdentifierAndFilterFailTest() { List<FactMappingValue> fail = new ArrayList<>(); FactMappingValue factMappingValue = new FactMappingValue(); factMappingValue.setRawValue("TEST"); fail.add(factMappingValue); runnerHelper.groupByFactIdentifierAndFilter(fail, FactMappingType.GIVEN); }
public List<FactMappingValue> getFactMappingValuesByFactIdentifier(FactIdentifier factIdentifier) { return factMappingValues.stream().filter(e -> e.getFactIdentifier().equals(factIdentifier)).collect(toList()); }
public List<ScenarioExpect> extractExpectedValues(List<FactMappingValue> factMappingValues) { List<ScenarioExpect> scenarioExpect = new ArrayList<>(); Map<FactIdentifier, List<FactMappingValue>> groupByFactIdentifier = groupByFactIdentifierAndFilter(factMappingValues, FactMappingType.EXPECT); Set<FactIdentifier> inputFacts = factMappingValues.stream() .filter(elem -> FactMappingType.GIVEN.equals(elem.getExpressionIdentifier().getType())) .map(FactMappingValue::getFactIdentifier) .collect(Collectors.toSet()); for (Map.Entry<FactIdentifier, List<FactMappingValue>> entry : groupByFactIdentifier.entrySet()) { FactIdentifier factIdentifier = entry.getKey(); scenarioExpect.add(new ScenarioExpect(factIdentifier, entry.getValue(), !inputFacts.contains(factIdentifier))); } return scenarioExpect; }
public FactMappingValue addOrUpdateMappingValue(FactIdentifier factIdentifier, ExpressionIdentifier expressionIdentifier, Object value) { return getFactMappingValue(factIdentifier, expressionIdentifier).map(e -> { e.setRawValue(value); return e; }).orElseGet(() -> addMappingValue(factIdentifier, expressionIdentifier, value)); }
public void sort() { factMappingValues.sort((a, b) -> { Integer aIndex = simulationDescriptor.getIndexByIdentifier(a.getFactIdentifier(), a.getExpressionIdentifier()); Integer bIndex = simulationDescriptor.getIndexByIdentifier(b.getFactIdentifier(), b.getExpressionIdentifier()); return aIndex.compareTo(bIndex); }); }
@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 refreshErrorsTest() { scenarioGridModel.refreshErrors(); verify(gridCellMock, times(24)).setErrorMode(eq(true)); reset(gridCellMock); when(factMappingValueMock.isError()).thenReturn(false); scenarioGridModel.refreshErrors(); verify(gridCellMock, times(24)).setErrorMode(eq(false)); }
public Collection<String> getFactNames() { return factMappingValues.stream().map(e -> e.getFactIdentifier().getName()).collect(toSet()); }
@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); } } }
public FactMappingValue addOrUpdateMappingValue(FactIdentifier factIdentifier, ExpressionIdentifier expressionIdentifier, Object value) { return getFactMappingValue(factIdentifier, expressionIdentifier).map(e -> { e.setRawValue(value); return e; }).orElseGet(() -> addMappingValue(factIdentifier, expressionIdentifier, value)); }