Search in sources :

Example 1 with ScenarioException

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

the class ScenarioBeanUtil method navigateToObject.

public static ScenarioBeanWrapper<?> navigateToObject(Object rootObject, List<String> steps, boolean createIfNull) {
    Class<?> currentClass = rootObject != null ? rootObject.getClass() : null;
    Object currentObject = rootObject;
    for (String step : steps) {
        if (currentObject == null) {
            throw new ScenarioException(new StringBuilder().append("Impossible to reach field ").append(step).append(" because a step is not instantiated").toString());
        }
        Field declaredField = getField(currentClass, step);
        declaredField.setAccessible(true);
        currentClass = declaredField.getType();
        try {
            currentObject = getFieldValue(declaredField, currentObject, createIfNull);
        } catch (ReflectiveOperationException e) {
            throw new ScenarioException(new StringBuilder().append("Impossible to get or create class ").append(currentClass.getCanonicalName()).toString());
        }
    }
    return new ScenarioBeanWrapper<>(currentObject, currentClass);
}
Also used : Field(java.lang.reflect.Field) ScenarioException(org.drools.scenariosimulation.backend.runner.ScenarioException)

Example 2 with ScenarioException

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

Example 3 with ScenarioException

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

the class RULEScenarioValidation method validate.

/**
 * Validate structure of a RULE test scenario.
 * Supported checks for each column:
 * - empty column skip
 * - instance type removed
 * - navigation of bean still valid
 * - field type changed
 * @param simulation
 * @param settings
 * @param kieContainer
 * @return
 */
@Override
public List<FactMappingValidationError> validate(Simulation simulation, Settings settings, KieContainer kieContainer) {
    List<FactMappingValidationError> errors = new ArrayList<>();
    Map<String, Object> beanInstanceMap = new HashMap<>();
    for (FactMapping factMapping : simulation.getScesimModelDescriptor().getFactMappings()) {
        if (isToSkip(factMapping)) {
            continue;
        }
        // try to navigate using all the steps to verify if structure is still valid
        List<String> steps = expressionElementToString(factMapping);
        try {
            String instanceClassName = factMapping.getFactIdentifier().getClassName();
            if (steps.isEmpty()) {
                // in case of top level simple types just try to load the class
                loadClass(instanceClassName, kieContainer.getClassLoader());
            } else {
                Object bean = beanInstanceMap.computeIfAbsent(instanceClassName, className -> fillBean(className, Collections.emptyMap(), kieContainer.getClassLoader()));
                List<String> stepsToField = steps.subList(0, steps.size() - 1);
                String lastStep = steps.get(steps.size() - 1);
                ScenarioBeanWrapper<?> beanBeforeLastStep = navigateToObject(bean, stepsToField, true);
                ScenarioBeanWrapper<?> beanWrapper = navigateToObject(beanBeforeLastStep.getBean(), Collections.singletonList(lastStep), false);
                String targetClassName = beanWrapper.getBeanClass() != null ? beanWrapper.getBeanClass().getName() : null;
                // check if target field has valid type
                if (!Objects.equals(factMapping.getClassName(), targetClassName)) {
                    errors.add(createFieldChangedError(factMapping, targetClassName));
                }
            }
        } catch (ScenarioException e) {
            errors.add(createGenericError(factMapping, e.getMessage()));
        }
    }
    return errors;
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScenarioBeanUtil.navigateToObject(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.navigateToObject) ScenarioException(org.drools.scenariosimulation.backend.runner.ScenarioException)

Aggregations

ScenarioException (org.drools.scenariosimulation.backend.runner.ScenarioException)3 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Background (org.drools.scenariosimulation.api.model.Background)1 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)1 Scenario (org.drools.scenariosimulation.api.model.Scenario)1 ScenarioWithIndex (org.drools.scenariosimulation.api.model.ScenarioWithIndex)1 Settings (org.drools.scenariosimulation.api.model.Settings)1 RuleScenarioRunner (org.drools.scenariosimulation.backend.runner.RuleScenarioRunner)1 ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)1 ScenarioBeanUtil.navigateToObject (org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.navigateToObject)1 FactMappingValidationError (org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError)1 SimulationRunResult (org.drools.workbench.screens.scenariosimulation.model.SimulationRunResult)1 TestResultMessage (org.guvnor.common.services.shared.test.TestResultMessage)1 Test (org.junit.Test)1 Path (org.uberfire.backend.vfs.Path)1