use of org.drools.scenariosimulation.api.model.FactMappingValue 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.api.model.FactMappingValue in project drools by kiegroup.
the class RuleScenarioRunnerHelperTest method groupByFactIdentifierAndFilterFailTest.
@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);
}
use of org.drools.scenariosimulation.api.model.FactMappingValue 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.api.model.FactMappingValue 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.api.model.FactMappingValue in project drools by kiegroup.
the class AbstractRunnerHelperTest method fillResult.
@Test
public void fillResult() {
FactIdentifier factIdentifier = FactIdentifier.create("MyInstance", String.class.getCanonicalName());
ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("MyProperty", FactMappingType.GIVEN);
FactMappingValue expectedResultSpy = spy(new FactMappingValue(factIdentifier, expressionIdentifier, VALUE));
AtomicReference<ValueWrapper> resultWrapperAtomicReference = new AtomicReference<>();
Supplier<ValueWrapper<?>> resultWrapperSupplier = resultWrapperAtomicReference::get;
ExpressionEvaluator expressionEvaluator = new BaseExpressionEvaluator(AbstractRunnerHelper.class.getClassLoader());
// Success
resultWrapperAtomicReference.set(ValueWrapper.of(VALUE));
assertTrue(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
verify(expectedResultSpy, times(1)).resetStatus();
reset(expectedResultSpy);
// Fail with expected value
resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
reset(expectedResultSpy);
// Fail with exception while reverting actual value
resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
ExpressionEvaluator expressionEvaluatorMock = mock(ExpressionEvaluator.class);
when(expressionEvaluatorMock.fromObjectToExpression(any())).thenThrow(new IllegalArgumentException("Error"));
assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluatorMock).getResult());
verify(expectedResultSpy, times(1)).setExceptionMessage("Error");
reset(expectedResultSpy);
// Fail in collection case
List<String> pathToValue = Arrays.asList("field1", "fields2");
resultWrapperAtomicReference.set(ValueWrapper.errorWithCollectionPathToValue(VALUE, pathToValue));
assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
verify(expectedResultSpy, times(1)).setCollectionPathToValue(pathToValue);
verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
// Fail with exception
resultWrapperAtomicReference.set(ValueWrapper.errorWithMessage("detailedError"));
assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
verify(expectedResultSpy, times(1)).setExceptionMessage("detailedError");
}
Aggregations