Bump persisted PayloadVersion
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / ShardDataTreeSnapshotTest.java
1 /*
2  * Copyright (c) 2016, 2017 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 import static org.junit.Assert.assertTrue;
12
13 import com.google.common.collect.ImmutableMap;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutput;
21 import java.io.ObjectOutputStream;
22 import java.util.Map;
23 import java.util.Optional;
24 import org.junit.Test;
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 (ObjectOutputStream out = new ObjectOutputStream(bos)) {
47             snapshot.serialize(out);
48         }
49
50         ShardDataTreeSnapshot deserialized;
51         try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
52             deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
53         }
54
55         Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
56         assertTrue("rootNode present", 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 (ObjectOutputStream out = new ObjectOutputStream(bos)) {
73             snapshot.serialize(out);
74         }
75
76         ShardDataTreeSnapshot deserialized;
77         try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
78             deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
79         }
80
81         Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
82         assertTrue("rootNode present", 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     static class TestShardDataTreeSnapshotMetadata
89             extends ShardDataTreeSnapshotMetadata<TestShardDataTreeSnapshotMetadata> {
90         private static final long serialVersionUID = 1L;
91
92         private final String data;
93
94         TestShardDataTreeSnapshotMetadata(final String data) {
95             this.data = data;
96         }
97
98         @Override
99         public Class<TestShardDataTreeSnapshotMetadata> getType() {
100             return TestShardDataTreeSnapshotMetadata.class;
101         }
102
103         @Override
104         protected Externalizable externalizableProxy() {
105             return new Proxy(data);
106         }
107
108         @Override
109         public int hashCode() {
110             return data.hashCode();
111         }
112
113         @Override
114         public boolean equals(final Object obj) {
115             return obj instanceof TestShardDataTreeSnapshotMetadata
116                     && data.equals(((TestShardDataTreeSnapshotMetadata)obj).data);
117         }
118
119         private static class Proxy implements Externalizable {
120             private String data;
121
122             @SuppressWarnings("checkstyle:RedundantModifier")
123             public Proxy() {
124             }
125
126             Proxy(final String data) {
127                 this.data = data;
128             }
129
130             @Override
131             public void writeExternal(final ObjectOutput out) throws IOException {
132                 out.writeObject(data);
133             }
134
135             @Override
136             public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
137                 data = (String) in.readObject();
138             }
139
140             Object readResolve() {
141                 return new TestShardDataTreeSnapshotMetadata(data);
142             }
143         }
144     }
145 }

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