import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
-import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collections;
import java.util.Optional;
-import java.util.function.Function;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import org.junit.runners.Parameterized.Parameters;
import org.opendaylight.yangtools.yang.common.Empty;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.Uint64;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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.YangInstanceIdentifier.PathArgument;
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.DataContainerChild;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.w3c.dom.Node;
@RunWith(Parameterized.class)
public class NormalizedNodeStreamReaderWriterTest {
- public enum Unsigned implements Function<String, Number> {
- BIG_INTEGER {
- @Override
- public BigInteger apply(final String str) {
- return new BigInteger(str);
- }
- },
- UINT64 {
- @Override
- public Uint64 apply(final String str) {
- return Uint64.valueOf(str);
- }
- };
- }
-
@Parameters(name = "{0} {1}")
public static Iterable<Object[]> data() {
- return List.of(
- new Object[] { NormalizedNodeStreamVersion.LITHIUM, Unsigned.BIG_INTEGER,
- 1_050_286, 9_577_973, 171, 1_553, 103, 237, 98 },
- new Object[] { NormalizedNodeStreamVersion.NEON_SR2, Unsigned.BIG_INTEGER,
- 1_049_950, 5_577_993, 161, 1_163, 105, 235, 100 },
- new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.BIG_INTEGER,
- 1_049_619, 2_289_103, 139, 826, 103, 229, 99 },
- new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.UINT64,
- 1_049_618, 2_289_103, 139, 825, 103, 229, 99 },
- new Object[] { NormalizedNodeStreamVersion.MAGNESIUM, Unsigned.UINT64,
- 1_049_618, 2_289_103, 139, 825, 103, 229, 99 });
+ return Collections.singletonList(
+ new Object[] { NormalizedNodeStreamVersion.MAGNESIUM, 1_049_589, 2_289_103, 139, 796, 103, 229, 99 });
}
@Parameter(0)
public NormalizedNodeStreamVersion version;
@Parameter(1)
- public Unsigned uint64;
- @Parameter(2)
public int normalizedNodeStreamingSize;
- @Parameter(3)
+ @Parameter(2)
public int hugeEntriesSize;
- @Parameter(4)
+ @Parameter(3)
public int yiidStreamingSize;
- @Parameter(5)
+ @Parameter(4)
public int nnYiidStreamingSize;
- @Parameter(6)
+ @Parameter(5)
public int writePathArgumentSize;
- @Parameter(7)
+ @Parameter(6)
public int anyxmlStreamingSize;
- @Parameter(8)
+ @Parameter(7)
public int schemaPathSize;
@Test
public void testNormalizedNodeStreaming() throws IOException {
-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
- NormalizedNode testContainer = createTestContainer();
+ ContainerNode testContainer = createTestContainer();
nnout.writeNormalizedNode(testContainer);
QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
assertEquals(toasterContainer, nnin.readNormalizedNode());
}
- private NormalizedNode createTestContainer() {
- byte[] bytes1 = {1, 2, 3};
- LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
- new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
-
- byte[] bytes2 = {};
- LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
- new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
-
- return TestModel.createBaseTestContainerBuilder(uint64)
- .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
- new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
- .withChild(entry1).withChild(entry2).build())
- .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
- .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
- .withChild(Builders.orderedMapBuilder()
- .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
- .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
- TestModel.ID_QNAME, 11)).build()).build();
+ private static ContainerNode createTestContainer() {
+ final byte[] bytes1 = {1, 2, 3};
+ final byte[] bytes2 = {};
+
+ return TestModel.createBaseTestContainerBuilder()
+ .withChild(Builders.leafSetBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
+ .withChild(Builders.leafSetEntryBuilder()
+ .withNodeIdentifier(new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1))
+ .withValue(bytes1)
+ .build())
+ .withChild(Builders.leafSetEntryBuilder()
+ .withNodeIdentifier(new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2))
+ .withValue(bytes2)
+ .build())
+ .build())
+ .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
+ .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.value()))
+ .withChild(Builders.orderedMapBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
+ .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME, TestModel.ID_QNAME, 11))
+ .build())
+ .build();
}
@Test
@Test
public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
- final NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder(uint64).build();
+ final NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build();
final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
.node(TestModel.OUTER_LIST_QNAME)
.nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10)
@Test
public void testWithSerializable() {
- NormalizedNode input = TestModel.createTestContainer(uint64);
+ NormalizedNode input = TestModel.createTestContainer();
SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(version, input);
SampleNormalizedNodeSerializable clone = clone(serializable);
assertEquals(input, clone.getInput());
assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
- NormalizedNode anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
- Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
- .withValue(new DOMSource(xmlNode)).build()).build();
+ ContainerNode anyXmlContainer = Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
+ .withChild(Builders.anyXmlBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
+ .withValue(new DOMSource(xmlNode))
+ .build())
+ .build();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- transformer.transform(((DOMSourceAnyxmlNode)child.get()).body(), xmlOutput);
+ transformer.transform(((DOMSourceAnyxmlNode)child.orElseThrow()).body(), xmlOutput);
assertEquals("XML", xml, xmlOutput.getWriter().toString());
assertEquals("http://www.w3.org/TR/html4/",
- ((DOMSourceAnyxmlNode)child.get()).body().getNode().getNamespaceURI());
+ ((DOMSourceAnyxmlNode)child.orElseThrow()).body().getNode().getNamespaceURI());
}
@Test
*/
@Test
public void testHugeEntries() throws IOException {
- final CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapBuilder = Builders.mapBuilder()
- .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
- final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
- Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
+ final var mapBuilder = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
+ final var entryBuilder = Builders.mapEntryBuilder()
+ .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
for (int i = 0; i < 100_000; ++i) {
final String key = "xyzzy" + i;
assertEquals(expected, nnin.readNormalizedNode());
}
- @Test
- public void testAugmentationIdentifier() throws IOException {
- final List<QName> qnames = new ArrayList<>();
- for (int i = 0; i < 257; ++i) {
- qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
- }
-
- for (int i = 0; i < qnames.size(); ++i) {
- assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames.subList(0, i))));
- }
-
- for (int i = qnames.size(); i < 65536; ++i) {
- qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
- }
- assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames)));
- }
-
- private void assertAugmentationIdentifier(final AugmentationIdentifier expected) throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
- nnout.writePathArgument(expected);
- }
-
- final byte[] bytes = bos.toByteArray();
-
- NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
- PathArgument arg = nnin.readPathArgument();
- assertEquals(expected, arg);
- }
-
private static <T extends Serializable> T clone(final T obj) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {