e909e79a79cace27bc657465ee10cbb5bb7637a2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / ShardDataTreeSnapshotTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.datastore.persisted;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.util.Map;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27
28 /**
29  * Unit tests for ShardDataTreeSnapshot.
30  *
31  * @author Thomas Pantelis
32  */
33 public class ShardDataTreeSnapshotTest {
34
35     @Test
36     public void testShardDataTreeSnapshotWithNoMetadata() throws Exception {
37         NormalizedNode<?, ?> expectedNode = ImmutableContainerNodeBuilder.create()
38                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
39                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
40
41         MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(expectedNode);
42         ByteArrayOutputStream bos = new ByteArrayOutputStream();
43         snapshot.serialize(bos);
44
45         ShardDataTreeSnapshot deserialized = ShardDataTreeSnapshot.deserialize(
46                 new ByteArrayInputStream(bos.toByteArray()));
47
48         Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
49         assertEquals("rootNode present", true, actualNode.isPresent());
50         assertEquals("rootNode", expectedNode, actualNode.get());
51         assertEquals("Deserialized type", MetadataShardDataTreeSnapshot.class, deserialized.getClass());
52         assertEquals("Metadata size", 0, ((MetadataShardDataTreeSnapshot)deserialized).getMetadata().size());
53     }
54
55     @Test
56     public void testShardDataTreeSnapshotWithMetadata() throws Exception {
57         NormalizedNode<?, ?> expectedNode = ImmutableContainerNodeBuilder.create()
58                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
59                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
60
61         Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> expMetadata =
62                 ImmutableMap.of(TestShardDataTreeSnapshotMetadata.class, new TestShardDataTreeSnapshotMetadata("test"));
63         MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(expectedNode, expMetadata);
64         ByteArrayOutputStream bos = new ByteArrayOutputStream();
65         snapshot.serialize(bos);
66
67         ShardDataTreeSnapshot deserialized = ShardDataTreeSnapshot.deserialize(
68                 new ByteArrayInputStream(bos.toByteArray()));
69
70         Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
71         assertEquals("rootNode present", true, actualNode.isPresent());
72         assertEquals("rootNode", expectedNode, actualNode.get());
73         assertEquals("Deserialized type", MetadataShardDataTreeSnapshot.class, deserialized.getClass());
74         assertEquals("Metadata", expMetadata, ((MetadataShardDataTreeSnapshot)deserialized).getMetadata());
75     }
76
77     @Test
78     @Deprecated
79     public void testPreBoronShardDataTreeSnapshot() throws Exception {
80         NormalizedNode<?, ?> expectedNode = ImmutableContainerNodeBuilder.create()
81                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
82                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
83
84         PreBoronShardDataTreeSnapshot snapshot = new PreBoronShardDataTreeSnapshot(expectedNode);
85         ByteArrayOutputStream bos = new ByteArrayOutputStream();
86         snapshot.serialize(bos);
87
88         ShardDataTreeSnapshot deserialized = ShardDataTreeSnapshot.deserialize(bos.toByteArray());
89
90         Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
91         assertEquals("rootNode present", true, actualNode.isPresent());
92         assertEquals("rootNode", expectedNode, actualNode.get());
93         assertEquals("Deserialized type", PreBoronShardDataTreeSnapshot.class, deserialized.getClass());
94     }
95
96     static class TestShardDataTreeSnapshotMetadata
97             extends ShardDataTreeSnapshotMetadata<TestShardDataTreeSnapshotMetadata> {
98         private static final long serialVersionUID = 1L;
99
100         private final String data;
101
102         TestShardDataTreeSnapshotMetadata(String data) {
103             this.data = data;
104         }
105
106         @Override
107         public Class<TestShardDataTreeSnapshotMetadata> getType() {
108             return TestShardDataTreeSnapshotMetadata.class;
109         }
110
111         @Override
112         protected Externalizable externalizableProxy() {
113             return new Proxy(data);
114         }
115
116         @Override
117         public int hashCode() {
118             return data.hashCode();
119         }
120
121         @Override
122         public boolean equals(Object obj) {
123             return obj instanceof TestShardDataTreeSnapshotMetadata
124                     && data.equals(((TestShardDataTreeSnapshotMetadata)obj).data);
125         }
126
127         private static class Proxy implements Externalizable {
128             private String data;
129
130             @SuppressWarnings("checkstyle:RedundantModifier")
131             public Proxy() {
132             }
133
134             Proxy(String data) {
135                 this.data = data;
136             }
137
138             @Override
139             public void writeExternal(ObjectOutput out) throws IOException {
140                 out.writeObject(data);
141             }
142
143             @Override
144             public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
145                 data = (String) in.readObject();
146             }
147
148             Object readResolve() {
149                 return new TestShardDataTreeSnapshotMetadata(data);
150             }
151         }
152     }
153 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.