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);
}
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));
}
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;
}
Aggregations