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.assertThrows;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.ImmutableMap;
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.HashMap;
25 import java.util.List;
28 import org.junit.Test;
29 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
30 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
31 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
32 import org.opendaylight.controller.cluster.access.concepts.MemberName;
33 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
34 import org.opendaylight.controller.cluster.datastore.utils.MutableUnsignedLongSet;
36 public class FrontendShardDataTreeSnapshotMetadataTest {
39 public void testCreateMetadataSnapshotNullInput() {
40 assertThrows(NullPointerException.class, () -> new FrontendShardDataTreeSnapshotMetadata(null));
44 public void testCreateMetadataSnapshotEmptyInput() throws Exception {
45 final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
46 final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot, 127);
47 testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
51 public void testSerializeMetadataSnapshotWithOneClient() throws Exception {
52 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
53 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 162);
54 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
58 public void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
59 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
60 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 314);
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 assertEquals(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 assertEquals(client.getPurgedHistories(), copyClient.getPurgedHistories());
84 assertEquals(client.getCurrentHistories(), copyClient.getCurrentHistories());
88 private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() {
89 return new FrontendShardDataTreeSnapshotMetadata(List.of());
92 private static FrontendShardDataTreeSnapshotMetadata createMetadataSnapshot(final int size) {
93 final List<FrontendClientMetadata> clients = new ArrayList<>(size);
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 MutableUnsignedLongSet tmp = MutableUnsignedLongSet.of();
109 final ImmutableUnsignedLongSet purgedHistories = tmp.immutableCopy();
111 final Set<FrontendHistoryMetadata> currentHistories = Set.of(
112 new FrontendHistoryMetadata(num, num, true, ImmutableMap.of(UnsignedLong.ZERO, Boolean.TRUE),
115 return new FrontendClientMetadata(clientIdentifier, purgedHistories.immutableCopy(), currentHistories);
118 private static <T> void testObject(final T object, final T equalObject) {
119 assertEquals(object.hashCode(), equalObject.hashCode());
120 assertTrue(object.equals(object));
121 assertTrue(object.equals(equalObject));
122 assertFalse(object.equals(null));
123 assertFalse(object.equals("dummy"));
126 @SuppressWarnings("unchecked")
127 private static <T> T copy(final T obj, final int expectedSize) throws IOException, ClassNotFoundException {
128 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
129 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
130 oos.writeObject(obj);
133 final byte[] bytes = bos.toByteArray();
134 assertEquals(expectedSize, bytes.length);
136 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
137 return (T) ois.readObject();