Search in sources :

Example 1 with ScenarioResult

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);
        }
    }
}
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 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));
}
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 3 with ScenarioResult

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());
}
Also used : ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult)

Example 4 with ScenarioResult

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

Example 5 with ScenarioResult

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

Aggregations

ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)9 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)6 Test (org.junit.Test)5 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)4 ArrayList (java.util.ArrayList)3 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)3 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)3 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)3 Function (java.util.function.Function)2 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)2 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)2 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)2 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)2 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1