blob: 7dc9fb9e6d7549f10fb7f77f0714f1d946e80cde [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikov92511f22015-10-12 14:39:04 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
HIGUCHI Yuta805d96c2016-03-17 13:29:09 -070017package org.onosproject.persistence.impl;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070018
19import com.google.common.collect.Maps;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070020import org.junit.Before;
21import org.junit.Test;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070022import org.onosproject.store.service.Serializer;
23
Aaron Kruglikov92511f22015-10-12 14:39:04 -070024import java.util.Map;
25import java.util.Set;
26
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertFalse;
29import static org.junit.Assert.assertNull;
30import static org.junit.Assert.assertTrue;
31
32/**
33 * Test suite for Persistent Map.
34 */
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070035public class PersistentMapTest extends MapDBTest {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070036
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070037 private PersistentMap<Integer, Integer> map = null;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070038
39 /**
40 * Set up the database, create a map and a direct executor to handle it.
41 *
42 * @throws Exception if instantiation fails
43 */
44 @Before
45 public void setUp() throws Exception {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070046 //Creates, a map within it and a basic integer serializer
47 map = new PersistentMap<>(new Serializer() {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070048 @Override
49 public <T> byte[] encode(T object) {
50 if (object == null) {
51 return null;
52 }
53 int num = (Integer) object;
54 byte[] result = new byte[4];
55
56 result[0] = (byte) (num >> 24);
57 result[1] = (byte) (num >> 16);
58 result[2] = (byte) (num >> 8);
59 result[3] = (byte) num;
60 return result;
61 }
62
63 @Override
64 public <T> T decode(byte[] bytes) {
65 if (bytes == null) {
66 return null;
67 }
68 int num = 0x00000000;
69
70 num = num | bytes[0] << 24;
71 num = num | bytes[1] << 16;
72 num = num | bytes[2] << 8;
73 num = num | bytes[3];
74
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070075 return (T) Integer.valueOf(num);
Aaron Kruglikov92511f22015-10-12 14:39:04 -070076 }
Jordan Halterman2c83a102017-08-20 17:11:41 -070077
78 @Override
79 public <T> T copy(T object) {
80 return decode(encode(object));
81 }
Aaron Kruglikov92511f22015-10-12 14:39:04 -070082 }, fakeDB, "map");
83 }
84
Aaron Kruglikov92511f22015-10-12 14:39:04 -070085 @Test
86 public void testRemove() throws Exception {
87 //Checks removal and return values
88 fillMap(10);
89 assertEquals(10, map.size());
90 for (int i = 0; i < 10; i++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070091 assertEquals("The previous value was wrong.", Integer.valueOf(i), map.remove(i));
Aaron Kruglikov92511f22015-10-12 14:39:04 -070092 assertNull("The previous value was wrong.", map.remove(i));
93 //(i+1) compensates for base zero.
94 assertEquals("The size was wrong.", 10 - (i + 1), map.size());
95 }
96 }
97
98 @Test
99 public void testSize() throws Exception {
100 //Checks size values throughout addition and removal
101 for (int i = 0; i < 10; i++) {
102 map.put(i, i);
103 assertEquals("The map size is wrong.", i + 1, map.size());
104 }
105 for (int i = 0; i < 10; i++) {
106 map.remove(i);
107 assertEquals("The map size is wrong.", 9 - i, map.size());
108 }
109 }
110
111 @Test
112 public void testIsEmpty() throws Exception {
113 //Checks empty condition
114 //asserts that the map starts out empty
115 assertTrue("Map should be empty", map.isEmpty());
116 map.put(1, 1);
117 assertFalse("Map shouldn't be empty.", map.isEmpty());
118 map.remove(1);
119 assertTrue("Map should be empty", map.isEmpty());
120 }
121
122 @Test
123 public void testContains() throws Exception {
124 //Checks both containsKey and containsValue be aware the implementations vary widely (value does not use mapDB
125 //due to object '=='being an insufficient check)
126 for (int i = 0; i < 10; i++) {
127 assertFalse("Map should not contain the key", map.containsKey(i));
128 assertFalse("Map should not contain the value", map.containsValue(i));
129 map.put(i, i);
130 assertTrue("Map should contain the key", map.containsKey(i));
131 assertTrue("Map should contain the value", map.containsValue(i));
132 }
133 }
134
135 @Test
136 public void testGet() throws Exception {
137 //Tests value retrieval and nonexistent key return values
138 for (int i = 0; i < 10; i++) {
139 map.put(i, i);
140 for (int j = 0; j <= i; j++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -0700141 assertEquals("The value was wrong.", Integer.valueOf(j), map.get(j));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700142 }
143 }
144 assertNull("Null return value for nonexistent keys.", map.get(10));
145 }
146
147 @Test
148 public void testPutAll() throws Exception {
149 //Tests adding of an outside map
150 Map<Integer, Integer> testMap = Maps.newHashMap();
151 fillMap(10);
152 map.putAll(testMap);
153 for (int i = 0; i < 10; i++) {
154 assertTrue("The map should contain the current 'i' value.", map.containsKey(i));
155 assertTrue("The map should contain the current 'i' value.", map.containsValue(i));
156 }
157 }
158
159 @Test
160 public void testClear() throws Exception {
161 //Tests clearing the map
162 assertTrue("Map was initialized incorrectly, should be empty.", map.isEmpty());
163 fillMap(10);
164 assertFalse("Map should contain entries now.", map.isEmpty());
165 map.clear();
166 assertTrue("Map should have been cleared of entries.", map.isEmpty());
167
168 }
169
170 @Test
171 public void testKeySet() throws Exception {
172 //Tests key set generation
173 fillMap(10);
174 Set<Integer> keys = map.keySet();
175 for (int i = 0; i < 10; i++) {
176 assertTrue("The key set doesn't contain all keys 0-9", keys.contains(i));
177 }
178 assertEquals("The key set has an incorrect number of entries", 10, keys.size());
179 }
180
181 @Test
182 public void testValues() throws Exception {
183 //Tests value set generation
184 fillMap(10);
185 Set<Integer> values = (Set<Integer>) map.values();
186 for (int i = 0; i < 10; i++) {
187 assertTrue("The key set doesn't contain all keys 0-9", values.contains(i));
188 }
189 assertEquals("The key set has an incorrect number of entries", 10, values.size());
190 }
191
192 @Test
193 public void testEntrySet() throws Exception {
194 //Test entry set generation (violates abstraction by knowing the type of the returned entries)
195 fillMap(10);
196 Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
197 for (int i = 0; i < 10; i++) {
198 assertTrue("The key set doesn't contain all keys 0-9", entries.contains(Maps.immutableEntry(i, i)));
199 }
200 assertEquals("The key set has an incorrect number of entries", 10, entries.size());
201 }
202
203 @Test public void testPut() throws Exception {
204 //Tests insertion behavior (particularly the returning of previous value)
205 fillMap(10);
206 for (int i = 0; i < 10; i++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -0700207 assertEquals("Put should return the previous value", Integer.valueOf(i), map.put(i, i + 1));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700208 }
209 assertNull(map.put(11, 11));
210 }
211
212 /**
213 * Populated the map with pairs of integers from (0, 0) up to (numEntries - 1, numEntries -1).
214 * @param numEntries number of entries to add
215 */
216 private void fillMap(int numEntries) {
217 for (int i = 0; i < numEntries; i++) {
218 map.put(i, i);
219 }
220 }
Jonathan Hart4bb4b052015-10-26 18:10:56 -0700221}