e175a09eb41ff50f0b5efeba613d94f1afdd96a0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / FrontendShardDataTreeSnapshotMetadataTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.controller.cluster.datastore.persisted;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import com.google.common.collect.TreeRangeSet;
18 import com.google.common.primitives.UnsignedLong;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.junit.Test;
31 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
32 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
33 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
34 import org.opendaylight.controller.cluster.access.concepts.MemberName;
35
36 public class FrontendShardDataTreeSnapshotMetadataTest {
37
38     @Test(expected = NullPointerException.class)
39     public final void testCreateMetadataSnapshotNullInput() {
40         new FrontendShardDataTreeSnapshotMetadata(null);
41     }
42
43     @Test
44     public final void testCreateMetadataSnapshotEmptyInput() throws Exception {
45         final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
46         final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot);
47         testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
48     }
49
50     @Test
51     public final void testSerializeMetadataSnapshotWithOneClient() throws Exception {
52         final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
53         final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
54         testMetadataSnapshotEqual(origSnapshot, copySnapshot);
55     }
56
57     @Test
58     public final void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
59         final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
60         final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
61         testMetadataSnapshotEqual(origSnapshot, copySnapshot);
62     }
63
64     private static void testMetadataSnapshotEqual(final FrontendShardDataTreeSnapshotMetadata origSnapshot,
65             final FrontendShardDataTreeSnapshotMetadata copySnapshot) {
66
67         final List<FrontendClientMetadata> origClientList = origSnapshot.getClients();
68         final List<FrontendClientMetadata> copyClientList = copySnapshot.getClients();
69
70         assertTrue(origClientList.size() == copyClientList.size());
71
72         final Map<ClientIdentifier, FrontendClientMetadata> origIdent = new HashMap<>();
73         final Map<ClientIdentifier, FrontendClientMetadata> copyIdent = new HashMap<>();
74         origClientList.forEach(client -> origIdent.put(client.getIdentifier(), client));
75         origClientList.forEach(client -> copyIdent.put(client.getIdentifier(), client));
76
77         assertTrue(origIdent.keySet().containsAll(copyIdent.keySet()));
78         assertTrue(copyIdent.keySet().containsAll(origIdent.keySet()));
79
80         origIdent.values().forEach(client -> {
81             final FrontendClientMetadata copyClient = copyIdent.get(client.getIdentifier());
82             testObject(client.getIdentifier(), copyClient.getIdentifier());
83             assertTrue(client.getPurgedHistories().equals(copyClient.getPurgedHistories()));
84             assertTrue(client.getCurrentHistories().equals(copyClient.getCurrentHistories()));
85         });
86     }
87
88     private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() {
89         return new FrontendShardDataTreeSnapshotMetadata(Collections.<FrontendClientMetadata>emptyList());
90     }
91
92     private static FrontendShardDataTreeSnapshotMetadata createMetadataSnapshot(final int size) {
93         final List<FrontendClientMetadata> clients = new ArrayList<>();
94         for (long i = 0; i < size; i++) {
95             clients.add(createFrontedClientMetadata(i));
96         }
97         return new FrontendShardDataTreeSnapshotMetadata(clients);
98     }
99
100     private static FrontendClientMetadata createFrontedClientMetadata(final long num) {
101         final String index = String.valueOf(num);
102         final String indexName = "test_" + index;
103         final FrontendIdentifier frontendIdentifier = FrontendIdentifier.create(MemberName.forName(indexName),
104                 FrontendType.forName(index));
105         final ClientIdentifier clientIdentifier = ClientIdentifier.create(frontendIdentifier, num);
106
107         final RangeSet<UnsignedLong> purgedHistories = TreeRangeSet.create();
108         purgedHistories.add(Range.closed(UnsignedLong.ZERO, UnsignedLong.ONE));
109
110         final Collection<FrontendHistoryMetadata> currentHistories = Collections
111                 .singleton(new FrontendHistoryMetadata(num, num, num, true));
112
113         return new FrontendClientMetadata(clientIdentifier, purgedHistories, currentHistories);
114     }
115
116     private static <T> void testObject(final T object, final T equalObject) {
117         assertEquals(object.hashCode(), equalObject.hashCode());
118         assertTrue(object.equals(object));
119         assertTrue(object.equals(equalObject));
120         assertFalse(object.equals(null));
121         assertFalse(object.equals("dummy"));
122     }
123
124     @SuppressWarnings("unchecked")
125     private static <T> T copy(final T obj) throws IOException, ClassNotFoundException {
126         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
127         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
128             oos.writeObject(obj);
129         }
130
131         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
132             return (T) ois.readObject();
133         }
134     }
135 }