use of org.kie.dmn.model.v1_1.Definitions in project drools by kiegroup.
the class DMNXMLLoaderTest method testLoadingDecisionServices.
@Test
public void testLoadingDecisionServices() {
final DMNMarshaller DMNMarshaller = DMNMarshallerFactory.newMarshallerWithExtensions(Arrays.asList(new DecisionServicesExtensionRegister()));
final InputStream is = this.getClass().getResourceAsStream("0004-decision-services.dmn");
final InputStreamReader isr = new InputStreamReader(is);
final Definitions def = DMNMarshaller.unmarshal(isr);
assertThat(def.getDecisionService().size(), is(2));
DecisionService decisionService1 = def.getDecisionService().get(0);
assertThat(decisionService1.getId(), is("_70386614-9838-420b-a2ae-ff901ada63fb"));
assertThat(decisionService1.getName(), is("A Only Knowing B and C"));
assertThat(decisionService1.getDescription(), is("Description of A (BC)"));
assertThat(decisionService1.getOutputDecision().size(), is(1));
assertThat(decisionService1.getEncapsulatedDecision().size(), is(0));
assertThat(decisionService1.getInputDecision().size(), is(2));
assertThat(decisionService1.getInputData().size(), is(0));
assertThat(decisionService1.getOutputDecision().get(0).getHref(), is("#_c2b44706-d479-4ceb-bb74-73589d26dd04"));
DecisionService decisionService2 = def.getDecisionService().get(1);
assertThat(decisionService2.getId(), is("_4620ef13-248a-419e-bc68-6b601b725a03"));
assertThat(decisionService2.getName(), is("A only as output knowing D and E"));
assertThat(decisionService2.getOutputDecision().size(), is(1));
assertThat(decisionService2.getEncapsulatedDecision().size(), is(2));
assertThat(decisionService2.getInputDecision().size(), is(0));
assertThat(decisionService2.getInputData().size(), is(2));
assertThat(decisionService2.getInputData().get(0).getHref(), is("#_bcea16fb-6c19-4bde-b37d-73407002c064"));
assertThat(decisionService2.getInputData().get(1).getHref(), is("#_207b9195-a441-47f2-9414-2fad64b463f9"));
}
use of org.kie.dmn.model.v1_1.Definitions in project drools by kiegroup.
the class DMNXMLLoaderTest method testLoadingDishDecision.
@Test
@Ignore("No unmarshaller implemented")
public void testLoadingDishDecision() {
final DMNMarshaller DMNMarshaller = DMNMarshallerFactory.newDefaultMarshaller();
final InputStream is = this.getClass().getResourceAsStream("/src/test/resources/dish-decision.xml");
final InputStreamReader isr = new InputStreamReader(is);
final Object o = DMNMarshaller.unmarshal(isr);
final Definitions root = (Definitions) o;
assertNotNull(root);
}
use of org.kie.dmn.model.v1_1.Definitions in project drools by kiegroup.
the class DMNXMLLoaderTest method testLoadingWithNoDecisionServices.
@Test
public void testLoadingWithNoDecisionServices() {
final DMNMarshaller DMNMarshaller = DMNMarshallerFactory.newMarshallerWithExtensions(Arrays.asList(new DecisionServicesExtensionRegister()));
final InputStream is = this.getClass().getResourceAsStream("0001-input-data-string.dmn");
final InputStreamReader isr = new InputStreamReader(is);
final Definitions def = DMNMarshaller.unmarshal(isr);
// check if No DecisionServices in extended v1.1 does not NPE.
assertThat(def.getDecisionService().size(), is(0));
}
use of org.kie.dmn.model.v1_1.Definitions in project drools by kiegroup.
the class DMNXMLLoaderTest method test0004_multiple_extensions.
@Test
public void test0004_multiple_extensions() throws Exception {
DMNMarshaller marshaller = DMNMarshallerFactory.newMarshallerWithExtensions(Arrays.asList(new DecisionServicesExtensionRegister()));
final InputStream is = this.getClass().getResourceAsStream("0004-decision-services_multiple_extensions.dmn");
final InputStreamReader isr = new InputStreamReader(is);
final Definitions def = marshaller.unmarshal(isr);
assertThat(def.getExtensionElements().getAny().size(), is(1));
// if arrived here, means it did not fail with exception while trying to unmarshall unknown rss extension element, hence it just skipped it.
}
use of org.kie.dmn.model.v1_1.Definitions in project drools by kiegroup.
the class UnmarshalMarshalTest method testRoundTrip.
public void testRoundTrip(String subdir, String xmlfile, DMNMarshaller marshaller) 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.setSchemaSource(new StreamSource(this.getClass().getResource("/dmn.xsd").getFile()));
ValidationResult validateInputResult = v.validateInstance(new StreamSource(inputXMLFile));
if (!validateInputResult.isValid()) {
for (ValidationProblem p : validateInputResult.getProblems()) {
System.err.println(p);
}
}
assertTrue(validateInputResult.isValid());
final File subdirFile = new File(baseOutputDir, subdir);
if (!subdirFile.mkdirs()) {
logger.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();
System.out.println(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()) {
System.err.println(p);
}
}
assertTrue(validateOutputResult.isValid());
System.out.println("\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(System.out::println);
System.out.println("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"
*/
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") }));
Set<String> nodeHavingDefaultableAttr = new HashSet<>();
nodeHavingDefaultableAttr.addAll(Arrays.asList(new String[] { "definitions", "decisionTable", "itemDefinition", "itemComponent" }));
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;
}
}
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(System.err::println);
if (!checkSimilar.getDifferences().iterator().hasNext()) {
System.out.println("[ EMPTY - no diffs using customized similarity ]");
}
assertFalse("XML are NOT similar: " + checkSimilar.toString(), checkSimilar.hasDifferences());
}
Aggregations