Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / rfc7950 / antlr / IOSupportTest.java
1 /*
2  * Copyright (c) 2022 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.parser.rfc7950.antlr;
9
10 import static org.junit.Assert.assertEquals;
11
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;
24
25 class IOSupportTest {
26     private static YangIRSchemaSource FOO;
27
28     @BeforeAll
29     static void beforeClass() throws Exception {
30         FOO = TextToIRTransformer.transformText(TestUtils.assertSchemaSource("/bugs/YT1089/foo.yang"));
31     }
32
33     @Test
34     void testSerializedSize() throws IOException {
35         final byte[] bytes = serialize(FOO.getRootStatement());
36         assertEquals(485, bytes.length);
37     }
38
39     @Test
40     void testSerdes() throws IOException {
41         final var orig = FOO.getRootStatement();
42         assertEquals(orig, deserialize(serialize(orig)));
43     }
44
45     private static byte[] serialize(final IRStatement stmt) throws IOException {
46         final var baos = new ByteArrayOutputStream();
47
48         try (var dos = new DataOutputStream(baos)) {
49             IOSupport.writeStatement(dos, stmt);
50         }
51
52         return baos.toByteArray();
53     }
54
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());
59             return stmt;
60         }
61     }
62 }