package org.opendaylight.controller.cluster.datastore.node.utils.stream;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
-import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.xml.transform.dom.DOMSource;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.opendaylight.yangtools.util.xml.UntrustedXML;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
-import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.w3c.dom.Document;
+import org.xmlunit.builder.DiffBuilder;
public class SerializationUtilsTest {
-
- private static final QName CONTAINER_Q_NAME = QName.create("ns-1", "2017-03-17", "container1");
+ private static final QName CONTAINER1 = QName.create("ns-1", "2017-03-17", "container1");
@Test
- public void testSerializeDeserializeNodes() throws IOException {
- final ContainerNode normalizedNode = createNormalizedNode();
- final byte[] bytes = serializeNormalizedNode(normalizedNode);
+ public void testSerializeDeserializeNodes() throws Exception {
+ final var normalizedNode = createNormalizedNode();
+ final var bytes = serialize(normalizedNode);
assertEquals(10556, bytes.length);
- assertEquals(normalizedNode, deserializeNormalizedNode(bytes));
+ assertEquals(normalizedNode, deserialize(bytes));
}
@Test
public void testSerializeDeserializeAnyXmlNode() throws Exception {
- final ByteArrayInputStream is =
- new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset()));
- final Document parse = UntrustedXML.newDocumentBuilder().parse(is);
- final DOMSourceAnyxmlNode anyXmlNode = Builders.anyXmlBuilder()
- .withNodeIdentifier(id("anyXmlNode"))
- .withValue(new DOMSource(parse))
- .build();
- final byte[] bytes = serializeNormalizedNode(anyXmlNode);
+ final var parse = UntrustedXML.newDocumentBuilder().parse(
+ new ByteArrayInputStream("<xml><data/></xml>".getBytes(StandardCharsets.UTF_8)));
+ final var anyXmlNode = Builders.anyXmlBuilder()
+ .withNodeIdentifier(id("anyXmlNode"))
+ .withValue(new DOMSource(parse))
+ .build();
+ final byte[] bytes = serialize(anyXmlNode);
assertEquals(113, bytes.length);
- final NormalizedNode deserialized = deserializeNormalizedNode(bytes);
- final DOMSource value = (DOMSource) deserialized.body();
- final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.body().getNode(),
- value.getNode().getOwnerDocument());
- assertTrue(diff.toString(), diff.similar());
+
+ final var diff = DiffBuilder.compare(anyXmlNode.body().getNode())
+ // FIXME: why all this magic?
+ .withTest(((DOMSource) deserialize(bytes).body()).getNode().getOwnerDocument())
+ .checkForSimilar()
+ .build();
+ assertFalse(diff.toString(), diff.hasDifferences());
}
@Test
public void testSerializeDeserializePath() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final DataOutput out = new DataOutputStream(bos);
- final YangInstanceIdentifier path = YangInstanceIdentifier.builder()
- .node(id("container1"))
- .node(listId("list1", "keyName1", "keyValue1"))
- .node(leafSetId("leafSer1", "leafSetValue1"))
- .build();
- SerializationUtils.writePath(out, path);
+ final var path = YangInstanceIdentifier.builder()
+ .node(id("container1"))
+ .node(listId("list1", "keyName1", "keyValue1"))
+ .node(leafSetId("leafSer1", "leafSetValue1"))
+ .build();
+
+ final var bos = new ByteArrayOutputStream();
+ try (var out = new DataOutputStream(bos)) {
+ SerializationUtils.writePath(out, path);
+ }
- final byte[] bytes = bos.toByteArray();
+ final var bytes = bos.toByteArray();
assertEquals(105, bytes.length);
- final YangInstanceIdentifier deserialized =
- SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bytes)));
- assertEquals(path, deserialized);
+ assertEquals(path, SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bytes))));
}
@Test
public void testSerializeDeserializePathAndNode() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final DataOutput out = new DataOutputStream(bos);
- final NormalizedNode node = createNormalizedNode();
- final YangInstanceIdentifier path = YangInstanceIdentifier.of(id("container1"));
- SerializationUtils.writeNodeAndPath(out, path, node);
+ final var path = YangInstanceIdentifier.of(id("container1"));
+ final var node = createNormalizedNode();
+
+ final var bos = new ByteArrayOutputStream();
+ try (var out = new DataOutputStream(bos)) {
+ SerializationUtils.writeNodeAndPath(out, path, node);
+ }
final byte[] bytes = bos.toByteArray();
assertEquals(10558, bytes.length);
- final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
- final AtomicBoolean applierCalled = new AtomicBoolean(false);
- SerializationUtils.readNodeAndPath(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
- assertEquals(path, deserializedPath);
- assertEquals(node, deserializedNode);
- applierCalled.set(true);
- });
+ final var applierCalled = new AtomicBoolean(false);
+ try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
+ SerializationUtils.readNodeAndPath(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
+ assertEquals(path, deserializedPath);
+ assertEquals(node, deserializedNode);
+ applierCalled.set(true);
+ });
+ }
assertTrue(applierCalled.get());
}
- private static NormalizedNode deserializeNormalizedNode(final byte[] bytes) throws IOException {
+ private static NormalizedNode deserialize(final byte[] bytes) throws Exception {
return SerializationUtils.readNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)))
.orElseThrow();
}
- private static byte[] serializeNormalizedNode(final NormalizedNode node) throws IOException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ private static byte[] serialize(final NormalizedNode node) throws Exception {
+ final var bos = new ByteArrayOutputStream();
SerializationUtils.writeNormalizedNode(new DataOutputStream(bos), node);
return bos.toByteArray();
}
private static ContainerNode createNormalizedNode() {
- final LeafSetNode<Object> leafSetNode = Builders.leafSetBuilder()
- .withNodeIdentifier(id("leafSetNode"))
- .withChild(createLeafSetEntry("leafSetNode", "leafSetValue1"))
- .withChild(createLeafSetEntry("leafSetNode", "leafSetValue2"))
- .build();
- final LeafSetNode<Object> orderedLeafSetNode = Builders.orderedLeafSetBuilder()
- .withNodeIdentifier(id("orderedLeafSetNode"))
- .withChild(createLeafSetEntry("orderedLeafSetNode", "value1"))
- .withChild(createLeafSetEntry("orderedLeafSetNode", "value2"))
- .build();
- final LeafNode<Boolean> booleanLeaf = createLeaf("booleanLeaf", true);
- final LeafNode<Byte> byteLeaf = createLeaf("byteLeaf", (byte) 0);
- final LeafNode<Short> shortLeaf = createLeaf("shortLeaf", (short) 55);
- final LeafNode<Integer> intLeaf = createLeaf("intLeaf", 11);
- final LeafNode<Long> longLeaf = createLeaf("longLeaf", 151515L);
- final LeafNode<String> stringLeaf = createLeaf("stringLeaf", "stringValue");
- final LeafNode<String> longStringLeaf = createLeaf("longStringLeaf", getLongString());
- final LeafNode<QName> qNameLeaf = createLeaf("stringLeaf", QName.create("base", "qName"));
- final LeafNode<YangInstanceIdentifier> idLeaf = createLeaf("stringLeaf", YangInstanceIdentifier.of());
- final MapEntryNode entry1 = Builders.mapEntryBuilder()
- .withNodeIdentifier(listId("mapNode", "key", "key1"))
- .withChild(stringLeaf)
- .build();
- final MapEntryNode entry2 = Builders.mapEntryBuilder()
- .withNodeIdentifier(listId("mapNode", "key", "key2"))
- .withChild(stringLeaf)
- .build();
- final MapNode mapNode = Builders.mapBuilder()
- .withNodeIdentifier(id("mapNode"))
- .withChild(entry1)
- .withChild(entry2)
- .build();
- final UserMapNode orderedMapNode = Builders.orderedMapBuilder()
- .withNodeIdentifier(id("orderedMapNode"))
- .withChild(entry2)
- .withChild(entry1)
- .build();
- final UnkeyedListEntryNode unkeyedListEntry1 = Builders.unkeyedListEntryBuilder()
- .withNodeIdentifier(id("unkeyedList"))
- .withChild(stringLeaf)
- .build();
- final UnkeyedListEntryNode unkeyedListEntry2 = Builders.unkeyedListEntryBuilder()
- .withNodeIdentifier(id("unkeyedList"))
- .withChild(stringLeaf)
- .build();
- final UnkeyedListNode unkeyedListNode = Builders.unkeyedListBuilder()
- .withNodeIdentifier(id("unkeyedList"))
- .withChild(unkeyedListEntry1)
- .withChild(unkeyedListEntry2)
- .build();
- final ChoiceNode choiceNode = Builders.choiceBuilder()
- .withNodeIdentifier(id("choiceNode"))
- .withChild(createLeaf("choiceLeaf", 12))
- .build();
+ final var stringLeaf = createLeaf("stringLeaf", "stringValue");
+ final var entry1 = Builders.mapEntryBuilder()
+ .withNodeIdentifier(listId("mapNode", "key", "key1"))
+ .withChild(stringLeaf)
+ .build();
+ final var entry2 = Builders.mapEntryBuilder()
+ .withNodeIdentifier(listId("mapNode", "key", "key2"))
+ .withChild(stringLeaf)
+ .build();
+
return Builders.containerBuilder()
- .withNodeIdentifier(new NodeIdentifier(CONTAINER_Q_NAME))
- .withChild(booleanLeaf)
- .withChild(byteLeaf)
- .withChild(shortLeaf)
- .withChild(intLeaf)
- .withChild(longLeaf)
+ .withNodeIdentifier(new NodeIdentifier(CONTAINER1))
+ .withChild(createLeaf("booleanLeaf", true))
+ .withChild(createLeaf("byteLeaf", (byte) 0))
+ .withChild(createLeaf("shortLeaf", (short) 55))
+ .withChild(createLeaf("intLeaf", 11))
+ .withChild(createLeaf("longLeaf", 151515L))
.withChild(stringLeaf)
- .withChild(longStringLeaf)
- .withChild(qNameLeaf)
- .withChild(idLeaf)
- .withChild(mapNode)
- .withChild(orderedMapNode)
- .withChild(unkeyedListNode)
- .withChild(leafSetNode)
- .withChild(orderedLeafSetNode)
+ .withChild(createLeaf("longStringLeaf", "0123456789".repeat(1000)))
+ .withChild(createLeaf("stringLeaf", QName.create("base", "qName")))
+ .withChild(createLeaf("stringLeaf", YangInstanceIdentifier.of()))
+ .withChild(Builders.mapBuilder()
+ .withNodeIdentifier(id("mapNode"))
+ .withChild(entry1)
+ .withChild(entry2)
+ .build())
+ .withChild(Builders.orderedMapBuilder()
+ .withNodeIdentifier(id("orderedMapNode"))
+ .withChild(entry2)
+ .withChild(entry1)
+ .build())
+ .withChild(Builders.unkeyedListBuilder()
+ .withNodeIdentifier(id("unkeyedList"))
+ .withChild(Builders.unkeyedListEntryBuilder()
+ .withNodeIdentifier(id("unkeyedList"))
+ .withChild(stringLeaf)
+ .build())
+ .withChild(Builders.unkeyedListEntryBuilder()
+ .withNodeIdentifier(id("unkeyedList"))
+ .withChild(stringLeaf)
+ .build())
+ .build())
+ .withChild(Builders.leafSetBuilder()
+ .withNodeIdentifier(id("leafSetNode"))
+ .withChild(createLeafSetEntry("leafSetNode", "leafSetValue1"))
+ .withChild(createLeafSetEntry("leafSetNode", "leafSetValue2"))
+ .build())
+ .withChild(Builders.orderedLeafSetBuilder()
+ .withNodeIdentifier(id("orderedLeafSetNode"))
+ .withChild(createLeafSetEntry("orderedLeafSetNode", "value1"))
+ .withChild(createLeafSetEntry("orderedLeafSetNode", "value2"))
+ .build())
.withChild(createLeaf("aug1", "aug1Value"))
.withChild(createLeaf("aug2", "aug2Value"))
- .withChild(choiceNode)
+ .withChild(Builders.choiceBuilder()
+ .withNodeIdentifier(id("choiceNode"))
+ .withChild(createLeaf("choiceLeaf", 12))
+ .build())
.build();
}
.build();
}
- private static YangInstanceIdentifier.NodeIdentifier id(final String name) {
- return new YangInstanceIdentifier.NodeIdentifier(QName.create(CONTAINER_Q_NAME, name));
+ private static NodeIdentifier id(final String name) {
+ return new NodeIdentifier(QName.create(CONTAINER1, name));
}
- private static YangInstanceIdentifier.NodeIdentifierWithPredicates listId(final String listName,
- final String keyName,
- final Object keyValue) {
- return YangInstanceIdentifier.NodeIdentifierWithPredicates.of(QName.create(CONTAINER_Q_NAME, listName),
- QName.create(CONTAINER_Q_NAME, keyName), keyValue);
+ private static NodeIdentifierWithPredicates listId(final String listName, final String keyName,
+ final Object keyValue) {
+ return NodeIdentifierWithPredicates.of(QName.create(CONTAINER1, listName), QName.create(CONTAINER1, keyName),
+ keyValue);
}
- private static <T> YangInstanceIdentifier.NodeWithValue<T> leafSetId(final String node, final T value) {
- return new YangInstanceIdentifier.NodeWithValue<>(QName.create(CONTAINER_Q_NAME, node), value);
- }
-
- private static String getLongString() {
- final StringBuilder builder = new StringBuilder(10000);
- for (int i = 0; i < 1000; i++) {
- builder.append("0123456789");
- }
- return builder.toString();
+ private static <T> NodeWithValue<T> leafSetId(final String node, final T value) {
+ return new NodeWithValue<>(QName.create(CONTAINER1, node), value);
}
}