use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult 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.ScenarioResult in project drools by kiegroup.
the class RuleStatefulScenarioExecutableBuilderTest method testBuilder.
@Test
public void testBuilder() {
when(executableBuilderMock.newApplicationContext(anyString())).thenReturn(executableBuilderMock);
when(executableBuilderMock.setKieContainer(any())).thenReturn(kieContainerFluent);
when(kieContainerFluent.newSessionCustomized(any(), any())).thenReturn(kieSessionFluentMock);
when(kieSessionFluentMock.dispose()).thenReturn(executableBuilderMock);
when(kieSessionFluentMock.addCommand(any())).thenReturn(kieSessionFluentMock);
when(executableRunnerMock.execute(Mockito.<Executable>any())).thenReturn(requestContextMock);
when(requestContextMock.getOutputs()).thenReturn(Collections.emptyMap());
RuleStatefulScenarioExecutableBuilder builder = new RuleStatefulScenarioExecutableBuilder(null, null) {
@Override
protected ExecutableBuilder createExecutableBuilder() {
return executableBuilderMock;
}
@Override
protected ExecutableRunner<RequestContext> createExecutableRunner() {
return executableRunnerMock;
}
};
Object toInsert = new Object();
String agendaGroup = "agendaGroup";
String ruleFlowGroup = "ruleFlowGroup";
FactMappingValue indexFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, null);
builder.setActiveAgendaGroup(agendaGroup);
verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
reset(kieContainerFluent);
builder.setActiveRuleFlowGroup(ruleFlowGroup);
verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
reset(kieContainerFluent);
builder.insert(toInsert);
verify(kieSessionFluentMock, times(1)).insert(eq(toInsert));
reset(kieContainerFluent);
builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(indexFMV, null));
Map<String, Object> result = builder.run();
verify(kieSessionFluentMock, times(1)).fireAllRules();
verify(kieSessionFluentMock, times(3)).addCommand(commandArgumentCaptor.capture());
List<ExecutableCommand<?>> allAddCommands = commandArgumentCaptor.getAllValues();
assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(ValidateFactCommand.class::isAssignableFrom));
assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(AddCoverageListenerCommand.class::isAssignableFrom));
assertTrue(result.containsKey(RuleScenarioExecutableBuilder.COVERAGE_LISTENER));
verify(kieSessionFluentMock, times(1)).out(eq(RULES_AVAILABLE));
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult in project drools by kiegroup.
the class AbstractRunnerHelper method fillResult.
protected ScenarioResult fillResult(FactMappingValue expectedResult, Supplier<ValueWrapper<?>> resultSupplier, ExpressionEvaluator expressionEvaluator) {
ValueWrapper<?> resultValue = resultSupplier.get();
if (resultValue.isValid()) {
// result is satisfied so clean up previous error state
expectedResult.resetStatus();
} else if (resultValue.getErrorMessage().isPresent()) {
// propagate error message
expectedResult.setExceptionMessage(resultValue.getErrorMessage().get());
} else if (resultValue.getCollectionPathToValue() != null) {
expectedResult.setCollectionPathToValue(resultValue.getCollectionPathToValue());
expectedResult.setErrorValue(resultValue.getValue());
} else {
try {
// set actual as proposed value
expectedResult.setErrorValue(expressionEvaluator.fromObjectToExpression(resultValue.getValue()));
} catch (Exception e) {
// otherwise generic error message
expectedResult.setExceptionMessage(e.getMessage());
}
}
return new ScenarioResult(expectedResult, resultValue.getValue()).setResult(resultValue.isValid());
}
use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult 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.ScenarioResult in project drools by kiegroup.
the class RuleScenarioRunnerHelperTest method validateAssertionTest.
@Test
public void validateAssertionTest() {
List<ScenarioResult> scenarioFailResult = new ArrayList<>();
scenarioFailResult.add(new ScenarioResult(amountNameExpectedFactMappingValue, "SOMETHING_ELSE"));
try {
runnerHelper.validateAssertion(scenarioFailResult, simulation.getScesimModelDescriptor());
fail();
} catch (IllegalStateException exception) {
assertEquals("Illegal FactMappingValue status", exception.getMessage());
}
amountNameExpectedFactMappingValue.resetStatus();
amountNameExpectedFactMappingValue.setErrorValue("Error");
scenarioFailResult.add(new ScenarioResult(amountNameExpectedFactMappingValue, "SOMETHING_ELSE"));
try {
runnerHelper.validateAssertion(scenarioFailResult, simulation.getScesimModelDescriptor());
fail();
} catch (ScenarioException exception) {
assertTrue(exception.isFailedAssertion());
assertEquals(ScenarioSimulationServerMessages.getFactWithWrongValueExceptionMessage("Fact 2.amount", amountNameExpectedFactMappingValue.getRawValue(), amountNameExpectedFactMappingValue.getErrorValue()), exception.getMessage());
}
String exceptionMessage = "Message";
amountNameExpectedFactMappingValue.resetStatus();
amountNameExpectedFactMappingValue.setExceptionMessage(exceptionMessage);
scenarioFailResult.add(new ScenarioResult(amountNameExpectedFactMappingValue, "SOMETHING_ELSE"));
try {
runnerHelper.validateAssertion(scenarioFailResult, simulation.getScesimModelDescriptor());
fail();
} catch (ScenarioException exception) {
assertFalse(exception.isFailedAssertion());
assertEquals(ScenarioSimulationServerMessages.getGenericScenarioExceptionMessage(exceptionMessage), exception.getMessage());
}
List<String> pathToValue = Arrays.asList("Item #2");
amountNameExpectedFactMappingValue.resetStatus();
amountNameExpectedFactMappingValue.setCollectionPathToValue(pathToValue);
scenarioFailResult.add(new ScenarioResult(amountNameExpectedFactMappingValue, "SOMETHING_ELSE"));
try {
runnerHelper.validateAssertion(scenarioFailResult, simulation.getScesimModelDescriptor());
fail();
} catch (ScenarioException exception) {
assertTrue(exception.isFailedAssertion());
assertEquals(ScenarioSimulationServerMessages.getCollectionFactExceptionMessage("Fact 2.amount", pathToValue, amountNameExpectedFactMappingValue.getErrorValue()), exception.getMessage());
}
List<ScenarioResult> scenarioSuccessResult = new ArrayList<>();
scenarioSuccessResult.add(new ScenarioResult(amountNameExpectedFactMappingValue, amountNameExpectedFactMappingValue.getRawValue()).setResult(true));
runnerHelper.validateAssertion(scenarioSuccessResult, simulation.getScesimModelDescriptor());
}
Aggregations