2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.persisted;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
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;
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;
36 public class FrontendShardDataTreeSnapshotMetadataTest {
38 @Test(expected = NullPointerException.class)
39 public final void testCreateMetadataSnapshotNullInput() {
40 new FrontendShardDataTreeSnapshotMetadata(null);
44 public final void testCreateMetadataSnapshotEmptyInput() throws Exception {
45 final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
46 final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot);
47 testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
51 public final void testSerializeMetadataSnapshotWithOneClient() throws Exception {
52 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
53 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
54 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
58 public final void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
59 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
60 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
61 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
64 private static void testMetadataSnapshotEqual(final FrontendShardDataTreeSnapshotMetadata origSnapshot,
65 final FrontendShardDataTreeSnapshotMetadata copySnapshot) {
67 final List<FrontendClientMetadata> origClientList = origSnapshot.getClients();
68 final List<FrontendClientMetadata> copyClientList = copySnapshot.getClients();
70 assertTrue(origClientList.size() == copyClientList.size());
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));
77 assertTrue(origIdent.keySet().containsAll(copyIdent.keySet()));
78 assertTrue(copyIdent.keySet().containsAll(origIdent.keySet()));
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()));
88 private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() {
89 return new FrontendShardDataTreeSnapshotMetadata(Collections.<FrontendClientMetadata>emptyList());
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));
97 return new FrontendShardDataTreeSnapshotMetadata(clients);
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);
107 final RangeSet<UnsignedLong> purgedHistories = TreeRangeSet.create();
108 purgedHistories.add(Range.closed(UnsignedLong.ZERO, UnsignedLong.ONE));
110 final Collection<FrontendHistoryMetadata> currentHistories = Collections
111 .singleton(new FrontendHistoryMetadata(num, num, num, true));
113 return new FrontendClientMetadata(clientIdentifier, purgedHistories, currentHistories);
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"));
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);
131 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
132 return (T) ois.readObject();