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