7e84a4e8fe08463671f4cedd3bc8c422a242d3f9
[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 package org.opendaylight.controller.cluster.schema.provider.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.io.CharSource;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
22
23 public class YangTextSourceSerializationProxyTest {
24     private YangTextSchemaSource schemaSource;
25
26     @Before
27     public void setUp() {
28         schemaSource = YangTextSchemaSource.delegateForCharSource(new SourceIdentifier("test", "2015-10-30"),
29             CharSource.wrap("Test source."));
30     }
31
32     @Test
33     public void serializeAndDeserializeProxy() throws ClassNotFoundException, IOException {
34         YangTextSchemaSourceSerializationProxy proxy = new YangTextSchemaSourceSerializationProxy(schemaSource);
35         ByteArrayOutputStream bos = new ByteArrayOutputStream();
36         ObjectOutputStream oos = new ObjectOutputStream(bos);
37
38         oos.writeObject(proxy);
39
40         final byte[] bytes = bos.toByteArray();
41         assertEquals(333, bytes.length);
42
43         ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
44
45         YangTextSchemaSourceSerializationProxy deserializedProxy =
46                 (YangTextSchemaSourceSerializationProxy) ois.readObject();
47
48         assertEquals(deserializedProxy.getRepresentation().getIdentifier(), proxy.getRepresentation().getIdentifier());
49         assertEquals(deserializedProxy.getRepresentation().read(), proxy.getRepresentation().read());
50     }
51
52     @Test
53     public void testProxyEqualsBackingYangTextSource() throws IOException {
54         YangTextSchemaSourceSerializationProxy serializationProxy =
55                 new YangTextSchemaSourceSerializationProxy(schemaSource);
56
57         assertEquals(serializationProxy.getRepresentation().getIdentifier(), schemaSource.getIdentifier());
58         assertEquals(serializationProxy.getRepresentation().read(), schemaSource.read());
59     }
60 }