f9796224b06f377db4104ce0cd762f786d93df4f
[yangtools.git] /
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.codec.binfmt;
9
10 import com.google.common.io.ByteStreams;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
20
21 abstract class AbstractSerializationTest {
22     static final <T extends NormalizedNode> T assertEquals(final NormalizedNodeStreamVersion version, final T node,
23             final int expectedSize) {
24         final var baos = new ByteArrayOutputStream();
25         try (var nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
26             nnout.writeNormalizedNode(node);
27         } catch (IOException e) {
28             throw new AssertionError("Failed to serialize", e);
29         }
30
31         final byte[] bytes = baos.toByteArray();
32         Assert.assertEquals(expectedSize, bytes.length);
33
34         final NormalizedNode deser;
35         try {
36             deser = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes)).readNormalizedNode();
37         } catch (IOException e) {
38             throw new AssertionError("Failed to deserialize", e);
39         }
40         Assert.assertEquals(node, deser);
41         return node;
42     }
43
44     static final <T> T assertEquals(final NormalizedNodeStreamVersion version, final T value, final int expectedSize) {
45         return assertEquals(version, ImmutableNodes.leafNode(TestModel.TEST_QNAME, value), expectedSize).body();
46     }
47
48     static final <T extends PathArgument> T assertEquals(final NormalizedNodeStreamVersion version, final T arg,
49             final int expectedSize) {
50         final var baos = new ByteArrayOutputStream();
51         try (var nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
52             nnout.writePathArgument(arg);
53         } catch (IOException e) {
54             throw new AssertionError("Failed to serialize", e);
55         }
56
57         final byte[] bytes = baos.toByteArray();
58         Assert.assertEquals(expectedSize, bytes.length);
59
60         final PathArgument deser;
61         try {
62             deser = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes)).readPathArgument();
63         } catch (IOException e) {
64             throw new AssertionError("Failed to deserialize", e);
65         }
66         Assert.assertEquals(arg, deser);
67         return (T) deser;
68     }
69
70     static final void assertEqualsTwice(final NormalizedNodeStreamVersion version, final PathArgument arg,
71             final int expectedSize) {
72         final var baos = new ByteArrayOutputStream();
73         try (var nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
74             nnout.writePathArgument(arg);
75             nnout.writePathArgument(arg);
76         } catch (IOException e) {
77             throw new AssertionError("Failed to serialize", e);
78         }
79
80         final byte[] bytes = baos.toByteArray();
81         Assert.assertEquals(expectedSize, bytes.length);
82
83         try {
84             final var in = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
85             Assert.assertEquals(arg, in.readPathArgument());
86             Assert.assertEquals(arg, in.readPathArgument());
87         } catch (IOException e) {
88             throw new AssertionError("Failed to deserialize", e);
89         }
90     }
91
92     static final void assertSame(final NormalizedNodeStreamVersion version, final Object value,
93             final int expectedSize) {
94         Assert.assertSame(value, assertEquals(version, value, expectedSize));
95     }
96
97     static final void assertSame(final NormalizedNodeStreamVersion version, final PathArgument arg,
98             final int expectedSize) {
99         Assert.assertSame(arg, assertEquals(version, arg, expectedSize));
100     }
101
102     static final List<QName> generateQNames(final int size) {
103         final var qnames = new ArrayList<QName>(size);
104         for (int i = 0; i < size; ++i) {
105             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
106         }
107         return qnames;
108     }
109 }