Merge "Bug 2265: Modified NormalizedNodeOutputStreamWriter to implement yangtools...
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeStreamReaderWriterTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
12
13
14 import org.apache.commons.lang.SerializationUtils;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.util.TestModel;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25
26 public class NormalizedNodeStreamReaderWriterTest {
27
28     final NormalizedNode<?, ?> input = TestModel.createTestContainer();
29
30     @Test
31     public void testNormalizedNodeStreamReaderWriter() throws IOException {
32
33         byte[] byteData = null;
34
35         try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
36             NormalizedNodeStreamWriter writer = new NormalizedNodeOutputStreamWriter(byteArrayOutputStream)) {
37
38             NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(writer);
39             normalizedNodeWriter.write(input);
40             byteData = byteArrayOutputStream.toByteArray();
41
42         }
43
44         try(NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
45                 new ByteArrayInputStream(byteData))) {
46
47             NormalizedNode<?,?> node = reader.readNormalizedNode();
48             Assert.assertEquals(input, node);
49
50         }
51     }
52
53     @Test
54     public void testWithSerializable() {
55         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
56         SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
57
58         Assert.assertEquals(input, clone.getInput());
59
60     }
61
62 }