RULEScenarioValidation extends AbstractScenarioValidation { @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; } List<String> steps = expressionElementToString(factMapping); try { String instanceClassName = factMapping.getFactIdentifier().getClassName(); if (steps.isEmpty()) { 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().getCanonicalName() : null; if (!Objects.equals(factMapping.getClassName(), targetClassName)) { errors.add(createFieldChangedError(factMapping, targetClassName)); } } } catch (ScenarioException e) { errors.add(createGenericError(factMapping, e.getMessage())); } } return errors; }  @Override List<FactMappingValidationError> validate(Simulation simulation, Settings settings, KieContainer kieContainer); static final RULEScenarioValidation INSTANCE; }
@Test public void validate() { RULEScenarioValidation validation = new RULEScenarioValidation(); Simulation test0 = new Simulation(); test0.getScesimModelDescriptor().addFactMapping( FactIdentifier.DESCRIPTION, ExpressionIdentifier.create(VALUE, FactMappingType.OTHER)); test0.getScesimModelDescriptor().addFactMapping( FactIdentifier.EMPTY, ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN)); List<FactMappingValidationError> errorsTest0 = validation.validate(test0, settingsLocal, kieContainerMock); checkResult(errorsTest0); Simulation test1 = new Simulation(); test1.getScesimModelDescriptor().addFactMapping( FactIdentifier.create("mySimpleType", int.class.getCanonicalName()), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN)); List<FactMappingValidationError> errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock); checkResult(errorsTest1); FactMapping mySimpleType = test1.getScesimModelDescriptor().addFactMapping( FactIdentifier.create("mySimpleType", "notValidClass"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN)); mySimpleType.addExpressionElement("notValidClass", "notValidClass"); errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock); checkResult(errorsTest1, "Impossible to load class notValidClass"); Simulation test2 = new Simulation(); FactIdentifier myFactIdentifier = FactIdentifier.create("mySimpleType", SampleBean.class.getCanonicalName()); FactMapping nameFM = test2.getScesimModelDescriptor().addFactMapping( myFactIdentifier, ExpressionIdentifier.create("name", FactMappingType.GIVEN)); nameFM.addExpressionElement("SampleBean", String.class.getCanonicalName()); nameFM.addExpressionElement("name", String.class.getCanonicalName()); FactMapping parentFM = test2.getScesimModelDescriptor().addFactMapping( myFactIdentifier, ExpressionIdentifier.create("parent", FactMappingType.EXPECT)); parentFM.addExpressionElement("SampleBean", SampleBean.class.getCanonicalName()); parentFM.addExpressionElement("parent", SampleBean.class.getCanonicalName()); List<FactMappingValidationError> errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock); checkResult(errorsTest2); parentFM.addExpressionElement("notExisting", String.class.getCanonicalName()); errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock); checkResult(errorsTest2, "Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"); FactMapping nameWrongTypeFM = test2.getScesimModelDescriptor().addFactMapping( myFactIdentifier, ExpressionIdentifier.create("parent2", FactMappingType.EXPECT)); nameWrongTypeFM.addExpressionElement("SampleBean", Integer.class.getCanonicalName()); nameWrongTypeFM.addExpressionElement("name", Integer.class.getCanonicalName()); errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock); checkResult(errorsTest2, "Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean", "Field type has changed: old 'java.lang.Integer', current 'java.lang.String'"); Simulation test3 = new Simulation(); FactMapping topLevelListFM = test3.getScesimModelDescriptor().addFactMapping( FactIdentifier.create("mySimpleType", List.class.getCanonicalName()), ExpressionIdentifier.create("name", FactMappingType.GIVEN)); topLevelListFM.addExpressionElement("List", List.class.getCanonicalName()); topLevelListFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName())); FactMapping addressesFM = test3.getScesimModelDescriptor().addFactMapping( myFactIdentifier, ExpressionIdentifier.create("addresses", FactMappingType.EXPECT)); addressesFM.addExpressionElement("SampleBean", List.class.getCanonicalName()); addressesFM.addExpressionElement("addresses", List.class.getCanonicalName()); addressesFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName())); List<FactMappingValidationError> errorsTest3 = validation.validate(test3, settingsLocal, kieContainerMock); checkResult(errorsTest3); }