Search in sources :

Example 1 with FactMappingValue

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);
        }
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) DMNMessage(org.kie.dmn.api.core.DMNMessage) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult)

Example 2 with FactMappingValue

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);
}
Also used : FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ArrayList(java.util.ArrayList) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 3 with FactMappingValue

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");
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Dispute(org.drools.scenariosimulation.backend.model.Dispute) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 4 with FactMappingValue

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));
}
Also used : ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ExecutableCommand(org.kie.api.command.ExecutableCommand) RequestContext(org.kie.api.runtime.RequestContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with FactMappingValue

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");
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AtomicReference(java.util.concurrent.atomic.AtomicReference) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) Test(org.junit.Test)

Aggregations

FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)24 Test (org.junit.Test)15 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)9 ArrayList (java.util.ArrayList)7 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)7 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)7 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)7 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)7 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)6 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)6 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)5 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Background (org.drools.scenariosimulation.api.model.Background)4 ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)4 Collectors.toList (java.util.stream.Collectors.toList)3 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)3 ExpressionEvaluatorResult (org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult)3