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.primitives.UnsignedLong;
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.junit.Test;
27 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
28 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
29 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
30 import org.opendaylight.controller.cluster.access.concepts.MemberName;
31 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
32 import org.opendaylight.controller.cluster.datastore.utils.MutableUnsignedLongSet;
33 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongBitmap;
35 public class FrontendShardDataTreeSnapshotMetadataTest {
38 public void testCreateMetadataSnapshotNullInput() {
39 assertThrows(NullPointerException.class, () -> new FrontendShardDataTreeSnapshotMetadata(null));
43 public void testCreateMetadataSnapshotEmptyInput() throws Exception {
44 final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
45 final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot, 86);
46 testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
50 public void testSerializeMetadataSnapshotWithOneClient() throws Exception {
51 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
52 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 121);
53 testMetadataSnapshotEqual(origSnapshot, copySnapshot);
57 public void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
58 final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
59 final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 273);
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 assertEquals(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.clientId(), client));
74 origClientList.forEach(client -> copyIdent.put(client.clientId(), client));
76 assertTrue(origIdent.keySet().containsAll(copyIdent.keySet()));
77 assertTrue(copyIdent.keySet().containsAll(origIdent.keySet()));
79 origIdent.values().forEach(client -> {
80 final var copyClient = copyIdent.get(client.clientId());
81 testObject(client.clientId(), copyClient.clientId());
82 assertEquals(client.getPurgedHistories(), copyClient.getPurgedHistories());
83 assertEquals(client.getCurrentHistories(), copyClient.getCurrentHistories());
87 private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() {
88 return new FrontendShardDataTreeSnapshotMetadata(List.of());
91 private static FrontendShardDataTreeSnapshotMetadata createMetadataSnapshot(final int size) {
92 final List<FrontendClientMetadata> clients = new ArrayList<>(size);
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 num) {
100 final String index = String.valueOf(num);
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, num);
105 final ImmutableUnsignedLongSet purgedHistories = MutableUnsignedLongSet.of(0).immutableCopy();
107 return new FrontendClientMetadata(clientIdentifier, purgedHistories, List.of(
108 new FrontendHistoryMetadata(num, num, true,
109 UnsignedLongBitmap.copyOf(Map.of(UnsignedLong.ZERO, Boolean.TRUE)), purgedHistories)));
112 private static <T> void testObject(final T object, final T equalObject) {
113 assertEquals(object.hashCode(), equalObject.hashCode());
114 assertTrue(object.equals(object));
115 assertTrue(object.equals(equalObject));
116 assertFalse(object.equals(null));
117 assertFalse(object.equals("dummy"));
120 @SuppressWarnings("unchecked")
121 private static <T> T copy(final T obj, final int expectedSize) throws IOException, ClassNotFoundException {
122 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
123 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
124 oos.writeObject(obj);
127 final byte[] bytes = bos.toByteArray();
128 assertEquals(expectedSize, bytes.length);
130 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
131 return (T) ois.readObject();