DMNScenarioValidation extends AbstractScenarioValidation { @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 rootDMNType; try { rootDMNType = 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(rootDMNType, steps); if (!isDMNFactMappingValid(factMapping.getClassName(), factMapping, fieldType)) { errors.add(createFieldChangedError(factMapping, fieldType.getName())); } } catch (IllegalStateException e) { errors.add(createGenericError(factMapping, e.getMessage())); } } return errors; }  @Override List<FactMappingValidationError> validate(Simulation simulation, Settings settings, KieContainer kieContainer); static final DMNScenarioValidation INSTANCE; }
@Test public void validate() { DMNScenarioValidation validationSpy = spy(new DMNScenarioValidation() { @Override protected DMNModel getDMNModel(KieContainer kieContainer, String dmnPath) { return dmnModelMock; } }); 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); 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); Simulation test2 = new Simulation(); 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"); 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.addExpressionElement("notExisting", "notExisting"); errorsTest2 = validationSpy.validate(test2, settingsLocal, null); checkResult(errorsTest2, "Impossible to find field 'notExisting' in type 'tPARENT'"); 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, "Impossible to find field 'notExisting' in type 'tPARENT'", "Field type has changed: old 'java.lang.Integer', current 'tNAME'"); Simulation test3 = new Simulation(); 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); 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); 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, "Node type has changed: old 'tMYSIMPLETYPE', current 'node not found'"); }