Skip to content

Commit

Permalink
[#694] Serialization: Allow partial elements
Browse files Browse the repository at this point in the history
  • Loading branch information
opatrascoiu committed Aug 2, 2024
1 parent a362aeb commit 946e9b7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,19 @@ public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingC
}

protected void writeChildrenNode(HierarchicalStreamWriter writer, MarshallingContext context, Object node, String nodeAlias) {
writer.startNode(nodeAlias);
context.convertAnother(node);
writer.endNode();
if (node != null) {
writer.startNode(nodeAlias);
context.convertAnother(node);
writer.endNode();
}
}

protected void writeChildrenNodeAsValue(HierarchicalStreamWriter writer, MarshallingContext context, String nodeValue, String nodeAlias) {
writer.startNode(nodeAlias);
writer.setValue(nodeValue);
writer.endNode();
if (nodeValue != null) {
writer.startNode(nodeAlias);
writer.setValue(nodeValue);
writer.endNode();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package com.gs.dmn.serialization.xstream.v1_1;

import com.gs.dmn.ast.TExpression;
import com.gs.dmn.ast.TUnaryTests;
import com.gs.dmn.serialization.DMNVersion;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
Expand Down Expand Up @@ -41,7 +40,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
TExpression e = (TExpression) parent;

if (!(e instanceof TUnaryTests) && e.getTypeRef() != null) {
if (e.getTypeRef() != null) {
writer.addAttribute(TYPE_REF, DMNBaseConverter.formatQName(e.getTypeRef(), e, version));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
TNamedElement ne = (TNamedElement) parent;

writer.addAttribute(NAME, ne.getName());
if (ne.getName() != null) {
writer.addAttribute(NAME, ne.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingC

protected void writeChildrenNode(HierarchicalStreamWriter writer, MarshallingContext context, Object node, String nodeAlias) {
writer.startNode(nodeAlias);
context.convertAnother(node);
writer.endNode();
}

protected void writeChildrenNodeAsValue(HierarchicalStreamWriter writer, MarshallingContext context, String nodeValue, String nodeAlias) {
writer.startNode(nodeAlias);
writer.setValue(nodeValue);
if (node != null) {
context.convertAnother(node);
}
writer.endNode();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -31,6 +32,50 @@ public void testRead() {
File input = new File(resource("dmn/input/1.1/test-dmn.dmn"));

TDefinitions definitions = this.dmnSerializer.readModel(input);
checkModel(definitions);
}

@Test
public void testWrite() throws IOException {
// Test partial objects
File outputFile = File.createTempFile("jdmn-", "-dmn");
outputFile.deleteOnExit();

assertDoesNotThrow(() -> this.dmnSerializer.writeModel(null, outputFile));

assertDoesNotThrow(() -> {
TDefinitions definitions = new TDefinitions();
definitions.getElementInfo().getNsContext().putAll(DMNVersion.LATEST.getPrefixToNamespaceMap());
this.dmnSerializer.writeModel(definitions, outputFile);
});

assertDoesNotThrow(() -> {
TDefinitions definitions = new TDefinitions();
definitions.getElementInfo().getNsContext().putAll(DMNVersion.LATEST.getPrefixToNamespaceMap());
TDecision decision = new TDecision();
TDecisionTable value = new TDecisionTable();
TDecisionRule rule = null;
value.getRule().add(rule);
decision.setExpression(value);
definitions.getDrgElement().add(decision);
this.dmnSerializer.writeModel(definitions, outputFile);
});
}

@Test
public void testRoundTrip() {
File input = new File(resource("dmn/input/1.1/test-dmn.dmn"));

TDefinitions definitions = this.dmnSerializer.readModel(input);
File outputFile = new File("target", "test-dmn.dmn");
this.dmnSerializer.writeModel(definitions, outputFile);

definitions = this.dmnSerializer.readModel(outputFile);

checkModel(definitions);
}

private void checkModel(TDefinitions definitions) {
List<TDRGElement> drgElementList = definitions.getDrgElement();
assertEquals(1, drgElementList.size());

Expand Down Expand Up @@ -70,6 +115,8 @@ public void testRead() {
assertEquals(5, inputEntryList.size());
TUnaryTests firstUnaryTest = inputEntryList.get(0);
assertEquals("= \"Female\"", firstUnaryTest.getText());
TUnaryTests secondUnaryTest = inputEntryList.get(1);
assertEquals("\"£ [ ] & < $ \\u20ac\"", secondUnaryTest.getText());
}

private void assertLiteralExpression(TLiteralExpression inputExpression, String stringType, String id, String text) {
Expand Down
2 changes: 1 addition & 1 deletion dmn-core/src/test/resources/dmn/input/1.1/test-dmn.dmn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<text><![CDATA[= "Female"]]></text>
</inputEntry>
<inputEntry id="UnaryTests_11h9jt8">
<text></text>
<text>"£ [ ] &amp; &lt; $ \u20ac"</text>
</inputEntry>
<inputEntry id="UnaryTests_1oudzn9">
<text></text>
Expand Down

0 comments on commit 946e9b7

Please sign in to comment.