Search in sources :

Example 1 with FactMappingValidationError

use of org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError in project drools-wb by kiegroup.

the class ScenarioSimulationEditorI18nServerManagerTest method retrieveMessageFieldChangedError.

@Test
public void retrieveMessageFieldChangedError() {
    String newType = "newType";
    FactMappingValidationError error = FactMappingValidationError.createFieldChangedError(factMapping, newType);
    String message = ScenarioSimulationEditorI18nServerManager.retrieveMessage(error);
    String expected = ScenarioSimulationEditorConstants.INSTANCE.scenarioValidationFieldChangedError("tMYTYPE", newType);
    assertEquals(expected, message);
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) Test(org.junit.Test)

Example 2 with FactMappingValidationError

use of org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError in project drools-wb by kiegroup.

the class RULEScenarioValidationTest method validate.

@Test
public void validate() {
    RULEScenarioValidation validation = new RULEScenarioValidation();
    // Test 0 - skip empty or not GIVEN/EXPECT columns
    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);
    // Test 1 - simple type
    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, new ExpectedError("Impossible to load class notValidClass"));
    // Test 2 - nested field
    Simulation test2 = new Simulation();
    // nameFM is valid
    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());
    // parentFM is valid
    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 is not valid anymore
    parentFM.addExpressionElement("notExisting", String.class.getCanonicalName());
    errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"));
    // nameWrongTypeFM has a wrong type
    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, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "java.lang.String")));
    // Test 3 - list
    Simulation test3 = new Simulation();
    // topLevelListFM is valid
    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()));
    // addressesFM is valid
    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);
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test)

Example 3 with FactMappingValidationError

use of org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError 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)

Example 4 with FactMappingValidationError

use of org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError in project drools-wb by kiegroup.

the class DMNScenarioValidationTest method validate.

@Test
public void validate() {
    DMNScenarioValidation validationSpy = spy(new DMNScenarioValidation() {

        @Override
        protected DMNModel getDMNModel(KieContainer kieContainer, String dmnPath) {
            return dmnModelMock;
        }
    });
    // Test 0 - skip empty or not GIVEN/EXPECT columns
    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 = validationSpy.validate(test0, settingsLocal, null);
    checkResult(errorsTest0);
    // Test 1 - simple type
    Simulation test1 = new Simulation();
    test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "tMYSIMPLETYPE"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    createDMNType("mySimpleType", "mySimpleType");
    List<FactMappingValidationError> errorsTest1 = validationSpy.validate(test1, settingsLocal, null);
    checkResult(errorsTest1);
    // Test 2 - nested field
    Simulation test2 = new Simulation();
    // nameFM is valid
    FactIdentifier myComplexFactIdentifier = FactIdentifier.create("myComplexType", "tMYCOMPLEXTYPE");
    FactMapping nameFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    nameFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
    nameFM.addExpressionElement("name", "tNAME");
    createDMNType("myComplexType", "myComplexType", "name");
    // parentFM is valid
    FactMapping parentFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent", FactMappingType.EXPECT));
    parentFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
    parentFM.addExpressionElement("parent", "tPARENT");
    createDMNType("myComplexType", "myComplexType", "parent");
    List<FactMappingValidationError> errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
    checkResult(errorsTest2);
    // parentFM is not valid anymore
    parentFM.addExpressionElement("notExisting", "notExisting");
    errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"));
    // nameWrongTypeFM has a wrong type
    FactMapping nameWrongTypeFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent2", FactMappingType.EXPECT));
    nameWrongTypeFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
    nameWrongTypeFM.addExpressionElement("name", Integer.class.getCanonicalName());
    errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")));
    // color parameter - Constraint added for its type (string to tColor with allowed values)
    FactMapping colorsAddedConstraintFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent3", FactMappingType.GIVEN));
    colorsAddedConstraintFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
    colorsAddedConstraintFM.addExpressionElement("color", BuiltInType.STRING.getName());
    createDMNType("myComplexType", "myComplexType", "color");
    DMNType baseDMNType = initDMNType(BuiltInType.STRING);
    when(mapOfMockDecisions.get("myComplexType").getResultType().getFields().get("color").getAllowedValues()).thenReturn(Arrays.asList(new UnaryTestImpl(null, "Value")));
    when(mapOfMockDecisions.get("myComplexType").getResultType().getFields().get("color").getBaseType()).thenReturn(baseDMNType);
    errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_ADDED_CONSTRAINT_ERROR, Collections.emptyList()));
    // age parameter - Constraint removed for its type (tAge to numeric without allowed values)
    FactMapping ageConstraintFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent4", FactMappingType.GIVEN));
    ageConstraintFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
    ageConstraintFM.addExpressionElement("age", "age");
    createDMNType("myComplexType", "myComplexType", "age");
    errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_ADDED_CONSTRAINT_ERROR, Collections.emptyList()), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_REMOVED_CONSTRAINT_ERROR, Collections.emptyList()));
    // Test 3 - list
    Simulation test3 = new Simulation();
    // topLevelListFM is valid
    FactMapping topLevelListFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("myList", List.class.getCanonicalName()), ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    topLevelListFM.addExpressionElement("tPERSON", List.class.getCanonicalName());
    topLevelListFM.setGenericTypes(Collections.singletonList("tPERSON"));
    createDMNType("myList", "person");
    when(mapOfMockDecisions.get("myList").getResultType().isCollection()).thenReturn(true);
    // addressesFM is valid
    FactMapping addressesFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("myComplexObject", "tMYCOMPLEXOBJECT"), ExpressionIdentifier.create("addresses", FactMappingType.EXPECT));
    addressesFM.addExpressionElement("tMYCOMPLEXOBJECT", "tMYCOMPLEXOBJECT");
    addressesFM.addExpressionElement("addresses", List.class.getCanonicalName());
    addressesFM.setGenericTypes(Collections.singletonList("tADDRESSES"));
    createDMNType("myComplexObject", "myComplexObject", "addresses");
    when(mapOfMockDecisions.get("myComplexObject").getResultType().getFields().get("addresses").isCollection()).thenReturn(true);
    List<FactMappingValidationError> errorsTest3 = validationSpy.validate(test3, settingsLocal, null);
    checkResult(errorsTest3);
    // Test 4 - not existing node
    Simulation test4 = new Simulation();
    FactMapping factMappingNodeRemoved = test4.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "tMYSIMPLETYPE"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    factMappingNodeRemoved.addExpressionElement("tMYSIMPLETYPE", "tMYSIMPLETYPE");
    when(dmnModelMock.getDecisionByName(anyString())).thenReturn(null);
    List<FactMappingValidationError> errorsTest4 = validationSpy.validate(test4, settingsLocal, null);
    checkResult(errorsTest4, new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_NODE_CHANGED_ERROR, Arrays.asList("tMYSIMPLETYPE", "node not found")));
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) UnaryTestImpl(org.kie.dmn.feel.runtime.UnaryTestImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) DMNType(org.kie.dmn.api.core.DMNType) Test(org.junit.Test)

Example 5 with FactMappingValidationError

use of org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError in project drools-wb by kiegroup.

the class DMNScenarioValidation method validate.

/**
 * Validate structure of a DMN test scenario.
 * Supported checks for each column:
 * - empty column skip
 * - DMN node removed
 * - simple type becomes complex type
 * - navigation of data type 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<>();
    String dmnFilePath = settings.getDmnFilePath();
    DMNModel dmnModel = getDMNModel(kieContainer, dmnFilePath);
    for (FactMapping factMapping : simulation.getScesimModelDescriptor().getFactMappings()) {
        if (isToSkip(factMapping)) {
            continue;
        }
        String nodeName = factMapping.getFactIdentifier().getName();
        DMNType factDMNType;
        try {
            factDMNType = dmnModel.getDecisionByName(nodeName) != null ? dmnModel.getDecisionByName(nodeName).getResultType() : dmnModel.getInputByName(nodeName).getType();
        } catch (NullPointerException e) {
            errors.add(createNodeChangedError(factMapping, "node not found"));
            continue;
        }
        List<String> steps = expressionElementToString(factMapping);
        try {
            DMNType fieldType = navigateDMNType(factDMNType, steps);
            if (!isDMNFactMappingValid(factMapping, fieldType)) {
                errors.add(defineFieldChangedError(factMapping, factDMNType, fieldType));
            }
        } catch (IllegalStateException 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) ArrayList(java.util.ArrayList) DMNSimulationUtils.extractDMNModel(org.drools.scenariosimulation.backend.util.DMNSimulationUtils.extractDMNModel) DMNModel(org.kie.dmn.api.core.DMNModel) DMNType(org.kie.dmn.api.core.DMNType) DMNUtils.navigateDMNType(org.drools.workbench.screens.scenariosimulation.backend.server.util.DMNUtils.navigateDMNType)

Aggregations

FactMappingValidationError (org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError)9 Test (org.junit.Test)7 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)2 Simulation (org.drools.scenariosimulation.api.model.Simulation)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNType (org.kie.dmn.api.core.DMNType)2 HashMap (java.util.HashMap)1 ScenarioException (org.drools.scenariosimulation.backend.runner.ScenarioException)1 DMNSimulationUtils.extractDMNModel (org.drools.scenariosimulation.backend.util.DMNSimulationUtils.extractDMNModel)1 ScenarioBeanUtil.navigateToObject (org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.navigateToObject)1 DMNUtils.navigateDMNType (org.drools.workbench.screens.scenariosimulation.backend.server.util.DMNUtils.navigateDMNType)1 KieContainer (org.kie.api.runtime.KieContainer)1 UnaryTestImpl (org.kie.dmn.feel.runtime.UnaryTestImpl)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Matchers.anyString (org.mockito.Matchers.anyString)1