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

©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.