2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.binfmt;
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;
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;
25 @RunWith(Parameterized.class)
26 public class NipSerializationTest extends AbstractSerializationTest {
27 @Parameters(name = "{0}")
28 public static Iterable<Object[]> data() {
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 });
47 public int twiceSize65792;
50 public void testEmptyIdentifier() {
51 assertEquals(createIdentifier(0), emptySize);
55 public void testOneIdentifier() {
56 assertEquals(createIdentifier(1), oneSize);
61 assertEquals(createIdentifier(5), size5);
65 public void test256() {
66 assertEquals(createIdentifier(256), size256);
70 public void test65536() {
71 assertEquals(createIdentifier(65792), size65792);
75 public void testTwice65792() {
76 final NodeIdentifierWithPredicates nip = createIdentifier(65792);
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);
86 final byte[] bytes = baos.toByteArray();
87 Assert.assertEquals(twiceSize65792, bytes.length);
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);
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");
103 return NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME, predicates);