use of org.drools.scenariosimulation.backend.runner.model.ScenarioExpect in project drools by kiegroup.
the class DMNScenarioRunnerHelperTest method verifyConditions.
@Test
public void verifyConditions() {
ScenarioRunnerData scenarioRunnerData1 = new ScenarioRunnerData();
scenarioRunnerData1.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(firstNameExpectedValue)));
// test 1 - no decision generated for specific decisionName
assertThatThrownBy(() -> runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData1, expressionEvaluatorFactory, requestContextMock)).isInstanceOf(ScenarioException.class).hasMessage("DMN execution has not generated a decision result with name Fact 1");
when(dmnResultMock.getDecisionResultByName(anyString())).thenReturn(dmnDecisionResultMock);
when(dmnDecisionResultMock.getEvaluationStatus()).thenReturn(DecisionEvaluationStatus.SUCCEEDED);
// test 2 - when decisionResult contains a null value skip the steps and just do the comparison (that should be false in this case)
runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData1, expressionEvaluatorFactory, requestContextMock);
assertEquals(1, scenarioRunnerData1.getResults().size());
assertFalse(scenarioRunnerData1.getResults().get(0).getResult());
when(dmnDecisionResultMock.getResult()).thenReturn("");
// test 3 - now result is not null but data structure is wrong (expected steps but data is a simple string)
assertThatThrownBy(() -> runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData1, expressionEvaluatorFactory, requestContextMock)).isInstanceOf(ScenarioException.class).hasMessage("Wrong resultRaw structure because it is not a complex type as expected");
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("firstName", "WrongValue");
when(dmnDecisionResultMock.getResult()).thenReturn(resultMap);
ScenarioRunnerData scenarioRunnerData2 = new ScenarioRunnerData();
scenarioRunnerData2.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(firstNameExpectedValue)));
// test 4 - check are performed (but fail)
runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData2, expressionEvaluatorFactory, requestContextMock);
assertEquals(1, scenarioRunnerData2.getResults().size());
assertFalse(scenarioRunnerData2.getResults().get(0).getResult());
ScenarioRunnerData scenarioRunnerData3 = new ScenarioRunnerData();
scenarioRunnerData3.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(firstNameExpectedValue)));
resultMap.put("firstName", NAME);
// test 5 - check are performed (but success)
runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData3, expressionEvaluatorFactory, requestContextMock);
assertEquals(1, scenarioRunnerData3.getResults().size());
assertTrue(scenarioRunnerData3.getResults().get(0).getResult());
// test 6 - verify that when expression evaluation fails the corresponding expression is marked as error
ExpressionEvaluatorFactory expressionEvaluatorFactoryMock = mock(ExpressionEvaluatorFactory.class);
when(expressionEvaluatorFactoryMock.getOrCreate(any())).thenReturn(mock(ExpressionEvaluator.class));
runnerHelper.verifyConditions(simulation.getScesimModelDescriptor(), scenarioRunnerData3, expressionEvaluatorFactoryMock, requestContextMock);
assertEquals(FactMappingValueStatus.FAILED_WITH_ERROR, scenarioRunnerData3.getResults().get(0).getFactMappingValue().getStatus());
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioExpect in project drools by kiegroup.
the class RuleScenarioRunnerHelperTest method executeScenario.
@Test
public void executeScenario() {
ArgumentCaptor<Object> insertCaptor = ArgumentCaptor.forClass(Object.class);
ScenarioRunnerData scenarioRunnerData = new ScenarioRunnerData();
scenarioRunnerData.addBackground(new InstanceGiven(personFactIdentifier, new Person()));
scenarioRunnerData.addBackground(new InstanceGiven(disputeFactIdentifier, new Dispute()));
scenarioRunnerData.addGiven(new InstanceGiven(personFactIdentifier, new Person()));
FactMappingValue factMappingValue = new FactMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, NAME);
scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), false));
scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), true));
int inputObjects = scenarioRunnerData.getBackgrounds().size() + scenarioRunnerData.getGivens().size();
String ruleFlowGroup = "ruleFlowGroup";
settings.setRuleFlowGroup(ruleFlowGroup);
runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings);
verify(ruleScenarioExecutableBuilderMock, times(1)).setActiveRuleFlowGroup(ruleFlowGroup);
verify(ruleScenarioExecutableBuilderMock, times(inputObjects)).insert(insertCaptor.capture());
for (Object value : insertCaptor.getAllValues()) {
assertTrue(value instanceof Person || value instanceof Dispute);
}
verify(ruleScenarioExecutableBuilderMock, times(1)).addInternalCondition(eq(Person.class), any(), any());
verify(ruleScenarioExecutableBuilderMock, times(1)).run();
assertEquals(1, scenarioRunnerData.getResults().size());
// test not rule error
settings.setType(ScenarioSimulationModel.Type.DMN);
assertThatThrownBy(() -> runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings)).isInstanceOf(ScenarioException.class).hasMessageStartingWith("Impossible to run");
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioExpect in project drools by kiegroup.
the class RuleScenarioRunnerHelperTest method getScenarioResultsTest.
@Test
public void getScenarioResultsTest() {
List<InstanceGiven> scenario1Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario1.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
List<ScenarioExpect> scenario1Outputs = runnerHelper.extractExpectedValues(scenario1.getUnmodifiableFactMappingValues());
assertTrue(scenario1Inputs.size() > 0);
InstanceGiven input1 = scenario1Inputs.get(0);
scenario1Outputs = scenario1Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input1.getFactIdentifier())).collect(toList());
List<ScenarioResult> scenario1Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, input1, expressionEvaluatorFactory);
assertEquals(1, scenario1Results.size());
assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
List<InstanceGiven> scenario2Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario2.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
List<ScenarioExpect> scenario2Outputs = runnerHelper.extractExpectedValues(scenario2.getUnmodifiableFactMappingValues());
assertTrue(scenario2Inputs.size() > 0);
InstanceGiven input2 = scenario2Inputs.get(0);
scenario2Outputs = scenario2Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input2.getFactIdentifier())).collect(toList());
List<ScenarioResult> scenario2Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario2Outputs, input2, expressionEvaluatorFactory);
assertEquals(1, scenario2Results.size());
assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
List<ScenarioExpect> newFact = singletonList(new ScenarioExpect(personFactIdentifier, emptyList(), true));
List<ScenarioResult> scenario2NoResults = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), newFact, input2, expressionEvaluatorFactory);
assertEquals(0, scenario2NoResults.size());
Person person = new Person();
person.setFirstName("ANOTHER STRING");
InstanceGiven newInput = new InstanceGiven(personFactIdentifier, person);
List<ScenarioResult> scenario3Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, newInput, expressionEvaluatorFactory);
assertEquals(FactMappingValueStatus.FAILED_WITH_ERROR, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
assertEquals(1, scenario3Results.size());
assertEquals(person.getFirstName(), scenario3Results.get(0).getResultValue().get());
assertEquals("NAME", scenario3Results.get(0).getFactMappingValue().getRawValue());
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioExpect in project drools by kiegroup.
the class DMNScenarioRunnerHelper method verifyConditions.
@Override
protected void verifyConditions(ScesimModelDescriptor scesimModelDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, Map<String, Object> requestContext) {
DMNResult dmnResult = (DMNResult) requestContext.get(DMNScenarioExecutableBuilder.DMN_RESULT);
List<DMNMessage> dmnMessages = dmnResult.getMessages();
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 = scesimModelDescriptor.getFactMapping(factIdentifier, expressionIdentifier).orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen"));
ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(expectedResult);
ScenarioResult scenarioResult = fillResult(expectedResult, () -> getSingleFactValueResult(factMapping, expectedResult, decisionResult, dmnMessages, expressionEvaluator), expressionEvaluator);
scenarioRunnerData.addResult(scenarioResult);
}
}
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioExpect in project drools by kiegroup.
the class RuleScenarioRunnerHelper method verifyConditions.
@Override
protected void verifyConditions(ScesimModelDescriptor scesimModelDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, Map<String, Object> requestContext) {
for (InstanceGiven input : scenarioRunnerData.getGivens()) {
FactIdentifier factIdentifier = input.getFactIdentifier();
List<ScenarioExpect> assertionOnFact = scenarioRunnerData.getExpects().stream().filter(elem -> !elem.isNewFact()).filter(elem -> Objects.equals(elem.getFactIdentifier(), factIdentifier)).collect(toList());
// check if this fact has something to check
if (assertionOnFact.isEmpty()) {
continue;
}
getScenarioResultsFromGivenFacts(scesimModelDescriptor, assertionOnFact, input, expressionEvaluatorFactory).forEach(scenarioRunnerData::addResult);
}
}
Aggregations