Populate codec/ directory
[yangtools.git] / codec / yang-data-codec-binfmt / src / test / java / org / opendaylight / yangtools / yang / data / codec / binfmt / NipSerializationTest.java
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.collect.Maps;
11 import com.google.common.io.ByteStreams;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.util.Arrays;
15 import java.util.Map;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.junit.runners.Parameterized;
20 import org.junit.runners.Parameterized.Parameter;
21 import org.junit.runners.Parameterized.Parameters;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24
25 @RunWith(Parameterized.class)
26 public class NipSerializationTest extends AbstractSerializationTest {
27     @Parameters(name = "{0}")
28     public static Iterable<Object[]> data() {
29         return Arrays.asList(
30             new Object[] { NormalizedNodeStreamVersion.LITHIUM,     99, 118, 194, 5203, 1_443_411, 2_693_479 },
31             new Object[] { NormalizedNodeStreamVersion.NEON_SR2,   101, 116, 176, 4181, 1_180_245, 1_772_383 },
32             new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1,  95, 107, 156, 3409,   982_867, 1_443_164 },
33             new Object[] { NormalizedNodeStreamVersion.MAGNESIUM,   95, 107, 156, 3409,   982_867, 1_443_164 });
34     }
35
36     @Parameter(1)
37     public int emptySize;
38     @Parameter(2)
39     public int oneSize;
40     @Parameter(3)
41     public int size5;
42     @Parameter(4)
43     public int size256;
44     @Parameter(5)
45     public int size65792;
46     @Parameter(6)
47     public int twiceSize65792;
48
49     @Test
50     public void testEmptyIdentifier() {
51         assertEquals(createIdentifier(0), emptySize);
52     }
53
54     @Test
55     public void testOneIdentifier() {
56         assertEquals(createIdentifier(1), oneSize);
57     }
58
59     @Test
60     public void test5() {
61         assertEquals(createIdentifier(5), size5);
62     }
63
64     @Test
65     public void test256() {
66         assertEquals(createIdentifier(256), size256);
67     }
68
69     @Test
70     public void test65536() {
71         assertEquals(createIdentifier(65792), size65792);
72     }
73
74     @Test
75     public void testTwice65792() {
76         final NodeIdentifierWithPredicates nip = createIdentifier(65792);
77
78         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
79         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
80             nnout.writePathArgument(nip);
81             nnout.writePathArgument(nip);
82         } catch (IOException e) {
83             throw new AssertionError("Failed to serialize", e);
84         }
85
86         final byte[] bytes = baos.toByteArray();
87         Assert.assertEquals(twiceSize65792, bytes.length);
88
89         try {
90             final NormalizedNodeDataInput input = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
91             Assert.assertEquals(nip, input.readPathArgument());
92             Assert.assertEquals(nip, input.readPathArgument());
93         } catch (IOException e) {
94             throw new AssertionError("Failed to deserialize", e);
95         }
96     }
97
98     private static NodeIdentifierWithPredicates createIdentifier(final int size) {
99         final Map<QName, Object> predicates = Maps.newHashMapWithExpectedSize(size);
100         for (QName qname : generateQNames(size)) {
101             predicates.put(qname, "a");
102         }
103         return NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME, predicates);
104     }
105 }