use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DMNRuntimeTest method test_countCSATradeRatings.
@Test
public void test_countCSATradeRatings() {
// DROOLS-1563
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("countCSATradeRatings.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_1a7d184c-2e38-4462-ae28-15591ef6d534", "countCSATradeRatings");
assertThat(dmnModel, notNullValue());
assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
final DMNContext ctx = runtime.newContext();
final List<Map<?, ?>> ratings = new ArrayList<>();
ratings.add(prototype(entry("Agency", "FITCH"), entry("Value", "val1")));
ratings.add(prototype(entry("Agency", "MOODY"), entry("Value", "val2")));
ctx.set("CSA Trade Ratings", ratings);
final DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
final DMNContext result = dmnResult.getContext();
assertThat(result.get("Trade Ratings"), is(new BigDecimal(2)));
final DMNContext ctx2 = runtime.newContext();
ctx2.set("CSA Trade Ratings", null);
final DMNResult dmnResult2 = runtime.evaluateAll(dmnModel, ctx2);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(true));
assertThat(dmnResult2.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.FEEL_EVALUATION_ERROR)), is(true));
assertThat(dmnResult2.getDecisionResultByName("Trade Ratings").getEvaluationStatus(), is(DecisionEvaluationStatus.FAILED));
final DMNResult dmnResult3 = runtime.evaluateAll(dmnModel, runtime.newContext());
assertThat(DMNRuntimeUtil.formatMessages(dmnResult3.getMessages()), dmnResult3.hasErrors(), is(true));
assertThat(dmnResult3.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.REQ_NOT_FOUND)), is(true));
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DMNRuntimeTest method testNPE.
@Test
public void testNPE() {
// DROOLS-1512
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("NPE.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_95b7ee22-1964-4be5-b7db-7db66692c707", "Dessin 1");
assertThat(dmnModel, notNullValue());
assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
final DMNContext context = runtime.newContext();
final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(true));
assertThat(dmnResult.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.REQ_NOT_FOUND)), is(true));
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DMNRuntimeTest method testUnionofLetters.
@Test
public void testUnionofLetters() {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Union_of_letters.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_76362694-41e8-400c-8dea-e5f033d4f405", "Union of letters");
assertThat(dmnModel, notNullValue());
assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
final DMNContext ctx1 = runtime.newContext();
ctx1.set("A1", Arrays.asList("a", "b"));
ctx1.set("A2", Arrays.asList("b", "c"));
final DMNResult dmnResult1 = runtime.evaluateAll(dmnModel, ctx1);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult1.getMessages()), dmnResult1.hasErrors(), is(false));
assertThat((List<?>) dmnResult1.getContext().get("D1"), contains("a", "b", "c"));
final DMNContext ctx2 = runtime.newContext();
ctx2.set("A1", Arrays.asList("a", "b"));
ctx2.set("A2", Arrays.asList("b", "x"));
final DMNResult dmnResult2 = runtime.evaluateAll(dmnModel, ctx2);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(true));
assertThat(dmnResult2.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.ERROR_EVAL_NODE)), is(true));
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class UnmarshalMarshalTest method testRoundTrip.
public void testRoundTrip(String subdir, String xmlfile, DMNMarshaller marshaller, Source... schemaSource) throws Exception {
File baseOutputDir = new File("target/test-xmlunit/");
File testClassesBaseDir = new File("target/test-classes/");
File inputXMLFile = new File(testClassesBaseDir, subdir + xmlfile);
FileInputStream fis = new FileInputStream(inputXMLFile);
Definitions unmarshal = marshaller.unmarshal(new InputStreamReader(fis));
Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
v.setSchemaSources(schemaSource);
ValidationResult validateInputResult = v.validateInstance(new StreamSource(inputXMLFile));
if (!validateInputResult.isValid()) {
for (ValidationProblem p : validateInputResult.getProblems()) {
LOG.error("{}", p);
}
}
assertTrue(validateInputResult.isValid());
final File subdirFile = new File(baseOutputDir, subdir);
if (!subdirFile.mkdirs()) {
LOG.warn("mkdirs() failed for File: ", subdirFile.getAbsolutePath());
}
FileOutputStream sourceFos = new FileOutputStream(new File(baseOutputDir, subdir + "a." + xmlfile));
Files.copy(new File(testClassesBaseDir, subdir + xmlfile).toPath(), sourceFos);
sourceFos.flush();
sourceFos.close();
LOG.debug("{}", marshaller.marshal(unmarshal));
File outputXMLFile = new File(baseOutputDir, subdir + "b." + xmlfile);
try (FileWriter targetFos = new FileWriter(outputXMLFile)) {
marshaller.marshal(unmarshal, targetFos);
}
// Should also validate output XML:
ValidationResult validateOutputResult = v.validateInstance(new StreamSource(outputXMLFile));
if (!validateOutputResult.isValid()) {
for (ValidationProblem p : validateOutputResult.getProblems()) {
LOG.error("{}", p);
}
}
assertTrue(validateOutputResult.isValid());
LOG.debug("\n---\nDefault XMLUnit comparison:");
Source control = Input.fromFile(inputXMLFile).build();
Source test = Input.fromFile(outputXMLFile).build();
Diff allDiffsSimilarAndDifferent = DiffBuilder.compare(control).withTest(test).build();
allDiffsSimilarAndDifferent.getDifferences().forEach(m -> LOG.debug("{}", m));
LOG.info("XMLUnit comparison with customized similarity for defaults:");
// in the following a manual DifferenceEvaluator is needed until XMLUnit is configured for properly parsing the XSD linked inside the XML,
// in order to detect the optional+defaultvalue attributes of xml element which might be implicit in source-test, and explicit in test-serialized.
/*
* $ grep -Eo "<xsd:attribute name=\\\"([^\\\"]*)\\\" type=\\\"([^\\\"]*)\\\" use=\\\"optional\\\" default=\\\"([^\\\"])*\\\"" dmn.xsd
<xsd:attribute name="expressionLanguage" type="xsd:anyURI" use="optional" default="http://www.omg.org/spec/FEEL/20140401"
<xsd:attribute name="typeLanguage" type="xsd:anyURI" use="optional" default="http://www.omg.org/spec/FEEL/20140401"
<xsd:attribute name="isCollection" type="xsd:boolean" use="optional" default="false"
<xsd:attribute name="hitPolicy" type="tHitPolicy" use="optional" default="UNIQUE"
<xsd:attribute name="preferredOrientation" type="tDecisionTableOrientation" use="optional" default="Rule-as-Row"
DMNv1.2:
<xsd:attribute name="kind" type="tFunctionKind" default="FEEL"/>
<xsd:attribute name="textFormat" type="xsd:string" default="text/plain"/>
<xsd:attribute name="associationDirection" type="tAssociationDirection" default="None"/>
DMNDIv1.2:
<xsd:attribute name="isCollapsed" type="xsd:boolean" use="optional" default="false"/>
*/
Set<QName> attrWhichCanDefault = new HashSet<QName>();
attrWhichCanDefault.addAll(Arrays.asList(new QName[] { new QName("expressionLanguage"), new QName("typeLanguage"), new QName("isCollection"), new QName("hitPolicy"), new QName("preferredOrientation"), new QName("kind"), new QName("textFormat"), new QName("associationDirection"), new QName("isCollapsed") }));
Set<String> nodeHavingDefaultableAttr = new HashSet<>();
nodeHavingDefaultableAttr.addAll(Arrays.asList(new String[] { "definitions", "decisionTable", "itemDefinition", "itemComponent", "encapsulatedLogic", "textAnnotation", "association", "DMNShape" }));
Diff checkSimilar = DiffBuilder.compare(control).withTest(test).withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, ((comparison, outcome) -> {
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ELEMENT_NUM_ATTRIBUTES) {
if (comparison.getControlDetails().getTarget().getNodeName().equals(comparison.getTestDetails().getTarget().getNodeName()) && nodeHavingDefaultableAttr.contains(safeStripDMNPRefix(comparison.getControlDetails().getTarget()))) {
return ComparisonResult.SIMILAR;
}
}
// DMNDI/DMNDiagram#documentation is actually deserialized escaped with newlines as by the XML JDK infra.
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ATTR_VALUE) {
if (comparison.getControlDetails().getTarget().getNodeName().equals(comparison.getTestDetails().getTarget().getNodeName()) && comparison.getControlDetails().getTarget().getNodeType() == Node.ATTRIBUTE_NODE && comparison.getControlDetails().getTarget().getLocalName().equals("documentation")) {
return ComparisonResult.SIMILAR;
}
}
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ATTR_NAME_LOOKUP) {
boolean testIsDefaulableAttribute = false;
QName whichDefaultableAttr = null;
if (comparison.getControlDetails().getValue() == null && attrWhichCanDefault.contains(comparison.getTestDetails().getValue())) {
for (QName a : attrWhichCanDefault) {
boolean check = comparison.getTestDetails().getXPath().endsWith("@" + a);
if (check) {
testIsDefaulableAttribute = true;
whichDefaultableAttr = a;
continue;
}
}
}
if (testIsDefaulableAttribute) {
if (comparison.getTestDetails().getXPath().equals(comparison.getControlDetails().getXPath() + "/@" + whichDefaultableAttr)) {
// TODO missing to check the explicited option attribute has value set to the actual default value.
return ComparisonResult.SIMILAR;
}
}
}
return outcome;
}))).ignoreWhitespace().checkForSimilar().build();
checkSimilar.getDifferences().forEach(m -> LOG.error("{}", m));
if (!checkSimilar.getDifferences().iterator().hasNext()) {
LOG.info("[ EMPTY - no diffs using customized similarity ]");
}
assertFalse("XML are NOT similar: " + checkSimilar.toString(), checkSimilar.hasDifferences());
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class PMMLValidatorImportTest method testImportPMML2.
@Test
public void testImportPMML2() throws IOException {
// DROOLS-4395 [DMN Designer] Validation fails for included PMML model
try (Reader defsReader = getReader("KiePMMLScoreCard_wInputType.dmn", DMNRuntimePMMLTest.class)) {
final Definitions defs = getDefinitions(defsReader, "http://www.trisotech.com/definitions/_ca466dbe-20b4-4e88-a43f-4ce3aff26e4f", "KiePMMLScoreCard");
DMNValidator.ValidatorBuilder.ValidatorImportReaderResolver resolver = (ns, name, i) -> {
if (ns.equals(defs.getNamespace()) && name.equals(defs.getName()) && i.equals(defs.getImport().get(0).getLocationURI())) {
return getReader("test_scorecard.pmml", DMNRuntimePMMLTest.class);
} else {
return null;
}
};
final List<DMNMessage> messages = validator.validateUsing(Validation.VALIDATE_MODEL, Validation.VALIDATE_COMPILATION).usingImports(resolver).theseModels(defs);
assertThat(ValidatorUtil.formatMessages(messages), messages.size(), is(0));
}
}
Aggregations