084fd5242f2276818a92c7b82080cfb38fc4a120
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / schema / provider / impl / YangTextSourceSerializationProxyTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.schema.provider.impl;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import com.google.common.io.ByteSource;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.nio.charset.StandardCharsets;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26
27 public class YangTextSourceSerializationProxyTest {
28
29     private YangTextSchemaSource schemaSource;
30
31     @Before
32     public void setUp() {
33         String source = "Test source.";
34         schemaSource = YangTextSchemaSource.delegateForByteSource(
35                 RevisionSourceIdentifier.create("test", Revision.of("2015-10-30")),
36                 ByteSource.wrap(source.getBytes(StandardCharsets.UTF_8)));
37     }
38
39
40     @Test
41     public void serializeAndDeserializeProxy() throws ClassNotFoundException, IOException {
42         YangTextSchemaSourceSerializationProxy proxy = new YangTextSchemaSourceSerializationProxy(schemaSource);
43         ByteArrayOutputStream bos = new ByteArrayOutputStream();
44         ObjectOutputStream oos = new ObjectOutputStream(bos);
45
46         oos.writeObject(proxy);
47
48         final byte[] bytes = bos.toByteArray();
49         assertEquals(353, bytes.length);
50
51         ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
52
53         YangTextSchemaSourceSerializationProxy deserializedProxy =
54                 (YangTextSchemaSourceSerializationProxy) ois.readObject();
55
56         assertEquals(deserializedProxy.getRepresentation().getIdentifier(), proxy.getRepresentation().getIdentifier());
57         assertArrayEquals(deserializedProxy.getRepresentation().read(), proxy.getRepresentation().read());
58     }
59
60     @Test
61     public void testProxyEqualsBackingYangTextSource() throws IOException {
62         YangTextSchemaSourceSerializationProxy serializationProxy =
63                 new YangTextSchemaSourceSerializationProxy(schemaSource);
64
65         assertEquals(serializationProxy.getRepresentation().getIdentifier(), schemaSource.getIdentifier());
66         assertArrayEquals(serializationProxy.getRepresentation().read(), schemaSource.read());
67     }
68 }