Search in sources :

Example 1 with ScenarioRunnerData

use of org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData in project drools by kiegroup.

the class AbstractScenarioRunner method singleRunScenario.

protected Optional<ScenarioResultMetadata> singleRunScenario(ScenarioWithIndex scenarioWithIndex, RunNotifier runNotifier, Settings settings, Background background) {
    ScenarioRunnerData scenarioRunnerData = new ScenarioRunnerData();
    String scenarioName = scenarioWithIndex.getScesimData().getDescription();
    int index = scenarioWithIndex.getIndex();
    Description descriptionForScenario = getDescriptionForScenario(getFilePath(), index, scenarioName);
    runNotifier.fireTestStarted(descriptionForScenario);
    try {
        internalRunScenario(scenarioWithIndex, scenarioRunnerData, settings, background);
    } catch (Exception e) {
        runNotifier.fireTestFailure(new Failure(descriptionForScenario, defineFailureException(e, index, scenarioName)));
    }
    runNotifier.fireTestFinished(descriptionForScenario);
    return scenarioRunnerData.getMetadata();
}
Also used : Description(org.junit.runner.Description) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Failure(org.junit.runner.notification.Failure)

Example 2 with ScenarioRunnerData

use of org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData 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);
    }
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) RuleScenarioExecutableBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder) ScenarioResultMetadata(org.drools.scenariosimulation.backend.runner.model.ScenarioResultMetadata) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExpressionElement(org.drools.scenariosimulation.api.model.ExpressionElement) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScenarioBeanUtil.fillBean(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.fillBean) Map(java.util.Map) COVERAGE_LISTENER(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.COVERAGE_LISTENER) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) RuleScenarioExecutableBuilder.createBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.createBuilder) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) ScenarioBeanWrapper(org.drools.scenariosimulation.backend.util.ScenarioBeanWrapper) CoverageAgendaListener(org.drools.scenariosimulation.backend.fluent.CoverageAgendaListener) Settings(org.drools.scenariosimulation.api.model.Settings) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ScenarioBeanUtil(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil) Set(java.util.Set) ConstantsHolder(org.drools.scenariosimulation.api.utils.ConstantsHolder) KieContainer(org.kie.api.runtime.KieContainer) Objects(java.util.Objects) Type(org.drools.scenariosimulation.api.model.ScenarioSimulationModel.Type) ExpressionEvaluatorFactory(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory) List(java.util.List) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) Collectors.toList(java.util.stream.Collectors.toList) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) RULES_AVAILABLE(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.RULES_AVAILABLE) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier)

Example 3 with ScenarioRunnerData

use of org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData 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 ScenarioRunnerData

use of org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData 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());
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ExpressionEvaluatorFactory(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory) HashMap(java.util.HashMap) Mockito.anyString(org.mockito.Mockito.anyString) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) DMNFeelExpressionEvaluator(org.drools.scenariosimulation.backend.expression.DMNFeelExpressionEvaluator) Test(org.junit.Test)

Example 5 with ScenarioRunnerData

use of org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData in project drools-wb by kiegroup.

the class ScenarioRunnerServiceImplTest method runFailed.

@Test
public void runFailed() {
    when(buildInfoServiceMock.getBuildInfo(any())).thenReturn(buildInfoMock);
    when(buildInfoMock.getKieContainer()).thenReturn(kieContainerMock);
    simulationLocal.addData();
    Scenario scenario = simulationLocal.getDataByIndex(0);
    scenario.setDescription("Test Scenario");
    String errorMessage = "Test Error";
    scenarioRunnerService.setRunnerSupplier((kieContainer, scenarioRunnerDTO) -> new RuleScenarioRunner(kieContainer, scenarioRunnerDTO) {

        @Override
        protected void internalRunScenario(ScenarioWithIndex scenarioWithIndex, ScenarioRunnerData scenarioRunnerData, Settings settings, Background background) {
            throw new ScenarioException(errorMessage);
        }
    });
    SimulationRunResult test = scenarioRunnerService.runTest("test", mock(Path.class), simulationLocal.getScesimModelDescriptor(), simulationLocal.getScenarioWithIndex(), settingsLocal, backgroundLocal);
    TestResultMessage value = test.getTestResultMessage();
    List<org.guvnor.common.services.shared.test.Failure> failures = value.getFailures();
    assertEquals(1, failures.size());
    String testDescription = String.format("#%d: %s", 1, scenario.getDescription());
    String errorMessageFormatted = String.format("#%d %s: %s", 1, scenario.getDescription(), errorMessage);
    org.guvnor.common.services.shared.test.Failure failure = failures.get(0);
    assertEquals(errorMessageFormatted, failure.getMessage());
    assertEquals(1, value.getRunCount());
    assertTrue(failure.getDisplayName().startsWith(testDescription));
}
Also used : Path(org.uberfire.backend.vfs.Path) Background(org.drools.scenariosimulation.api.model.Background) TestResultMessage(org.guvnor.common.services.shared.test.TestResultMessage) RuleScenarioRunner(org.drools.scenariosimulation.backend.runner.RuleScenarioRunner) Scenario(org.drools.scenariosimulation.api.model.Scenario) SimulationRunResult(org.drools.workbench.screens.scenariosimulation.model.SimulationRunResult) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Settings(org.drools.scenariosimulation.api.model.Settings) ScenarioException(org.drools.scenariosimulation.backend.runner.ScenarioException) Test(org.junit.Test)

Aggregations

ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)8 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)6 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)5 Test (org.junit.Test)5 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)4 Map (java.util.Map)3 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)3 ScenarioWithIndex (org.drools.scenariosimulation.api.model.ScenarioWithIndex)3 Settings (org.drools.scenariosimulation.api.model.Settings)3 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)3 ExpressionEvaluatorFactory (org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Collectors.toList (java.util.stream.Collectors.toList)2 ExpressionElement (org.drools.scenariosimulation.api.model.ExpressionElement)2