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;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.collect.TreeRangeSet;
17 import com.google.common.primitives.UnsignedLong;
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
29 import org.junit.Test;
30 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
31 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
32 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
33 import org.opendaylight.controller.cluster.access.concepts.MemberName;
35 public class FrontendShardDataTreeSnapshotMetadataTest {
37 @Test(expected = NullPointerException.class)
38 public final void testCreateMetadataSnapshotNullInput() {
39 new FrontendShardDataTreeSnapshotMetadata(null);
43 public final void testCreateMetadataSnapshotEmptyInput() throws Exception {
44 final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
45 final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot);
46 testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
50 public final void testSerializeMetadataSnapshotWithOneClient() throws Exception {
51 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
52 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
53 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
57 public final void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
58 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
59 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
60 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
63 private static void testMetadataSnapshotEqual(final FrontendShardDataTreeSnapshotMetadata origSnapshot,
64 final FrontendShardDataTreeSnapshotMetadata copySnapshot) {
66 final List<FrontendClientMetadata> origClientList = origSnapshot.getClients();
67 final List<FrontendClientMetadata> copyClientList = copySnapshot.getClients();
69 assertTrue(origClientList.size() == copyClientList.size());
71 final Map<ClientIdentifier, FrontendClientMetadata> origIdent = new HashMap<>();
72 final Map<ClientIdentifier, FrontendClientMetadata> copyIdent = new HashMap<>();
73 origClientList.forEach(client -> origIdent.put(client.getIdentifier(), client));
74 origClientList.forEach(client -> copyIdent.put(client.getIdentifier(), client));
76 assertTrue(origIdent.keySet().containsAll(copyIdent.keySet()));
77 assertTrue(copyIdent.keySet().containsAll(origIdent.keySet()));
79 origIdent.values().forEach(client -> {
80 final FrontendClientMetadata copyClient = copyIdent.get(client.getIdentifier());
81 testObject(client.getIdentifier(), copyClient.getIdentifier());
82 assertTrue(client.getPurgedHistories().equals(copyClient.getPurgedHistories()));
83 assertTrue(client.getCurrentHistories().equals(copyClient.getCurrentHistories()));
87 private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() {
88 return new FrontendShardDataTreeSnapshotMetadata(Collections.<FrontendClientMetadata> emptyList());
91 private static FrontendShardDataTreeSnapshotMetadata createMetadataSnapshot(final int size) {
92 final List<FrontendClientMetadata> clients = new ArrayList<>();
93 for (long i = 0; i < size; i++) {
94 clients.add(createFrontedClientMetadata(i));
96 return new FrontendShardDataTreeSnapshotMetadata(clients);
99 private static FrontendClientMetadata createFrontedClientMetadata(final long i) {
100 final String index = String.valueOf(i);
101 final String indexName = "test_" + index;
102 final FrontendIdentifier frontendIdentifier = FrontendIdentifier.create(MemberName.forName(indexName),
103 FrontendType.forName(index));
104 final ClientIdentifier clientIdentifier = ClientIdentifier.create(frontendIdentifier, i);
106 final RangeSet<UnsignedLong> purgedHistories = TreeRangeSet.create();
107 purgedHistories.add(Range.closed(UnsignedLong.ZERO, UnsignedLong.ONE));
109 final Collection<FrontendHistoryMetadata> currentHistories = Collections
110 .singleton(new FrontendHistoryMetadata(i, i, i, true));
112 return new FrontendClientMetadata(clientIdentifier, purgedHistories, currentHistories);
115 private static final <T> void testObject(final T object, final T equalObject) {
116 assertEquals(object.hashCode(), equalObject.hashCode());
117 assertTrue(object.equals(object));
118 assertTrue(object.equals(equalObject));
119 assertFalse(object.equals(null));
120 assertFalse(object.equals("dummy"));
123 @SuppressWarnings("unchecked")
124 private static <T> T copy(final T o) throws IOException, ClassNotFoundException {
125 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
126 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
130 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
131 return (T) ois.readObject();