2 * Copyright (c) 2022 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.parser.rfc7950.antlr;
10 import static org.junit.Assert.assertEquals;
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.DataInputStream;
15 import java.io.DataOutputStream;
16 import java.io.IOException;
17 import org.junit.jupiter.api.BeforeAll;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.ir.IOSupport;
20 import org.opendaylight.yangtools.yang.ir.IRStatement;
21 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
23 import org.opendaylight.yangtools.yang.stmt.TestUtils;
26 private static YangIRSchemaSource FOO;
29 static void beforeClass() throws Exception {
30 FOO = TextToIRTransformer.transformText(TestUtils.assertSchemaSource("/bugs/YT1089/foo.yang"));
34 void testSerializedSize() throws IOException {
35 final byte[] bytes = serialize(FOO.getRootStatement());
36 assertEquals(485, bytes.length);
40 void testSerdes() throws IOException {
41 final var orig = FOO.getRootStatement();
42 assertEquals(orig, deserialize(serialize(orig)));
45 private static byte[] serialize(final IRStatement stmt) throws IOException {
46 final var baos = new ByteArrayOutputStream();
48 try (var dos = new DataOutputStream(baos)) {
49 IOSupport.writeStatement(dos, stmt);
52 return baos.toByteArray();
55 private static IRStatement deserialize(final byte[] bytes) throws IOException {
56 try (var dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
57 final var stmt = IOSupport.readStatement(dis);
58 assertEquals(0, dis.available());